Reputation: 7170
I'm trying to integrate Stripe payment on my web site.
I can't understand how to know if a payment is successfull or not.
This is my code:
\Stripe\Stripe::setApiKey("my_secret_key");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];
$charge = \Stripe\Charge::create([
'amount' => 50,
'currency' => 'eur',
'description' => 'My product name',
'source' => $token,
]);
Now, I can assume that $charge could contain an error or a succesfull code but I don't know if it is true.
Thanks
Upvotes: 0
Views: 644
Reputation: 8747
Phil Cross is mostly right, except that you'll also want to catch errors to determine if the Charge succeeded or not.
If it was declined, it will throw a \Stripe\CardError that includes the reasons why; if it does not throw an error, that means it succeeded.
Upvotes: 1
Reputation: 9302
https://stripe.com/docs/api#create_charge
You can see the response if a charge has been successfully received by Stripe.
If there was some kind of network error, or failure, the Stripe SDK would throw an exception (https://stripe.com/docs/api#error_handling)
There are also a few things to look out for in the response.
captured
is whether Stripe has captured the charge. In some instances, you can tell Stripe to check payment can be made, but don't make it. In this case, captured would be false.
The failure_code
and outcome
fields will give you extra information about whether a charge was successfully made or not.
Upvotes: 1