💳 Support payments with Stripe (#822)

This commit is contained in:
Luke Vella 2023-08-23 15:29:40 +01:00 committed by GitHub
parent 969ae35971
commit 6f425edeaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 712 additions and 229 deletions

View file

@ -1,8 +1,11 @@
import type { IncomingMessage, ServerResponse } from "http";
import { getIronSession } from "iron-session/edge";
import { NextRequest, NextResponse } from "next/server";
import { sessionConfig } from "../session-config";
export const getSession = async (req: NextRequest, res: NextResponse) => {
export const getSession = async (
req: Request | IncomingMessage,
res: Response | ServerResponse,
) => {
return getIronSession(req, res, sessionConfig);
};

View file

@ -1,3 +1,5 @@
import type { IncomingMessage, ServerResponse } from "http";
import { getIronSession } from "iron-session";
import { withIronSessionApiRoute, withIronSessionSsr } from "iron-session/next";
import {
GetServerSideProps,
@ -13,6 +15,13 @@ export function withSessionRoute(handler: NextApiHandler) {
return withIronSessionApiRoute(handler, sessionConfig);
}
export const getSession = async (
req: Request | IncomingMessage,
res: Response | ServerResponse,
) => {
return getIronSession(req, res, sessionConfig);
};
export function withSessionSsr(
handler: GetServerSideProps | GetServerSideProps[],
options?: {

View file

@ -19,6 +19,7 @@
"@trpc/server": "^10.13.0",
"iron-session": "^6.3.1",
"spacetime": "^7.4.4",
"stripe": "^13.2.0",
"superjson": "^1.12.2",
"timezone-soft": "^1.4.1"
}

View file

@ -0,0 +1,8 @@
import Stripe from "stripe";
export type { Stripe } from "stripe";
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
apiVersion: "2023-08-16",
typescript: true,
});

View file

@ -1,10 +1,10 @@
import { prisma } from "@rallly/database";
import { z } from "zod";
import { publicProcedure, router } from "../trpc";
import { privateProcedure, router } from "../trpc";
export const user = router({
getBilling: publicProcedure.query(async ({ ctx }) => {
getBilling: privateProcedure.query(async ({ ctx }) => {
return await prisma.userPaymentData.findUnique({
select: {
subscriptionId: true,
@ -19,7 +19,50 @@ export const user = router({
},
});
}),
changeName: publicProcedure
subscription: privateProcedure.query(async ({ ctx }) => {
const user = await prisma.user.findUnique({
where: {
id: ctx.user.id,
},
select: {
subscription: {
select: {
active: true,
},
},
},
});
if (user?.subscription?.active === true) {
return {
active: true,
};
}
const userPaymentData = await prisma.userPaymentData.findUnique({
where: {
userId: ctx.user.id,
},
select: {
endDate: true,
},
});
if (
userPaymentData?.endDate &&
userPaymentData.endDate.getTime() > Date.now()
) {
return {
active: true,
legacy: true,
};
}
return {
active: false,
};
}),
changeName: privateProcedure
.input(
z.object({
name: z.string().min(1).max(100),