To create a checkout with Stripe, Next.js, Nest.js, and GraphQL, follow these steps:
- Set up your project with Next.js and Nest.js
First, you'll need to set up a new project with Next.js for the frontend and Nest.js for the backend. Make sure you have node and npm installed on your system.
For creating Next.js application:
npx create-next-app your-frontend-app
cd your-frontend-app
For creating your Nest.js application:
npm install -g @nestjs/cli
nest new your-backend-app
cd your-backend-app
- Set up Stripe in your Nest.js backend
Install the Stripe npm package:
npm install --save stripe
Sign up for a Stripe account (if you don't have one) and obtain your API key from the dashboard.
Create a Stripe service src/stripe/stripe.service.ts:
import { Injectable } from '@nestjs/common';
import Stripe from 'stripe';
@Injectable()
export class StripeService {
private stripe: Stripe;
constructor() {
this.stripe = new Stripe('your_stripe_secret_key', {
apiVersion: '2020-08-27',
});
}
async createCheckoutSession(
lineItems: Stripe.Checkout.SessionCreateParams.LineItem[],
): Promise<Stripe.Checkout.Session> {
const session = await this.stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: lineItems,
mode: 'payment',
success_url: 'https://your-success-url',
cancel_url: 'https://your-cancel-url',
});
return session;
}
}
Update your app.module.ts
import { StripeModule } from './stripe/stripe.service';
@Module({
imports: [
// ...
StripeModule,
],
// ...
})
export class AppModule {}
- Set up GraphQL and define a mutation to create a checkout
Install the necessary GraphQL packages in your Nest.js backend:
npm install --save @nestjs/graphql graphql-tools graphql apollo-server-express
Update your app.module.ts to import the GraphQL module:
import { GraphQLModule } from '@nestjs/graphql';
@Module({
imports: [
// ...
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
}),
],
// ...
})
export class AppModule {}
Create a checkout resolver src/checkout/checkout.resolver.ts:
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { StripeService } from '../stripe/stripe.service';
@Resolver('Checkout')
export class CheckoutResolver {
constructor(private readonly stripeService: StripeService) {}
@Mutation('createCheckoutSession')
async createCheckoutSession(
@Args('lineItems') lineItems: any[],
): Promise<string> {
const session = await this.stripeService.createCheckoutSession(lineItems);
return session.id;
}
}
Update your app.module.ts to include the resolver:
import { CheckoutResolver } from './checkout/checkout.resolver';
@Module({
imports: [
// ...
],
providers: [CheckoutResolver, StripeService],
})
export class AppModule {}
- Make a request from your Next.js frontend to create a checkout
Install the necessary packages:
npm install --save @apollo/client graphql
Add a call to the createCheckoutSession mutation in your Next.js frontend using the Apollo Client:
import { gql, useMutation } from '@apollo/client';
const CREATE_CHECKOUT_SESSION = gql`
mutation CreateCheckoutSession($lineItems: [LineItemInput]) {
createCheckoutSession(lineItems: $lineItems)
}
`;
function CheckoutButton() {
const [createCheckoutSession] = useMutation(CREATE_CHECKOUT_SESSION);
const handleCheckout = async () => {
const { data } = await createCheckoutSession({
variables: {
lineItems: [
{
name: 'Product Name',
description: 'Product Description',
amount: 500, // Amount in cents
currency: 'usd',
quantity: 1,
},
],
},
});
const sessionId = data.createCheckoutSession;
// Redirect to Stripe Checkout
const stripe = Stripe('your_stripe_publishable_key');
stripe.redirectToCheckout({ sessionId });
};
return <button onClick={handleCheckout}>Checkout</button>;
}
With these steps, you should now have a working setup using Stripe, Next.js, Nest.js, and GraphQL for creating a checkout. Adjust these examples to fit your specific use case and styling preferences.