Javascript 使用Ruby on Rails后端创建条带令牌&;Reactjs前端
正在尝试创建条带标记 这是我的前端抓取Javascript 使用Ruby on Rails后端创建条带令牌&;Reactjs前端,javascript,ruby-on-rails,stripe-payments,Javascript,Ruby On Rails,Stripe Payments,正在尝试创建条带标记 这是我的前端抓取 const response = fetch('api/v1/charges', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(paymentData), }); 然后stripe给出了在服务器端创建令牌的示例代码。哪里是放置这个的理想地点?控制器,模型,初始化器 Str
const response = fetch('api/v1/charges', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(paymentData),
});
然后stripe给出了在服务器端创建令牌的示例代码。哪里是放置这个的理想地点?控制器,模型,初始化器
Stripe.api_key = 'sk_test_3Sq3Q'
token = params[:stripeToken]
charge = Stripe::Charge.create({
amount: 999,
currency: 'usd',
description: 'Example charge',
source: token,
})
很明显,我对这一点还不熟悉,但如果您能给我一些建议,我将不胜感激 我会将API密钥
Stripe.API_key='sk_test_3Sq3Q'
添加到一个初始值设定项中,以考虑应用程序代码的结构,并将其与一个配置文件相结合
第二部分是接收请求参数并创建一个新对象Stripe::Charge
。这将在控制器中
另一种方法是将与Stripe相关的逻辑封装在一个小型Stripe客户机类中。此类可以具有处理与条带API的连接的方法
例如:
class StripeClient
def create_charge(options)
# Here can be handled different exceptions as
# what to return in case of a failure?
Stripe::Charge.create({
amount: options[:amount],
currency: options[currency],
description: options[:description],
source: options[:token],
})
end
end
然后从控制器使用StripeClient
token = params[:stripeToken]
options = {
amount: 999,
currency: 'usd',
description: 'Example charge',
source: token
}
StripeClient.new.create_charge(options)
根据我的经验,我发现在特定的类或模块中调用第三方API更干净
希望这对你有帮助