small medium large xlarge

Back to: All Forums  Take My Money
Generic-user-small
12 Nov 2016, 12:41
Max Shelley (2 posts)

In Beta version 5, page 34, there’s the following code

Payment.transaction do
  purchase_tickets
  create_payment
  charge
  @success = payment.succeeded?
end

This transaction calls the .charge method which is this:

def charge
  charge = StripeCharge.charge(token: stripe_token, payment: payment)
  payment.update!(status: charge.status, response_id: charge.id, full_response: charge.to_json)
end

This code takes place within the transaction so that if the update! call raises an exception, the database will be rolled back. However while working I had an instance where the Stripe charge was successful, but the update! call failed. That means the database erases everything it knows about the charge, but the customer has still had money taken.

Because Stripe won’t charge the same token twice, there isn’t a particular issue with the payment being taken twice, but it does create a scenario where the app doesn’t know about a financial transaction that has been made.

I’m guessing this is a simplified version to keep the code succinct for the book (or I’m not writing my version correctly), but I’m curious about alternative ways you’d approach handling the idea that the payment came back successfully, but our code still raises errors. Presumably, we still want to make sure we’ve kept the charge information no matter what.

Thanks.

Head_shot_pragsmall
12 Nov 2016, 12:46
Noel Rappin (49 posts)

Thanks for the question.

On page 35, we’re still just talking about happy paths (perhaps I could be clearer about that…) and not yet talking about error mitigation. Keep reading – chapter 5 on “Failure is totally an Option”, will have what you are looking for. But if that doesn’t answer your question, please let me know.

Thanks!

Noel

Generic-user-small
12 Nov 2016, 14:07
Max Shelley (2 posts)

Great, thanks for the quick reply, just skipped ahead and found:

If the payment goes through but the code fails after… ugh. This is a bad case. You want to make sure this error is captured and a notification is sent, because there may be some administrative clean-up in your future. In some cases you might need to automatically refund the payment.

:)

You must be logged in to comment