Ad

Working With Stripe And Node / Express To Charge Existing Customer With No Card

- 1 answer

I have customers created at Stripe with no card from an external integration.

So the customer object exists, but it doesn't have a card.

I'd like the customer to be able to go to a payment page and make a payment through the checkout. However, the customer ID is already known. So we're just asking the customer to enter their 'reference' and then enter a card and pay.

I have this code:

app.post('/charge', (req, res) => {

  stripe.charges.create({
    amount: 4000,
    description: 'Sample Charge',
    currency: 'gbp',
    customer: req.body.stripeId
  },function(err,result){

    console.log(err);

    res.render('charge'});
  });
});

However this returns the error:

Error: Cannot charge a customer that has no active card

I thought the whole point of the checkout was that it created a card for the customer.

How do I store the card that is entered through the checkout against the specified customer and the charge it?

The checkout code is:

 <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_Some_key"
    data-email="customer email"
    data-billing-address="true"
    data-allow-remember-me="false"
    data-name="Company Limited"
    data-description="Example charge"
    data-image="an-image.jpg"
    data-locale="auto"
    data-zip-code="true"
    data-currency="gbp">
  </script>
Ad

Answer

I thought the whole point of the checkout was that it created a card for the customer.

It creates a card, but you still need to attach it to the customer yourself:

stripe.customers.update(req.body.stripeId, { // I assume this is the customer ID (`cus_xxx`)
  source: req.body.stripeToken // `tok_xxx` generated by Stripe Checkout
}, function(err, customer) {
  stripe.charges.create({
    amount: 4000,
    description: 'Sample Charge',
    currency: 'gbp',
    customer: customer.id
  }) ... 
});

https://stripe.com/docs/saving-cards + https://stripe.com/docs/api/customers/update?lang=node#update_customer-source

Ad
source: stackoverflow.com
Ad