Confirm a stripe paymentIntent using Node.js
This is the second article where we will confirm a stripe paymentIntent using Node.js. We have integrated the stripe payment gateway in React and created the demo of the checkout form.
So in the previous article, we have created the paymentIntent in React application but now we need to confirm that paymentIntent using the Stripe API in the Node.js. You can use any backend technology to confirm the stripe payment.
Stripe payment gateway integration.
- Part 1 – Integrate stripe payment gateway in React
- Part 2 – Confirm a stripe paymentIntent using Node.js (You are hereā¦)
Confirm a stripe paymentIntent using Node.js
- Setup server using express
- Install stripe dependency
- Collect Secret API key
- Create REST API to confirm paymentIntent
- Output
1. Setup server using express
Here, we will use the express framework in Node.js to create a REST API. Kindly follow the link below if you donāt know how to create REST API in Node.js using express framework.
Now, letās enable the CORS in the Node app. Follow the steps to Enable CORS in Node.js so we can access the REST API endpoint in the frontend application.
After adding packages, your file should look like the following.
server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var express = require('express'); const cors = require('cors'); const bodyParser = require('body-parser'); var app = express(); var port = process.env.PORT || 4000; // enable CORS app.use(cors()); // parse application/json app.use(bodyParser.json()); // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // request handlers app.get('/', (req, res) => { res.send('Stripe Integration! - Clue Mediator'); }); app.listen(port, () => { console.log('Server started on: ' + port); }); |
2. Install stripe dependency
Now, letās install the stripe package in the Node app. Run the following command to install the dependency.
1 | npm i stripe |
3. Collect Secret API key
We need a Secret API key to confirm paymentIntent. Follow the instructions if you donāt know where to get the API key.
4. Create REST API to confirm paymentIntent
Letās create a REST API endpoint to confirm the paymentIntent. We will confirm the PaymentIntent by setting the confirm
property to true
. We need the payment method id and amount from the API request to confirm the paymentIntent.
server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | var express = require('express'); const cors = require('cors'); const bodyParser = require('body-parser'); const stripe = require('stripe')('<YOUR_SECRET_KEY>'); var app = express(); var port = process.env.PORT || 4000; // enable CORS app.use(cors()); // parse application/json app.use(bodyParser.json()); // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // confirm the paymentIntent app.post('/pay', async (request, response) => { try { // Create the PaymentIntent let intent = await stripe.paymentIntents.create({ payment_method: request.body.payment_method_id, description: "Test payment", amount: request.body.amount * 100, currency: 'inr', confirmation_method: 'manual', confirm: true }); // Send the response to the client response.send(generateResponse(intent)); } catch (e) { // Display error on client return response.send({ error: e.message }); } }); const generateResponse = (intent) => { if (intent.status === 'succeeded') { // The payment didnāt need any additional actions and completed! // Handle post-payment fulfillment return { success: true }; } else { // Invalid status return { error: 'Invalid PaymentIntent status' }; } }; // request handlers app.get('/', (req, res) => { res.send('Stripe Integration! - Clue Mediator'); }); app.listen(port, () => { console.log('Server started on: ' + port); }); |
5. Output
Run the Node application by executing the node server.js
command and check the following output that is related to the frontend application.
Thatās it for today.
Thank you for reading. Happy Coding..!!