Skip to content
Advertisement

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 AmazonPayAPIClient($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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement