mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-17 08:17:26 +02:00
💳 Support payments with Stripe (#822)
This commit is contained in:
parent
969ae35971
commit
6f425edeaa
20 changed files with 712 additions and 229 deletions
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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?: {
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
8
packages/backend/stripe.ts
Normal file
8
packages/backend/stripe.ts
Normal 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,
|
||||
});
|
|
@ -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),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue