How can I get this Json value in Stripe Invoice? - php

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?

Related

How to read Specific value from curl Response

this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }
assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);

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

Searching Stripe's JSON response value with PHP

I know this question has been done several times, but after a few tests I really cant figure ho to do this.
I'm implementing SEPA payments with Stripe. I've managed to POST all the infos to Stripe, but having a hard time getting a value within the JSON response.
here it is and what I need is the "mandate_url" value toward the end:
{
"id": "XXXXXXXXXXX",
"object": "customer",
"account_balance": 0,
"created": XXXXXXXXXXX,
"currency": null,
"default_source": "XXXXXXXXXXX",
"delinquent": false,
"description": null,
"discount": null,
"email": null,
"livemode": true,
"metadata": {},
"shipping": null,
"sources": {
"object": "list",
"data": [
{
"id": "XXXXXXXXXXX",
"object": "source",
"amount": null,
"client_secret": "XXXXXXXXXXX",
"created": XXXXXXXXXXX,
"currency": "eur",
"customer": "XXXXXXXXXXX",
"flow": "none",
"livemode": true,
"metadata": {},
"owner": {
"address": {
"city": "XXXXXXXXXXX",
"country": "XXXXXXXXXXX",
"line1": null,
"line2": null,
"postal_code": "00000",
"state": null
},
"email": null,
"name": "John Doe",
"phone": null,
"verified_address": null,
"verified_email": null,
"verified_name": null,
"verified_phone": null
},
"status": "chargeable",
"type": "sepa_debit",
"usage": "reusable",
"sepa_debit": {
"bank_code": "XXXXXXXXXXX",
"branch_code": "XXXXXXXXXXX",
"country": "XXXXXXXXXXX",
"fingerprint": "XXXXXXXXXXX",
"last4": "XXXXXXXXXXX",
"mandate_reference": "XXXXXXXXXXX",
"mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
}
}
],
"has_more": false,
"total_count": 1,
"url": "XXXXXXXXXXX"
},
"subscriptions": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "XXXXXXXXXXX"
}
}
isn't there any way to get it without creating an enormous foreach?
thanks :)
first off you're json was invalid as you had several instances of "created": XXXXXXXXXXX, so below is corrected json and example of using json_decode
<?php
$json = '{
"id": "XXXXXXXXXXX",
"object": "customer",
"account_balance": 0,
"created": "XXXXXXXXXXX",
"currency": null,
"default_source": "XXXXXXXXXXX",
"delinquent": false,
"description": null,
"discount": null,
"email": null,
"livemode": true,
"metadata": {},
"shipping": null,
"sources": {
"object": "list",
"data": [{
"id": "XXXXXXXXXXX",
"object": "source",
"amount": null,
"client_secret": "XXXXXXXXXXX",
"created": "XXXXXXXXXXX",
"currency": "eur",
"customer": "XXXXXXXXXXX",
"flow": "none",
"livemode": true,
"metadata": {},
"owner": {
"address": {
"city": "XXXXXXXXXXX",
"country": "XXXXXXXXXXX",
"line1": null,
"line2": null,
"postal_code": "00000",
"state": null
},
"email": null,
"name": "John Doe",
"phone": null,
"verified_address": null,
"verified_email": null,
"verified_name": null,
"verified_phone": null
},
"status": "chargeable",
"type": "sepa_debit",
"usage": "reusable",
"sepa_debit": {
"bank_code": "XXXXXXXXXXX",
"branch_code": "XXXXXXXXXXX",
"country": "XXXXXXXXXXX",
"fingerprint": "XXXXXXXXXXX",
"last4": "XXXXXXXXXXX",
"mandate_reference": "XXXXXXXXXXX",
"mandate_url": "https://hooks.stripe.com/adapter/sepa_debit/file/random_keys"
}
}],
"has_more": false,
"total_count": 1,
"url": "XXXXXXXXXXX"
},
"subscriptions": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "XXXXXXXXXXX"
}
}';
$data = json_decode($json);
$mandate_url = $data->sources->data[0]->sepa_debit->mandate_url);
echo $mandate_url;
?>
Alternatively you could get the json into an associative array and use the following syntax
$data = json_decode($json, true);
$mandate_url = $data['sources']['data'][0]['sepa_debit']['mandate_url'];
echo $mandate_url;

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

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