[英]How to Call Stripe Checkout from a rails controller
I'm using the stripe checkout api with a rails app.我正在使用带有 rails 应用程序的条带结帐 api。 I want to redirect to the checkout page when a user submits a form.
当用户提交表单时,我想重定向到结帐页面。 The issue I'm running into is that Stripe checkout requires a button to be clicked which will trigger a javascript function, that takes the session id and redirects to the checkout page.
我遇到的问题是 Stripe 结帐需要单击一个按钮,该按钮将触发一个 javascript 函数,该函数获取会话 ID 并重定向到结帐页面。
My problem is that when the user submits the form I want to use their submitted email to create the checkout session and link their stripe customer account to their checkout.我的问题是,当用户提交表单时,我想使用他们提交的电子邮件来创建结帐会话并将他们的 Stripe 客户帐户链接到他们的结帐。 So on the form submit I am able to create the session fine, I'm just not able to also trigger the frontend javascript function once the session has been created.
因此,在表单提交上,我能够很好地创建会话,一旦创建会话,我就无法触发前端 javascript 函数。
I've tried to call a javascript function directly from the rails controller and pass it the session id, I was not able to get this working but feel it's likely close to the solution.我试图直接从 rails 控制器调用一个 javascript 函数并将会话 id 传递给它,我无法让它工作,但觉得它可能接近解决方案。 How specifically can I trigger the javascript function once the controller has created the session
一旦控制器创建了会话,我如何具体地触发 javascript 函数
This is the Javascript function I want to call in my controller这是我想在我的控制器中调用的 Javascript 函数
function go_to_stripe(sessionId) {
var stripe = Stripe("<%=ENV["STRIPE_PUB_KEY"]%>");
stripe.redirectToCheckout({
sessionId: sessionId
}).then(function (result) {
console.log("hitting the then with result of ", result);
});
}
I've tried this in the controller (and a few variations of it) but it doesn't call the javascript function.我已经在控制器中尝试过这个(以及它的一些变体),但它没有调用 javascript 函数。
# this returns the session id
@stripe_session_id = create_stripe_session(@cart)
respond_to do |format|
format.html
format.js { render :js => "go_to_stripe("+@stripe_session_id+");" }
end
found a fix based on https://signalvnoise.com/posts/3697-server-generated-javascript-responses 找到了基于https://signalvnoise.com/posts/3697-server-generation-javascript-responses的修复程序
The key was having remote: true on the form as well as naming the javascript file the same as the controller action. 关键是具有remote:表单上的true以及将javascript文件命名为与控制器操作相同的名称。 change the javascript in the file to contain the body of the function.
更改文件中的javascript以包含函数的主体。
javascript javascript
var stripe = Stripe("<%=ENV["STRIPE_PUB_KEY"]%>");
stripe.redirectToCheckout({
sessionId: "<%= @stripe_session_id %>"
}).then(function (result) {
console.log("hitting the then with result of ", result);
});
rails controller 导轨控制器
@stripe_session_id = create_stripe_session(@cart)
respond_to do |format|
format.html
format.js
end
I wrote a tutorial to redirect to Stripe Checkout from a Rails controller.我写了一个教程来从 Rails 控制器重定向到 Stripe Checkout。 The idea is to create the Stripe Session after a User is registered (using Devise), within
the after_sign_up_path_for
method:这个想法是在用户注册后(使用 Devise)在
the after_sign_up_path_for
方法中创建条纹会话:
def after_sign_up_path_for(resource)
super(resource)
Stripe.api_key = "#{ENV['STRIPE_API_KEY']}"
# See https://stripe.com/docs/api/checkout/sessions/create
# for additional parameters to pass.
# {CHECKOUT_SESSION_ID} is a string literal; do not change it!
# the actual Session ID is returned in the query parameter when your customer
# is redirected to the success page.
session = Stripe::Checkout::Session.create(
success_url: "success_url",
cancel_url: "cancel_url",
payment_method_types: ['card'],
mode: 'payment',
line_items: [{
# For metered billing, do not pass quantity
quantity: 1,
price: ENV['STRIPE_PRICE_ID']
}],
locale: "fr",
customer_email: current_user.email
)
return session.url
end
Doing so, the user is automatically redirected to Checkout.这样做后,用户会自动重定向到 Checkout。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.