1

I've follow the instruction how to integrate Amazon V2 for PHP

https://github.com/amzn/amazon-pay-api-sdk-php

But I could not find any section how can I implement the Complete Checkout Session function in order to get "chargeID".

Here is what I've implement

$payload = array(
    'webCheckoutDetails' => array(
    "checkoutResultReturnUrl" => HTTPS_SERVER . "index.php?route=payment/amazon/returnURL"
),
'paymentDetails' => array(
    'paymentIntent' => 'Authorize',
    'canHandlePendingAuthorization' => false,
    'chargeAmount' => array(
        'amount' => (int)$total_amount,
        'currencyCode' => 'JPY'
    ),
),
'merchantMetadata' => array(
    'merchantReferenceId' => $order_id,
    'merchantStoreName' => 'MWYW Online Store',
    'noteToBuyer' => 'Thank you for your order!'
)
);

try {

$client = new Amazon\Pay\API\Client($amazonpay_config);
$result = $client->updateCheckoutSession($checkoutSessionId, $payload);



$payload = array(
    'chargeAmount' => array(
            'amount' => (int)$total_amount,
            'currencyCode' => 'JPY'
        ),
);

$result = $client->completeCheckoutSession($checkoutSessionId, $payload);

But I got the error message like this

{"reasonCode":"InvalidCheckoutSessionStatus","message":"You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed"}

Could you please tell me what's wrong with my code?

Chhorn Soro
  • 3,061
  • 8
  • 27
  • 43

3 Answers3

2

The result of ::updateCheckoutSession() will give you a amazonPayRedirectUrl in webCheckoutDetails. You need to redirect the client to this URL.

After the client has finished their input on that external page they will be redirected back to your checkoutResultReturnUrl. At this point you will be able to complete the CheckoutSession.

marcus.kreusch
  • 648
  • 5
  • 15
  • Excuse me , where can I set the amazonPayRedirectUrl value? Now I got NULL from updateCheckoutSession() – Chhorn Soro Oct 06 '21 at 11:33
  • I've used this code to generate button – Chhorn Soro Oct 06 '21 at 12:00
  • $client = new Amazon\Pay\API\Client($amazonpay_config); $payload = '{"storeId":"'. $store_id .'","webCheckoutDetails":{"checkoutReviewReturnUrl":"'. $return_url .'"}}'; $signature = $client->generateButtonSignature($payload); – Chhorn Soro Oct 06 '21 at 12:00
  • @ChhornSoro The amazonPayRedirectUrl is not a URL you set. It is returned by the API and should be part of `$result` after your line `$result = $client->updateCheckoutSession($checkoutSessionId, $payload);`, if there are no constraints. – marcus.kreusch Oct 07 '21 at 12:16
2

Update Checkout Session response will include a Constraint object until all mandatory parameters have been provided. (mandatory parameters: checkoutResultReturnUrl, chargeAmount, paymentIntent)

Once there are no constraints, the response will return a unique amazonPayRedirectUrl. Redirect the buyer to that URL to have Amazon Pay run the transaction. Then the buyer will be redirected to checkoutResultReturnUrl after Amazon Pay has processed the transaction (including any decline flows if necessary). The Amazon Pay checkout session ID will be included as a query parameter on checkoutResultReturnUrl.

  • Excuse me, so what I missed on updateCheckoutSession() ? – Chhorn Soro Oct 06 '21 at 14:05
  • The workflow should be UpdateCheckoutSession -> check response for errors -> if no errors, redirect to amazonPayRedirectUrl (returned in updateCheckoutSession response) -> on checkoutResultReturnUrl page call getCheckoutSession to check for errors -> if no errors, call completeCheckoutSession. Based on the code you shared, you're calling completeCheckoutSession immediately after updateCheckoutSession. As you can see, there's a few more necessary steps, and the redirect to amazonPayRedirectUrl MUST happen -prior- to calling completeCheckoutSession. Hope that helps! – Debbie Martindale Oct 07 '21 at 15:43
0
$headers = array('x-amz-pay-Idempotency-Key' => uniqid()); // added externally

$client = new Amazon\Pay\API\Client($amazonpay_config);
$result = $client->updateCheckoutSession($checkoutSessionId, $payload , $headers);

This Worked for me for getting the web checkout details data in $response

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 19 '22 at 12:54