This is my json response but when I am trying to decode the Json string I got the NULL error. When i fix the same json output in online decode it works properly. What is the problem here? I can't fix the error.
{
"amount": 474,
"created": 1414385307,
"currency": "usd",
"id": "-snip-",
"livemode": false,
"paid": true,
"refunded": false,
"disputed": null,
"captured": true,
"description": null,
"statement_description": null,
"failure_message": null,
"failure_code": null,
"amount_refunded": 0,
"customer": "-snip-",
"invoice": null,
"refunds": {
"data": [],
"total_count": 0,
"has_more": false,
"url": "/v1/charges/-snip-/refunds",
"count": null
},
"card": {
"exp_month": 11,
"exp_year": 2025,
"last4": "-snip-",
"country": "US",
"type": null,
"name": null,
"id": "-snip-",
"customer": "-snip-",
"recipient": null,
"address_line1": null,
"address_line2": null,
"address_zip": null,
"address_city": null,
"address_state": null,
"address_country": null,
"address_zip_check": null,
"address_line1_check": null,
"cvc_check": null,
"fingerprint": "-snip-",
"brand": "Visa",
"funding": "credit"
},
"dispute": null,
"balance_transaction": "-snip-",
"metadata": {}
}
PHP code:
$curl = curl_init($service_url);
$curl_post_data = array(
'customerId' => 'Cus_id',
'amount' => 474
);
$data = json_encode($curl_post_data);
$headers = array('Content-type: application/json');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
$json=str_replace("JSON: ","","$curl_response");
var_dump(json_decode($json));
var_dump(json_decode($json, true));
Pass that entire string to json_decode() and it will work.
Try this one
$str = '{ "amount": 474, "created": 1414385307, "currency": "usd", "id": "-snip-", "livemode": false, "paid": true, "refunded": false, "disputed": null, "captured": true, "description": null, "statement_description": null, "failure_message": null, "failure_code": null, "amount_refunded": 0, "customer": "-snip-", "invoice": null, "refunds": { "data": [], "total_count": 0, "has_more": false, "url": "/v1/charges/-snip-/refunds", "count": null }, "card": { "exp_month": 11, "exp_year": 2025, "last4": "-snip-", "country": "US", "type": null, "name": null, "id": "-snip-", "customer": "-snip-", "recipient": null, "address_line1": null, "address_line2": null, "address_zip": null, "address_city": null, "address_state": null, "address_country": null, "address_zip_check": null, "address_line1_check": null, "cvc_check": null, "fingerprint": "-snip-", "brand": "Visa", "funding": "credit" }, "dispute": null, "balance_transaction": "-snip-", "metadata": {} }';
$data = json_decode($str);
print_r($data);
Related
I'm creating an invoice using Stripe's API call. Based on stripe documentation, there is no amount parameters for generated invoice and I always get zero in amount_due.
Another issue is I want to add line items in generate invoice, but there is no places to add line items for generated invoice.
Here is my code;
$params = [
'customer' => $stripeCustomer['stripeCustomerId'],
'auto_advance' => true,
'collection_method' => 'charge_automatically',
'currency' => Str::lower($record['CurrencyCode']),
'metadata' => $metaData,
];
$stripeInvoice = \Stripe\Invoice::create($params);
And stripe return object is here;
{
"id": "in_1M6otQLHPCwYGWEgSQraCHVf",
"object": "invoice",
"account_country": "SG",
"account_name": "Demo",
"account_tax_ids": null,
"amount_due": 0,
"amount_paid": 0,
"amount_remaining": 0,
"application": null,
"application_fee_amount": null,
"attempt_count": 0,
"attempted": false,
"auto_advance": true,
"automatic_tax": {
"enabled": false,
"status": null
},
"billing_reason": "manual",
"charge": null,
"collection_method": "charge_automatically",
"created": 1669094376,
"currency": "sgd",
"custom_fields": null,
"customer": "cus_MqVqznCTTb78Ct",
"customer_address": {
"city": "",
"country": "",
"line1": "",
"line2": "",
"postal_code": "",
"state": ""
},
"customer_email": "john.doe#doe.com",
"customer_name": "John Doe",
"customer_phone": null,
"customer_shipping": null,
"customer_tax_exempt": "none",
"customer_tax_ids": [
],
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"description": null,
"discount": null,
"discounts": [
],
"due_date": null,
"ending_balance": null,
"footer": null,
"from_invoice": null,
"hosted_invoice_url": null,
"invoice_pdf": null,
"last_finalization_error": null,
"latest_revision": null,
"lines": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/invoices/in_1M6otQLHPCwYGWEgSQraCHVf/lines"
},
"livemode": false,
"metadata": {
"invoiceNumber": "INV-0005",
"invoiceId": "fcca9507-411d-4087-b940-03251402beab"
},
"next_payment_attempt": 1669097976,
"number": null,
"on_behalf_of": null,
"paid": false,
"paid_out_of_band": false,
"payment_intent": null,
"payment_settings": {
"default_mandate": null,
"payment_method_options": null,
"payment_method_types": null
},
"period_end": 1669094376,
"period_start": 1669094376,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"quote": null,
"receipt_number": null,
"rendering_options": null,
"starting_balance": 0,
"statement_descriptor": null,
"status": "draft",
"status_transitions": {
"finalized_at": null,
"marked_uncollectible_at": null,
"paid_at": null,
"voided_at": null
},
"subscription": null,
"subtotal": 0,
"subtotal_excluding_tax": 0,
"tax": null,
"test_clock": null,
"total": 0,
"total_discount_amounts": [
],
"total_excluding_tax": 0,
"total_tax_amounts": [
],
"transfer_data": null,
"webhooks_delivered_at": 1669094376
}
Invoices Create API always creates an empty invoice first, then use Invoice Items Create API to add line items to the invoice later.
You may refer to the doc here for the step-by-step integration: https://stripe.com/docs/invoicing/integration
I get a list of invoices through api. See:
<?php
$url = "https://app.domain.tld/api/v2/accounts/censored/invoices.json?status=uncollectible";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"User-Agent: YourApp (yourname#example.com)",
"Authorization: Basic censored==",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
echo $resp = curl_exec($curl);
curl_close($curl);
?>
This data will be returned to me in JSON:
[{
"id": 23487088,
"custom_id": null,
"proforma": false,
"partial_proforma": false,
"number": "2022-05-0008",
"number_format_id": 363568,
"variable_symbol": "2022050008",
"your_name": "Censored s.r.o.",
"your_street": "Street 1801/22",
"your_street2": null,
"your_city": "Praha",
"your_zip": "10900",
"your_country": "CZ",
"your_registration_no": "00055222",
"your_vat_no": "CZ00055222",
"your_local_vat_no": null,
"client_name": "Firma s.r.o.",
"client_street": "Street 508/2",
"client_street2": null,
"client_city": "Prague",
"client_zip": "111 22",
"client_country": "CZ",
"client_registration_no": "0000000",
"client_vat_no": "",
"client_local_vat_no": "",
"subject_id": 14471217,
"subject_custom_id": null,
"generator_id": null,
"related_id": null,
"correction": false,
"correction_id": null,
"paypal": false,
"gopay": false,
"token": "zuqpytRSBg",
"status": "uncollectible",
"order_number": "",
"issued_on": "2022-05-13",
"taxable_fulfillment_due": "2022-05-13",
"due": 7,
"due_on": "2022-05-20",
"sent_at": null,
"paid_at": null,
"reminder_sent_at": null,
"accepted_at": null,
"cancelled_at": null,
"webinvoice_seen_at": null,
"note": "",
"footer_note": "Společnost je zapsána v obchodním rejstříku vedeném Městským soudem v Praze oddíl C, vložka 000000.",
"private_note": null,
"tags": [],
"bank_account": "0000000000/2010",
"iban": "CZ000000000000000000000",
"swift_bic": "FIOBCZPPXXX",
"show_already_paid_note_in_pdf": false,
"payment_method": "bank",
"hide_bank_account": false,
"currency": "CZK",
"exchange_rate": "1.0",
"language": "cz",
"transferred_tax_liability": false,
"eu_electronic_service": false,
"oss": "disabled",
"vat_price_mode": "without_vat",
"supply_code": "",
"subtotal": "3000.0",
"total": "3630.0",
"native_subtotal": "3000.0",
"native_total": "3630.0",
"remaining_amount": "3630.0",
"remaining_native_amount": "3630.0",
"paid_amount": "0.0",
"eet": false,
"eet_cash_register": null,
"eet_store": null,
"eet_records": [],
"lines": [{
"id": 52921163,
"name": "IT services",
"quantity": "1.0",
"unit_name": "",
"unit_price": "3000.0",
"vat_rate": 21,
"unit_price_without_vat": "3000.0",
"unit_price_with_vat": "3630.0"
}],
"attachment": null,
"html_url": "https://app.fakturoid.cz/censored/invoices/23487088",
"public_html_url": "https://app.fakturoid.cz/censored/p/zuqpytRSBg/2022-05-0008",
"url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487088.json",
"pdf_url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487088/download.pdf",
"subject_url": "https://app.fakturoid.cz/api/v2/accounts/censored/subjects/14471217.json",
"created_at": "2022-05-21T16:27:32.488+02:00",
"updated_at": "2022-05-21T16:27:35.464+02:00"
}, {
"id": 23487082,
"custom_id": null,
"proforma": false,
"partial_proforma": false,
"number": "2022-05-0007",
"number_format_id": 363568,
"variable_symbol": "2022050007",
"your_name": "censored s.r.o.",
"your_street": "censored",
"your_street2": null,
"your_city": "Praha",
"your_zip": "10900",
"your_country": "CZ",
"your_registration_no": "00055222",
"your_vat_no": "CZ00055222",
"your_local_vat_no": null,
"client_name": "Firma s.r.o.",
"client_street": "Street 508/2",
"client_street2": null,
"client_city": "Prague",
"client_zip": "111 22",
"client_country": "CZ",
"client_registration_no": "0000000",
"client_vat_no": "",
"client_local_vat_no": "",
"subject_id": 14471217,
"subject_custom_id": null,
"generator_id": null,
"related_id": null,
"correction": false,
"correction_id": null,
"paypal": false,
"gopay": false,
"token": "7xIDMf64iQ",
"status": "uncollectible",
"order_number": "",
"issued_on": "2022-05-04",
"taxable_fulfillment_due": "2022-05-04",
"due": 1,
"due_on": "2022-05-05",
"sent_at": null,
"paid_at": null,
"reminder_sent_at": null,
"accepted_at": null,
"cancelled_at": null,
"webinvoice_seen_at": null,
"note": "",
"footer_note": "Společnost je zapsána v obchodním rejstříku vedeném Městským soudem v Praze oddíl C, vložka 000000.",
"private_note": null,
"tags": [],
"bank_account": "000000000/2010",
"iban": "CZ000000000000000000000",
"swift_bic": "FIOBCZPPXXX",
"show_already_paid_note_in_pdf": false,
"payment_method": "bank",
"hide_bank_account": false,
"currency": "CZK",
"exchange_rate": "1.0",
"language": "cz",
"transferred_tax_liability": false,
"eu_electronic_service": false,
"oss": "disabled",
"vat_price_mode": "without_vat",
"supply_code": "",
"subtotal": "5000.0",
"total": "6050.0",
"native_subtotal": "5000.0",
"native_total": "6050.0",
"remaining_amount": "6050.0",
"remaining_native_amount": "6050.0",
"paid_amount": "0.0",
"eet": false,
"eet_cash_register": null,
"eet_store": null,
"eet_records": [],
"lines": [{
"id": 52921141,
"name": "IT services",
"quantity": "1.0",
"unit_name": "",
"unit_price": "5000.0",
"vat_rate": 21,
"unit_price_without_vat": "5000.0",
"unit_price_with_vat": "6050.0"
}],
"attachment": null,
"html_url": "https://app.fakturoid.cz/censored/invoices/23487082",
"public_html_url": "https://app.fakturoid.cz/censored/p/7xIDMf64iQ/2022-05-0007",
"url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487082.json",
"pdf_url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487082/download.pdf",
"subject_url": "https://app.fakturoid.cz/api/v2/accounts/censored/subjects/14471217.json",
"created_at": "2022-05-21T16:25:51.916+02:00",
"updated_at": "2022-05-21T16:26:54.323+02:00"
}]
The problem is that I can't throw it into a nice boostrap table, columns using PHP and possibly save it to a database. I tried it this way, but I did not meet with success.
<?php
$data = json_decode($resp->data, true);
foreach ($data as $key => $value) {
$invoice_id = $value['array']['id']; // id invoice
$invoice_status = $value['array']['status']; // paid, unpaid, uncollectible etc...
$invoice_price = $value['array']['total']; // total price with VAT
}
?>
But I'm not successful. I'm getting a error:
Error: Trying to get property 'data' of non-object
I'm at the end, please help
please use this php code, your array contains objects
$data = json_decode($resp);
foreach ($data as $key => $value) {
$invoice_id = $value->id; // id invoice
$invoice_status = $value->status; // paid, unpaid, uncollectible etc...
$invoice_price = $value->total; // total price with VAT
}
i get a response of my code . that response has a variable value which is before a constant string and after another constant string . i want get this variable and echo it .
example : i have this json:
{ "id": "tok_1Cs0osBsNOaFjmrSIBFEAkPd", "object": "token", "client":
{ "id": "card_1Cs0osBsNOaFjmrSl9fAs5hT", "object": "name", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": "74455", "address_zip_check": "unchecked", "country": "RU", "metadata": {}, "name": "2022 Nemelek\r\n", "tokenization_method": null }, "client_ip": "41.199.157.183", "created": 1532578278, "livemode": true, "type": "Uid", "used": false }
i wanna extract tok_1Cs0osBsNOaFjmrSIBFEAkPd which is after { "id": " and before ", "object":
btw : this value is variable but what is before and first word after it are constants
how can i code it in php
Use json_decode():
$str = '{ "id": "tok_1Cs0osBsNOaFjmrSIBFEAkPd", "object": "token", "client": { "id": "card_1Cs0osBsNOaFjmrSl9fAs5hT", "object": "name", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": "74455", "address_zip_check": "unchecked", "country": "RU", "metadata": {}, "name": "2022 Nemelek\r\n", "tokenization_method": null }, "client_ip": "41.199.157.183", "created": 1532578278, "livemode": true, "type": "Uid", "used": false }';
$v = json_decode($str);
//tok_1Cs0osBsNOaFjmrSIBFEAkPd
echo $v->id;
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;
I am trying to access a value in my JSON Array. This is my PHP code to get the JSON content:
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
$event = \Stripe\Event::retrieve($event_id);
This is the output:
Stripe\Event JSON: {
"id": "evt_6WV8LzHBPwRYW4",
"created": 1435683755,
"livemode": false,
"type": "charge.succeeded",
"data": {
"object": {
"id": "ch_6WV8a3Sdj3ihE2",
"object": "charge",
"created": 1435683755,
"livemode": false,
"paid": true,
"status": "succeeded",
"amount": 5000,
"currency": "usd",
"refunded": false,
"source": {
"id": "card_6WV8kV4c36X1Gl",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 2,
"exp_year": 2022,
"fingerprint": "QXn7Az3ZTTaLBbhx",
"country": "US",
"name": "oliverbusk#hotmail.com",
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"cvc_check": "pass",
"address_line1_check": null,
"address_zip_check": null,
"tokenization_method": null,
"dynamic_last4": null,
"metadata": [],
"customer": "cus_6WV87uyazq5L64"
},
"captured": true,
"balance_transaction": "txn_6WV8Bz6aZSB3tb",
"failure_message": null,
"failure_code": null,
"amount_refunded": 0,
"customer": "cus_6WV87uyazq5L64",
"invoice": null,
"description": null,
"dispute": null,
"metadata": {
"product": "deposit"
},
"statement_descriptor": null,
"fraud_details": [],
"receipt_email": null,
"receipt_number": null,
"shipping": null,
"destination": null,
"application_fee": null,
"refunds": {
"object": "list",
"total_count": 0,
"has_more": false,
"url": "\/v1\/charges\/ch_6WV8a3Sdj3ihE2\/refunds",
"data": []
}
}
},
"object": "event",
"pending_webhooks": 1,
"request": "iar_6WV8y99x1gReuF",
"api_version": "2015-06-15"
}
To be specific, I wish to access the data->metadata->product value - which in this example is "deposit"
This is how I am trying to access it:
$event->data->metadata->product;
Although that returns blank.
What am I doing wrong?
There is an object property under data. Do a print_r() on $event to see the structure.
Use: $event->data->object->metadata->product
You can see in the print_r():
stdClass Object
(
[id] => evt_6WV8LzHBPwRYW4
.....
[data] => stdClass Object
(
[object] => stdClass Object
(
.....
[metadata] => stdClass Object
(
[product] => deposit
)