Accessing plan id during Stripe response - php

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

Related

problem adding products in akeneo api using API

I need help in creating products in akeneo product api usi api call.
when i add product in that api It always give error "This value should not be blank." I have changed all the parameters given in the documentation but with no result
parameters {
"identifier": "AT-TN-136-B-NCAL",
"enabled": true,
"family": "camcorders",
"categories": [
"master"
],
"groups": [],
"parent": null,
"values": {},
"associations": {}
}
result
{
"code": 422,
"message": "Validation failed.",
"errors": [
{
"property": "identifier",
"message": "This value should not be blank."
}
]
}
There is a key:value pair probably missing inside your parameters.
Try using the akeneo api with Postman to see the in and outs for the api.
Link to api docs => https://api.akeneo.com/getting-started/your-first-tutorial-4x/welcome.html
Here is an example i took trough postman, it might give you a better look at how an api Product Post should look.
{
"identifier": "new_product",
"family": "tshirts",
"groups": [],
"parent": null,
"categories": [
"tvs_projectors"
],
"enabled": true,
"values": {
"clothing_size": [
{
"locale": null,
"scope": null,
"data": "s"
}
],
"description": [
{
"locale": "en_US",
"scope": "mobile",
"data": "Akeneo T-Shirt"
},
{
"locale": "en_US",
"scope": "print",
"data": "Akeneo T-Shirt with short sleeve"
}
],
"main_color": [
{
"locale": null,
"scope": null,
"data": "black"
}
],
"name": [
{
"locale": null,
"scope": null,
"data": "Akeneo T-Shirt black and purple with short sleeve"
}
],
"secondary_color": [
{
"locale": null,
"scope": null,
"data": "purple"
}
],
"tshirt_materials": [
{
"locale": null,
"scope": null,
"data": "cotton"
}
],
"tshirt_style": [
{
"locale": null,
"scope": null,
"data": [
"crewneck",
"short_sleeve"
]
}
],
"price": [
{
"locale": null,
"scope": null,
"data": [
{
"amount": 10,
"currency": "EUR"
},
{
"amount": 14,
"currency": "USD"
}
]
}
]
},
"created": "2017-03-30T14:55:26+02:00",
"updated": "2017-05-04T23:56:09+02:00",
"associations": {
"SUBSTITUTION": {
"groups": [],
"products": [
"AKNSTK"
]
}
}
}

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'];

Stripe API JSON returns null

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
}

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?

iterating a php object

I am sure this is a complete noob question but I am trying to iterate through an api response from stripe.com and I am able to echo out the array in php to be something like this:
{ "count": 3,
"data": [ { "amount": 29900,
"object": "plan",
"interval": "month",
"livemode": false,
"currency": "usd",
"name": "vb Group unlimited",
"id": "vb-std-group2" },
{ "amount": 9900,
"object": "plan",
"interval": "year",
"livemode": false,
"currency": "usd",
"name": "vb Group to 20",
"id": "vb-std-group" },
{ "amount": 1900,
"object": "plan",
"interval": "year",
"livemode": false,
"currency": "usd",
"name": "vb-Individual",
"id": "vb-std-individual" }
]
}
What I'm trying to do is echo out the "data" array info. Thanks for any insight.
The string you have is JSON encoded. Decode it using json_decode into a PHP object, say $obj. Then, $obj['data'] is an array. Iterate over it using a foreach loop.
foreach($obj['data'] as $key=>$object)
foreach($object as $key=>$value)
echo $key . " : " . $value;
Try something like this:
$reponseData = $response['data'];
foreach($reponseData as $eachDataObj)
{
foreach($eachDataObj as $key => $val)
{
echo $key . " => ".$val;
}
}
This assumes that $response is already an object, if it is actually just TEXT then you have to use
$response = json_decode($response);
Check this out it would help you
$myjson = '{ "count": 3,
"data": [ { "amount": 29900,
"object": "plan",
"interval": "month",
"livemode": false,
"currency": "usd",
"name": "vb Group unlimited",
"id": "vb-std-group2" },
{ "amount": 9900,
"object": "plan",
"interval": "year",
"livemode": false,
"currency": "usd",
"name": "vb Group to 20",
"id": "vb-std-group" },
{ "amount": 1900,
"object": "plan",
"interval": "year",
"livemode": false,
"currency": "usd",
"name": "vb-Individual",
"id": "vb-std-individual" }
]
}';
$data =json_decode($myjson);
$arrData = get_object_vars($data);
foreach($arrData as $key=>$value){
$arrData[$key] = $value;
}
//print_r($arrData['data']);
foreach($arrData['data'] as $ke=>$va):
foreach($va as $k=>$v):
echo $k."-".$v."<br/>";
endforeach;
endforeach;

Categories