admin管理员组

文章数量:1271801

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

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?

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

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?

Share Improve this question asked May 19, 2015 at 19:38 corvidcorvid 11.2k12 gold badges70 silver badges134 bronze badges 1
  • It looks like you are passing the entire token dictionary, when in this manner I think you should only pass the tokenID when creating the customer. – hybrdthry911 Commented May 20, 2015 at 18:06
Add a ment  | 

1 Answer 1

Reset to default 11

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

For more information, I'd remend the Stripe docs at https://support.stripe./questions/can-i-save-a-card-and-charge-it-later and https://stripe./docs/api/node#create_customer.

Let me know if that solves your problem!

本文标签: javascriptHow to create both a customer and a card in a single action with StripeStack Overflow