Abdullah Ergun
Abdullah Ergun

Reputation: 21

PHP Assign Return Object Value to Local Variable

I am currently working on a WordPress/Woocomerce project I am trying to write a plugin which calls the stripe API using the following method

$stripe = new \Stripe\StripeClient('apikey');

$stripe->charges->create([
  'amount' => 1500,
  'currency' => 'gbp',
  'source' => 'accountid',
]); 
}

This API returns an object containing information regarding the call for example

"id": "callid",
"object": "payment_intent",
"amount":43650,
"amount_capturable":0,

I want to assign this ID in the return object to a local variable. How can I achieve this ? I am a .net developer and my PHP skills are limited I need the simple as possible solution.

Thank you.

Upvotes: 1

Views: 73

Answers (1)

Olumuyiwa
Olumuyiwa

Reputation: 343

After confirming the response is 200 and contains the “id” in the your response object, you can simply call the property ‘id’ on the object to get the value of id and save it to a new variable as below.

$response = $stripe->charges->create([
    'amount' => 1500,
    'currency' => 'gbp',
    'source' => 'accountid',
]); 

/* 
   Confirm response status is 200 and 
   response has id field before proceeding
*/

$id = $response->id;

Upvotes: 1

Related Questions