This repository demonstrates how to handle Stripe payments using a checkout session and webhook events with a React frontend and a Node.js backend. The frontend is written in TypeScript.
- Node.js and npm installed
- Stripe account and Stripe CLI installed
- Create a new Stripe API key from the Stripe Dashboard
-
Initialize the Node.js project:
- Navigate to the project root directory.
- Run
npm init -y
to initialize a new Node.js project.
-
Install necessary packages:
- Run
npm install express stripe body-parser
to install the required packages.
- Run
-
Create the server file:
- Create a
server.js
file in the project root directory. - Implement the server logic to handle Stripe checkout sessions and webhooks.
- Create a
-
Run the server:
- Execute
node server.js
to start the Node.js server.
- Execute
-
Create a new React app with TypeScript template:
- Run
npx create-react-app client --template typescript
to create a new React app.
- Run
-
Install necessary packages:
- Navigate to the
client
directory. - Run
npm install @stripe/stripe-js @stripe/react-stripe-js
to install Stripe's JavaScript libraries.
- Navigate to the
-
Implement the checkout logic:
- Modify
src/App.tsx
to include the checkout button and Stripe integration.
- Modify
-
Start the React app:
- In the
client
directory, runnpm start
to start the React development server.
- In the
-
Login to Stripe CLI:
- Run
stripe login
to authenticate the Stripe CLI.
- Run
-
Listen for webhook events:
- Run
stripe listen --forward-to localhost:4242/webhook
to forward webhook events to your local server.
- Run
-
Start the backend server:
- Ensure you are in the root directory of the project.
- Run
node server.js
.
-
Start the frontend server:
- Navigate to the
client
directory. - Run
npm start
.
- Navigate to the
-
Testing the Setup:
- Open your browser and go to
http://localhost:3000
. - Click on the Checkout button.
- Complete the payment using Stripe's test card details.
- Verify the success message and check the server logs for webhook event processing.
- Open your browser and go to
- Replace
YOUR_STRIPE_SECRET_KEY
,YOUR_STRIPE_PUBLIC_KEY
, andYOUR_STRIPE_WEBHOOK_SECRET
with your actual Stripe keys. - Ensure both frontend and backend servers are running concurrently.
- Use Stripe CLI to forward webhook events to your local development environment.