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
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.
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
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);
});
</your_secret_key>
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.
Output - Integrate stripe payment gateway - Clue Mediator
Thatās it for today.
Thank you for reading. Happy Coding..!!