Working With Stripe And Node / Express To Charge Existing Customer With No Card
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>
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
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error