I am trying to retrieve the Stripe charge id upon a failed charge, so I can retrieve my record thanks to that id when the charge.failed
hook is fired. I tried inspect the exception fired but I cannot find any way to get it. Here is my code :
def charge
token = params[:stripeToken]
type = params[:stripeTokenType]
metadata = {}
record = Record.new(amount: Random.rand(2000), valid: false)
charge = nil
begin
charge = Stripe::Charge.create(
{
amount: 2000,
currency: 'eur',
source: token,
description: 'Test',
metadata: metadata
}, { stripe_account: 'xxxxx' })
record.stripe_charge_id
flash[:notice] = 'Transaction validée'
rescue Exception => e
record.error = e.code
flash[:error] = 'Erreur de paiement'
end
flash[:error] = 'Erreur de paiement' unless record.save || flash[:error]
redirect_to :stripe_test
end
I finally used the meta data to store my record id with the charge. So I am able to retrieve it using this metadata.
charge = Stripe::Charge.create(
{
amount: 2000,
currency: 'eur',
source: token,
description: 'Test',
metadata: { record_id: 23 }
}, { stripe_account: 'xxxxx' })