Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe success_url format in a rails app not creating correct URL

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?

like image 499
DeeBee Avatar asked May 17 '20 17:05

DeeBee


1 Answers

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

like image 58
max pleaner Avatar answered Oct 09 '22 14:10

max pleaner