...

/

Processing Payments Using Stripe

Processing Payments Using Stripe

Learn to integrate stripe payment in the e-commerce store.

Integrating stripe with Next.js

Stripe is one of the best financial services out there; it’s straightforward to use and offers excellent documentation to understand how to integrate their APIs.

Before continuing with this lesson, make sure to open an account at Stripe. Once we have an account, we can log in and go to Dashboard, where we’ll retrieve the following information: the publishable key and secret key. We’ll need to store them inside of two environment variables, following this naming convention:

NEXT_PUBLIC_STRIPE_SHARABLE_KEY=
STRIPE_SECRET_KEY=

Note: Please double-check that you’re not exposing the STRIPE_SECRET_KEY variable and that the .env.local file is not added to the Git history by including it in the .gitignore file.

We create a new file named lib/stripe/index.js with the following script:

Press + to interact
import { loadStripe } from '@stripe/stripe-js';
const key = process.env.NEXT_PUBLIC_STRIPE_SHARABLE_KEY;
let stripePromise;
const getStripe = () => {
if (!stripePromise) {
stripePromise = loadStripe(key);
}
return stripePromise;
};
export default getStripe;

This script will ...