如何使用Stripe在单个操作中创建客户和卡? [英] How to create both a customer and a card in a single action with Stripe?

查看:279
本文介绍了如何使用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
    })
  }
});

似乎Stripe API不喜欢这个

It would seem that the Stripe API doesn't like this


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

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

是有一种规范的方法可以在服务器上为一个令牌制作多个条带请求吗?

Is there a canonical way to make multiple requests to stripe on the server for a single token?

推荐答案

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

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. 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 
    });
});

有关详细信息,我建议使用 https://support.stripe.com/questions/can-i-save- a-card-and-charge-it-later https://stripe.com / docs / api / node #create_customer

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.

请告诉我这是否解决了您的问题!

Let me know if that solves your problem!

这篇关于如何使用Stripe在单个操作中创建客户和卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆