Stripe Payments in Node JS Using Express

In this article, we are using Stripe Charge API to create a customer in stripe and make a payment.

First, we need to create an express application. Here is the process for creating an express app.

We need to install a stripe in our application to use Stripe Payment, You can install it using the below command.

npm install --save stripe

Now need to add a secret key and publishable key to your environment file to access it from any place. (Note: You can get it from stripe dashboard: https://i.imgur.com/y1RGZ1x.png).

Now add the below code to add the payment form in your page.

<div class="container">
            <h2>Welcome to Payment Gateway</h2>
            <form action="payment" method="POST">
                <script
                src="//checkout.stripe.com/v2/checkout.js"
                class="stripe-button"    
                data-key="<%= key %>"
                data-amount="1000"
                data-currency="usd"
                data-name="Test Test"
                data-description="Test Purchase using express and Node"
                data-Locale="auto" >

                </script>
            </form>

Note: In the above code, I have added a publishable key, which is I have rendered in view request from js side using below code. Also, there are many other values which is you need to set dynamically. Currently, I have set it static for the demo.

res.render('dashboard/index', {
       key: process.env.STRIPE_PUBLISHABLE_KEY
   });

In the above code, I have imported PUBLISHABLE_KEY from the environment config file.

Now you need to add the stripe payment function in your controller.

First of all, let’s add and configure your stripe in the application controller using the below code.

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

Now need to create a payment method that will call on submit of a payment request from the front-end ( we have set it in the script tag which is I provided above. You need to change the function name if). just add the below code to your controller.

async function payment(req, res) {
    try {
        
        console.log(req.body);
        const customer = stripe.customers.create({
            email: req.body.stripeEmail,
            source: req.body.stripeToken,
            name: "Test Test",

        })
        .then(customer => {
            console.log(customer);
            return stripe.charges.create({
                amount:1000,
                description: "Test Purchase using express and Node",
                currency: "USD",
                customer: customer.id
            })
            
        })
        .then(charge => {
            console.log(charge);
            res.send("Payment Success!!!");
        })
        .catch(err=> {
            res.send(err);
        })

        return true;

    }
    catch (error) {
        return false;
    }
}

Yeah, and one more thing don’t forget to add payment method in the export module.

That’s it, Now it’s ready to use. just run the project to see stripe payment integration in action.

OUTPUT

Thanks for view my blog.

You can check out my other blogs from here.

Submit a Comment

Your email address will not be published. Required fields are marked *

Footer Logo

Subscribe

Select Categories