How to create both a client and a map in one action using Stripe?

I am trying to initialize the client for the first time. I have a form where they sign up and that's it, and they submit it. The following happens on the client:

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

On the server, I'm 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 is not like

Raw Failure Error: You cannot use the Stripe marker more than once

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

+6
source share
1 answer

It seems that you are faced with this problem because you accidentally try to reuse a token to create a new card for a client, when, without knowing it, you have already used this token to create this card for this user. Creating a client with a saved map is actually much easier than you expect: when you initialize a client object with a marker, the Stripe API goes ahead and saves this map with a new client. That is, you can immediately start and hold the client responsible for creating, 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 would recommend 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 this solves your problem!

+7
source

Source: https://habr.com/ru/post/987523/


All Articles