From be65e581f36676f4c640355761ccb7bc430ae984 Mon Sep 17 00:00:00 2001 From: Kristian Duda Date: Fri, 28 Jun 2024 14:28:34 +0200 Subject: [PATCH] checkout --- .env.example | 3 ++- app/api/checkout/route.ts | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/api/checkout/route.ts diff --git a/.env.example b/.env.example index 5881494df..7c2b20ab6 100644 --- a/.env.example +++ b/.env.example @@ -3,4 +3,5 @@ TWITTER_CREATOR="@vercel" TWITTER_SITE="https://nextjs.org/commerce" SITE_NAME="Next.js Commerce" -CMS_URL="http://localhost:3000" +APP_URL="http://localhost:3000" +CMS_URL="http://localhost:8000" diff --git a/app/api/checkout/route.ts b/app/api/checkout/route.ts new file mode 100644 index 000000000..3b034fced --- /dev/null +++ b/app/api/checkout/route.ts @@ -0,0 +1,21 @@ +import { ajax } from 'lib/shopify/ajax'; +import { cookies } from 'next/headers'; +import { NextResponse } from 'next/server'; + +export async function GET(): Promise { + const cartId = cookies().get('cartId')?.value; + + if (!cartId) { + return NextResponse.json({ message: 'Cart not found.' }, { status: 404 }); + } + + try { + const session = await ajax('POST', `${process.env.CMS_URL}/api/carts/${cartId}/checkout`, { + url: process.env.APP_URL + }); + + return NextResponse.redirect(session.url, 303); + } catch (err: any) { + return NextResponse.json({ message: err.message }, { status: err.statusCode }); + } +}