Monday, November 16, 2009

Amazon Seller Central API Integration with Shopping Cart

We had a task with a German client Integrate her online shop with Amazon API so she can update inventory of her two websites on Amazon through Amazon API, which took three days with sleepless nights but at last we succeeded Integrating OSCommerce & XTCommerce shopping carts with Amazon Seller Central API. It took a lot of our time in reading development API manuals, forums and articles on internet but of no use. Most of the people were found complaining of insufficient help material and non-availability of example source code in many development languages in practice today.

Each step moving forward working on it, you will find a new way of doing it and you will simply be revolving around in the dark amazon. Sending request to amazon through SOAP attachment having xml data of all the products to be updated, or using the API URLs to send flat file attachment to. You will just mess up everything.

First of all, I would like to share my experience with SOAP attachments with Action for amazon defined in XML as _POST_INVENTORY_AVAILABILITY_DATA_ (and others using Posting Data guidlines)

$messageType = '_POST_INVENTORY_AVAILABILITY_DATA_';
$document = 'http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">1.01' . $merchantid . '
Inventory';
//loop through quantity
$document .='
1
Update
10009334
25
';
$document .='';

Then submit the above Document string to thie following function which will send it to appropriate amazon url:

postDocument($messageType, $document)

function postDocument($messageType, $document) {
$this->error = null;
// create the attachment to add to the SOAP call
$attachment = new SOAP_Attachment('doc', 'application/binary', null, $document);
$attachment->options['attachment']['encoding'] = '8bit';
$params = array('merchant' => $this->merchant,
'messageType' => $messageType,
'doc' => $attachment);
$options = array('trace' => true, 'timeout' => '10', 'attachments' => 'Mime');
$result = $this->client->call('postDocument', $params, $options);
if(PEAR::isError($result)) {
echo $this->error = "Error: " . $result->getMessage();
return false;
}
return $result;
}

And what you get at last is an error in all the ways you do it:
Error: Unable to retrieve WSDL https://merchant-api.amazon.de/gateway/merchant-interface-mime,

If you access this url directly, It will prompt for seller central user id and password but it complained that wsdl cannot be found even I tried saving this wsdl to local directory …
Then on the second attempt we tried Amazon AIM Document and used URL based amazon functions which works with tab delimited file formats. At first, we tried sending the simplest Price/Quantity Update flat file through CURL as attachment and spends whole day find solution to this particular error:

Feed Processing Summary:
Number of records processed 0
Number of records successful 0
original-record-number sku error-code error-type error-message
0 6000 Error Column "sku" is missing and is required.
0 6000 Warning The following columns are not recognized and will be ignored:

WoAH !!! SKU is present in the file and it is still complaining its missing? Oh man!!! Curl is not sending the flt file as attachment. After realizing this we login to the seller central account and uploaded the same file for Price/Quantity Updated … Oh Its Working.

Finally!

$user = '';
$pass = '';
$id = ''; // Merchant ID
$name = ''; // Merchant store name
$authentication = base64_encode($user . ":" . $pass);
$itemsFeedURL = "https://secure.amazon.de/exec/panama/seller-admin/catalog-upload/add-modify-delete";
// TAKE BATCH FILE PATH WHICH INCLUDE RECORDS TO BE UPDATED
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$file = $root."PriceInventory.txt";
$authentication = base64_encode($user . ":" . $pass);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $itemsFeedURL);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($file_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . $authentication ,
'Content-Type: text/xml;charset=UTF-8',
'BatchID: Y'
));
echo $result = curl_exec($ch);
curl_close($ch);

So you can populate your products quantity/Price against their SKU from any shopping cart DB in an inventory batch file template (get it from your Seller Central Account Products Update Area), attach it wil Curl Request and submit to amazon API.

The API do not promise to do it for you instantly, so you have to wait for a few minutes to see the results of this batch file. I have mentioned ‘BatchID:Y’ in Curl header request, which means API will return your BatchID being processed, you can view status of this BatchID in a separate request or you can login Seller Central Account to see the result of Your file Batches being processed.