Chhorn Soro
Chhorn Soro

Reputation: 3151

How to CompleteCheckoutSession for PHP Amazon Pay SDK?

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?

Upvotes: 1

Views: 877

Answers (3)

Abdullah Jatu
Abdullah Jatu

Reputation: 61

$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

Upvotes: 0

Debbie Martindale
Debbie Martindale

Reputation: 96

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.

Upvotes: 2

marcus.kreusch
marcus.kreusch

Reputation: 753

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.

Upvotes: 2

Related Questions