Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

getSubscriptionProductId → getCheckoutSessionProductId (#3693)

Steven Tey6d2e82a9467f
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index 4698cd8e17..228d5e024f 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -29,7 +29,7 @@ import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
import { getConnectedCustomer } from "./utils/get-connected-customer";
import { getPromotionCode } from "./utils/get-promotion-code";
-import { getSubscriptionProductId } from "./utils/get-subscription-product-id";
+import { getCheckoutSessionProductId } from "./utils/get-checkout-session-product-id";
import { updateCustomerWithStripeCustomerId } from "./utils/update-customer-with-stripe-customer-id";
// Handle event "checkout.session.completed"
@@ -445,8 +445,8 @@ export async function checkoutSessionCompleted(
| undefined = undefined;
if (link && link.programId && link.partnerId) {
- const productId = await getSubscriptionProductId({
- stripeSubscriptionId: charge.subscription as string,
+ const productId = await getCheckoutSessionProductId({
+ checkoutSessionId: charge.id,
stripeAccountId,
mode,
});
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-checkout-session-product-id.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-checkout-session-product-id.ts
new file mode 100644
index 0000000000..2191ecfee1
--- /dev/null
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-checkout-session-product-id.ts
@@ -0,0 +1,68 @@
+import { stripeAppClient } from "@/lib/stripe";
+import { StripeMode } from "@/lib/types";
+import type Stripe from "stripe";
+
+function productIdFromLineItemPrice(
+ price: Stripe.Price | string | null | undefined,
+): string | null {
+ if (!price || typeof price === "string") {
+ return null;
+ }
+
+ if (!price.product) {
+ return null;
+ }
+
+ const product = price.product;
+
+ if (typeof product === "string") {
+ return product;
+ }
+
+ if ("deleted" in product && product.deleted) {
+ return null;
+ }
+
+ return product.id;
+}
+
+export async function getCheckoutSessionProductId({
+ checkoutSessionId,
+ stripeAccountId,
+ mode,
+}: {
+ checkoutSessionId: string;
+ stripeAccountId?: string | null;
+ mode: StripeMode;
+}): Promise<string | null> {
+ if (!stripeAccountId) {
+ return null;
+ }
+
+ try {
+ const lineItems = await stripeAppClient({
+ mode,
+ }).checkout.sessions.listLineItems(
+ checkoutSessionId,
+ {
+ expand: ["data.price.product"],
+ limit: 10,
+ },
+ {
+ stripeAccount: stripeAccountId,
+ },
+ );
+
+ for (const item of lineItems.data) {
+ const productId = productIdFromLineItemPrice(item.price);
+ if (productId) {
+ return productId;
+ }
+ }
+
+ return null;
+ } catch (error) {
+ console.log("Failed to get checkout session product ID:", error);
+ return null;
+ }
+}
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-subscription-product-id.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-subscription-product-id.ts
deleted file mode 100644
index 14eaf2b99b..0000000000
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-subscription-product-id.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { stripeAppClient } from "@/lib/stripe";
-import { StripeMode } from "@/lib/types";
-
-export async function getSubscriptionProductId({
- stripeSubscriptionId,
- stripeAccountId,
- mode,
-}: {
- stripeSubscriptionId?: string | null;
- stripeAccountId?: string | null;
- mode: StripeMode;
-}) {
- if (!stripeAccountId || !stripeSubscriptionId) {
- return null;
- }
-
- try {
- const subscription = await stripeAppClient({
- mode,
- }).subscriptions.retrieve(stripeSubscriptionId, {
- stripeAccount: stripeAccountId,
- });
-
- return subscription.items.data[0].price.product as string;
- } catch (error) {
- console.log("Failed to get subscription price ID:", error);
- return null;
- }
-}