0

I am facing issue in Amazon payment gateway while completing checkoutsession.

Error which I am facing:

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

My code:

public function completeCheckout(Request $request){
        $amazonpay_config = array(
            'public_key_id' => config('amazonkey.public_key_id'),
            'private_key'   => config('amazonkey.private_key'),
            'region'        => 'UK',
            'sandbox'       => true
        );

        $payload = array(
            "chargeAmount" => array(
                "amount" => "100",
                "currencyCode"=>  "USD"
            ),
             
         );

         $payload = json_encode($payload);

         try {
            $checkoutSessionId = $request->checkoutSessionId;
            // dd($checkoutSessionId);
            $client = new ApayAPI($amazonpay_config);
            
            $result = $client->completeCheckoutSession($checkoutSessionId, $payload, $headers = null);
            if ($result['status'] === 200) {
                // dd($result);
                $response = json_decode($result['response'], true);
                return $response;
                // $response = json_decode($result['response'], true);
                // $amazonPayRedirectUrl = $response['webCheckoutDetails']['amazonPayRedirectUrl'];
                // echo "amazonPayRedirectUrl=$amazonPayRedirectUrl\n";
            } else {
                // check the error
                echo 'status=' . $result['status'] . '; response=' . $result['response'] . "\n";
            }
        } catch (\Exception $e) {
            // handle the exception
            echo $e . "\n";
        }
    }
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sufyan
  • 1
  • 1
  • Answer to a similar issue found here, https://stackoverflow.com/questions/65535379/amazon-pay-sdk-completecheckoutsession-error – mzm Dec 20 '22 at 19:09
  • hi mzm i have read the given link discerption but in my case amazon in not returning **amazonPayRedirectUrl ** in my updateSession response – Sufyan Dec 20 '22 at 19:37

1 Answers1

0

Make sure that prior to sending a request to complete checkout session that 1/the buyer has been redirected to checkoutResultReturnUrl (returned in response to update checkout session) and 2/ there are no constraints in the constraint object returned from update checkout session. If both of these conditions have been satisfied, know that complete checkout session must be requested within 24 hours of a buyer's redirect to checkoutResultReturnUrl or the checkout session will automatically cancelled.