Amazon MWS PHP Mark order as Shipped - Submit Order Fulfillment with Shipping with XML in variable

耗尽温柔 提交于 2019-12-22 00:27:11

问题


How to mark an order shipped in Amazon MWS by using an XML feed - using the correct endpoint, service, version and headers?

This was the question, and it took me 3 days to manage it since amazon api is extremely chaotic, documentation is medieval, and most critical information like proper endpoint addresses to submit any particular information is a mystery to find. I was only able to create it by imitating a request at scratchpad, randomly testing sections, combining the examples for other requests for totally different sections and actions. Submitting XML feeds is the only way to manage your orders currently.

So, this may help someone.


回答1:


I was able to make below simple code work :

                        $param = array();
                        $param['AWSAccessKeyId']   = 'YOURKEY'; 
                        $param['Action']           = 'SubmitFeed'; 
                        $param['Merchant']         = 'YOURMERCHANTID'; 
                        $param['FeedType']         = '_POST_ORDER_FULFILLMENT_DATA_'; 
                        $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['MarketplaceId.Id.1']    = 'MARKETPLACEID';
                        $param['PurgeAndReplace']    = 'false';

                        $secret = 'YOURSECRETKEY';

                        $url = array();
                        foreach ($param as $key => $val) {

                            $key = str_replace("%7E", "~", rawurlencode($key));
                            $val = str_replace("%7E", "~", rawurlencode($val));
                            $url[] = "{$key}={$val}";
                        }

                        $amazon_feed='<?xml version="1.0"?>
                            <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
                            <Header>
                            <DocumentVersion>1.01</DocumentVersion>
                            <MerchantIdentifier>YOURMERCHANTID</MerchantIdentifier>
                            </Header>
                            <MessageType>OrderFulfillment</MessageType>
                            <Message>
                            <MessageID>1</MessageID>
                            <OrderFulfillment>
                            <AmazonOrderID>YOUROWNORDERIDHERE</AmazonOrderID>
                            <FulfillmentDate>'.$param['Timestamp'].'</FulfillmentDate>
                            <FulfillmentData>
                            <CarrierName>TRACKINGCONAMEHERE</CarrierName>
                            <ShippingMethod>TRACKINGCONAMEORMETHODHERE</ShippingMethod>
                            <ShipperTrackingNumber>TRACKINGNOHERE</ShipperTrackingNumber>
                            </FulfillmentData>
                            </OrderFulfillment>
                            </Message>
                            </AmazonEnvelope>';


                        sort($url);

                        $arr   = implode('&', $url);

                        $sign  = 'POST' . "\n";
                        $sign .= 'mws.amazonservices.com' . "\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/xml';
                        $httpHeader[]   =   'Content-MD5: ' . base64_encode(md5($amazon_feed, true));
                        $httpHeader[]   =   'Expect:';
                        $httpHeader[]   =   'Accept:';              

                        $signature = urlencode(base64_encode($signature));

                        $link  = "https://mws.amazonservices.com/Feeds/".$param['Version']."?";
                        $link .= $arr . "&Signature=" . $signature;
                        echo '<br>';
                        echo($link); //for debugging - you can paste this into a browser and see if it loads.
                        echo '<br>';
                        $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 = curl_exec($ch);
                        $info = curl_getinfo($ch);
                        curl_close($ch);

Gives the below output :

<?xml version="1.0"?><SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">SubmitFeedResult>FeedSubmissionInfo>FeedSubmissionId>XXXXXXXXXXXXXXXXXXX</FeedSubmissionId><FeedType>_POST_ORDER_FULFILLMENT_DATA_</FeedType><SubmittedDate>2014-09-09T00:36:29+00:00</SubmittedDate><FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus></FeedSubmissionInfo>/SubmitFeedResult>ResponseMetadata>RequestId>XXXXXXXXXXXXXXXXXXX</RequestId></ResponseMetadata></SubmitFeedResponse><?xml version="1.0"?><SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/"><SubmitFeedResult><FeedSubmissionInfo><FeedSubmissionId>XXXXXXXXXXXXXXXXXXX</FeedSubmissionId>FeedType>_POST_ORDER_FULFILLMENT_DATA_</FeedType><SubmittedDate>2014-09-09T00:36:29+00:00</SubmittedDate>FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus></FeedSubmissionInfo>/SubmitFeedResult>ResponseMetadata>RequestId>XXXXXXXXXXXXXXXXXXX</RequestId></ResponseMetadata></SubmitFeedResponse>


来源:https://stackoverflow.com/questions/25735271/amazon-mws-php-mark-order-as-shipped-submit-order-fulfillment-with-shipping-wi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!