The next component for this checkout logic is the API endpoint that will be handling the request sent by the StripeButton
.
Remember that the request is sent to:
javascript
1const response = await fetch(`/api/stripe/${plan}/checkout`, {. . .});
Make sure our API endpoint is designed with the right structure:
text
1src/app/api
2├── auth
3└── stripe
4 ├── cancel
5 ├── monthly
6 │ └── checkout
7 │ └── route.js <===== API handler for monthly subscription checkout
8 ├── onetime
9 ├── resume
10 └── webhook
Creating new stripe instance
There are a few things we need to do in this route handler. First, we need to initialize a new Stripe instance using the STRIPE_SECRET_KEY
we saved into our .env
file before:
api/stripe/subscribe/monthly/route.js
javascript
1import Stripe from "stripe";
2import { auth } from "@/libs/auth";
3
4export async function POST() {
5 // Initialize a new Stripe instance
6 const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
7
8 . . .
9}