How to retrieve the data from stripe responses using php

10,183

Solution 1

try this instead of dumping the entire object

print $charge->id;

Solution 2

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();

Solution 3

try this to convert object to simple array form

$charge->__toArray(TRUE);
Share:
10,183
Braham Dev Yadav
Author by

Braham Dev Yadav

PHP Web Developer - CodeIgniter Developer

Updated on July 11, 2022

Comments

  • Braham Dev Yadav
    Braham Dev Yadav almost 2 years

    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

  • Braham Dev Yadav
    Braham Dev Yadav almost 9 years
    and for other inner arrays also it worked as the same way $charge->source->id or $charge->source->last4 , $charge->source->fingerprint etc. thanks again