cdwyer cdwyer - 6 months ago 96
PHP Question

Stripe Response when Retrieving Customer Sources

I am trying to retrieve all cards for a particular customer, by supplying a customer ID. Per the documentation, I am expecting the following response:

Stripe\Collection JSON: {
"object" => "list",
"url" => "/v1/customers/cu_16jdAj2eZvKYlo2CbrCP4HRs/sources",
"has_more" => false,
"data" => [
[0] => Stripe\Card JSON: {
"id": "card_14bIFkou9GUI42eZvKYlo2Cs",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 11,
"exp_year": 2018,
"country": "US",
"name": "callingsudhanshu@gmail.com",
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"cvc_check": null,
"address_line1_check": null,
"address_zip_check": null,
"tokenization_method": null,
"dynamic_last4": null,
"metadata": {
},
"customer": "cus_6Nb0wxYHigk2mX"
}
[1] => <Stripe\Card[...] ...>
[2] => <Stripe\Card[...] ...>
]
}


And here's the request I'm making to Stripe. Trying to pull
data
(ie, an array of all cards) from the
$card_list
:

\Stripe\Stripe::setApiKey($this->stripe_sk);
$card_list = \Stripe\Customer::retrieve($customer_id)->sources->all(array(
'object' => 'card'
));

// Return a list of all cards.
$card_list = $card_list->data;
return $card_list;


I would like to know why the Stripe response from
$card_list
contains additional/unexpected data. Here's what I'm actually getting back:

array(1) {
[0]=&gt;
object(Stripe\Card)#292 (5) {
["_opts":protected]=&gt;
object(Stripe\Util\RequestOptions)#294 (2) {
["headers"]=&gt;
array(0) {
}
["apiKey"]=&gt;
string(32) "sk_test_d3..."
}
["_values":protected]=&gt;
array(23) {
["id"]=&gt;
string(29) "card_16jLIwLBSNgilYpnoEiPhNhA"
["object"]=&gt;
string(4) "card"
["last4"]=&gt;
string(4) "4242"
["brand"]=&gt;
string(4) "Visa"
["funding"]=&gt;
string(6) "credit"
["exp_month"]=&gt;
int(8)
["exp_year"]=&gt;
int(2016)
["fingerprint"]=&gt;
string(16) "T5RzXLIiXrF0nyqH"
["country"]=&gt;
string(2) "US"
["name"]=&gt;
NULL
["address_line1"]=&gt;
NULL
["address_line2"]=&gt;
NULL
["address_city"]=&gt;
NULL
["address_state"]=&gt;
NULL
["address_zip"]=&gt;
NULL
["address_country"]=&gt;
NULL
["cvc_check"]=&gt;
string(4) "pass"
["address_line1_check"]=&gt;
NULL
["address_zip_check"]=&gt;
NULL
["tokenization_method"]=&gt;
NULL
["dynamic_last4"]=&gt;
NULL
["metadata"]=&gt;
object(Stripe\AttachedObject)#261 (5) {
["_opts":protected]=&gt;
object(Stripe\Util\RequestOptions)#294 (2) {
["headers"]=&gt;
array(0) {
}
["apiKey"]=&gt;
string(32) "sk_test_d..."
}
["_values":protected]=&gt;
array(0) {
}
["_unsavedValues":protected]=&gt;
object(Stripe\Util\Set)#262 (1) {
["_elts":"Stripe\Util\Set":private]=&gt;
array(0) {
}
}
["_transientValues":protected]=&gt;
object(Stripe\Util\Set)#306 (1) {
["_elts":"Stripe\Util\Set":private]=&gt;
array(0) {
}
}
["_retrieveOptions":protected]=&gt;
array(0) {
}
}
["customer"]=&gt;
string(18) "cus_6xCtshHPQiYSdI"
}
["_unsavedValues":protected]=&gt;
object(Stripe\Util\Set)#270 (1) {
["_elts":"Stripe\Util\Set":private]=&gt;
array(0) {
}
}
["_transientValues":protected]=&gt;
object(Stripe\Util\Set)#120 (1) {
["_elts":"Stripe\Util\Set":private]=&gt;
array(0) {
}
}
["_retrieveOptions":protected]=&gt;
array(0) {
}
}
}


Hoping that someone can explain the "extended" version of the response, that I'm getting here. Thanks in advance!

Answer

I stuck in this problem a couple of days ago and I've managed to solve it using json_encode() API like this (need to export this as a AJAX return on my side).

$cards = \Stripe\Customer::retrieve($_SESSION['stripe_customer_id'])->sources->all(array("object" => "card"));
$list = json_encode($cards->data);
echo($list);

Hope this helps.

BTW : Your response looks alike a var_dump() output.