This commit is contained in:
Omer Sabic 2024-07-21 15:37:23 +02:00
parent 8c26d78afb
commit eed90d8bc0
5 changed files with 24 additions and 17 deletions

View File

@ -12,3 +12,7 @@ GOOGLE_SECRET=
OPENAI_TOKEN=
CERTS_URL=
STRIPE_KEY=
STRIPE_WEBHOOK_SECRET=
STRIPE_SUBSCRIPTION_PRICE_ID=

View File

@ -13,7 +13,7 @@ export const users = pgTable("users", {
channel_id: text("channel_id"),
uploads_playlist_id: text("uploads_playlist_id"),
subscription_tier: subscription_enum("subscription_tier").default("free").notNull(),
subscribed_until: date("subscribed_until").default(new Date(0)),
subscribed_until: date("subscribed_until").default(`1970-01-01`),
tokens_claimed: date("tokens_claimed"),
trial_used: boolean("trial_used").default(false),
tokens: integer("tokens").default(200).notNull()

View File

@ -65,10 +65,11 @@ export const meRoutes = (fastify, _, done) => {
const [user] = await db.select().from(users).where(eq(users.id, req.session.user_id));
if (!user) throw new Error("user not found");
if(new Date(user.subscribed_until) > new Date()) return reply.redirect(303, "/me/billing");
const checkout_url = await createCheckout("price_1PVrA2FrdGTeTMwd5ZDjQKDZ", user.stripe_id, {
console.log(new Date(user.subscribed_until).getTime())
const checkout_url = await createCheckout(env.STRIPE_SUBSCRIPTION_PRICE_ID, user.stripe_id, {
user_id: user.id
}, {
trial_end: new Date(Date.now() + 15 * 24 * 60 * 60 * 1000)
...(!user.trial_used ? {trial_end: new Date(Date.now() + 15 * 24 * 60 * 60 * 1000)} : {})
});
reply.redirect(303, checkout_url);

View File

@ -12,7 +12,10 @@ const envSchema = z.object({
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_SECRET: z.string(),
OPENAI_TOKEN: z.string(),
CERTS_URL: z.string().nullable().default(null)
CERTS_URL: z.string().nullable().default(null),
STRIPE_KEY: z.string(),
STRIPE_WEBHOOK_SECRET: z.string(),
STRIPE_SUBSCRIPTION_PRICE_ID: z.string(),
});
export const env = envSchema.parse(process.env);

View File

@ -4,7 +4,7 @@ import { db } from '../db';
import { users } from '../db/schemas';
import { eq, sql } from 'drizzle-orm';
const stripe = await loadStripe('sk_test_51MRcs0FrdGTeTMwdCgd6Z3I1913esqFD2W171b1W4PnRsdxfOCDrwtawiKFgS7R2ZDkWTSqfxp5Gl1GTd8aospWv00vFXvO6iC');
const stripe = await loadStripe(env.STRIPE_KEY);
/**
*
@ -55,7 +55,7 @@ export async function handleWebhook(body, signature) {
let data;
let eventType;
// Check if webhook signing is configured.
const webhookSecret = 'whsec_58bf2c639c4fbe412fa9338e1de48a7be52748eb36fc912af92d535b4897487a';
const webhookSecret = env.STRIPE_WEBHOOK_SECRET;
if (webhookSecret) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event;
@ -81,27 +81,26 @@ export async function handleWebhook(body, signature) {
}
switch (eventType) {
case 'checkout.session.completed':
console.log("new customer!")
break;
case 'invoice.paid':
console.log("invoice paid!")
const [user] = await db.select().from(users).where(eq(users.stripe_id, data.object.customer));
console.log(data);
if (!user) throw new Error("Failed to get user ID");
let currentDate = new Date();
let nextMonthDate = new Date(currentDate);
nextMonthDate.setMonth(currentDate.getMonth() + 1);
let subscriptionEndDate = new Date(currentDate);
if(user.trial_used) {
subscriptionEndDate.setMonth(currentDate.getMonth() + 1);
} else {
subscriptionEndDate.setDate(currentDate.getDate() + 14);
}
await db.update(users).set({
subscribed_until: nextMonthDate,
subscribed_until: subscriptionEndDate,
subscription_tier: "basic",
tokens: sql`${users.tokens} + 30`
tokens: sql`${users.tokens} + 30`,
trial_used: true
}).where(eq(users.stripe_id, data.object.customer));
break;
case 'invoice.payment_failed':
console.log("broke boy!")
break;
default:
break;
}