【发布时间】:2020-11-18 04:07:51
【问题描述】:
希望有人可以为我提供一些见解,因为我是一个新开发人员,所以我在这里不确定一些事情。
所以过去几天我一直在努力实现自定义条带结帐,我认为我在这里要做的是从我的 rails 视图执行一个异步操作,以便我结帐到我的服务器,以确认付款是否有效.
在 stripes 网站上,我找到了我想要实施的解决方案,但我不清楚他们的意思,我知道他们指的是什么,并且认为我可以做到,但老实说,我不确定我放在哪里我在 rails 中的服务器端代码来处理这个异步调用。它进入我的控制器吗?在我的控制器中的方法内?我是否在某处制作另一个文件?
function stripePaymentMethodHandler(result) {
if (result.error) {
// Show error in payment form
} else {
// Otherwise send paymentMethod.id to your server (see Step 4)
fetch('/pay', { // I am not sure about this part. They say to make an endpoint on my server to handle this, does this mean I make a route only for this action in my routes? Then this action gets performed via that route?
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
payment_method_id: result.paymentMethod.id,
})
}).then(function(result) {
// Handle server response (see Step 4)
result.json().then(function(json) {
handleServerResponse(json);
})
});
}
}
这是我需要实现的相应服务器端代码,而且,我理解代码本身,我只是不确定我把它放在哪里。在我的控制器中?在我的创建操作中?
post '/pay' do
data = JSON.parse(request.body.read.to_s)
begin
if data['payment_method_id']
# Create the PaymentIntent
intent = Stripe::PaymentIntent.create(
payment_method: data['payment_method_id'],
amount: 1099,
currency: 'usd',
confirmation_method: 'manual',
confirm: true,
)
elsif data['payment_intent_id']
intent = Stripe::PaymentIntent.confirm(data['payment_intent_id'])
end
rescue Stripe::CardError => e
# Display error on client
return [200, { error: e.message }.to_json]
end
return generate_response(intent)
end
def generate_response(intent)
# Note that if your API version is before 2019-02-11, 'requires_action'
# appears as 'requires_source_action'.
if intent.status == 'requires_action' &&
intent.next_action.type == 'use_stripe_sdk'
# Tell the client to handle the action
[
200,
{
requires_action: true,
payment_intent_client_secret: intent.client_secret
}.to_json
]
elsif intent.status == 'succeeded'
# The payment didn’t need any additional actions and is completed!
# Handle post-payment fulfillment
[200, { success: true }.to_json]
else
# Invalid status
return [500, { error: 'Invalid PaymentIntent status' }.to_json]
end
end
我明白这一切都在做什么,对我来说最令人困惑的部分是这里引用的“/pay”路线。我可以对我的网站已有的 url 执行这些请求吗?例如 '/orders/new' ?或者我是否需要创建一个名为“/pay”的新路由,如示例中所示,并且该路由仅用于此呼叫?如果是这样,我如何在我的路线中定义它?资源:支付,还是什么?这条路线有什么资源?它只是存储信息的占位符吗?
感谢任何帮助!我一直试图在网上找到一些使用 RoR 的明确示例,也许我用谷歌搜索了错误的东西,因为我找不到太多。
【问题讨论】:
-
这看起来像一个 Sinatra 示例。您能否指出您从中获取此文件的那部分文档?那我也许能帮上忙。
标签: ruby-on-rails asynchronous routes stripe-payments