File tree Expand file tree Collapse file tree
apps/web/app/(ee)/api/stripe/integration/webhook Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ import { waitUntil } from "@vercel/functions";
2929import type Stripe from "stripe" ;
3030import { getConnectedCustomer } from "./utils/get-connected-customer" ;
3131import { 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" ;
3333import { 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 } ) ;
Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments