Skip to content

Commit 6d2e82a

Browse files
authored
getSubscriptionProductId → getCheckoutSessionProductId (dubinc#3693)
1 parent 406db44 commit 6d2e82a

3 files changed

Lines changed: 71 additions & 32 deletions

File tree

apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { waitUntil } from "@vercel/functions";
2929
import type Stripe from "stripe";
3030
import { getConnectedCustomer } from "./utils/get-connected-customer";
3131
import { getPromotionCode } from "./utils/get-promotion-code";
32-
import { getSubscriptionProductId } from "./utils/get-subscription-product-id";
32+
import { getCheckoutSessionProductId } from "./utils/get-checkout-session-product-id";
3333
import { updateCustomerWithStripeCustomerId } from "./utils/update-customer-with-stripe-customer-id";
3434

3535
// Handle event "checkout.session.completed"
@@ -445,8 +445,8 @@ export async function checkoutSessionCompleted(
445445
| undefined = undefined;
446446

447447
if (link && link.programId && link.partnerId) {
448-
const productId = await getSubscriptionProductId({
449-
stripeSubscriptionId: charge.subscription as string,
448+
const productId = await getCheckoutSessionProductId({
449+
checkoutSessionId: charge.id,
450450
stripeAccountId,
451451
mode,
452452
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { stripeAppClient } from "@/lib/stripe";
2+
import { StripeMode } from "@/lib/types";
3+
import type Stripe from "stripe";
4+
5+
function productIdFromLineItemPrice(
6+
price: Stripe.Price | string | null | undefined,
7+
): string | null {
8+
if (!price || typeof price === "string") {
9+
return null;
10+
}
11+
12+
if (!price.product) {
13+
return null;
14+
}
15+
16+
const product = price.product;
17+
18+
if (typeof product === "string") {
19+
return product;
20+
}
21+
22+
if ("deleted" in product && product.deleted) {
23+
return null;
24+
}
25+
26+
return product.id;
27+
}
28+
29+
export async function getCheckoutSessionProductId({
30+
checkoutSessionId,
31+
stripeAccountId,
32+
mode,
33+
}: {
34+
checkoutSessionId: string;
35+
stripeAccountId?: string | null;
36+
mode: StripeMode;
37+
}): Promise<string | null> {
38+
if (!stripeAccountId) {
39+
return null;
40+
}
41+
42+
try {
43+
const lineItems = await stripeAppClient({
44+
mode,
45+
}).checkout.sessions.listLineItems(
46+
checkoutSessionId,
47+
{
48+
expand: ["data.price.product"],
49+
limit: 10,
50+
},
51+
{
52+
stripeAccount: stripeAccountId,
53+
},
54+
);
55+
56+
for (const item of lineItems.data) {
57+
const productId = productIdFromLineItemPrice(item.price);
58+
if (productId) {
59+
return productId;
60+
}
61+
}
62+
63+
return null;
64+
} catch (error) {
65+
console.log("Failed to get checkout session product ID:", error);
66+
return null;
67+
}
68+
}

apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-subscription-product-id.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)