updates
This commit is contained in:
parent
8c26d78afb
commit
eed90d8bc0
@ -12,3 +12,7 @@ GOOGLE_SECRET=
|
|||||||
OPENAI_TOKEN=
|
OPENAI_TOKEN=
|
||||||
|
|
||||||
CERTS_URL=
|
CERTS_URL=
|
||||||
|
|
||||||
|
STRIPE_KEY=
|
||||||
|
STRIPE_WEBHOOK_SECRET=
|
||||||
|
STRIPE_SUBSCRIPTION_PRICE_ID=
|
@ -13,7 +13,7 @@ export const users = pgTable("users", {
|
|||||||
channel_id: text("channel_id"),
|
channel_id: text("channel_id"),
|
||||||
uploads_playlist_id: text("uploads_playlist_id"),
|
uploads_playlist_id: text("uploads_playlist_id"),
|
||||||
subscription_tier: subscription_enum("subscription_tier").default("free").notNull(),
|
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"),
|
tokens_claimed: date("tokens_claimed"),
|
||||||
trial_used: boolean("trial_used").default(false),
|
trial_used: boolean("trial_used").default(false),
|
||||||
tokens: integer("tokens").default(200).notNull()
|
tokens: integer("tokens").default(200).notNull()
|
||||||
|
@ -65,10 +65,11 @@ export const meRoutes = (fastify, _, done) => {
|
|||||||
const [user] = await db.select().from(users).where(eq(users.id, req.session.user_id));
|
const [user] = await db.select().from(users).where(eq(users.id, req.session.user_id));
|
||||||
if (!user) throw new Error("user not found");
|
if (!user) throw new Error("user not found");
|
||||||
if(new Date(user.subscribed_until) > new Date()) return reply.redirect(303, "/me/billing");
|
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
|
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);
|
reply.redirect(303, checkout_url);
|
||||||
|
@ -12,7 +12,10 @@ const envSchema = z.object({
|
|||||||
GOOGLE_CLIENT_ID: z.string(),
|
GOOGLE_CLIENT_ID: z.string(),
|
||||||
GOOGLE_SECRET: z.string(),
|
GOOGLE_SECRET: z.string(),
|
||||||
OPENAI_TOKEN: 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);
|
export const env = envSchema.parse(process.env);
|
||||||
|
@ -4,7 +4,7 @@ import { db } from '../db';
|
|||||||
import { users } from '../db/schemas';
|
import { users } from '../db/schemas';
|
||||||
import { eq, sql } from 'drizzle-orm';
|
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 data;
|
||||||
let eventType;
|
let eventType;
|
||||||
// Check if webhook signing is configured.
|
// Check if webhook signing is configured.
|
||||||
const webhookSecret = 'whsec_58bf2c639c4fbe412fa9338e1de48a7be52748eb36fc912af92d535b4897487a';
|
const webhookSecret = env.STRIPE_WEBHOOK_SECRET;
|
||||||
if (webhookSecret) {
|
if (webhookSecret) {
|
||||||
// Retrieve the event by verifying the signature using the raw body and secret.
|
// Retrieve the event by verifying the signature using the raw body and secret.
|
||||||
let event;
|
let event;
|
||||||
@ -81,27 +81,26 @@ export async function handleWebhook(body, signature) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
case 'checkout.session.completed':
|
|
||||||
console.log("new customer!")
|
|
||||||
break;
|
|
||||||
case 'invoice.paid':
|
case 'invoice.paid':
|
||||||
console.log("invoice paid!")
|
console.log("invoice paid!")
|
||||||
const [user] = await db.select().from(users).where(eq(users.stripe_id, data.object.customer));
|
const [user] = await db.select().from(users).where(eq(users.stripe_id, data.object.customer));
|
||||||
console.log(data);
|
console.log(data);
|
||||||
if (!user) throw new Error("Failed to get user ID");
|
if (!user) throw new Error("Failed to get user ID");
|
||||||
let currentDate = new Date();
|
let currentDate = new Date();
|
||||||
let nextMonthDate = new Date(currentDate);
|
let subscriptionEndDate = new Date(currentDate);
|
||||||
nextMonthDate.setMonth(currentDate.getMonth() + 1);
|
if(user.trial_used) {
|
||||||
|
subscriptionEndDate.setMonth(currentDate.getMonth() + 1);
|
||||||
|
} else {
|
||||||
|
subscriptionEndDate.setDate(currentDate.getDate() + 14);
|
||||||
|
}
|
||||||
|
|
||||||
await db.update(users).set({
|
await db.update(users).set({
|
||||||
subscribed_until: nextMonthDate,
|
subscribed_until: subscriptionEndDate,
|
||||||
subscription_tier: "basic",
|
subscription_tier: "basic",
|
||||||
tokens: sql`${users.tokens} + 30`
|
tokens: sql`${users.tokens} + 30`,
|
||||||
|
trial_used: true
|
||||||
}).where(eq(users.stripe_id, data.object.customer));
|
}).where(eq(users.stripe_id, data.object.customer));
|
||||||
break;
|
break;
|
||||||
case 'invoice.payment_failed':
|
|
||||||
console.log("broke boy!")
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user