mirror of
https://github.com/fosrl/pangolin.git
synced 2026-04-30 09:41:51 +00:00
Add trial system
This commit is contained in:
@@ -12,9 +12,10 @@
|
||||
*/
|
||||
|
||||
import Stripe from "stripe";
|
||||
import { customers, db } from "@server/db";
|
||||
import { customers, db, subscriptions } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
import { generateId } from "@server/auth/sessions/app";
|
||||
|
||||
export async function handleCustomerCreated(
|
||||
customer: Stripe.Customer
|
||||
@@ -38,14 +39,31 @@ export async function handleCustomerCreated(
|
||||
return;
|
||||
}
|
||||
|
||||
await db.insert(customers).values({
|
||||
customerId: customer.id,
|
||||
orgId: customer.metadata.orgId,
|
||||
email: customer.email || null,
|
||||
name: customer.name || null,
|
||||
createdAt: customer.created,
|
||||
updatedAt: customer.created
|
||||
await db.transaction(async (trx) => {
|
||||
await trx.insert(customers).values({
|
||||
customerId: customer.id,
|
||||
orgId: customer.metadata.orgId,
|
||||
email: customer.email || null,
|
||||
name: customer.name || null,
|
||||
createdAt: customer.created,
|
||||
updatedAt: customer.created
|
||||
});
|
||||
|
||||
// Insert a 14-day trial subscription at tier3
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const trialExpiresAt = now + 10 * 24 * 60 * 60;
|
||||
const subscriptionId = `trial-${generateId(15)}`;
|
||||
await trx.insert(subscriptions).values({
|
||||
subscriptionId,
|
||||
customerId: customer.id,
|
||||
status: "active",
|
||||
type: "tier3",
|
||||
createdAt: now,
|
||||
expiresAt: trialExpiresAt,
|
||||
trial: true
|
||||
});
|
||||
});
|
||||
|
||||
logger.info(`Customer with ID ${customer.id} created successfully.`);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
|
||||
Reference in New Issue
Block a user