gpt4 book ai didi

javascript - 如何使用 Stripe 在一个操作中同时创建客户和卡片?

转载 作者:IT老高 更新时间:2023-10-28 13:25:50 26 4
gpt4 key购买 nike

我第一次尝试初始化客户。我有一个表格,他们在其中注册和所有内容,然后他们提交。在客户端,会发生以下情况:

var cardValues = AutoForm.getFormValues('credit-card-form').insertDoc;
Stripe.createToken(cardValues, function (err, token) {
if (!err && token) {
Meteor.call('Stripe.initializeCustomer', token);
}
});

在服务器端,我正在尝试做这样的事情:

Meteor.methods({
'Stripe.initializeCustomer': function (token) {
var Stripe = StripeAPI(process.env.STRIPE_KEY);
// some validation here that nobody cares about
Stripe.customers.create({
source: token
}).then(function (customer) {
return Stripe.customers.createCard(customer.id, {
source: token
})
}).catch(function (error) {
// need to do something here
})
}
});

Stripe API 似乎不喜欢这样

Unhandled rejection Error: You cannot use a Stripe token more than once

是否有一种规范的方法可以发出多个请求以在服务器上对单个 token 进行 strip 化?

最佳答案

您似乎遇到了这个问题,因为您不小心尝试重复使用 token 来为客户创建新卡,而您却不知道,您已经使用该 token 为其创建了该卡用户。使用存储卡创建客户实际上比您预期的要容易得多:当您使用 token 初始化客户对象时,Stripe API 会继续存储该卡与新客户相关联。也就是说,您可以在创建后立即向您的客户收费:

Stripe.customers.create({
source: token.id
}).then(function (customer) {
Stripe.charge.create({
amount: 1000,
currency: 'usd',
customer: customer.id
});
});

如需更多信息,我推荐 https://support.stripe.com/questions/can-i-save-a-card-and-charge-it-later 上的 Stripe 文档。和 https://stripe.com/docs/api/node#create_customer .

如果这能解决您的问题,请告诉我!

关于javascript - 如何使用 Stripe 在一个操作中同时创建客户和卡片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334678/

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