[英]How to create both a customer and a card in a single action with Stripe?
I am trying to initialize a customer for the first time. 我想第一次初始化客户。 I have a form where they sign up and everything, and they submit it. 我有一个表格,他们注册和一切,他们提交。 On the client, the following happens: 在客户端上,会发生以下情况:
var cardValues = AutoForm.getFormValues('credit-card-form').insertDoc;
Stripe.createToken(cardValues, function (err, token) {
if (!err && token) {
Meteor.call('Stripe.initializeCustomer', token);
}
});
On the serverside, I am trying to do something like this: 在服务器端,我试图做这样的事情:
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
})
}
});
It would seem that the Stripe API doesn't like this 似乎Stripe API不喜欢这样
Unhandled rejection Error: You cannot use a Stripe token more than once 未处理的拒绝错误:您不能多次使用Stripe令牌
Is there a canonical way to make multiple requests to stripe on the server for a single token? 有没有规范的方法可以在服务器上为单个令牌添加多个条带请求?
It seems that you're running into this issue because you're accidentally trying to reuse a token to create a new card for a customer when, unbeknownst to you, you've already used that token to create that card for that user. 您似乎遇到了这个问题,因为您不小心尝试重复使用令牌为客户创建新卡,而您不知道,您已经使用该令牌为该用户创建该卡。 Creating a customer with a stored card is actually much easier than you expect: when you initialize a customer object with a token, the Stripe API goes ahead and stores that card in association with the new customer. 使用存储卡创建客户实际上比您预期的要容易得多:当您使用令牌初始化客户对象时,Stripe API会继续存储并将该卡与新客户相关联。 That is, you can immediately go ahead and make a charge to your customer upon creation as in: 也就是说,您可以立即继续并在创建时向您的客户收取费用,如下所示:
Stripe.customers.create({
source: token.id
}).then(function (customer) {
Stripe.charge.create({
amount: 1000,
currency: 'usd',
customer: customer.id
});
});
For more information, I'd recommend the Stripe docs at https://support.stripe.com/questions/can-i-save-a-card-and-charge-it-later and https://stripe.com/docs/api/node#create_customer . 有关详细信息,请参阅https://support.stripe.com/questions/can-i-save-a-card-and-charge-it-later和https://stripe.com/上的Stripe文档。 docs / api / node #create_customer 。
Let me know if that solves your problem! 如果能解决您的问题,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.