Braham Dev Yadav
Braham Dev Yadav

Reputation: 363

How to retrieve the data from stripe responses using php

Here is my php code for stripe transaction for taking the application fee from customer using my platform:

$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
$charge = \Stripe\Charge::create(
  array(
    "amount" => 1000, // amount in cents
    "currency" => "usd",
    "source" => $token,
    "description" => "Event charge",
    "application_fee" => 123 // amount in cents
  ),
  array("stripe_account" => $sInfo->stripe_user_id)
);
echo '<pre>';
print_r($charge);

and here is my response (partial)

Stripe\Charge Object
(
[_opts:protected] => Stripe\Util\RequestOptions Object
    (
        [headers] => Array
            (
                [Stripe-Account] => acct_16JkaUHzfYmjyH68
            )

        [apiKey] => sk_test_mHnhDuaVjnKmdkEApnYAKfGY
    )

[_values:protected] => Array
    (
        [id] => ch_16K6q5HzfYmjyH786HG5a2gp
        [object] => charge
        [created] => 1435840249
        [livemode] => 
        [paid] => 1
        [status] => succeeded
        [amount] => 1000
        [currency] => usd
        [refunded] => 
        [source] => Stripe\Card Object

i am having difficulty for how i can catch the the value "id => ch_16K6q5HzfYmjyH786HG5a2gp" from _values:protected array

i have tried the following syntaxes

$charge->_values:protected and $charge['_values:protected']

but not able to grab the response, can any one here to help to catch the response in stripe connect transactions using php

Upvotes: 2

Views: 11273

Answers (3)

RVPatel
RVPatel

Reputation: 41

try this to convert object to simple array form

$charge->__toArray(TRUE);

Upvotes: 2

Reginald Goolsby
Reginald Goolsby

Reputation: 645

For anyone that may come across this, Stripe's PHP library has a function to create an array

public function jsonSerialize()
{
    return $this->__toArray(true);
}

Use this to get an workable Array from the Object. Ex.

$charge->jsonSerialize();

Upvotes: 11

Alex Andrei
Alex Andrei

Reputation: 7283

try this instead of dumping the entire object

print $charge->id;

Upvotes: 7

Related Questions