如何使用Stripe在单个操作中创建客户和卡?

cor*_*vid 6 javascript mongodb node.js stripe-payments

我想第一次初始化客户.我有一个表格,他们注册和一切,他们提交.在客户端上,会发生以下情况:

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

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

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
    })
  }
});
Run Code Online (Sandbox Code Playgroud)

似乎Stripe API不喜欢这样

未处理的拒绝错误:您不能多次使用Stripe令牌

有没有规范的方法可以在服务器上为单个令牌添加多个条带请求?

age*_*ops 10

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

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

有关详细信息,请参阅https://support.stripe.com/questions/can-i-save-a-card-and-charge-it-laterhttps://stripe.com/上的Stripe文档.docs/api/node #create_customer.

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