I have an app that uses Stripe Checkout and creates a Stripe session and then redirects to checkout. In my controller, this code works fine:
session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
subscription_data: {
items: [{ plan: stripe_plan }],
},
customer: stripe_customer_id,
customer_email: stripe_customer_email,
client_reference_id: current_user.id,
success_url: "http://localhost:3000/welcome_new_subscriber?session_id={CHECKOUT_SESSION_ID}",
cancel_url: "http://localhost:3000/charges/new?stripe_plan=" + stripe_plan,
)
And correctly redirects to the URL:
http://localhost:3000/welcome_new_subscriber?session_id=cs_test_abcd1234
When I change the success_url line to the following (as advised on a few websites) it does not work:
success_url: welcome_new_subscriber_url(:session_id => '{CHECKOUT_SESSION_ID}'),
The URL resolves to:
http://localhost:3000/welcome_new_subscriber?session_id=%7BCHECKOUT_SESSION_ID%7D
It seems the {CHECKOUT_SESSION_ID} is not resolving correctly. Any idea what I am doing incorrectly - I've tried every syntax change I can think of?
When you use
welcome_new_subscriber_url(:session_id => '{CHECKOUT_SESSION_ID}')
Rails is trying to be helpful by URI-escaping {
and }
. However the Stripe gem using these as special characters to know where to replace template variables.
So you could just use
welcome_new_subscriber_url + "?session_id={CHECKOUT_SESSION_ID}"
Not the most elegant thing, but then again Stripe's system here is kinda wierd (why don't they let you just pass CHECKOUT_SESSION_ID
yourself?), so we gotta make do
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With