File size: 2,223 Bytes
6076169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Stripe from "stripe";
import getDbConnection from "./db";

export async function handleSubscriptionDeleted({
	subscriptionId,
	stripe,
}: {
	subscriptionId: string;
	stripe: Stripe;
}) {
	try {
		const subscription = await stripe.subscriptions.retrieve(subscriptionId);
		const sql = await getDbConnection();
		await sql`UPDATE users SET status = 'cancelled' WHERE customer_id = ${subscription.customer}`;
	} catch (error) {
		console.error("Error handling subscription deletion", error);
		throw error;
	}
}

export async function handleCheckoutSessionCompleted({
	session,
	stripe,
}: {
	session: Stripe.Checkout.Session;
	stripe: Stripe;
}) {
	const customerId = session.customer as string;
	const customer = await stripe.customers.retrieve(customerId);
	const priceId = session.line_items?.data[0].price?.id;

	const sql = await getDbConnection();

	if ("email" in customer && priceId) {
		await createOrUpdateUser(sql, customer, customerId);
		//update user subscription
		await updateUserSubscription(sql, priceId, customer.email as string);
		//insert the payment
		await insertPayment(sql, session, priceId, customer.email as string);
	}
}

async function insertPayment(
	sql: any,
	session: Stripe.Checkout.Session,
	priceId: string,
	customerEmail: string,
) {
	try {
		await sql`INSERT INTO payments (amount, status, stripe_payment_id, price_id, user_email) VALUES (${session.amount_total}, ${session.status}, ${session.id}, ${priceId}, ${customerEmail})`;
	} catch (err) {
		console.error("Error in inserting payment", err);
	}
}

async function createOrUpdateUser(
	sql: any,
	customer: Stripe.Customer,
	customerId: string,
) {
	try {
		const user = await sql`SELECT * FROM users WHERE email = ${customer.email}`;
		if (user.length === 0) {
			await sql`INSERT INTO users (email, full_name, customer_id) VALUES (${customer.email}, ${customer.name}, ${customerId})`;
		}
	} catch (err) {
		console.error("Error in inserting user", err);
	}
}

async function updateUserSubscription(
	sql: any,
	priceId: string,
	email: string,
) {
	try {
		await sql`UPDATE users SET price_id = ${priceId}, status = 'active' where email = ${email}`;
	} catch (err) {
		console.error("Error in updating user", err);
	}
}