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
Related
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"
}
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
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;
I am working with Shopify Webhook for order fulfillment(in PHP) now i want the payment details of "Information from the gateway" which are available in the shopify admin but not showing in Webhook details so anyone can please give any solution for this. following is the screenshot of details which i want to get in order.
These following are details which I want when any new order is confirmed:
1) Authorization key
2) Name on Credit card
3) Exp month
4) Exp year
details which i want to get in order
For this information you needs to use following shopify method
GET /admin/orders/#{id}/transactions.json
And following response you get:
HTTP/1.1 200 OK
{
"transactions": [
{
"id": 179259969,
"order_id": 450789469,
"amount": "209.00",
"kind": "refund",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-05T12:59:12-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {},
"error_code": null,
"source_name": "web"
},
{
"id": 389404469,
"order_id": 450789469,
"amount": "409.94",
"kind": "authorization",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-01T11:57:11-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {
"testcase": true,
"authorization": "123456"
},
"error_code": null,
"source_name": "web",
"payment_details": {
"credit_card_bin": null,
"avs_result_code": null,
"cvv_result_code": null,
"credit_card_number": "•••• •••• •••• 4242",
"credit_card_company": "Visa"
}
},
{
"id": 801038806,
"order_id": 450789469,
"amount": "250.94",
"kind": "capture",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-05T10:22:51-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {},
"error_code": null,
"source_name": "web"
}
]
}
And you want to more details than please refer the below link:
https://help.shopify.com/api/reference/transaction
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?