amzn / amazon-pay-sdk-php

Amazon Pay PHP SDK
https://pay.amazon.com/documentation
Apache License 2.0
208 stars 93 forks source link

Different amazon response format to "SetOrderReferenceDetails" method #19

Closed KoulSlou closed 8 years ago

KoulSlou commented 8 years ago

Hello everybody,

I'm investigating support case when user receives 500 error during checkout process.

I have found the following message in log file:

PHP Warning:  simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/authorcloudware/public_html/vendor/amzn/login-and-pay-with-amazon-sdk-php/PayWithAmazon/ResponseParser.php on line 69

PHP Warning:  simplexml_load_string(): HTTP/1.1 200 OK in /home/authorcloudware/public_html/vendor/amzn/login-and-pay-with-amazon-sdk-php/PayWithAmazon/ResponseParser.php on line 69

PHP Warning:  simplexml_load_string(): ^ in /home/authorcloudware/public_html/vendor/amzn/login-and-pay-with-amazon-sdk-php/PayWithAmazon/ResponseParser.php on line 69

PHP Fatal error:  Call to a member function addChild() on a non-object in /home/authorcloudware/public_html/vendor/amzn/login-and-pay-with-amazon-sdk-php/PayWithAmazon/ResponseParser.php on line 72

I started to dig into this problem. So, the first step in checkout process is send "SetOrderReferenceDetails" request. I do it, I receive response from Amazon and I call toArray() function on the received response in order to check "OrderStatus".

If you check toArray() function in ResponseParser class you will see that it calls private _simpleXmlObject method. And here is a particular line that causes problem:

 $response = simplexml_load_string((string) $response['ResponseBody']);

For the problem order $response['ResponseBody'] contains:

HTTP/1.1 200 OK
Date: Fri, 13 Nov 2015 16:45:03 GMT
Server: AmazonMWS
X-Amz-Date: Fri, 13 Nov 2015 16:45:03 GMT
x-amzn-Authorization: AAA SignedHeaders=X-Amz-Date;x-amzn-ActionTrace, identity=com.amazon.aaa.MarketplacePaymentService.AndromedaControlService.amzn1.aaa.id.qhz3ylg755gkejyk5sh44qm3wy.Default/1, Signed=true, Encrypted=true, Signature=8OHYWjZ8ApH0pN409nW4qrGrBtSu138H1psENEFcySw=, Algorithm=HmacSHA256
x-amzn-ActionTrace: amzn1.tr.e2da79bd-8a25-11e5-a6ea-0a5838420000.0..@mfNis
x-mws-request-id: 340a07b0-dc8e-4f86-a0d6-113bc09bc4d6
x-mws-timestamp: 2015-11-13T16:45:02.870Z
x-mws-response-context: C7YhiTl9ytxfKxXnS3jV02ZvK0x/RJ1ejYwnsY8n/f7xgbImW57/J/7WtlczRwGWR0oQhWsLlyb6 ZgMY4b+mug==
Content-Type: text/xml
Content-Length: 1446
Vary: Accept-Encoding,User-Agent

<SetOrderReferenceDetailsResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
  <SetOrderReferenceDetailsResult>
    <OrderReferenceDetails>
      <OrderReferenceStatus>
        <State>Draft</State>
      </OrderReferenceStatus>
      <ExpirationTimestamp>2016-05-11T16:07:38.659Z</ExpirationTimestamp>
      <SellerOrderAttributes>
        <StoreName>StoreName</StoreName>
        <SellerOrderId>571</SellerOrderId>
      </SellerOrderAttributes>
      <OrderTotal>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>99.80</Amount>
      </OrderTotal>
      <ReleaseEnvironment>Live</ReleaseEnvironment>
      <SellerNote>book &quot;Tilde skills: Design and Produce Text Documents, Word 2013&quot; (digital)  - 1 item(s);
book &quot;Accelerated Tilde Skills Produce Spreadsheets: Excel 2013&quot; (digital)  - 1 item(s);
book &quot;Tilde skills: Produce Desktop Published Documents, Publisher 2013&quot; (digital)  - 1 item(s);
book &quot;Tilde skills: Create Electronic Presentations, PowerPoint 2013&quot; (digital)  - 1 item(s);</SellerNote>
      <AmazonOrderReferenceId>P01-5757159-5192176</AmazonOrderReferenceId>
      <CreationTimestamp>2015-11-13T16:07:38.659Z</CreationTimestamp>
    </OrderReferenceDetails>
  </SetOrderReferenceDetailsResult>
  <ResponseMetadata>
    <RequestId>340a07b0-dc8e-4f86-a0d6-113bc09bc4d6</RequestId>
  </ResponseMetadata>
</SetOrderReferenceDetailsResponse>

that doesn't look like a valid XML, while for another order (which go through system without any problem) $response['ResponceBody'] looks like:

<SetOrderReferenceDetailsResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
  <SetOrderReferenceDetailsResult>
    <OrderReferenceDetails>
      <OrderReferenceStatus>
        <State>Draft</State>
      </OrderReferenceStatus>
      <ExpirationTimestamp>2016-05-11T16:07:38.659Z</ExpirationTimestamp>
      <SellerOrderAttributes>
        <StoreName>StoreName</StoreName>
        <SellerOrderId>573</SellerOrderId>
      </SellerOrderAttributes>
      <OrderTotal>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>14.95</Amount>
      </OrderTotal>
      <ReleaseEnvironment>Live</ReleaseEnvironment>
      <SellerNote>book &quot;The Psychedelic Renaissance&quot; (digital)  - 1 item(s);</SellerNote>
      <AmazonOrderReferenceId>P01-5757159-5192176</AmazonOrderReferenceId>
      <CreationTimestamp>2015-11-13T16:07:38.659Z</CreationTimestamp>
    </OrderReferenceDetails>
  </SetOrderReferenceDetailsResult>
  <ResponseMetadata>
    <RequestId>a584e730-07fd-4d2d-9fae-5c9059c428f5</RequestId>
  </ResponseMetadata>
</SetOrderReferenceDetailsResponse>

So, no HTTP headers included in response. My question is why it happens? Why there are different response format for one method? I will appreciate any help.

Regards, Tamara.

shrakuma commented 8 years ago

Hi Tamara Thanks for bringing this to our attention. So to summarize , some responses are coming with headers attached and some don't. I am assuming there is no pattern here and it's random as to which responses come with the headers and which don't. The one having headers is failing to be parsed into array. Is that correct?

If so, could you please let me know how often this is occurring.

KoulSlou commented 8 years ago

Hello Shrakuma,

Thank you for response. Yes, you understood it right. The one with headers can not be processed by toArray() function in "ResponseParser" class.

It happens with one set of order items but all the time. If I try to purchase another items everything works as expected - HTTP headers are not attached to the ResponseBody and response is processed correctly.

Tamara.

shrakuma commented 8 years ago

can you please give me the input you provide to the SetOrderReferenceDetails API call for that one set of order items. I will test it to reproduce the issue

KoulSlou commented 8 years ago

Ok, here are parameters that I pass to setOrderReferenceDetails() in PayWithAmazon/Client class:

array (
  'amazon_order_reference_id' => 'P01-7152974-9047150',
  'amount' => 99.799999999999997,
  'currency_code' => 'USD',
  'seller_note' => 'book "Tilde skills: Design and Produce Text Documents, Word 2013" (digital)  - 1 item(s);
book "Accelerated Tilde Skills Produce Spreadsheets: Excel 2013" (digital)  - 1 item(s);
book "Tilde skills: Produce Desktop Published Documents, Publisher 2013" (digital)  - 1 item(s);
book "Tilde skills: Create Electronic Presentations, PowerPoint 2013" (digital)  - 1 item(s);
',
  'store_name' => 'AuthorCloudware.com',
  'seller_order_id' => '579',
)

And here is Parameter String that actually is sent to Amazon server

'AWSAccessKeyId=AKIAIEPJAWY6PLTT5U6A&Action=SetOrderReferenceDetails&AmazonOrderReferenceId=P01-7152974-9047150&OrderReferenceAttributes.OrderTotal.Amount=99.8&OrderReferenceAttributes.OrderTotal.CurrencyCode=USD&OrderReferenceAttributes.SellerNote=book%20%22Tilde%20skills%3A%20Design%20and%20Produce%20Text%20Documents%2C%20Word%202013%22%20%28digital%29%20%20-%201%20item%28s%29%3B%0Abook%20%22Accelerated%20Tilde%20Skills%20Produce%20Spreadsheets%3A%20Excel%202013%22%20%28digital%29%20%20-%201%20item%28s%29%3B%0Abook%20%22Tilde%20skills%3A%20Produce%20Desktop%20Published%20Documents%2C%20Publisher%202013%22%20%28digital%29%20%20-%201%20item%28s%29%3B%0Abook%20%22Tilde%20skills%3A%20Create%20Electronic%20Presentations%2C%20PowerPoint%202013%22%20%28digital%29%20%20-%201%20item%28s%29%3B&OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId=579&OrderReferenceAttributes.SellerOrderAttributes.StoreName=AuthorCloudware.com&SellerId=A1H0QDJ0J9ZUMG&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-11-13T21%3A04%3A27.000Z&Version=2013-01-01&Signature=FR5Auv6MZ8E2Eu0AF6ULYJtygPaYvyRO2vW%2BBAqd8ew%3D'
shrakuma commented 8 years ago

confirmed it's a bug. The seller note being different is adding headers. Will add logic to check for headers and remove them so that smple_xml_object doesn't crash.

islandskater43 commented 8 years ago

Rather than just code around whether or not headers are present, can we determine why the headers are still present in this case?

On Fri, Nov 13, 2015 at 4:56 PM, shravan notifications@github.com wrote:

confirmed it's a bug. The seller note being different is adding headers. Will add logic to check for headers and remove them so that smple_xml_object doesn't crash.

— Reply to this email directly or view it on GitHub https://github.com/amzn/login-and-pay-with-amazon-sdk-php/issues/19#issuecomment-156601466 .

Matt Neilson (860) 861-1750

shrakuma commented 8 years ago

Headers are always there The headers are split in this line line : 1468 : // Split the API response into Response Body and the other parts of the response into other list($other, $responseBody) = explode("\r\n\r\n", $response, 2); In the case for when the seller note contains special characters as below: 'book "Tilde skills: Design and Produce Text Documents, Word 2013" (digital) - 1 item(s); book "Accelerated Tilde Skills Produce Spreadsheets: Excel 2013" (digital) - 1 item(s); book "Tilde skills: Produce Desktop Published Documents, Publisher 2013" (digital) - 1 item(s); book "Tilde skills: Create Electronic Presentations, PowerPoint 2013" (digital) - 1 item(s); ' first the HTTP response is moving to response code 100 (continue) and then moving to 200OK. Hence in the response it adds another line saying HTTP 1.1 100 continue which breaks the explode statement above.

I am fixing this code now to not send headers at all by the code curl_setopt($ch, CURLOPT_HEADER, false);

and adding

$info = curl_getinfo($ch); printing/using $info for example will give the HTTP status code as needed.

shrakuma commented 8 years ago

issue fixed and verified.

KoulSlou commented 8 years ago

Described order has been processed successfully. Thank you for quick turnaround.