2

I am currently trying to figure out how to upload my own invoice pdf files to Amazon MWS with php/curl: 1.) Upload pdf to Amazon MWS using Action: SubmitFeed and FeedType: UPLOAD_VAT_INVOICE This is giving a positive response with a FeedSubmissionId

2.) Get Feed Submission Result with that FeedSubmissionId is reporting an Error 79523 "Please provide only one valid marketplace while uploading an invoice."

So I assume that my upload is not working properly, but I can not find the mistake.

The Error Code is described on Page 21 in this PDF File from Amazon: VAT Calculation Service (VCS) Developer Guide

I have spent 2 days so far, trying to solve this, and I was not able to find anything about it on google. Hope someone here can help me. Thanks!

Here is the code:

    <?php 
$param = array();
$param['AWSAccessKeyId']   = 'XXXXX'; 
$param['Action']           = 'SubmitFeed';
$param['Merchant']         = 'XXXXX';
$param['FeedType']         = '_UPLOAD_VAT_INVOICE_';//'_POST_ORDER_FULFILLMENT_DATA_';
$param['FeedOptions']      = 'metadata:OrderId='.$amz_order_id.';metadata:TotalAmount='.$TotalAmount.';metadata:TotalVATAmount='.$TotalVATAmount.';metadata:InvoiceNumber='.$InvoiceNumber.';metadata:documenttype=Invoice';
$param['SignatureMethod']  = 'HmacSHA256';
$param['SignatureVersion'] = '2';
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2009-01-01';
$param['MarketplaceIdList.Id.1']    = 'A1PA6795UKMFR9';
$param['PurgeAndReplace']   = 'false'; 

$secret = 'XXXXX';

$url = array();
foreach ($param as $key => $val) {
    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));
    $url[] = "{$key}={$val}";
}
sort($url);

$file = fopen($link_to_pdf_file,"r");
$pdf_feed=fread($file,filesize($FileName));
fclose($file);

$amazon_feed='<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>'.$param['Merchant'].'</MerchantIdentifier>
</Header>
<MessageType>OrderFulfillment</MessageType>
<Message>
<MessageID>'.getserial().'</MessageID>
<OrderFulfillment>
<AmazonOrderID>'.$amz_order_id.'</AmazonOrderID>
<FulfillmentDate>'.$param['Timestamp'].'</FulfillmentDate>
<FulfillmentData>
<CarrierName>Deutsche Post</CarrierName>
<ShippingMethod>Brief</ShippingMethod>
<ShipperTrackingNumber></ShipperTrackingNumber>
</FulfillmentData>
</OrderFulfillment>
</Message>
</AmazonEnvelope>';

    $arr   = implode('&', $url);
    $sign  = 'POST' . "\n";
    $sign .= 'mws.amazonservices.de' . "\n";
    $sign .= '/Feeds/'.$param['Version'].'' . "\n";
    $sign .= $arr;
    $signature = hash_hmac("sha256", $sign, $secret, true);
    $httpHeader     =   array();
    $httpHeader[]   =   'Transfer-Encoding: chunked';
    $httpHeader[]   =   'Content-Type: application/octet-stream';//application/pdf
    $httpHeader[]   =   'Content-MD5: ' . base64_encode(md5($pdf_feed, true));
    $httpHeader[]   =   'Expect:';
    $httpHeader[]   =   'Accept:';
    $signature = urlencode(base64_encode($signature));
    $link  = "https://mws.amazonservices.de/Feeds/".$param['Version']."?";
    $link .= $arr . "&Signature=" . $signature;
    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $amazon_feed);
    $response['reply'] = curl_exec($ch);
    $response['info'] = curl_getinfo($ch);      
    curl_close($ch);
  ?>
3dphilip
  • 21
  • 4
  • Are you posting to the right endpoint? Your marketplace ID implies Germany, which uses the mws-eu endpoint: https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html Also I expect that the parameter name should be singular, ```MarketplaceId```, because ```MarketplaceIdList``` implies multiple. – David Dec 04 '19 at 15:59
  • I have tried several endpoints, 'mws.amazonservices.de' is working. For the param 'MarketplaceId' I have tried all the different formats that I could find online: `'Marketplace', 'MarketplaceId', 'MarketplaceList.Id', 'MarketplaceId.Id.1'`, which does not seem to make any difference – 3dphilip Dec 04 '19 at 16:10
  • There seems to be no functional difference between the endpoints `mws.amazonservices.de` and `mws-eu.amazonservices.com` – 3dphilip Dec 04 '19 at 16:17
  • I have found the solution and updated my code. Solution: The Marketplace Id Parameter name must be `MarketplaceIdList.Id.1` – 3dphilip Dec 05 '19 at 16:53
  • Please don't switch off `CURLOPT_SSL_VERIFYPEER`. It is making your code vulnerable to attacks. – Dharman Feb 07 '20 at 11:05
  • 1
    can you write working code in answer $amazon_feed is also not defined – Ketan Borada Jul 11 '20 at 11:06
  • I have updated the code above and added $amazon_feed. – 3dphilip Nov 16 '20 at 12:12
  • I am struggling to see how you are submitting the pdf content `$pdf_feed`. It looks like it is only used for `md5` calculation – Optimus Nov 16 '20 at 12:22
  • I am not a professional programmer, but as far as I understand it, the pdf file itself is transmitted as md5 encrypted content – 3dphilip Nov 20 '20 at 14:20

1 Answers1

1

There is no XML to post with this feed. FeedContent (CURLOPT_POSTFIELDS) is the content of the PDF ($pdf_feed)

Optimus
  • 1,703
  • 4
  • 22
  • 41