Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smacy's avatar
Level 1

get specific value from response paypal

this is response received from paypal how can it get $amount= purchase_units->payments>captures>amount in controller

{
   "id":"6E664330RY678345V",
   "status":"COMPLETED",
   "purchase_units":[
      {
         "reference_id":"default",
         "shipping":{
            "name":{
               "full_name":"John Doe"
            },
            "address":{
               "address_line_1":"1 Cheeseman Ave Brighton East",
               "admin_area_2":"Melbourne",
               "admin_area_1":"Victoria",
               "postal_code":"3001",
               "country_code":"AU"
            }
         },
         "payments":{
            "captures":[
               {
                  "id":"4GE11174NV766131P",
                  "status":"PENDING",
                  "status_details":{
                     "reason":"PENDING_REVIEW"
                  },
                  "amount":{
                     "currency_code":"USD",
                     "value":"2.00"
                  },
                  "final_capture":true,
                  "seller_protection":{
                     "status":"NOT_ELIGIBLE"
                  },
                  "links":[
                     {
                        "href":"https://api.sandbox.paypal.com/v2/payments/captures/4GE11174NV766131P",
                        "rel":"self",
                        "method":"GET"
                     },
                     {
                        "href":"https://api.sandbox.paypal.com/v2/payments/captures/4GE11174NV766131P/refund",
                        "rel":"refund",
                        "method":"POST"
                     },
                     {
                        "href":"https://api.sandbox.paypal.com/v2/checkout/orders/6E664330RY678345V",
                        "rel":"up",
                        "method":"GET"
                     }
                  ],
                  "create_time":"2022-05-19T08:36:15Z",
                  "update_time":"2022-05-19T08:36:15Z"
               }
            ]
         }
      }
   ],
   "payer":{
      "name":{
         "given_name":"John",
         "surname":"Doe"
      },
      "email_address":"[email protected]",
      "payer_id":"AD9L9U9LLGV7S",
      "address":{
         "address_line_1":"1 Cheeseman Ave Brighton East",
         "admin_area_2":"Melbourne",
         "admin_area_1":"Victoria",
         "postal_code":"3001",
         "country_code":"AU"
      }
   },
   "links":[
      {
         "href":"https://api.sandbox.paypal.com/v2/checkout/orders/6E664330RY678345V",
         "rel":"self",
         "method":"GET"
      }
   ]
}
0 likes
5 replies
automica's avatar

@smacy use json_decode

$result = json_decode($data);

$amount = $result->purchase_units->payments->captures->amount;
smacy's avatar
Level 1

@automica getting json_decode(): Argument #1 ($json) must be of type string, array given if you would like to see the controller then my controller is

 public function membershipStore(Request $request)
    {
        $amount=5;
        $provider = new PayPalClient;
        $provider->setApiCredentials(config('paypal'));
        $paypalToken = $provider->getAccessToken();

        $response = $provider->createOrder([
            "intent" => "CAPTURE",
            "application_context" => [
                "return_url" => route('processSuccess'),
                "cancel_url" => route('processCancel'),
            ],
            "purchase_units" => [
                0 => [
                    "amount" => [
                        "currency_code" => "USD",
                        "value" => $amount
                    ]
                ]
            ]
        ]);

        if (isset($response['id']) && $response['id'] != null) {
            foreach ($response['links'] as $links) {
                if ($links['rel'] == 'approve') {
                    return  Inertia::location($links['href']);
                }
            }

            return redirect()
                ->route('front.index')
                ->with('error', 'Something went wrong.');
        } else {

            dd('something went wrong else');
            return redirect()
                ->route('front.index')
                ->with('error', $response['message'] ?? 'Something went wrong.');
        }
    }


    public function processSuccess(Request $request)
    {


        $provider = new PayPalClient;
        $provider->setApiCredentials(config('paypal'));
        $provider->getAccessToken();
        $response = $provider->capturePaymentOrder($request['token']);

        if (isset($response['status']) && $response['status'] == 'COMPLETED') {
            $result = json_decode($response);
            $amount = $result->purchase_units->payments->captures->amount;
            dd($amount); 
            return redirect()
                ->route('membershipList')
                ->with('success', 'Transaction complete.');
        } else {
            return redirect()
                ->route('front.index')
                ->with('error', $response['message'] ?? 'Something went wrong.');
        }
    }
automica's avatar

@smacy your response has already been decoded as an array.

You can use:

$amount = $response['purchase_units']['payments']['captures']['amount'];

smacy's avatar
Level 1

@automica still getting error as

Undefined array key "payments

            $amount = $response['purchase_units']['payments']['captures']['amount']['value'];
            dd($amount);
automica's avatar

@smacy theres some nested arrays so will need to do the following

return $result['purchase_units'][0]['payments']['captures'][0]['amount'];

Please or to participate in this conversation.