Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
♻️ Refactor checkout complete hook (#2274)
Luke Vellae76b9cab84dc
diff --git a/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts b/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.tsindex 56d5d2ab0..246d0dacd 100644--- a/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts+++ b/apps/web/src/app/api/stripe/webhook/handlers/checkout/completed.ts@@ -5,84 +5,80 @@ import { licenseCheckoutMetadataSchema } from "@/features/licensing/schema";import { licenseManager } from "@/features/licensing/server";import { getEmailClient } from "@/utils/emails";-async function handleSelfHostedCheckoutSessionCompleted(- checkoutSession: Stripe.Checkout.Session,-) {- const { success, data } = licenseCheckoutMetadataSchema.safeParse(- checkoutSession.metadata,- );+export async function onCheckoutSessionCompleted(event: Stripe.Event) {+ const checkoutSession = event.data.object as Stripe.Checkout.Session;- if (!success) {- // If there is no metadata than this is likely a donation from a payment link- console.info("No metadata found for session: ", checkoutSession.id);- return;- }+ if (checkoutSession.subscription === null) {+ const { success, data } = licenseCheckoutMetadataSchema.safeParse(+ checkoutSession.metadata,+ );- const { licenseType, version, seats } = data;+ if (!success) {+ // If there is no metadata than this is likely a donation from a payment link+ console.info("No metadata found for session: ", checkoutSession.id);+ return;+ }- const customerDetails = checkoutSession.customer_details;+ const { licenseType, version, seats } = data;- if (!customerDetails) {- throw new Error(- `No customer details found for session: ${checkoutSession.id}`,- );- }+ const customerDetails = checkoutSession.customer_details;- const { email } = customerDetails;+ if (!customerDetails) {+ throw new Error(+ `No customer details found for session: ${checkoutSession.id}`,+ );+ }- if (!email) {- throw new Error(- `No email found for customer details in session: ${checkoutSession.id}`,- );- }+ const { email } = customerDetails;- const license = await licenseManager.createLicense({- type: licenseType,- licenseeEmail: email,- licenseeName: customerDetails.name ?? undefined,- version,- seats,- });+ if (!email) {+ throw new Error(+ `No email found for customer details in session: ${checkoutSession.id}`,+ );+ }- posthog()?.capture({- distinctId: email,- event: "license_purchase",- properties: {- licenseType,- seats,+ const license = await licenseManager.createLicense({+ type: licenseType,+ licenseeEmail: email,+ licenseeName: customerDetails.name ?? undefined,version,- $set: {- tier: licenseType,- },- },- });-- if (!license || !license.data) {- throw new Error(- `Failed to create license for session: ${checkoutSession.id} - ${license?.error}`,- );- }+ seats,+ });- const emailClient = await getEmailClient();+ if (!license || !license.data) {+ throw new Error(+ `Failed to create license for session: ${checkoutSession.id} - ${license?.error}`,+ );+ }- await emailClient.sendTemplate("LicenseKeyEmail", {- to: email,- from: {- name: "Luke from Rallly",- address: env.SUPPORT_EMAIL,- },- props: {- licenseKey: license.data.key,- seats,- tier: licenseType,- },- });-}+ const emailClient = await getEmailClient();-export async function onCheckoutSessionCompleted(event: Stripe.Event) {- const checkoutSession = event.data.object as Stripe.Checkout.Session;+ await emailClient.sendTemplate("LicenseKeyEmail", {+ to: email,+ from: {+ name: "Luke from Rallly",+ address: env.SUPPORT_EMAIL,+ },+ props: {+ licenseKey: license.data.key,+ seats,+ tier: licenseType,+ },+ });- if (checkoutSession.subscription === null) {- await handleSelfHostedCheckoutSessionCompleted(checkoutSession);+ posthog()?.capture({+ uuid: event.id,+ distinctId: email,+ event: "license_purchase",+ properties: {+ licenseType,+ seats,+ version,+ eventId: event.id,+ $set: {+ tier: licenseType,+ },+ },+ });}}