gpt4 book ai didi

ruby-on-rails - redirect_to 外部 Stripe 结帐 URL 在 Rails 7 中不起作用

转载 作者:行者123 更新时间:2023-12-05 01:54:17 24 4
gpt4 key购买 nike

对于 Stripe 的新结帐,需要在创建 session 后重定向到外部 URL。

def create_checkout_session
Stripe.api_key = "sk_test_"

session = Stripe::Checkout::Session.create({
line_items: [{
price_data: {
currency: 'usd',
product_data: {
name: 'KYC services',
},
unit_amount: 1000,
},
quantity: 1,
}],
mode: 'payment',
# These placeholder URLs will be replaced in a following step.
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel'
})


redirect_to session.url, status: 303, allow_other_host: true

我的 redirect_to 没有带我去任何地方,终端也没有错误。如果我不包含 allow_other_host: true,我会收到一条错误消息,提示 Unsafe redirect to "https://checkout.stripe.com

如何在 Rails 7 中对外部 URL 执行 redirect_to?为了这个演示应用程序,我不介意漏洞。

最佳答案

我也遇到了这个错误。 Strip Checkout 重定向在 Rails 6 中运行良好,但是当我更新到 Rails 7 时,重定向被阻止了。这是我在控制台中看到的错误:

new:1 Access to fetch at '<VALID & FUNCTIONING STRIPE URL HERE>' (redirected from 'http://localhost:3000/sessions/1/charges') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Controller 代码如下:

  def create
@charge = Charge.new(charge_params)
@charge.seller = @session.user
@charge.purchaser = current_user if current_user
@charge.session = @session

if @charge.save
the_root_url = URI.join(root_url).to_s.chomp('/')
success_url = the_root_url + session_charge_success_path(@session, @charge)
cancel_url = the_root_url + session_path(@session)

@charge.update "success_url": success_url
@charge.update "cancel_url": cancel_url
stripe_session = Stripe::Checkout::Session.create({
line_items: [{
price_data: {
currency: 'usd',
product_data: {
name: "Download the high resolution photo",
},
unit_amount: @session.default_price_cents,
},
quantity: 1,
}],
mode: 'payment',
success_url: @charge.success_url,
cancel_url: @charge.cancel_url,
})
redirect_to stripe_session.url, status: 303, allow_other_host: true
else
redirect_to @session, notice: "Sorry, something went wrong."
end

结束

这在本地发生的与在生产中发生的一样。

解决方案! (上面有介绍,但是一时想不起来)

我改变了这个:

<%= form.submit "Buy", class: "btn btn-primary btn-block col-12 my-1" %>

为此:

<%= form.submit "Buy", data: { turbo: false }, class: "btn btn-primary btn-block col-12 my-1" %>

关于ruby-on-rails - redirect_to 外部 Stripe 结帐 URL 在 Rails 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70733756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com