簡體   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