Skip to content

Commit

Permalink
fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Nov 27, 2024
1 parent 8ea2403 commit 5098a6d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 13 additions & 0 deletions web/app/api/webhook/handlers/checkout-complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CustomerData } from "../types";
import { updateClerkMetadata } from "@/lib/services/clerk";
import { trackLoopsEvent } from "@/lib/services/loops";
import Stripe from "stripe";
import { updateAnonymousUserEmail } from "../../anon";

function createCustomerDataFromSession(
session: Stripe.Checkout.Session
Expand All @@ -26,6 +27,18 @@ export const handleCheckoutComplete = createWebhookHandler(
async (event) => {
const session = event.data.object as Stripe.Checkout.Session;
const customerData = createCustomerDataFromSession(session);
// if it's a top up we check if the user is anonymous and update their email
if (customerData.product === "top_up") {
const wasAnonymous = await updateAnonymousUserEmail(
session.metadata?.userId || "",
session.customer_details?.email || ""
);
if (wasAnonymous) {
console.log(
`Updated email for user ${session.metadata?.userId} to ${session.customer_details?.email}`
);
}
}

await updateClerkMetadata(customerData);
await trackLoopsEvent({
Expand Down
1 change: 0 additions & 1 deletion web/app/api/webhook/handlers/payment-intent-succeeded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { eq, sql } from "drizzle-orm";
import { updateUserSubscriptionData } from "../utils";
import Stripe from "stripe";
import { trackLoopsEvent } from '@/lib/services/loops';
import { updateAnonymousUserEmail } from '../../anon';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2022-11-15",
Expand Down

0 comments on commit 5098a6d

Please sign in to comment.