Stripe API JSON returns null - php

Using the Stripe API in PHP all attempts to pull a value off of it is returning null, r not displaying a result at all. I've tried
$customers = \Stripe\Customer::all();
$customers_json = $customers->__toJSON();
json_decode($customers_json);
echo $customers_json->data->id;
and also
$customers = \Stripe\Customer::all();
$customer = $customers->__toArray(true);
echo $customer["data"]["id"];
but each time both result in a blank echo. However, when I just output the original variable without parsing it to JSON or anything, it returns a string full of JSON values, just not parsed. The output of just raw $customers is
Stripe\Collection JSON: { "object": "list", "has_more": false, "url": "\/v1\/customers", "data": [ { "id": "cus_6u32tOQ6MRXuqm", "object": "customer", "created": 1441114587, "livemode": false, "description": "test customer", "email": "test#respice.xyz", "shipping": null, "delinquent": false, "metadata": [ ], "subscriptions": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/customers\/cus_6u32tOQ6MRXuqm\/subscriptions", "data": [ ] }, "discount": null, "account_balance": 0, "currency": null, "sources": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/customers\/cus_6u32tOQ6MRXuqm\/sources", "data": [ ] }, "default_source": null } ] }

The issue is that the data field is not an object, but an array of objects. Sticking with the $customer = $customers->__toArray(true); method, you should try the following:
echo $customer['data'][0]['id'];
If you wish to iterate through all customers, try doing this:
foreach($customer['data'] as $currentCustomerData){
// do stuff here
}

Related

PHP - Retrieve JSON data and values [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm new into PHP and JSON and I have a problem, I want to retrieve a item and value from a JSON:
{
"status": true,
"webhook_type": 100,
"data": {
"product": {
"id": "lSEADIQ",
"attachment_id": null,
"title": "Registration",
"description": null,
"image": null,
"unlisted": false,
"type": "service",
"price": 1,
"currency": "EUR",
"email": {
"enabled": false
},
"stock_warning": 0,
"quantity": {
"min": 1,
"max": 1
},
"confirmations": 1,
"custom_fields": [
{
"name": "Forum username",
"type": "text",
"required": true
}
],
"gateways": [
"Bitcoin"
],
"webhook_urls": [],
"dynamic_url": "",
"position": null,
"created_at": "2018-10-01 12:51:12",
"updated_at": "2018-10-01 12:55:46",
"stock": 9223372036854776000,
"accounts": []
},
"order": {
"id": "8e23b496-121a-4dc6-8ec4-c45835680db2",
"created_at": "Tue, 02 Oct 2018 00:54:56 +0200",
"paid_at": null,
"transaction_id": null,
"confirmations": 1,
"required_confirmations": 3,
"received_amount": 0,
"crypto_address": "1NeNQws7JLbTr6bjekfeaXSV7XiyRsv7V8",
"crypto_amount": "0.4815",
"quantity": 1,
"price": 19.99,
"currency": "EUR",
"exchange_rate": "1.21",
"gateway": "BTC",
"email": "webhook#site.gg",
"ip_address": "123.456.789.111",
"agent": {
"geo": {
"ip": "214.44.18.6",
"iso_code": "US",
"country": "United States"
},
"data": {
"is_mobile": false,
"is_table": false,
"is_desktop": true,
"browser": {
"name": "Chrome",
"version": "63.0.3239.132"
}
}
},
"custom_fields": [
{
"name": "user_id",
"value": 184191
}
],
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)"
}
}
}
I want to retrieve items from data -> order, for example "id" or "ip_address".
Thank you for read this, I hope someone can help me in this, because I'm lost, I started to code very recently and I'm trying to learn a lot.
Regards!
Where test.json is the json you uploaded, place it in a file named test.json and ensure its placed in the same directory.
<?php
$load = file_get_contents("test.json") or die("JSON load failed");
$json_a = json_decode($load, true);
print $json_a['data']['order']['ip_address'] . "\n";
?>
Gives:
123.456.789.111
My answer reads the JSON from a file as were it dumped directly in your code, which indeed it could be, it would make the code less readable and your file more messy.
If you dont want to place the file in the same directory, simply specify the full file path. E.g. file_get_contents("this/dir/here/test.json");
You can read about how json_decode works here, its essential we pass it the true parameter to make our arrays associative.
You can extract your need array from JSON data. You can use a loop too to read all your data inside the order array.
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];

Get subscription id from invoice.payment_failed webhook in Stripe incorrect Hierarchy

What is the hierarchy here?
It looks like it goes
$event->data->object->lines->data->subscription
I've tried that though, and Im not getting an ID. It's actually pretty confusing.
Here is the response, and why I am assuming the hierarchy is
$event->data->object->lines->data->subscription
I figured if I used say
$subID = $event->data->object->lines->data->subscription;
then
$subID should = "sub_randcomSUB"
{
"object": {
"id": "in_randomnumbers",
"object": "invoice",
"amount_due": 20000,
"amount_paid": 0,
"amount_remaining": 20000,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": true,
"billing": "charge_automatically",
"billing_reason": "manual",
"charge": "ch_randomnumbers",
"closed": false,
"currency": "usd",
"customer": "cus_randomnumbers",
"date": 1532039357,
"description": null,
"discount": null,
"due_date": null,
"ending_balance": 0,
"forgiven": false,
"hosted_invoice_url": "https://pay.stripe.com/invoice/invst_randomnumbers",
"invoice_pdf": "https://pay.stripe.com/invoice/invst_randomcnumbers/pdf",
"lines": {
"object": "list",
"data": [
{
"id": "ii_randomnumbn",
"object": "line_item",
"amount": 20000,
"currency": "usd",
"description": "SSS",
"discountable": true,
"invoice_item": "ii_random",
"livemode": false,
"metadata": {
},
"period": {
"end": 1532039351,
"start": 1532039351
},
"plan": null,
"proration": false,
"quantity": 1,
"subscription": "sub_randcomSUB",
"type": "invoiceitem"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_randcom/lines"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": 1532298568,
"number": "5186095-0004",
"paid": false,
"period_end": 1534715394,
"period_start": 1532036994,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"subscription": null,
"subtotal": 20000,
"tax": null,
"tax_percent": null,
"total": 20000,
"webhooks_delivered_at": 1532039363
},
"previous_attributes": null
}
$event->data->object->lines->data is an array. You need to get the first element of the array like $event->data->object->lines->data[0] then subscription like $event->data->object->lines->data[0]->subscription

Accessing plan id during Stripe response

Here the json sent by Stripe to get invoices status:
Stripe\Collection JSON: {
...
"data" => [
[0] => Stripe\Invoice JSON: {
...
"lines": {
"data": [
{
...
"subscription": null,
"quantity": 1,
"plan": {
"interval": "month",
"name": "X-Large - 12 mois",
"created": 1435711579,
"amount": 52800,
"currency": "cad",
"id": "X-LARGE_12M",
"object": "plan",
"livemode": false,
"interval_count": 12,
"trial_period_days": null,
"metadata": {
},
"statement_descriptor": null
},
...
}
],
...
},
...
}
]
}
The json response can be found here:
https://stripe.com/docs/api#list_customer_invoices
I would like to access to the plan id (i.e.: X-LARGE_12M).
So I tried:
$invoice->lines->data->plan->id
But it doesn't work.
It works for others fields.
Any reason why ?
Thanks.
"data": [
{
The data element is an array of objects. So you'd need something like
foreach($invoice->lines->data as $data) {
echo $data->plan->id, "\r\n";
}

Counting array in API JSON Response

I'm trying to do a simple count of how many refunds are in my Stripe Response but count() isn't working and I don't really know any other way of achieving this.
Could anyone point me in the right direction?
$retrieve_event = Stripe_Event::retrieve("evt_00000000000000");
$event_json_id = json_decode($retrieve_event);
$refund_array = $event_json_id->{'data'}->{'object'}->{'refunds'};
die(count($refund_array));
This is the response of $retrieve_event
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "charge.refunded",
"object": "event",
"request": null,
"data": {
"object": {
"id": "ch_00000000000000",
"object": "charge",
"created": 1402433517,
"livemode": false,
"paid": true,
"amount": 1000,
"currency": "usd",
"refunded": true,
"card": {
"id": "card_00000000000000",
"object": "card",
"last4": "0028",
"type": "Visa",
"exp_month": 8,
"exp_year": 2015,
"fingerprint": "a5KWlTcrmCYk5DIYa",
"country": "US",
"name": "First Last",
"address_line1": "null",
"address_line2": null,
"address_city": "null",
"address_state": "null",
"address_zip": "null",
"address_country": "US",
"cvc_check": null,
"address_line1_check": "fail",
"address_zip_check": "pass",
"customer": "cus_00000000000000"
},
"captured": true,
"refunds": [
{
"id": "re_104CKt4uGeYuVLAahMwLA2TK",
"amount": 100,
"currency": "usd",
"created": 1402433533,
"object": "refund",
"charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
"balance_transaction": "txn_104CKt4uGeYuVLAaSNZCR867",
"metadata": {}
},
{
"id": "re_104CKt4uGeYuVLAaDIMHoIos",
"amount": 200,
"currency": "usd",
"created": 1402433539,
"object": "refund",
"charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
"balance_transaction": "txn_104CKt4uGeYuVLAaqSwkNKPO",
"metadata": {}
},
{
"id": "re_4CL6n1r91dY5ME",
"amount": 700,
"currency": "usd",
"created": 1402434306,
"object": "refund",
"charge": "ch_4CL6FNWhGzVuAV",
"balance_transaction": "txn_4CL6qa4vwlVaDJ"
}
],
"balance_transaction": "txn_00000000000000",
"failure_message": null,
"failure_code": null,
"amount_refunded": 1000,
"customer": "cus_00000000000000",
"invoice": null,
"description": "this is a description",
"dispute": null,
"metadata": {},
"statement_description": "this is a description",
"fee": 0
}
}
}
It's just the way you're trying to output the count. This example outputs 3:
echo count($refund_array);
exit;
Whereas this example doesn't:
die(count($refund_array));
The reason is because you're simply passing in an integer into die(). From the manual:
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
This example works because the message is a string:
die('Count: ' . count($refund_array)); // Count: 3
...or:
die((string) count($refund_array)); // 3
The count() just needs to be cast as a string:
die((string) count($refund_array));
As per new version of Stripe API which was released on 2014-06-17, Refunds object is modified for Charges Method.
Now you can directly get refund count using total_count parameter in refunds object.
data->object->refund->total_count

How can I get this Json value in Stripe Invoice?

I am able to retrieve the first 4 values fine, but $item_name is eluding me. What is wrong with the syntax I'm using to retrieve the plan: name: "LITE" value
this is my php - the first 4 return fine but $item_name returns invalid.
$customer = $event_json->data->object->customer;
$amount = $event_json->data->object->total;
$period_end = $event_json->data->object->period_end;
$paid = $event_json->data->object->paid;
$item_name = $event_json->data->object->lines->data->plan->name;
and this is what is being sent from Stripe
{
"id": "evt_1CTCF3tibBeC2y",
"created": 1359565368,
"livemode": false,
"type": "invoice.payment_succeeded",
"data": {
"object": {
"period_end": 1359565367,
"charge": "ch_1CTCMvOgHE1Q9K",
"discount": null,
"period_start": 1359565367,
"livemode": false,
"customer": "cus_1CTCYQNoghibwb",
"amount_due": 7500,
"lines": {
"count": 1,
"object": "list",
"url": "/v1/invoices/in_1CTCCBbENUpsE6/lines",
"data": [
{
"type": "subscription",
"livemode": false,
"period": {
"end": 1391101367,
"start": 1359565367
},
"object": "line_item",
"proration": false,
"plan": {
"trial_period_days": null,
"livemode": false,
"interval": "year",
"object": "plan",
"name": "LITE",
"amount": 7500,
"currency": "usd",
"id": "LITE",
"interval_count": 1
},
Data is an array:
$item_name = $event_json->data->object->lines->data[0]->plan->name;
Right?

Categories