mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-07 05:01:49 +02:00
♻️ Migrate checkout to app directory
This commit is contained in:
parent
c2e1c289eb
commit
2a30aab3b8
1 changed files with 25 additions and 29 deletions
|
@ -1,38 +1,36 @@
|
||||||
import { getProPricing, stripe } from "@rallly/billing";
|
import { getProPricing, stripe } from "@rallly/billing";
|
||||||
import { prisma } from "@rallly/database";
|
import { prisma } from "@rallly/database";
|
||||||
import { absoluteUrl } from "@rallly/utils/absolute-url";
|
import { absoluteUrl } from "@rallly/utils/absolute-url";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextRequest } from "next/server";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { getServerSession } from "@/auth";
|
import { getServerSession } from "@/auth";
|
||||||
|
|
||||||
export const config = {
|
|
||||||
edge: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const inputSchema = z.object({
|
const inputSchema = z.object({
|
||||||
period: z.enum(["monthly", "yearly"]).optional(),
|
period: z.enum(["monthly", "yearly"]).optional(),
|
||||||
success_path: z.string().optional(),
|
success_path: z.string().optional(),
|
||||||
return_path: z.string().optional(),
|
return_path: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default async function handler(
|
export async function POST(request: NextRequest) {
|
||||||
req: NextApiRequest,
|
const userSession = await getServerSession();
|
||||||
res: NextApiResponse,
|
const formData = await request.formData();
|
||||||
) {
|
const { period = "monthly", return_path } = inputSchema.parse(
|
||||||
const userSession = await getServerSession(req, res);
|
Object.fromEntries(formData.entries()),
|
||||||
const { period = "monthly", return_path } = inputSchema.parse(req.body);
|
);
|
||||||
|
|
||||||
if (!userSession || userSession.user.email === null) {
|
if (!userSession || userSession.user.email === null) {
|
||||||
// You need to be logged in to subscribe
|
// You need to be logged in to subscribe
|
||||||
res.redirect(
|
return NextResponse.redirect(
|
||||||
|
new URL(
|
||||||
|
`/login${
|
||||||
|
return_path ? `?redirect=${encodeURIComponent(return_path)}` : ""
|
||||||
|
}`,
|
||||||
|
request.url,
|
||||||
|
),
|
||||||
303,
|
303,
|
||||||
`/login${
|
|
||||||
return_path ? `?redirect=${encodeURIComponent(return_path)}` : ""
|
|
||||||
}`,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
|
@ -51,14 +49,15 @@ export default async function handler(
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
res.status(404).end();
|
return new NextResponse(null, { status: 404 });
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.subscription?.active === true) {
|
if (user.subscription?.active === true) {
|
||||||
// User already has an active subscription. Take them to customer portal
|
// User already has an active subscription. Take them to customer portal
|
||||||
res.redirect(303, "/api/stripe/portal");
|
return NextResponse.redirect(
|
||||||
return;
|
new URL("/api/stripe/portal", request.url),
|
||||||
|
303,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const proPricingData = await getProPricing();
|
const proPricingData = await getProPricing();
|
||||||
|
@ -70,15 +69,12 @@ export default async function handler(
|
||||||
cancel_url: absoluteUrl(return_path),
|
cancel_url: absoluteUrl(return_path),
|
||||||
...(user.customerId
|
...(user.customerId
|
||||||
? {
|
? {
|
||||||
// use existing customer if available to reuse payment details
|
|
||||||
customer: user.customerId,
|
customer: user.customerId,
|
||||||
customer_update: {
|
customer_update: {
|
||||||
// needed for tax id collection
|
|
||||||
name: "auto",
|
name: "auto",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
// supply email if user is not a customer yet
|
|
||||||
customer_email: user.email,
|
customer_email: user.email,
|
||||||
}),
|
}),
|
||||||
mode: "subscription",
|
mode: "subscription",
|
||||||
|
@ -111,11 +107,11 @@ export default async function handler(
|
||||||
|
|
||||||
if (session.url) {
|
if (session.url) {
|
||||||
// redirect to checkout session
|
// redirect to checkout session
|
||||||
res.redirect(303, session.url);
|
return NextResponse.redirect(new URL(session.url), 303);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
return NextResponse.json(
|
||||||
.status(500)
|
{ error: "Something went wrong while creating a checkout session" },
|
||||||
.json({ error: "Something went wrong while creating a checkout session" });
|
{ status: 500 },
|
||||||
|
);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue