Reputation: 139
I am looking for the php code syntax to capture if the call was successful or failed to stripe API. I need to capture one of the objects property to determine this but I am not sure what the best approach is. Here's a sample response from a successful ACH api charge. I am looking for the php code syntax to capture if call was successful or failed
Stripe\Charge Object
(
[id] => py_1EFlRJBuwnzOEo57Poikh194
[object] => charge
[amount] => 50
[amount_refunded] => 0
[application] =>
[application_fee] =>
[application_fee_amount] =>
[balance_transaction] => txn_1EFlRJBuwnzOEo57dupt8SWp
[captured] => 1
[created] => 1553015365
[currency] => usd
[customer] => cus_Ec6cAdg6N2yYcF
[description] =>
[destination] =>
[dispute] =>
[failure_code] =>
[failure_message] =>
[fraud_details] => Array
(
)
[invoice] =>
[livemode] =>
[metadata] => Stripe\StripeObject Object
(
)
[on_behalf_of] =>
[order] =>
[outcome] => Stripe\StripeObject Object
(
[network_status] => approved_by_network
[reason] =>
[risk_level] => not_assessed
[seller_message] => Payment complete.
[type] => authorized
)
[paid] =>
[payment_intent] =>
[receipt_email] =>
[receipt_number] =>
[receipt_url] => https://pay.stripe.com/receipts/acct_1E8sOiBuwnzOEo57/py_1EFlRJBuwnzOEo57Poikh194/rcpt_EjDtGP7iknHI9RCBJ3iUODNC6bVBVyM
[refunded] =>
[refunds] => Stripe\Collection Object
(
[object] => list
[data] => Array
(
)
[has_more] =>
[total_count] => 0
[url] => /v1/charges/py_1EFlRJBuwnzOEo57Poikh194/refunds
)
[review] =>
[shipping] =>
[source] => Stripe\BankAccount Object
(
[id] => ba_1E8sTZBuwnzOEo57MKqXEvb7
[object] => bank_account
[account_holder_name] => Rassi Stern
[account_holder_type] => individual
[bank_name] => STRIPE TEST BANK
[country] => US
[currency] => usd
[customer] => cus_Ec6cAdg6N2yYcF
[fingerprint] => 9l7up0pswCYSO7eu
[last4] => 6789
[metadata] => Stripe\StripeObject Object
(
)
[routing_number] => 110000000
[status] => verified
)
[source_transfer] =>
[statement_descriptor] =>
[status] => pending
[transfer_data] =>
[transfer_group] =>
)
Upvotes: 1
Views: 1143
Reputation: 314
You can fetch the value of the "paid" OR "status" parameters from the charge object.
$success = json_encode($charge->paid); // Expected value = true
$success = json_encode($charge->status); // Expected value = succeeded
Then you can do your check:
if($success) { // or $success == "succeeded" depending on which array key you go for.
// Payment succeeded! Do something...
} else {
print_r(json_encode($charge->failure_message));
// Do something else...
}
The code may also work without json_encode(), I've found that my code is less buggy by using json_encode.
This is as per the Stripe API Docs: https://stripe.com/docs/api/charges/create
Upvotes: 1
Reputation: 1278
Are you using the Stripe PHP library?
I implemented something like this:
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // in pence - min 30p
"currency" => $currency,
"description" => $description,
"statement_descriptor" => $statementDescriptor,
"source" => $token,
"metadata" => array (
'code' => $code
),
"receipt_email" => $email
));
$success = true;
} catch(\Stripe\Error\Card $e) {
$success = false;
// Some other stuff to capture the reason for failure and tell the user etc.
}
If you're not using their library, it's probably worth investigating it: there are a lot of varying reasons why a payment could succeed or fail, so looking through that massive Charge Object for the answer may not 100% yield the results you want.
Upvotes: 2