希望有人可以为我提供一些见识,因为我是一名新开发人员,因此我对此不确定。
因此,过去几天来我一直在实现自定义条带化结帐,我想在这里要做的就是从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”路由。我可以对我的网站已经拥有的网址执行这些请求吗?例如'/ orders / new'吗?还是我需要像在该示例中那样创建一条称为“ / pay”的新路由,而该路由将仅用于此呼叫?如果是这样,如何在路线中定义?资源:支付,还是什么?这条路线将拥有哪些资源?它只是存储信息的占位符吗?
任何帮助表示赞赏!我一直在尝试使用RoR在网络上找到一些明确的示例,也许我在搜索错误的东西,因为我找不到太多。
这些示例使用Sinatra作为Web框架,因此实际的路由与Rails看起来有些不同。基本上在任何地方您都可以看到示例中定义的路由,get
或者post
您想在routes.rb文件中创建一条路由,并添加一个映射到该路由并使用示例逻辑的控制器动作。您可以从翻译data = JSON.parse(request.body.read.to_s)
到params
与下面的下面的例子相当预期的方式。
在Rails中,您可以使用现有路线,也可以在route.rb文件中定义一个新路线:
# routes.rb
resources :orders, only: [:create]
然后,在JavaScript中,您可以更新fetch调用中使用的路径,以映射到自己在Rails中定义的路由。因此可能是:
fetch('/orders', // ...
然后,处理请求的逻辑将存在于OrdersController内部。在这种情况下,您可能需要create
采取措施来处理此POST请求:
# OrdersController
def create
begin
if params[:payment_method_id]
# Create the PaymentIntent
intent = Stripe::PaymentIntent.create(
payment_method: params[:payment_method_id],
amount: 1099,
currency: 'usd',
confirmation_method: 'manual',
confirm: true,
)
elsif params[:payment_intent_id]
intent = Stripe::PaymentIntent.confirm(params[:payment_intent_id])
end
rescue Stripe::CardError => e
# Display error on client
render json: { error: e.message }
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
render json: {
requires_action: true,
payment_intent_client_secret: intent.client_secret
}
elsif intent.status == 'succeeded'
# The payment didn’t need any additional actions and is completed!
# Handle post-payment fulfillment
render json: { success: true }
else
# Invalid status
render json: { error: 'Invalid PaymentIntent status' }, 500, .to_json]
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句