繁体   English   中英

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

[英]How to create both a customer and a card in a single action with Stripe?

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

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不喜欢这样

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

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

您似乎遇到了这个问题,因为您不小心尝试重复使用令牌为客户创建新卡,而您不知道,您已经使用该令牌为该用户创建该卡。 使用存储卡创建客户实际上比您预期的要容易得多:当您使用令牌初始化客户对象时,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-laterhttps://stripe.com/上的Stripe文档。 docs / api / node #create_customer

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM