ylliX - Online Advertising Network

Tutorial: How to create Amazon Fulfillment order using php?

Follow my steps and you will get working service:

1. First, you need php framework which you can download here: https://developer.amazonservices.ca/doc/fba/outbound/v20101001/php.html/181-2401128-3222006

2. Assuming your vhost root is at /var/www/amazon, unpack content to /var/www/amazon/FBAOutboundServiceMWS so you will have Client.php at /var/www/amazon/FBAOutboundServiceMWS\Client.php

3. Create /var/www/amazon/index.php file with such content:

define('AWS_ACCESS_KEY_ID', 'xxxxxx'); // this should be your AWS access key
define('AWS_SECRET_ACCESS_KEY', 'yyyy'); // this should be your AWS secret key

define('APPLICATION_NAME', 'MyApp');
define('APPLICATION_VERSION', '1');

define ('MERCHANT_ID', 'zzzzzz'); // this should be your merchant id
define ('MARKETPLACE_ID', 'A1PA6795UKMFR9');
set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/amazon/');

function __autoload($className){
    $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    $includePaths = explode(PATH_SEPARATOR, get_include_path());
    foreach($includePaths as $includePath){
        if(file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)){
            require_once $filePath;
            return;
        }
    }
}

Here is some description of used constants:
APPLICATION_NAME – it can be whatever you want, it is used by Amazon to debug your calls
APPLICATION_VERSION – same as above, when you need to talk with tech support, you should give them app name and version so they can easy track your requests
MERCHANT_ID – merchant ID given by amazon, to get it go to item from seller you want (f.e. http://www.amazon.com/Sushi-Everyone-Andy-Matsuda/dp/061521018X/ref=aag_m_pw_dp?ie=UTF8), then in right frame click “Sold by XXX” and you will get link like: http://www.amazon.com/gp/aag/main?ie=UTF8&asin=&isAmazonFulfilled=1&isCBA=&marketplaceID=ATVPDKIKX0DER&orderID=&seller=AXF7Y6KWIGBC – last parameter is your seller ID
MARKETPLACE_ID – it is identifier used by amazon, for europe, you can find them here: http://docs.developer.amazonservices.com/en_US/feeds/Feeds_Overview.html#Feeds_EU_Global_Seller
AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY are the values which you will get when creating user account on AWS

4. Now we have to add some more code to our index.php file:

$serviceUrl = "https://mws-eu.amazonservices.com/FulfillmentOutboundShipment/2010-10-01";
$config = array (
   'ServiceURL' => $serviceUrl,
   'ProxyHost' => null,
   'ProxyPort' => -1,
   'MaxErrorRetry' => 3,
 );

$service = new FBAOutboundServiceMWS_Client(
        AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY,
		 $config,
        APPLICATION_NAME,
        APPLICATION_VERSION
        );
 //Create Address
$address = new FBAOutboundServiceMWS_Model_Address();

//Set Address Parameters
$address-> setName("Test Order");
$address-> setLine1("Delivery 1");
$address-> setLine2("Delivery 2");
$address-> setCity("City");
$address-> setStateOrProvinceCode("CA");
$address-> setCountryCode('DE');
$address-> setPostalCode("90210");
$address-> setPhoneNumber("123456");

//Create item
$item1 = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItem();

//Set item parameters
$item1-> setSellerSKU("1115-K100");
$item1-> setSellerFulfillmentOrderItemId("1115-K100");
$item1-> setQuantity("2");

$req = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderRequest();
$req->setSellerId(MERCHANT_ID); 
$req->setSellerFulfillmentOrderId('333TEST124');
$req->setDisplayableOrderId('333TEST124');
$req->setDisplayableOrderDateTime('2014-05-06T17:22:00Z');
$req->setDisplayableOrderComment('Thank you for your order!');
$req->setShippingSpeedCategory('Standard');
$req->setDestinationAddress($address);
 
$list = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItemList();
$list->withmember($item1);
 
$req->setItems($list);

So what happens here?
$serviceUrl – is URL where you will send your request, they are different for some regions but given one is for Europe
FBAOutboundServiceMWS_Client – is our class for handling requests/responses to and from amazon
FBAOutboundServiceMWS_Model_Address – this class is used to create delivery address
FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItem – using this you can create shipped items and add them to request, setSellerSKU and setSellerFulfillmentOrderItemId can be same
FBAOutboundServiceMWS_Model_CreateFulfillmentOrderRequest – is main request class, setSellerFulfillmentOrderId and setDisplayableOrderId should be the same, setDisplayableOrderDateTime should be created using php’s date(“c”)

5. Last part is to add function which will do real request and handle response, so at bottom of index.php add:

try {
  $response = $service->CreateFulfillmentOrder($req);

  echo ("Service Response\n");
  echo ("=============================================================================\n");

  $dom = new DOMDocument();
  $dom->loadXML($response->toXML());
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  echo $dom->saveXML();
  echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");

} catch (FBAOutboundServiceMWS_Exception $ex) {
  echo("Caught Exception: " . $ex->getMessage() . "\n");
  echo("Response Status Code: " . $ex->getStatusCode() . "\n");
  echo("Error Code: " . $ex->getErrorCode() . "\n");
  echo("Error Type: " . $ex->getErrorType() . "\n");
  echo("Request ID: " . $ex->getRequestId() . "\n");
  echo("XML: " . $ex->getXML() . "\n");
  echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
}

This last part sends your request, if in response you will see something like:

<?xml version="1.0" encoding="UTF-8"?>
<CreateFulfillmentOrderResponse xmlns="http://mws.amazonaws.com/FulfillmentOutboundShipment/2010-10-01/">
   <ResponseMetadata>
        <RequestId>xxxxxx</RequestId>
    </ResponseMetadata>
</CreateFulfillmentOrderResponse>

where xxxxx will be more like unique identifier – its working, if not, there will be error description.

Keep in mind that order needs ca. 15 mins to show in your panel, but you can use search for checking if its working.

9 thoughts on “Tutorial: How to create Amazon Fulfillment order using php?

  1. hi,

    Thanks for post.

    But i am not getting output as required. it is showing a string. is i am doing something wrong? Please suggest.

     

  2. thanks,

    but i have error message like this

    (Caught Exception: Seller is not registered for Basic fulfillment. Response Status Code: 400 )

  3. can this will work on localhost brother ? i am facing this error

    Parse error: syntax error, unexpected ‘$response’ (T_VARIABLE) in C:\xampp\htdocs\amazon\index.php on line 80

Leave a Reply