One instance of Stripe Checkout works, the other gives a Preflight response code of 403

Advertisements

I am implementing another instance of the Checkout Session in my app. In my donations controller, the following create action works fine:

def create
        @donation = Donation.create(create_params)

        if @donation.save
            if Rails.env.development?
                success_url = "http://localhost:3000/donations_success?session_id={CHECKOUT_SESSION_ID}"
                cancel_url = "http://localhost:3000/"
            elsif Rails.env.production?
                success_url = "https://www.dbsan.org/donations_success?session_id={CHECKOUT_SESSION_ID}"
                cancel_url = "https://www.dbsan.org/"
            end

            data = {
                line_items: [{
                    price_data: {
                        currency: 'usd',
                        product_data: {
                        name: @donation.program
                    },
                    unit_amount: @donation.amount.to_i
                    },
                    quantity: 1,
                }], 
                mode: 'payment',
                customer_email: @donation.email,
                success_url: success_url,
                cancel_url: cancel_url
            }

            session = Stripe::Checkout::Session.create(data)
            redirect_to session.url, allow_other_host: true
        end
    end

I copied the relevant Stripe part into my participant registration controller:

def create
        @registrant = @challenge.challenge_participants.build(register_params)
        @registrant.user_id = current_user.id
        unless @registrant.donations.empty?
            @registrant.donations.first.user_id = current_user.id
            @registrant.donations.first.email = current_user.email
        end

        if @registrant.save
            @challenge = @registrant.challenge
            ChallengeMailer.with(registrant: @registrant).registered.deliver_now
            if @registrant.price.price == 0
                redirect_to challenge_participant_path(@challenge, @registrant)
            else
                if Rails.env.development?
                    success_url = "http://localhost:3000/donations_success?session_id={CHECKOUT_SESSION_ID}"
                    cancel_url = "http://localhost:3000/"
                elsif Rails.env.production?
                    success_url = "https://www.dbsan.org/donations_success?session_id={CHECKOUT_SESSION_ID}"
                    cancel_url = "https://www.dbsan.org/"
                end

                data = {
                    line_items: [{
                        price_data: {
                            currency: 'usd',
                            product_data: {
                            name: "Registration"
                        },
                        unit_amount: 100
                        },
                        quantity: 1,
                    }], 
                    mode: 'payment',
                    success_url: success_url,
                    cancel_url: cancel_url
                }

                session = Stripe::Checkout::Session.create(data)
                redirect_to session.url, allow_other_host: true
            end
        end

The Donations one will redirect to Stripe without issue; however, the registration one, if a pricing selected is greater than 0, it will then attempt to initiate a Stripe Checkout. In my browser console I get a Preflight response was not successful error code 403 with some TypeError that it is not giving me details of.

on both of the views, the Stripe API Javascript is included just above the submit button:

= javascript_include_tag "https://js.stripe.com/v3"

Since I copied the code over from the donations controller, I’m not seeing what my error is.

I haven’t updated the success_url yet as I’m trying to first get redirected to Stripe. The name and unit_amount are right now hard coded in case my variables aren’t working.

>Solution :

The code you shared is a simple HTTP redirect server-side in Ruby and shouldn’t cause a CORS error in the browser unless your client-side code is making an ajax request instead of a page/form submit.

Alternatively, it’s possible your form submission is mis-configured and Rails turns this in a turbo request. Adding data-turbo=false to your form might solve that problem.

Leave a ReplyCancel reply