Searching Stripe's JSON response value with PHP - 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;

Related

How to create plan on stripe and return plan id with prefix plan_

I am creating a plan with Stripe, but the returned id is prefixed with plan_ (plan_JpCO5hQmxIMCJS) not price_ (price_JpCO5hQmxIMCJS) like on the document, tell me why and how to fix it, thanks.
This is data return in document stripe
{
"id": "price_1JDP7V2eZvKYlo2Ci6hxmkDV",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 2000,
"amount_decimal": "2000",
"billing_scheme": "per_unit",
"created": 1626334593,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_Jr66HgcAtDZEOI",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
}

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

Json response from stripe to variables in php

I have a code in php that listen to stripe which sends to the my server a json format information. I would like to retrieve the email from the json and assign it to a variable $email. I don't understand what is wrong with my php code.
PHP code:
<?php
include "connect.php";
echo "Works";
require_once('stripe_final/init.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("this is hidden key");
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
$event_id = $event_json->id;
if(isset($event_json->id)) {
try {
// to verify this is a real event, we re-retrieve the event from Stripe
// $event = Stripe_Event::retrieve($event_id);
// successful payment
if($event->type == 'charge.succeeded') {
// send a payment receipt email here
// retrieve the payer's information
$email = $event_json->name;
$sql = "INSERT INTO stripe (email) VALUES ('$email')";
if (mysqli_query($db, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($db);
}
mysqli_close($db);
}
} catch (Exception $e) {
}
}
http_response_code(200); // PHP 5.4 or greater
?>
Json code:
{
"id": "evt_7nxAKynL74HXUu",
"object": "event",
"api_version": "2012-11-07",
"created": 1454008591,
"data": {
"object": {
"id": "ch_7nxA7QWn3069zJ",
"object": "charge",
"amount": 15000,
"amount_refunded": 0,
"application_fee": null,
"balance_transaction": "txn_7nxAqCOclHRnAv",
"captured": true,
"card": {
"id": "card_7nxAJFJ52PhS8G",
"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_7nxAxI4lGICtyv",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 11,
"exp_year": 2022,
"fingerprint": "lEO6YJCu2ASyQbvB",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": "futjakot#gmail.com",
"tokenization_method": null,
"type": "Visa"
},
"created": 1454008591,
"currency": "gbp",
"customer": "cus_7nxAxI4lGICtyv",
"description": null,
"destination": null,
"dispute": null,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": null,
"livemode": false,
"metadata": {},
"order": null,
"paid": true,
"receipt_email": "futjakot#gmail.com",
"receipt_number": null,
"refunded": false,
"refunds": [],
"shipping": null,
"source": {
"id": "card_7nxAJFJ52PhS8G",
"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_7nxAxI4lGICtyv",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 11,
"exp_year": 2022,
"fingerprint": "lEO6YJCu2ASyQbvB",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": "futjakot#gmail.com",
"tokenization_method": null,
"type": "Visa"
},
"statement_descriptor": null,
"status": "paid",
"statement_description": null,
"fee": 455,
"fee_details": [
{
"amount": 455,
"amount_refunded": 0,
"application": null,
"currency": "gbp",
"description": "Stripe processing fees",
"type": "stripe_fee"
}
]
}
},
"livemode": false,
"pending_webhooks": 1,
"request": "req_7nxAFmUej9UkFy",
"type": "charge.succeeded"
}
Actually, you have 3 emails in that JSON.
$event_json = json_decode($input);
$email1 = $event_json->data->object->card->name;
$email2 = $event_json->data->object->receipt_email;
$email3 = $event_json->data->object->source->name;
You could have figured it out by yourself by doing a series of var_dump statements on $event_json.
As you see name is in object card of object named object in object named data so the retrieval of e-mail should be:
$email = $event_json->data->object->card->name;

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