Can you give me graphql schema for a Checkout, CreateCheckoutInput &…
- User
Can you give me graphql schema for a Checkout, CreateCheckoutInput & createCheckoutSession mutation for working with stripe
- Assistant
Below is a GraphQL schema for a Checkout type, CreateCheckoutInput input type, and createCheckoutSession mutation that can be used for working with Stripe. Keep in mind that you might need to make adjustments based on your specific application requirements.
scalar DateTime type Checkout { id: ID! createdAt: DateTime! total: Float! currency: String! sessionId: String! customerEmail: String lineItems: [LineItem!]! } type LineItem { id: ID! product: Product! quantity: Int! } type Product { id: ID! price: Float! title: String! description: String } input CreateCheckoutInput { items: [CreateCheckoutItemInput!]! customerEmail: String successUrl: String! cancelUrl: String! } input CreateCheckoutItemInput { productId: ID! quantity: Int! } type Mutation { createCheckoutSession(input: CreateCheckoutInput!): Checkout } - User
I have a nestjs backend where I can create checkout sessions, and take payments with stripe and graphql. How can I create a checkout page that communicates with this on a nextjs frontend?
- Assistant
- User
In stripe how would I handle a booking system, for example there are multiple van variations with different price per day and a user can book multiple days and also have additional extras that cost more money. How would I structure that in stripe dashboard so I can easily book it from a custom frontend?
- Assistant
- User
I have this mutation:
@Mutation('createCheckoutSession') async createCheckoutSession( @Args('lineItems') lineItems: any[], ): Promise { console.log({ lineItems }); const session = await this.stripeService.createCheckoutSession(lineItems); return session.id; }
I have this service:
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://vanzzz.co.uk', cancel_url: 'https://vanzzz.co.uk', });
return session;}
And this on the frontend:
const [createCheckoutSession, { data }] = useCreateCheckoutSessionMutation({ variables: { input: { lineItems: [ { productId: "prod_NtOONmaPPwHe5J", quantity: 1, }, ], }, }, });
const handleCheckoutClick = async () => { const { data } = await createCheckoutSession(); const sessionId = data?.createCheckoutSession?.sessionId;
- Assistant
It seems like you're not passing the input object correctly in the mutation. In your current mutation definition, the
createCheckoutSessionmutation expects an argument namedlineItems:@Mutation('createCheckoutSession') async createCheckoutSession( @Args('lineItems') lineItems: any[], ): Promise<string> { // ... }However, in your frontend code, you are passing an
inputobject with alineItemsfield:const [createCheckoutSession, { data }] = useCreateCheckoutSessionMutation({ variables: { input: { lineItems: [ { productId: "prod_NtOONmaPPwHe5J", quantity: 1, }, ], }, }, });To fix the issue, you should update your mutation definition to expect an input object with a
lineItemsfield:// Modify this import if necessary import { CreateCheckoutInput } from './your-path-to-generated-graphql-types'; @Mutation('createCheckoutSession') async createCheckoutSession( @Args('input') input: CreateCheckoutInput, ): Promise<string> { console.log({ lineItems: input.lineItems }); const session = await this.stripeService.createCheckoutSession(input.lineItems); return session.id; } - User
Stripe line_items is saying that that productId does not exist - Received unknown parameter: line_items[0][productId]
- Assistant
The error "Received unknown parameter: line_items[0][productId]" occurs because the
line_itemsobject in the Stripe API does not have aproductIdproperty.You should use the
price_dataproperty and associate it with the product as follows:- Update your
lineItemsin the frontend code to includeprice(amount) as well:
const lineItems = [ { productId: "prod_NtOONmaPPwHe5J", quantity: 1, price: 100, // Amount specific to this product, in the currency's smallest unit (e.g. cents for USD) }, ];- Remove the
productIdfield and useprice_datawith theproductfield in the function:
- Update your
- User
I have this mutation:
@Mutation('createCheckoutSession') async createCheckoutSession( @Args('input') input: CreateCheckoutInput, ): Promise { const session = await this.stripeService.createCheckoutSession( input.lineItems, );
return session.id;}
And this service:
async createCheckoutSession( lineItems: LineItem[], ): Promise<Stripe.Checkout.Session> { const session = await this.stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: lineItems.map((item) => ({ price_data: { currency: 'gbp', product: item.productId, unit_amount: 100, }, quantity: item.quantity, })), mode: 'payment', success_url: 'https://vanzzz.co.uk', cancel_url: '', });
- Assistant
The issue you're encountering is because your GraphQL schema expects the return type of the
createCheckoutSessionmutation to beCheckout, which has asessionIdfield. But in the mutation, you are returning a Stripe session'siddirectly.To fix this issue, you can simply update the return type of the
createCheckoutSessionmutation toStringand adjust the frontend code accordingly.- Update your GraphQL schema:
type Mutation { createCheckoutSession(input: CreateCheckoutInput!): String }- Update your frontend code:
const handleCheckoutClick = async () => { const { data } = await createCheckoutSession(); const sessionId = data?.createCheckoutSession; console.log({ sessionId }); const stripe = await stripePromise; if (sessionId && stripe) { const { error } = await stripe.redirectToCheckout({ sessionId, }); if (error) { console.log(error); } } };These changes will make the
createCheckoutSessionmutation return the Stripe session ID as a string directly, and the frontend code will correctly use that ID to redirect to the checkout. - User
How can I create a product with multiple prices using stripe api?
- Assistant