Amazon payment integeration in Laravel and Vue.js

296 Views Asked by At

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";
        }
    }
1

There are 1 best solutions below

0
On

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.