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.

account for live/test-mode in stripeAppClient

Steven Tey0f59c2fc119d
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
index fcd9fb312e..0dc20baeb0 100644
--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -151,6 +151,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
const connectedCustomer = await getConnectedCustomer({
stripeCustomerId,
stripeAccountId,
+ livemode: event.livemode,
});
if (!connectedCustomer || !connectedCustomer.metadata.dubCustomerId) {
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index 697bbdae42..f5edb85f20 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -31,6 +31,7 @@ export async function invoicePaid(event: Stripe.Event) {
const connectedCustomer = await getConnectedCustomer({
stripeCustomerId,
stripeAccountId,
+ livemode: event.livemode,
});
const dubCustomerId = connectedCustomer?.metadata.dubCustomerId;
diff --git a/apps/web/app/api/stripe/integration/webhook/utils.ts b/apps/web/app/api/stripe/integration/webhook/utils.ts
index 5758e4f714..e59a14d9f2 100644
--- a/apps/web/app/api/stripe/integration/webhook/utils.ts
+++ b/apps/web/app/api/stripe/integration/webhook/utils.ts
@@ -129,21 +129,22 @@ export async function createNewCustomer(event: Stripe.Event) {
export async function getConnectedCustomer({
stripeCustomerId,
stripeAccountId,
+ livemode = true,
}: {
stripeCustomerId?: string | null;
stripeAccountId?: string | null;
+ livemode?: boolean;
}) {
// if stripeCustomerId or stripeAccountId is not provided, return null
if (!stripeCustomerId || !stripeAccountId) {
return null;
}
- const connectedCustomer = await stripeAppClient.customers.retrieve(
- stripeCustomerId,
- {
- stripeAccount: stripeAccountId,
- },
- );
+ const connectedCustomer = await stripeAppClient({
+ livemode,
+ }).customers.retrieve(stripeCustomerId, {
+ stripeAccount: stripeAccountId,
+ });
if (connectedCustomer.deleted) {
return null;
diff --git a/apps/web/lib/stripe/index.ts b/apps/web/lib/stripe/index.ts
index bee86ca27d..536eca7364 100644
--- a/apps/web/lib/stripe/index.ts
+++ b/apps/web/lib/stripe/index.ts
@@ -9,13 +9,18 @@ export const stripe = new Stripe(`${process.env.STRIPE_SECRET_KEY}`, {
});
// Stripe Integration App client
-export const stripeAppClient = new Stripe(
- `${process.env.STRIPE_APP_SECRET_KEY}`,
- {
- apiVersion: "2022-11-15",
- appInfo: {
- name: "Dub.co",
- version: "0.1.0",
+export const stripeAppClient = ({
+ livemode,
+}: {
+ livemode?: boolean;
+} = {}) =>
+ new Stripe(
+ `${!livemode ? process.env.STRIPE_APP_SECRET_KEY_TEST : process.env.STRIPE_APP_SECRET_KEY}`,
+ {
+ apiVersion: "2022-11-15",
+ appInfo: {
+ name: "Dub.co",
+ version: "0.1.0",
+ },
},
- },
-);
+ );
diff --git a/apps/web/scripts/stripe/backfill-stripe-webhook-events.ts b/apps/web/scripts/stripe/backfill-stripe-webhook-events.ts
index b8390383f9..045bbfa7cf 100644
--- a/apps/web/scripts/stripe/backfill-stripe-webhook-events.ts
+++ b/apps/web/scripts/stripe/backfill-stripe-webhook-events.ts
@@ -26,7 +26,9 @@ async function main() {
if (!customer.email) return;
if (customer.stripeCustomerId) return;
- const stripeCustomer = await stripeAppClient.customers.list(
+ const stripeCustomer = await stripeAppClient({
+ livemode: false,
+ }).customers.list(
{
email: customer.email,
},
diff --git a/apps/web/scripts/stripe/get-connected-customer.ts b/apps/web/scripts/stripe/get-connected-customer.ts
index 53f84a6bbf..bb45f9a884 100644
--- a/apps/web/scripts/stripe/get-connected-customer.ts
+++ b/apps/web/scripts/stripe/get-connected-customer.ts
@@ -2,12 +2,11 @@ import "dotenv-flow/config";
import { stripeAppClient } from "../../lib/stripe";
async function main() {
- const connectedCustomer = await stripeAppClient.customers.retrieve(
- "cus_xxx",
- {
- stripeAccount: "acct_xxx",
- },
- );
+ const connectedCustomer = await stripeAppClient({
+ livemode: false,
+ }).customers.retrieve("cus_xxx", {
+ stripeAccount: "acct_xxx",
+ });
console.log(connectedCustomer);
}