user2355051
user2355051

Reputation: 645

Parsing Output from Stripe_Charge Stripe Payments

The following output is a result of calling var_export($charge);

How can I access the output of 'paid'=>true from $charge? Any ideas would be appreciated.

I have tried $charge->_values->paid, $charge->paid, etc.. I haven't had any luck.

I have also tried $charge['_values']['paid'];

Stripe_Charge::__set_state(array(
   '_apiKey' => 'sk_test_BPZyFpcAM',
   '_values' => 
  array (
    'id' => 'ch_102kMF29T6',
    'object' => 'charge',
    'created' => 1381688,
    'livemode' => false,
    'paid' => true,
    'amount' => 104000,
    'currency' => 'usd',
    'refunded' => false,
    'card' => 
    Stripe_Card::__set_state(array(
       '_apiKey' => 'sk_test_BPZyFpc',
       '_values' => 
      array (
        'id' => 'card_102kMF29T6',
        'object' => 'card',
        'last4' => '4242',
        'type' => 'Visa',
        'exp_month' => 2,
        'exp_year' => 2015,
        'fingerprint' => '7sRY4jiFM',
        'customer' => NULL,
        'country' => 'US',
        'name' => NULL,
        'address_line1' => NULL,
        'address_line2' => NULL,
        'address_city' => NULL,
        'address_state' => NULL,
        'address_zip' => NULL,
        'address_country' => NULL,
        'cvc_check' => 'pass',
        'address_line1_check' => NULL,
        'address_zip_check' => NULL,
      ),
       '_unsavedValues' => 
      Stripe_Util_Set::__set_state(array(
         '_elts' => 
        array (
        ),
      )),
       '_transientValues' => 
      Stripe_Util_Set::__set_state(array(
         '_elts' => 
        array (
        ),
      )),
       '_retrieveOptions' => 
      array (
      ),
    )),
    'captured' => true,
    'refunds' => 
    array (
    ),
    'balance_transaction' => 'txn_102kMF29T6Z',
    'failure_message' => NULL,
    'failure_code' => NULL,
    'amount_refunded' => 0,
    'customer' => NULL,
    'invoice' => NULL,
    'description' => '[email protected]',
    'dispute' => NULL,
    'metadata' => 
    array (
    ),
  ),
   '_unsavedValues' => 
  Stripe_Util_Set::__set_state(array(
     '_elts' => 
    array (
    ),
  )),
   '_transientValues' => 
  Stripe_Util_Set::__set_state(array(
     '_elts' => 
    array (
    ),
  )),
   '_retrieveOptions' => 
  array (
  ),
))

Upvotes: 5

Views: 4580

Answers (4)

Dharmender Tuli
Dharmender Tuli

Reputation: 109

you can use __toArray() method:

$array = $collection->__toArray(true);

This will work for this case

Upvotes: 2

Banty Roy
Banty Roy

Reputation: 967

whatever returns please parse it with json_decode and you will get a assoc array in php i.e suppose return array is $ret

$ret = json_decode($ret); 

$ret->paid;
$ret->captured;
$ret->card->id; 
and so on..

Hope it will help you.

Upvotes: 1

Andre Sugai
Andre Sugai

Reputation: 157

You can use the __toArray($recursive = false) function to get the data in array form.

Example:

$chargeArray = $charge->__toArray(true);
echo $chargeArray['paid'];

I was also able to access the object's data using an array structure before even converting to an array. For me, $charge['paid'] gets me the value.

Upvotes: 13

i_a
i_a

Reputation: 2763

echo gettype($charge->paid); //boolean
var_dump($charge->paid); //bool(true)

if($charge->paid==true)echo "==true"; //==true
if($charge->paid===true)echo "===true"; //===true
if($charge->paid==1)echo "==1"; //==1
if($charge->paid===1)echo "===1"; //nada, it's a boolean, not numeric

So using if($charge->paid===true), you should be guaranteed that you are testing what you want to know.

Here in the output via print_r, you can see that paid is displayed as 1.

 [_values:protected] => Array ( [id] => ch_10as29724hknftGBm9TxC [object] => charge [created] => 1384696845 [livemode] => [paid] => 1 [amount] => 1400

Thus, depending on where and how paid is being evaluated, it may appear as a number, string, or a boolean. You could run a series of tests for the three possibilities, or just use if($charge->paid){...}

When paid is false, it would probably be returned as empty (""), 0, or a boolean false. A possible thing to look out for is the boolean false being transposed to a string, in which case it would be interpreted as true.

$val = "false";
if($val){
    echo "true"; //true
}

So, in your test, just add intval($val) to normalize the values and be on the safe side.

if(intval($charge->paid)){...}

Cheers

Upvotes: 0

Related Questions