I am working with the stripe payment system, and essentially what happens is that after a charge is made, I output the json value, and where i would like to store the id in order to capture the charge.
Below is how the charge is conducted
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'cad',
'capture' => 'false',
'description'=> $courseTitle
));
echo $charge;
} catch(\Stripe\Error\Card $e) {
// The card has been declined
$stripChargeValid = false;
echo "failed";
}
below is what is echo
Stripe\Charge JSON: { "id": "ch_15usNKIOMQSe8X844GzjEpyM", "object": "charge", "created": 1429826450, "livemode": false, "paid": true, "status": "succeeded", "amount": 41000, "currency": "cad", "refunded": false, "source": { "id": "card_15usMsIOMQSe8X84foA2Qb69", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 1, "exp_year": 2016, "fingerprint": "GLlDGRtbDBjmZDdb", "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, "dynamic_last4": null, "metadata": [], "customer": "cus_676avKg8aDycIL" }, "captured": false, "balance_transaction": null, "failure_message": null, "failure_code": null, "amount_refunded": 0, "customer": "cus_676avKg8aDycIL", "invoice": null, "description": "Microsoft Office 2013: Transition from Office 2007\/2010", "dispute": null, "metadata": [], "statement_descriptor": null, "fraud_details": [], "receipt_email": "jonathanetienne#live.com", "receipt_number": null, "shipping": null, "application_fee": null, "refunds": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/charges\/ch_15usNKIOMQSe8X844GzjEpyM\/refunds", "data": [] } }
I would like to grab the id value so i can post it into a mysqli query
Update:
object(Stripe\Charge)#25 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#22 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) ""
}
["_values":protected]=>
array(27) {
["id"]=>
string(27) "ch_15uu0dIOMQSe8X84gJIVis9y"
["object"]=>
string(6) "charge"
["created"]=>
int(1429832731)
["livemode"]=>
bool(false)
["paid"]=>
bool(true)
["status"]=>
string(9) "succeeded"
["amount"]=>
int(338400)
["currency"]=>
string(3) "cad"
["refunded"]=>
bool(false)
["source"]=>
object(Stripe\Card)#26 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#22 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) ""
}
["_values":protected]=>
array(22) {
["id"]=>
string(29) "card_15utkNIOMQSe8X845N5xbl7y"
["object"]=>
string(4) "card"
["last4"]=>
string(4) "4242"
["brand"]=>
string(4) "Visa"
["funding"]=>
string(6) "credit"
["exp_month"]=>
int(1)
["exp_year"]=>
int(2016)
["fingerprint"]=>
string(16) "GLlDGRtbDBjmZDdb"
["country"]=>
string(2) "US"
["name"]=>
NULL
["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
["dynamic_last4"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#29 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#22 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) ""
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#33 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#34 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["customer"]=>
string(18) "cus_678HJKOahVnt6v"
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#30 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#31 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["captured"]=>
bool(false)
["balance_transaction"]=>
NULL
["failure_message"]=>
NULL
["failure_code"]=>
NULL
["amount_refunded"]=>
int(0)
["customer"]=>
string(18) "cus_678HJKOahVnt6v"
["invoice"]=>
NULL
["description"]=>
string(39) "PMP Exam Prep Training (PMBOK Guide V5)"
["dispute"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#32 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#22 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) ""
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#36 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#37 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
["fraud_details"]=>
array(0) {
}
["receipt_email"]=>
string(24) ""
["receipt_number"]=>
NULL
["shipping"]=>
NULL
["application_fee"]=>
NULL
["refunds"]=>
object(Stripe\Collection)#35 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#22 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) ""
}
["_values":protected]=>
array(5) {
["object"]=>
string(4) "list"
["total_count"]=>
int(0)
["has_more"]=>
bool(false)
["url"]=>
string(47) "/v1/charges/ch_15uu0dIOMQSe8X84gJIVis9y/refunds"
["data"]=>
array(0) {
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#39 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#40 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#27 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#28 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
Update:
Below is outputs:
$charge_json = $charge->__toJSON();
echo "<pre>";
var_dump($charge_json);
echo "</pre>";
string(1677) "{
"id": "ch_15vDxQIOMQSe8X84NBckjjil",
"object": "charge",
"created": 1429909412,
"livemode": false,
"paid": true,
"status": "succeeded",
"amount": 41000,
"currency": "cad",
"refunded": false,
"source": {
"id": "card_15vDwlIOMQSe8X843FPN4gAt",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 1,
"exp_year": 2016,
"fingerprint": "GLlDGRtbDBjmZDdb",
"country": "US",
"name": "john",
"address_line1": "",
"address_line2": null,
"address_city": "",
"address_state": "",
"address_zip": "",
"address_country": null,
"cvc_check": "pass",
"address_line1_check": "pass",
"address_zip_check": "pass",
"dynamic_last4": null,
"metadata": [],
"customer": "cus_67StWh5kSlXJWN"
},
"captured": false,
"balance_transaction": null,
"failure_message": null,
"failure_code": null,
"amount_refunded": 0,
"customer": "cus_67StWh5kSlXJWN",
"invoice": null,
"description": "",
"dispute": null,
"metadata": [],
"statement_descriptor": null,
"fraud_details": [],
"receipt_email": "",
"receipt_number": null,
"shipping": null,
"application_fee": null,
"refunds": {
"object": "list",
"total_count": 0,
"has_more": false,
"url": "\/v1\/charges\/ch_15vDxQIOMQSe8X84NBckjjil\/refunds",
"data": []
}
}"
Your echo instruction is calling the __toString method from Object. Your charge variable is however a PHPStripeObject.
That being said, I guess you could do the following to be sure to get an array at the end, containing your data:
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'cad',
'capture' => 'false',
'description'=> $courseTitle
));
$chargeArray = \Stripe\Util\Util::convertStripeObjectToArray($charge);
$id = $chargeArray['id'];
...
} catch(\Stripe\Error\Card $e) {
// The card has been declined
$stripChargeValid = false;
echo "failed";
}
Simply you can deal with $charge as a JSON object.:
$charge->id;
To know its properties and values use:
echo "<pre>";
var_dump($charge);
echo "</pre>";
Related
I have an array like below.is there any way or one line code to empty all values inside array without foreach loop.
$array=json_decode('{
"client": "4",
"gateWay": "1",
"store": "store.shop.com",
"valid": "true",
"po": 34535,
"additionalPO": 23423,
"customerNotes": "",
"orderItems": [
{
"item": "123",
"quantity": 10,
"supplierLotNo": "",
"customsValue": "",
"customsDescription": "",
"hsCode": ""
},
{
"item": "345",
"quantity": 50
}
],
"shippingInfos": [
{
"address": {
"city": "Chennai",
"country": "India",
"postalCode": "86715",
"state": "TN",
"streetAddress1": "6971 North Street",
"streetAddress2": null
},
"contact": {
"company": null,
"email": "info#store.com",
"firstName": "test",
"lastName": "test",
"phoneNo": null
},
"ServiceId": "3",
"thirdPartyAccountNo": "",
"signatureConfirmation": false,
"saturdayDelivery": false
}
]
}',true);
The expected output should be I need empty string for string values,0 for integer values.
any help would be greatly appreciated.
You can do it with array_walk, assuming boolean is set to be NULL, here is the code.
Code
<?php
$array=json_decode('{
"client": "4",
"gateWay": "1",
"store": "store.shop.com",
"valid": "true",
"po": 34535,
"additionalPO": 23423,
"customerNotes": "",
"orderItems": [
{
"item": "123",
"quantity": 10,
"supplierLotNo": "",
"customsValue": "",
"customsDescription": "",
"hsCode": ""
},
{
"item": "345",
"quantity": 50
}
],
"shippingInfos": [
{
"address": {
"city": "Chennai",
"country": "India",
"postalCode": "86715",
"state": "TN",
"streetAddress1": "6971 North Street",
"streetAddress2": null
},
"contact": {
"company": null,
"email": "info#store.com",
"firstName": "test",
"lastName": "test",
"phoneNo": null
},
"ServiceId": "3",
"thirdPartyAccountNo": "",
"signatureConfirmation": false,
"saturdayDelivery": false
}
]
}',true);
($fx = function (&$in) use (&$fx) { is_array($in) ? array_walk($in, $fx) : ($in = is_string($in) ? "" : (is_int($in) ? 0 : NULL));})($array);
var_dump($array);
Output
array(9) {
["client"]=>
string(0) ""
["gateWay"]=>
string(0) ""
["store"]=>
string(0) ""
["valid"]=>
string(0) ""
["po"]=>
int(0)
["additionalPO"]=>
int(0)
["customerNotes"]=>
string(0) ""
["orderItems"]=>
array(2) {
[0]=>
array(6) {
["item"]=>
string(0) ""
["quantity"]=>
int(0)
["supplierLotNo"]=>
string(0) ""
["customsValue"]=>
string(0) ""
["customsDescription"]=>
string(0) ""
["hsCode"]=>
string(0) ""
}
[1]=>
array(2) {
["item"]=>
string(0) ""
["quantity"]=>
int(0)
}
}
["shippingInfos"]=>
array(1) {
[0]=>
array(6) {
["address"]=>
array(6) {
["city"]=>
string(0) ""
["country"]=>
string(0) ""
["postalCode"]=>
string(0) ""
["state"]=>
string(0) ""
["streetAddress1"]=>
string(0) ""
["streetAddress2"]=>
NULL
}
["contact"]=>
array(5) {
["company"]=>
NULL
["email"]=>
string(0) ""
["firstName"]=>
string(0) ""
["lastName"]=>
string(0) ""
["phoneNo"]=>
NULL
}
["ServiceId"]=>
string(0) ""
["thirdPartyAccountNo"]=>
string(0) ""
["signatureConfirmation"]=>
NULL
["saturdayDelivery"]=>
NULL
}
}
}
Just as the title says, I am able to get the data from the "source" (I guess that would be the $token) but nothing else.
<?php
require_once('stripe/init.php');
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxxx");
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
$name = $event_json->data->object->name;
$cvc_check = $event_json->data->object->cvc_check;
?>
=
I can get the $name (which is the email address) and the $cvc_check, but if I do the same for, let's say, status, there's nothing there:
=
$status = $event_json->data->object->status;
=
Can somebody tell / guide me in the right direction? Why can't I get anything outside of the "source"?
{
"object": {
"id": "ch_1EIdL8IcP22cq9aQpA",
"object": "charge",
"amount": 500,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_1EIdL8IcP22cq9",
"source": {
"id": "card_1EIdL3IcP22cq",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_EmBix3cJ",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 2,
"exp_year": 2022,
"fingerprint": "zrzwvqXQTWp",
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "someemailaddress#yahoo.com",
"tokenization_method": null
},
"source_transfer": null,
"statement_descriptor": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}
}
=
Stripe test response:
=
object(stdClass)#1 (9) {
["created"]=>
int(1326853478)
["livemode"]=>
bool(false)
["id"]=>
string(31) "charge.succeeded_00000000000000"
["type"]=>
string(16) "charge.succeeded"
["object"]=>
string(5) "event"
["request"]=>
NULL
["pending_webhooks"]=>
int(1)
["api_version"]=>
string(10) "2018-10-31"
["data"]=>
object(stdClass)#14 (1) {
["object"]=>
object(stdClass)#2 (41) {
["id"]=>
string(17) "ch_00000000000000"
["object"]=>
string(6) "charge"
["amount"]=>
int(500)
["amount_refunded"]=>
int(0)
["application"]=>
NULL
["application_fee"]=>
NULL
["application_fee_amount"]=>
NULL
["balance_transaction"]=>
string(18) "txn_00000000000000"
["billing_details"]=>
object(stdClass)#4 (4) {
["address"]=>
object(stdClass)#3 (6) {
["city"]=>
NULL
["country"]=>
NULL
["line1"]=>
NULL
["line2"]=>
NULL
["postal_code"]=>
NULL
["state"]=>
NULL
}
["email"]=>
NULL
["name"]=>
string(25) "someemail#yahoo.com"
["phone"]=>
NULL
}
["captured"]=>
bool(false)
["created"]=>
int(1553691107)
["currency"]=>
string(3) "usd"
["customer"]=>
string(18) "cus_00000000000000"
["description"]=>
NULL
["destination"]=>
NULL
["dispute"]=>
NULL
["failure_code"]=>
string(13) "card_declined"
["failure_message"]=>
string(23) "Your card was declined."
["fraud_details"]=>
object(stdClass)#5 (1) {
["stripe_report"]=>
string(10) "fraudulent"
}
["invoice"]=>
NULL
["livemode"]=>
bool(false)
["metadata"]=>
object(stdClass)#6 (0) {
}
["on_behalf_of"]=>
NULL
["order"]=>
NULL
["outcome"]=>
object(stdClass)#7 (6) {
["network_status"]=>
string(19) "not_sent_to_network"
["reason"]=>
string(18) "merchant_blacklist"
["risk_level"]=>
string(7) "highest"
["risk_score"]=>
int(98)
["seller_message"]=>
string(28) "Stripe blocked this payment."
["type"]=>
string(7) "blocked"
}
["paid"]=>
bool(true)
["payment_intent"]=>
NULL
["payment_method_details"]=>
object(stdClass)#10 (2) {
["card"]=>
object(stdClass)#8 (10) {
["brand"]=>
string(4) "visa"
["checks"]=>
object(stdClass)#9 (3) {
["address_line1_check"]=>
NULL
["address_postal_code_check"]=>
NULL
["cvc_check"]=>
string(11) "unavailable"
}
["country"]=>
string(2) "US"
["exp_month"]=>
int(1)
["exp_year"]=>
int(2022)
["fingerprint"]=>
string(16) "Gawa768Trrk4fEmb"
["funding"]=>
string(6) "credit"
["last4"]=>
string(4) "0019"
["three_d_secure"]=>
NULL
["wallet"]=>
NULL
}
["type"]=>
string(4) "card"
}
["receipt_email"]=>
NULL
["receipt_number"]=>
NULL
["receipt_url"]=>
string(118) "https://pay.stripe.com/receipts/acct_1DU4J1IcP22cq9aQ/ch_1EIbENIcP22cq9aQk7r9xN3u/rcpt_Em9Xy3Ambv1b8QZISOsIHdbxCn"
["refunded"]=>
bool(false)
["refunds"]=>
object(stdClass)#11 (5) {
["object"]=>
string(4) "list"
["data"]=>
array(0) {
}
["has_more"]=>
bool(false)
["total_count"]=>
int(0)
["url"]=>
string(47) "/v1/charges/ch_1EIbENIcP22cq9aQk7r9xN3u/refunds"
}
["review"]=>
NULL
["shipping"]=>
NULL
["source"]=>
object(stdClass)#12 (23) {
["id"]=>
string(19) "card_00000000000000"
["object"]=>
string(4) "card"
["address_city"]=>
NULL
["address_country"]=>
NULL
["address_line1"]=>
NULL
["address_line1_check"]=>
NULL
["address_line2"]=>
NULL
["address_state"]=>
NULL
["address_zip"]=>
NULL
["address_zip_check"]=>
NULL
["brand"]=>
string(4) "Visa"
["country"]=>
string(2) "US"
["customer"]=>
string(18) "cus_00000000000000"
["cvc_check"]=>
string(11) "unavailable"
["dynamic_last4"]=>
NULL
["exp_month"]=>
int(1)
["exp_year"]=>
int(2022)
["fingerprint"]=>
string(16) "Gawa768Trrk4fEmb"
["funding"]=>
string(6) "credit"
["last4"]=>
string(4) "0019"
["metadata"]=>
object(stdClass)#13 (0) {
}
["name"]=>
string(25) "someemail#yahoo.com"
["tokenization_method"]=>
NULL
}
["source_transfer"]=>
NULL
["statement_descriptor"]=>
NULL
["status"]=>
string(6) "failed"
["transfer_data"]=>
NULL
["transfer_group"]=>
NULL
}
}
}
=
I'd like to get data from ["status"]=>
string(6) "failed" (the last part of the code).
I used stripe to return a Customer Object. I want to extract the bank account ID and save it in a database for later use. What is the proper syntax to access the array properly?
Here is what I have tried:
$bank_account=$customer->lastResponse['json']['sources']['data'][0]['id'];
I was told to try:
$bank_account=$customer->bank_accounts->data[0]['id'];
But that came up null as well...
Im not sure how to work with the object or what the right way to do this is.
I need the bank_account_id
This is what the var_dump($customer) looks like after some formatting and removing information that is not pertinent. I think I can either use a JSON decode or access the array called 'json' i dont know the proper syntax for either of those solutions. Help please (:
object(Stripe\Customer)#3572 (6)
{
["_opts":protected]=> object(Stripe\Util\RequestOptions)#3576 (2) { ["headers"]=> array(1) { ["Stripe-Account"]=> string(21) "acct_1BNn74AprSj6yALS" } ["apiKey"]=> string(32) "secretkey" }
["_values":protected]=> array(15) {//Removed For brevity}
["_unsavedValues":protected]=> object(Stripe\Util\Set)#3590 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_transientValues":protected]=> object(Stripe\Util\Set)#3602 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_retrieveOptions":protected]=> array(0) { }
["_lastResponse":protected]=>
object(Stripe\ApiResponse)#3714 (4)
{
["headers"]=> array(15) { //Removed for brevity}
// Json
["body"]=> string(1148) "{
"id": "cus_BoJOjFghfV7mmq", "object": "customer", "account_balance": 0, "created": 1511290036, "currency": null, "default_source": "bank_account_id", "delinquent": false, "description": "Name", "discount": null, "email": null, "livemode": true, "metadata": {}, "shipping": null, "sources": { "object": "list", "data": [ { "id": "bank_account_id", "object": "bank_account", "account_holder_name": "Daniel Taylor", "account_holder_type": "individual", "bank_name": "Bank Name", "country": "US", "currency": "usd", "customer": "cus_id", "fingerprint": "info", "last4": "last four of account", "metadata": {}, "routing_number": "routing number", "status": "new" } ], "has_more": false, "total_count": 1, "url": "/v1/customers/cus_BoJOjFghfV7mmq/sources" }, "subscriptions": { "object": "list", "data": [], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_BoJOjFghfV7mmq/subscriptions" }
} "
["json"]=> array(15)
{
["id"]=> string(18) "cus_id"
["object"]=> string(8) "customer"
["account_balance"]=> int(0)
["created"]=> int(1511290036)
["currency"]=> NULL
["default_source"]=> string(27) "bank_account_id"
["delinquent"]=> bool(false)
["description"]=> string(13) "Name"
["discount"]=> NULL
["email"]=> NULL ["livemode"]=> bool(true)
["metadata"]=> array(0) { }
["shipping"]=> NULL
["sources"]=> array(5)
{
["object"]=> string(4) "list"
["data"]=> array(1)
{
[0]=> array(13)
{
["id"]=> string(27) "bank_account_id" ["object"]=> string(12) "bank_account"
["account_holder_name"]=> string(13) "name"
["account_holder_type"]=> string(10) "individual"
["bank_name"]=> string(26) "Bank Name"
["country"]=> string(2) "US" ["currency"]=> string(3) "usd"
["customer"]=> string(18) "cus_ID"
["fingerprint"]=> string(16) "fingerprint"
["last4"]=> string(4) "lastfour" ["metadata"]=> array(0) { }
["routing_number"]=> string(9) "routenumber"
["status"]=> string(3) "new"
}
}
["has_more"]=> bool(false)
["total_count"]=> int(1)
["url"]=> string(40) "/v1/customers/cus_BoJOjFghfV7mmq/sources"
}
["subscriptions"]=> array(5) { ["object"]=> string(4) "list" ["data"]=> array(0) { } ["has_more"]=> bool(false) ["total_count"]=> int(0) ["url"]=> string(46) "/v1/customers/cus_id/subscriptions" }
}
["code"]=> int(200)
}
}
I think it should be:
$bank_account = $customer->sources->data[0]->id
If the customer has multiple fund sources, you may need to loop through the data array to find the one you want. $customer->sources->data[$i]->object will be "bank_account" for the source you want.
Pretty sure there is a getter in there. You can just use the properties you would expect on the object.
Example:
$customer_id = $customer->id;
$bank_account_id = $customer->sources->data[0]->id;
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]=>
object(Stripe\Card)#292 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#294 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "sk_test_d3..."
}
["_values":protected]=>
array(23) {
["id"]=>
string(29) "card_16jLIwLBSNgilYpnoEiPhNhA"
["object"]=>
string(4) "card"
["last4"]=>
string(4) "4242"
["brand"]=>
string(4) "Visa"
["funding"]=>
string(6) "credit"
["exp_month"]=>
int(8)
["exp_year"]=>
int(2016)
["fingerprint"]=>
string(16) "T5RzXLIiXrF0nyqH"
["country"]=>
string(2) "US"
["name"]=>
NULL
["address_line1"]=>
NULL
["address_line2"]=>
NULL
["address_city"]=>
NULL
["address_state"]=>
NULL
["address_zip"]=>
NULL
["address_country"]=>
NULL
["cvc_check"]=>
string(4) "pass"
["address_line1_check"]=>
NULL
["address_zip_check"]=>
NULL
["tokenization_method"]=>
NULL
["dynamic_last4"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#261 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#294 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "sk_test_d..."
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#262 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#306 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["customer"]=>
string(18) "cus_6xCtshHPQiYSdI"
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#270 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#120 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
Hoping that someone can explain the "extended" version of the response, that I'm getting here. Thanks in advance!
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.
If you want to normalize data in array form to get value easily then add __toArray(); to get response in array.
Ex: $cards->data->__toArray();
I make a JSON call using an API and get the following, which seems to be correctly formatted JSON:
{
"pagination": {},
"meta": {
"code": 200
},
"data": [
{
"tags": [],
"location": {
"latitude": 37.42833,
"name": "Stanford University",
"longitude": -122.1668,
"id": 10138861
},
"comments": {
"count": 0,
"data": []
},
"filter": "Rise",
"created_time": "1331327429",
"link": "http://instagr.am/p/H91ykZpqUW/",
"likes": {
"count": 3,
"data": [
{
"username": "razzles39",
"profile_picture": "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg",
"id": "14316422",
"full_name": "razzles39"
},
{
"username": "mscaliti",
"profile_picture": "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg",
"id": "10827166",
"full_name": "mscaliti"
},
{
"username": "mariecox",
"profile_picture": "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg",
"id": "3987147",
"full_name": "Marie Cox"
}
]
},
"images": {
"low_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg",
"width": 612,
"height": 612
}
},
"caption": {
"created_time": "1331327500",
"text": "Chillin with Brady at Stanford",
"from": {
"username": "nicolelainefox",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"id": "17982472",
"full_name": "nicolelainefox"
},
"id": "143507936292283648"
},
"type": "image",
"id": "143507334669706518_17982472",
"user": {
"username": "nicolelainefox",
"website": "",
"bio": "",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"full_name": "nicolelainefox",
"id": "17982472"
}
}
]
}
I then use json_decode in my PHP script to make it something I can manipulate, and this is the output of var_dump
object(stdClass)#1 (3) { ["pagination"]=> object(stdClass)#2 (0) { } ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(1) { [0]=> object(stdClass)#4 (12) { ["tags"]=> array(0) { } ["location"]=> object(stdClass)#5 (4) { ["latitude"]=> float(37.42833) ["name"]=> string(19) "Stanford University" ["longitude"]=> float(-122.1668) ["id"]=> int(10138861) } ["comments"]=> object(stdClass)#6 (2) { ["count"]=> int(0) ["data"]=> array(0) { } } ["filter"]=> string(4) "Rise" ["created_time"]=> string(10) "1331327429" ["link"]=> string(31) "http://instagr.am/p/H91ykZpqUW/" ["likes"]=> object(stdClass)#7 (2) { ["count"]=> int(3) ["data"]=> array(3) { [0]=> object(stdClass)#8 (4) { ["username"]=> string(9) "razzles39" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg" ["id"]=> string(8) "14316422" ["full_name"]=> string(9) "razzles39" } [1]=> object(stdClass)#9 (4) { ["username"]=> string(8) "mscaliti" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg" ["id"]=> string(8) "10827166" ["full_name"]=> string(8) "mscaliti" } [2]=> object(stdClass)#10 (4) { ["username"]=> string(8) "mariecox" ["profile_picture"]=> string(72) "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg" ["id"]=> string(7) "3987147" ["full_name"]=> string(9) "Marie Cox" } } } ["images"]=> object(stdClass)#11 (3) { ["low_resolution"]=> object(stdClass)#12 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg" ["width"]=> int(306) ["height"]=> int(306) } ["thumbnail"]=> object(stdClass)#13 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg" ["width"]=> int(150) ["height"]=> int(150) } ["standard_resolution"]=> object(stdClass)#14 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg" ["width"]=> int(612) ["height"]=> int(612) } } ["caption"]=> object(stdClass)#15 (4) { ["created_time"]=> string(10) "1331327500" ["text"]=> string(30) "Chillin with Brady at Stanford" ["from"]=> object(stdClass)#16 (4) { ["username"]=> string(14) "nicolelainefox" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["id"]=> string(8) "17982472" ["full_name"]=> string(14) "nicolelainefox" } ["id"]=> string(18) "143507936292283648" } ["type"]=> string(5) "image" ["id"]=> string(27) "143507334669706518_17982472" ["user"]=> object(stdClass)#17 (6) { ["username"]=> string(14) "nicolelainefox" ["website"]=> string(0) "" ["bio"]=> string(0) "" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(14) "nicolelainefox" ["id"]=> string(8) "17982472" } } } }
However, when I use echo($instagram_data["data"]); everything crashes. How do I access the 'data' array in this associative array? Here's the whole code:
$instagram_handler = fopen("https://api.instagram.com/v1/locations/10138861/media/recent/?client_id=MY_ID", "r");
$instagram_json = stream_get_contents($instagram_handler);
fclose($instagram_handler);
$instagram_data = json_decode($instagram_json);
echo($instagram_data["data"]); //Breaks page
the JSON is read as an object, not an associative array. load it like so and you'll be good:
$instagram_data = json_decode($instagram_json, TRUE);
alternatively, loading it like you do currently, run:
echo $instagram_data->data
Note the assoc param of json_decode function. If you omit it decode treats JSON as an object rather than an array. So you should be able access it with:
$instagram_data->data;
Other option is to decode as array as following:
$instagram_data = json_decode($instagram_json, TRUE);