In my Rails applications, I am trying to only run a Stripe Charge command IF a Stripe Error does not exist. And I want to be able to inspect the Stripe Error.
charge = Stripe::Charge.create(
:amount => amount_in_cents,
:customer => stripe_customer,
:description => "my application",
)
Should be something like:
unless Stripe.errors.exists? do
charge = Stripe::Charge.create(
:amount => amount_in_cents,
:customer => stripe_customer,
:description => "my application",
)
end
And to be confident my code is working, I am trying to puts
the Stripe Error, if it exists. Is the Stripe Error an object that I can puts
?
Such as:
if Stripe.error.exists?
puts Stripe.error
end
Trying to see the output with puts
commands in my model, as well as in my testing file like so:
To intentionally create an error:
StripeMock.prepare_card_error(:card_declined)
Me trying to puts
this (but none of the below works):
puts "Stripe Card Error: #{Stripe::CardError}"
begin
rescue Stripe::CardError => e
body = e.json_body
err = body[:error]
puts err.exists?
puts err
end
puts err.exists?
puts err
When I try something like:
error = Stripe::CardError
if error
rescue Stripe::CardError => e
end
it is not valid, and neither are if Stripe.error
or if Stripe::CardError
or if Stripe::CardError.exists?
.
There must be some Stripe Error object
that we can apply logic to?!?
Any help on puts
ing / inspecting the Stripe Error would be appreciated, thank you.
Building off the comments on the original question: you want to catch the exception thrown by Stripe::charge.create()
.
begin
puts "About to create charge"
charge = Stripe::Charge.create(
:amount => amount_in_cents,
:customer => stripe_customer,
:description => "my application",
)
# The next line only runs if Charge.create() did not raise an exception
puts "Created charge #{charge.id}"
MyDatabase.insert(charge.id)
rescue Stripe::CardError => e
# An error was thrown, so execution of the begin block did not complete
puts e
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.