英文:
One instance of Stripe Checkout works, the other gives a Preflight response code of 403
问题
我在我的应用程序中正在实现另一个结账会话的实例。在我的捐赠控制器中,以下创建操作正常工作:
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
我将相关的Stripe部分复制到了我的参与者注册控制器中:
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
end
捐赠部分将无问题地重定向到Stripe,然而,如果选择的价格大于0,则注册部分将尝试启动Stripe Checkout。在浏览器控制台中,我收到了"Preflight response was not successful"错误代码403,并带有一些未提供详细信息的TypeError。
在这两个视图中,Stripe API的JavaScript在提交按钮上方包含:
= javascript_include_tag "https://js.stripe.com/v3"
由于我从捐赠控制器复制了代码,我没有看到我的错误在哪里。
我尚未更新success_url,因为我首先要尝试重定向到Stripe。name和unit_amount目前是硬编码的,以防我的变量不起作用。
英文:
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.
答案1
得分: 0
你分享的代码是Ruby中的一个简单HTTP重定向服务器端操作,除非你的客户端代码是使用ajax请求而不是页面/表单提交,否则不应在浏览器中引发CORS错误。
另外,有可能是你的表单提交配置错误,导致Rails将其转换为Turbo请求。将data-turbo=false
添加到你的表单可能会解决这个问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论