#Ishan-charge-failed
1 messages · Page 1 of 1 (latest)
Hi 👋 apologies for the delayed response. I would expect that catching StripeException and Exception would be enough, but have you tried it to see if it meets your needs?
Regarding your second point, there is quite a bit on that page and I was wondering if you'd be able to point me towards the section you're referring to?
Considering first point, I am creating a charge object to capture some amount from customer's card. What I need is when charge object creation fails, I need the charge id so that I can retrieve the charge and get the card details. Is StripeException the super exception class of all other stripe exception classes ?
Considering second point, I was pointing to the statement 'Handle the API error returned when a payment fails. For blocked and card issuer-declined payments, the error includes the charge’s ID, which you can then use to retrieve the charge.'
I have used this : stripeException.getStripeError().getCharge() to get the failed charge id. Is this right ?
Sounds like you're working with our Java library, is that right?
Yes
Gotcha, so you can take a look through the repo and see what an exception extends:
https://github.com/stripe/stripe-java/blob/110ae567c68aa67cfe697a6309d35e3d13c5e419/src/main/java/com/stripe/exception/CardException.java
For example, we can see here that CardException does extend StripeExtension. You can also see how that exception contains the charge object.
try {
} catch(StripeException stripeException) {
String chargeId = stripeException.getStripeError().getCharge();
} catch(Exception e) {
}
Will this work ? I mean can I get chargeId like this by having just this 2 catch blocks ?
Also, whatever the reason of charge failure, will I always get charge id in exception ?
Catching the StripeException should catch anything that extends it, I would just recommend double checking that's true for the exceptions you care about.
I'm not terribly familiar with this Getter functionality is being imported, so I'm not sure of the exact syntax for getting the charge ID. But you can use one of our test cards to force a decline and confirm if it works:
4000000000000002
https://stripe.com/docs/testing
I will try it.
Can you please answer my second question : 'Also, whatever the reason of charge failure, will I always get charge id in exception ?' ?
Let me try to double check that
Ok
And also considering this code, will the control go in Exception catch block ever if a stripe exception is thrown ?
Based on how it's written, no, it will only enter the first catch block when a StripeException is encountered.
Apologies for the delay, and for not realizing an overall better approach sooner. Since you want to capture information when a charge fails, rather than relying on exceptions (as a charge may be created successfully but then fail to process) I'd recommend that you look into catching webhook events. Particularly charge.failed events, especially as these events contain the charge object in them so you'll always be able to get the ID.
https://stripe.com/docs/webhooks
https://stripe.com/docs/api/events/types#event_types-charge.failed
So a charge object can get created successfully, but I can receive a 'charge.failed' webhook event after that ?
Sort of, Charges are not stateful so the creation/failure happen together.
I strongly suggest you look at moving to the payment intents API for better developer ergonomics here. The Payment Intent state machine wrapping around charges makes the flow/sequence much more explicit.
I have checked the checkbox 'Listen to events on Connected accounts'. Will I receive a 'charge.failed' webhook event ? The charge is getting captured using customer's credit card. I think 'charge.failed' in my case will be a platform event.
If the charge failure is on one of your Connected account, yes it would go to that endpoint if emitted by the account
Are you using Connect? Which type of charges?
We are creating a customer and a custom connected account. The customer will have a credit card attached to it. I am creating a 'Charge' object with 'capture' set to 'true'. So, the amount is getting credited to platform's balance. Then, I am creating a 'Transfer' object using the charge id and transferring money from platform's stripe balance to connected account's stripe balance.
Considering this flow, I will not get a 'charge.failed' webhook event because I am capturing charge using customer's credit card.
How will I get to know if a charge succeeded or failed ?
You could get that event on your platforms own account webhook, not a connect webhook though
if the charge exists on the platform, thats where the events will be emitted
'charge.failed' or 'charge.succeeded' webhooks will be shown in platform. That's right.
I want to know whether the charge was successfully captured or not. Depending on this, I want to do further processing.
OK great, that event would tell you which case happened
I want to know whether the charge was captured or not immediately after creating the charge object. Depending on this I want to do further processing in my code.
if the charge failed, it was not capture
If it succeeded, whether it is capture will be indicated by the captured attribute in the success event
try {
Charge.create()
} catch(StripeException stripeException) {
String chargeId = stripeException.getStripeError().getCharge();
} catch(Exception e) {
}
If the amount was not debited from customer's card due to some issue, will the control go in first catch block ?
Then can you please clarify the meaning of this sentence ?
And then what is the answer to this question ?
have you tried any of this?
you would understand it way better if you took the time to test for a few minutes in Test mode with all our test cards https://stripe.com/docs/testing#cards
I am gonna try it. Just wanted to clarify whether when amount is not debited from customer's card, will StripeException be thrown. If amount isn't debited, I want to write some logic.
I understand and I did answer that question and confirm, so did synthrider, the best option now is to try it which will only take a few minutes and confirm your findings.
If the bank declines the charge, then an exception is raised and a charge.failed event is also sent to your webhook endpoint if you have one
But I can depend on exception handling to handle this case instead of handling 'charge.failed' webhook event, right ?
yes
Ok. Sorry, I got confused. Thanks for the help.