简体   繁体   中英

Extract value from Stripe object

How do I get the value of 'name'?

I've been trying various methods, including array($charge) and __toArray.

First works, second does not:

$charge_id = $charge->id;
echo $charge_id;
$name=$charge->_values->name;
echo $name;

Here is portion of the object returned by Stripe (up to the name I want to extract):

Stripe\Charge::__set_state(array(
   '_opts' => 
  Stripe\Util\RequestOptions::__set_state(array(
     'headers' => 
    array (
    ),
     'apiKey' => 'sk_test_...',
  )),
   '_values' => 
  array (
    'id' => 'ch_73JaKii01nmbpZ',
    'object' => 'charge',
    'created' => 1443251926,
    'livemode' => false,
    'paid' => true,
    'status' => 'succeeded',
    'amount' => 1000,
    'currency' => 'usd',
    'refunded' => false,
    'source' => 
    Stripe\Card::__set_state(array(
       '_opts' => 
      Stripe\Util\RequestOptions::__set_state(array(
         'headers' => 
        array (
        ),
         'apiKey' => 'sk_test_...',
      )),
       '_values' => 
      array (
        'id' => 'card_73JZrJCDzl877J',
        'object' => 'card',
        'last4' => '1111',
        'brand' => 'Visa',
        'funding' => 'unknown',
        'exp_month' => 12,
        'exp_year' => 2021,
        'fingerprint' => 'hhzQQZIXaAVDNMPP',
        'country' => 'US',
        'name' => 'Steve Veltkamp',

(Note: you should edit your question to remove your secret key and replace it with eg 'sk_test_...' . Even if it's just a test key, secret keys are not meant to be shared.)

The property you're trying to access is the cardholder's name. The source attribute of a charge object is actually a card object :

php > echo get_class($charge);
Stripe\Charge

php > echo get_class($charge->source);
Stripe\Card

So to get the cardholder's name, all you have to do is:

$name = $charge->source->name;

Note that name is an optional attribute for cards, so it's possible that you'll get NULL , an empty string, an email address, etc.

according to this: https://github.com/stripe/stripe-php/blob/master/lib/Charge.php

you may try to play with retrieve method:

$charge::retrieve($charge->id);

or ( https://github.com/stripe/stripe-php/blob/master/lib/StripeObject.php ):

$chargeArr = $charge->__toArray(true);

add true as argument for recursion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM