Skip to content

Commit

Permalink
Log lemon squeezy webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 4, 2025
1 parent f5f8209 commit ca09c9d
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions apps/web/app/api/lemon-squeezy/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const POST = withError(async (request: Request) => {
const payload = await getPayload(request);
const userId = payload.meta.custom_data?.user_id;

console.log("===Lemon event type:", payload.meta.event_name);
logger.info("Lemon Squeezy webhook", {
event: payload.meta.event_name,
userId,
});

// ignored events
if (["subscription_payment_success"].includes(payload.meta.event_name)) {
Expand Down Expand Up @@ -58,9 +61,7 @@ export const POST = withError(async (request: Request) => {
const premiumId = premium?.id;

if (!premiumId) {
console.warn(
`No user found for lemonSqueezyCustomerId ${lemonSqueezyCustomerId}`,
);
logger.warn("No user found", { lemonSqueezyCustomerId });

return NextResponse.json({ ok: true });
}
Expand Down Expand Up @@ -150,6 +151,13 @@ async function subscriptionCreated({
payload: Payload;
userId: string;
}) {
logger.info("Subscription created", {
lemonSqueezyRenewsAt:
payload.data.attributes.renews_at &&
new Date(payload.data.attributes.renews_at),
userId,
});

const { updatedPremium, tier } = await handleSubscriptionCreated(
payload,
userId,
Expand Down Expand Up @@ -194,6 +202,13 @@ async function subscriptionPlanChanged({
payload: Payload;
userId: string;
}) {
logger.info("Subscription plan changed", {
lemonSqueezyRenewsAt:
payload.data.attributes.renews_at &&
new Date(payload.data.attributes.renews_at),
userId,
});

const { updatedPremium, tier } = await handleSubscriptionCreated(
payload,
userId,
Expand Down Expand Up @@ -234,6 +249,14 @@ async function handleSubscriptionCreated(payload: Payload, userId: string) {
if (!payload.data.attributes.first_subscription_item)
throw new Error("No subscription item");

logger.info("Subscription created", {
lemonSqueezyRenewsAt: lemonSqueezyRenewsAt,
lemonSqueezySubscriptionId:
payload.data.attributes.first_subscription_item.subscription_id,
lemonSqueezySubscriptionItemId:
payload.data.attributes.first_subscription_item.id,
});

const tier = getSubscriptionTier({
variantId: payload.data.attributes.variant_id,
});
Expand Down Expand Up @@ -265,6 +288,13 @@ async function lifetimeOrder({
if (!payload.data.attributes.first_order_item)
throw new Error("No order item");

logger.info("Lifetime order", {
lemonSqueezyOrderId: payload.data.attributes.first_order_item.order_id,
lemonSqueezyCustomerId: payload.data.attributes.customer_id,
lemonSqueezyProductId: payload.data.attributes.product_id,
lemonSqueezyVariantId: payload.data.attributes.variant_id,
});

const updatedPremium = await upgradeToPremium({
userId,
tier: PremiumTier.LIFETIME,
Expand Down Expand Up @@ -301,6 +331,11 @@ async function lifetimeSeatOrder({
if (!payload.data.attributes.first_order_item)
throw new Error("No order item");

logger.info("Lifetime seat order", {
quantity: payload.data.attributes.first_order_item.quantity,
premiumId,
});

const updatedPremium = await editEmailAccountsAccess({
premiumId,
count: payload.data.attributes.first_order_item.quantity,
Expand All @@ -327,6 +362,11 @@ async function subscriptionUpdated({
if (!payload.data.attributes.renews_at)
throw new Error("No renews_at provided");

logger.info("Subscription updated", {
lemonSqueezyRenewsAt: new Date(payload.data.attributes.renews_at),
premiumId,
});

const updatedPremium = await extendPremium({
premiumId,
lemonSqueezyRenewsAt: new Date(payload.data.attributes.renews_at),
Expand Down Expand Up @@ -364,6 +404,12 @@ async function subscriptionCancelled({
endsAt: NonNullable<Payload["data"]["attributes"]["ends_at"]>;
variantId: NonNullable<Payload["data"]["attributes"]["variant_id"]>;
}) {
logger.info("Subscription cancelled", {
endsAt: new Date(endsAt),
variantId,
premiumId,
});

const updatedPremium = await cancelPremium({
premiumId,
variantId,
Expand Down Expand Up @@ -397,6 +443,12 @@ async function subscriptionPaymentSuccess({
payload: Payload;
premiumId: string;
}) {
logger.info("Subscription payment success", {
premiumId,
lemonSqueezyId: payload.data.id,
lemonSqueezyType: payload.data.type,
});

if (payload.data.attributes.status !== "paid") {
throw new Error(
`Unexpected status for subscription payment success: ${payload.data.attributes.status}`,
Expand Down

0 comments on commit ca09c9d

Please sign in to comment.