Stripe Response when Retrieving Customer Sources - php

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

Related

Stripe webhook: able to retrieve data from "source" but nothing else

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).

Tricky PHP Array

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;

Google Drive API thumbnail parameters

I'm using Google Drive API (v3) with Google's PHP api client to list and display videos from my Drive, where I'm also displaying thumbnails for said videos. I have difficulties getting thumbnail urls back from the API, although I enabled thumbnailLink in request parameters:
'fields' => 'nextPageToken, files(id, name, thumbnailLink, webContentLink, webViewLink)'
Value for thumbnailLink is still empty, other values are returned successfully. I looked for thumbnail settings in api documentation, without success. API also doesn't return any error. What could it be?
Here's the code I'm using:
$drive_service = new Google_Service_Drive($client);
$optParams = array(
'pageSize' => 4,
'q' => "'0B_nSMgQioOWwNy1ha0tFWEE0QWs' in parents and mimeType contains 'video/'",
'fields' => 'nextPageToken, files(id, name, modifiedTime, mimeType, createdTime, thumbnailLink, webContentLink, webViewLink)'
);
return $files_list = $drive_service->files->listFiles($optParams)->getFiles();
and var_dump of response
array(4) {
[0]=>
object(Google_Service_Drive_DriveFile)#590 (56) {
["collection_key":protected]=>
string(6) "spaces"
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
string(24) "2016-09-12T19:54:22.000Z"
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(35) "1St7nQ3X3-ocrMUC2t-r6NOVc4xIdfhbIgg"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["isAppAuthorized"]=>
NULL
["kind"]=>
NULL
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(9) "video/mp4"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
string(24) "2016-12-27T11:55:43.958Z"
["name"]=>
string(12) "MOV_1950.mp4"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
string(82) "https://drive.google.com/uc?id=1St7nQ3X3-ocrMUC2t-r6NOVc4xIdfhbIgg&export=download"
["webViewLink"]=>
string(85) "https://drive.google.com/file/d/1St7nQ3X3-ocrMUC2t-r6NOVc4xIdfhbIgg/view?usp=drivesdk"
["writersCanShare"]=>
NULL
["internal_gapi_mappings":protected]=>
array(0) {
}
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}...
Try to double check if you properly use a correct scope. I suggest you to use the https://www.googleapis.com/auth/drive for full permissive scope to access all of a user's files. If it's still not working, then try the suggested solution here.
Try items.thumbnailLink
This is what the response from the Drive API looks like.
{
"kind": "drive#fileList",
"etag": "\"m64ksZC09nG4-fB1IRF3vg/1C1ON4SO5V-To1u55k7dU\"",
"selfLink": "https://www.googleapis.com/drive/v2/files?corpus=DEFAULT",
"nextPageToken": "V1*3|0|ChwwQjdXTWpmWkhhWHllTW1ObtQq",
"nextLink": "https://www.googleapis.com/drive/v2/files?corpus=DEFAULT&pageToken=V1*3%7C0%7CChwwQjdVUnFTR05mZGpREgcQsonGqtQq",
"items": [
{
"kind": "drive#file",
"id": "1DpsZKiII1pOsHxDrJDzBb_aW841Ovhm8pE5FCI",
"etag": "\"m64ksZC09nG4-fB1IRF3vg/1C1ON4SO5V-To1u55k7dU\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/1DpsZKiII1pOsHbrEzvxDrJDzBb_aW841Ovhm8pE5FCI"...
},
"defaultOpenWithLink": "https://docs.google.com/spreadsheets/d/1DpsZKihm8pE5FCI/edit?usp=drivesdk",
"iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_spreadsheet_list.png",
"thumbnailLink": "https://docs.google.com/feeds/vt?gd=true&id=1DpsZKiInoAAAAAWGPVboChWb270pOAY0M5hMr3FNa-IX6G&sz=s220",
"title": "Document Title",
"mimeType": "application/vnd.google-apps.spreadsheet",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},...
Hope this helps!
I know you're trying to get a list of files, and this only retrieves a link from one, but this would have helped me a lot, and I think it's similar enough in concept to be potentially helpful to others:
function getThumbnailUrl($fileId)
{
$service = new Google_Service_Drive($this->client());
$params = array('fields' => 'hasThumbnail, thumbnailLink');
$file = $service->files->get($fileId, $params);
$url = $file->getThumbnailLink();
return $url;
}
I am still interested in whether you were able to get thumbnail links for multiple files using getFiles().

json_decode() when there are multiple results

I am trying to get my head around how I can get the SKU's (and additionally the ID) from json results. I can do this when there are just one set of values called SKU but I have spent hours trying to understand how to do this when there are multiple.
Below is a sample of what my json returns
{
"variants": [
{
"id": 6852445,
"name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
"sku": "VK7i-S-SHX7",
},
{
"id": 6852388,
"name": "ikan Flyweight DSLR",
"sku": "ELE-FLWDSLR",
},
{
"id": 6838367,
"name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
"sku": "AO-ATOMSUN001",
},
]
}
I currently have this code (I am a newbee). What I am trying to do is make the sku and the ID both variables but I am just hitting a brick wall. Currently I get
Warning: Invalid argument supplied for foreach() in /home/pearingc/public_html/returns/test2.php on line 15
<?php
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Bearer *my token details* ")
)
);
$url = "http://api.tradegecko.com/variants/";
$data = file_get_contents($url, false, $context);
$json = json_decode($data, true);
$product = $json{'variant'}->{'sku'};
foreach ($product['sku'] as $sku) {
print $sku;
}
?>
Edit: This is what the var_dump($json); gives me
object(stdClass)#1 (2) { ["variants"]=> array(100) { [0]=> object(stdClass)#2 (42) { ["id"]=> int(6852445) ["created_at"]=> string(24) "2015-05-20T10:09:09.629Z" ["updated_at"]=> string(24) "2015-05-20T10:10:40.351Z" ["product_id"]=> int(1991122) ["default_ledger_account_id"]=> NULL ["buy_price"]=> string(5) "404.0" ["committed_stock"]=> string(1) "0" ["incoming_stock"]=> string(1) "0" ["composite"]=> bool(true) ["description"]=> NULL ["is_online"]=> bool(false) ["keep_selling"]=> bool(false) ["last_cost_price"]=> NULL ["manage_stock"]=> bool(true) ["max_online"]=> NULL ["moving_average_cost"]=> NULL ["name"]=> string(49) "Ikan VK7i 7" LCD Monitor for Sony L with sun hood" ["online_ordering"]=> bool(false) ["opt1"]=> NULL ["opt2"]=> NULL ["opt3"]=> NULL ["position"]=> int(6) ["product_name"]=> string(33) "ikan 7" HDMI Monitor W/ IPS Panel" ["product_status"]=> string(6) "active" ["product_type"]=> string(8) "Monitors" ["retail_price"]=> NULL ["sellable"]=> bool(true) ["sku"]=> string(11) "VK7i-S-SHX7" ["status"]=> string(6) "active" ["stock_on_hand"]=> string(1) "0" ["supplier_code"]=> NULL ["taxable"]=> bool(true) ["upc"]=> NULL ["weight"]=> NULL ["wholesale_price"]=> NULL ["image_ids"]=> array(0) { } ["variant_prices"]=> array(1) { [0]=> object(stdClass)#3 (2) { ["price_list_id"]=> string(3) "buy" ["value"]=> string(5) "404.0" } } ["locations"]=> array(1) { [0]=> object(stdClass)#4 (6) { ["location_id"]=> int(16377) ["stock_on_hand"]=> string(1) "0" ["committed"]=> string(1) "0" ["incoming"]=> NULL ["bin_location"]=> NULL ["reorder_point"]=> NULL } } ["prices"]=> object(stdClass)#5 (1) { ["buy"]=> string(5) "404.0" } ["stock_levels"]=> object(stdClass)#6 (1) { ["16377"]=> string(3) "0.0" } ["committed_stock_levels"]=> object(stdClass)#7 (1) { ["16377"]=> string(3) "0.0" } ["incoming_stock_levels"]=> object(stdClass)#8 (0) { } }
I think You have malformed JSON. Note: I removed commas after sku. Try this :
$data = '{
"variants": [
{
"id": 6852445,
"name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
"sku": "VK7i-S-SHX7"
},
{
"id": 6852388,
"name": "ikan Flyweight DSLR",
"sku": "ELE-FLWDSLR"
},
{
"id": 6838367,
"name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
"sku": "AO-ATOMSUN001"
}
]
}';
And after that:
$json = json_decode($data);
foreach ($json->variants as $row) {
print $row->sku;
}
<?php
$json = '{
"variants": [
{
"id": 6852445,
"name": "Ikan VK7i 7\" LCD Monitor for Sony L with sun hood",
"sku": "VK7i-S-SHX7"
},
{
"id": 6852388,
"name": "ikan Flyweight DSLR",
"sku": "ELE-FLWDSLR"
},
{
"id": 6838367,
"name": "Atomos Sun Hood for Ninja/Ninja-2 including Double Adapter",
"sku": "AO-ATOMSUN001"
}
]
}';
foreach(json_decode($json,true)['variants'] as $item) {
echo $item['sku'] . "<br />";
}
?>
Condensed it a little bit.

Storing a JSON value with PHP

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>";

Categories