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.

handle multiple stripe customers

Steven Tey945a85c2c296
diff --git a/apps/web/lib/tolt/update-stripe-customers.ts b/apps/web/lib/tolt/update-stripe-customers.ts
index 71f8448802..736f4ac4ec 100644
--- a/apps/web/lib/tolt/update-stripe-customers.ts
+++ b/apps/web/lib/tolt/update-stripe-customers.ts
@@ -1,6 +1,7 @@
import { prisma } from "@dub/prisma";
import { Customer, Project } from "@dub/prisma/client";
import { log } from "@dub/utils";
+import Stripe from "stripe";
import { stripeAppClient } from "../stripe";
import { MAX_BATCHES, toltImporter } from "./importer";
@@ -115,20 +116,31 @@ async function searchStripeAndUpdateCustomer({
return null;
}
- if (stripeCustomers.data.length > 1) {
- await log({
- message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug}`,
- type: "errors",
- });
+ let stripeCustomer: Stripe.Customer;
- console.error(
- `Stripe search returned multiple customers for ${customer.email}`,
+ if (stripeCustomers.data.length > 1) {
+ // look for the one with metadata.tolt_referral set
+ const toltReferralStripeCustomer = stripeCustomers.data.find(
+ (customer) => customer.metadata.tolt_referral,
);
- return null;
+ if (toltReferralStripeCustomer) {
+ stripeCustomer = toltReferralStripeCustomer;
+ } else {
+ await log({
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ type: "errors",
+ });
+
+ console.error(
+ `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ );
+
+ return null;
+ }
}
- const stripeCustomer = stripeCustomers.data[0];
+ stripeCustomer = stripeCustomers.data[0];
await prisma.customer.update({
where: {
diff --git a/apps/web/scripts/stripe/search-customers.ts b/apps/web/scripts/stripe/search-customers.ts
new file mode 100644
index 0000000000..59ca3e6668
--- /dev/null
+++ b/apps/web/scripts/stripe/search-customers.ts
@@ -0,0 +1,20 @@
+import "dotenv-flow/config";
+import { stripeAppClient } from "../../lib/stripe";
+
+async function main() {
+ const email = "xxx";
+
+ const stripeCustomers = await stripeAppClient({
+ livemode: true,
+ }).customers.search(
+ {
+ query: `email:'${email}'`,
+ },
+ {
+ stripeAccount: "xxx",
+ },
+ );
+ console.log(JSON.stringify(stripeCustomers, null, 2));
+}
+
+main();
diff --git a/apps/web/scripts/stripe/update-stripe-customers.ts b/apps/web/scripts/stripe/update-stripe-customers.ts
new file mode 100644
index 0000000000..9ed92b8c54
--- /dev/null
+++ b/apps/web/scripts/stripe/update-stripe-customers.ts
@@ -0,0 +1,86 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+import Stripe from "stripe";
+import { stripeAppClient } from "../../lib/stripe";
+
+async function main() {
+ const workspace = await prisma.project.findUniqueOrThrow({
+ where: {
+ id: "xxx",
+ },
+ });
+
+ if (!workspace.stripeConnectId) {
+ throw new Error("Workspace has no stripeConnectId");
+ }
+
+ const customers = await prisma.customer.findMany({
+ where: {
+ projectId: workspace.id,
+ stripeCustomerId: null,
+ },
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ take: 20,
+ });
+
+ for (const customer of customers) {
+ const stripeCustomers = await stripeAppClient({
+ livemode: true,
+ }).customers.search(
+ {
+ query: `email:'${customer.email}'`,
+ },
+ {
+ stripeAccount: workspace.stripeConnectId,
+ },
+ );
+
+ if (stripeCustomers.data.length === 0) {
+ console.error(`Stripe search returned no customer for ${customer.email}`);
+ continue;
+ }
+
+ let stripeCustomer: Stripe.Customer;
+
+ if (stripeCustomers.data.length > 1) {
+ // look for the one with metadata.tolt_referral set
+ const toltReferralStripeCustomer = stripeCustomers.data.find(
+ (customer) => customer.metadata.tolt_referral,
+ );
+
+ if (toltReferralStripeCustomer) {
+ stripeCustomer = toltReferralStripeCustomer;
+ } else {
+ console.error(
+ `Stripe search returned multiple customers for ${customer.email} and none had metadata.tolt_referral set`,
+ );
+
+ continue;
+ }
+ }
+
+ stripeCustomer = stripeCustomers.data[0];
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ stripeCustomerId: stripeCustomer.id,
+ },
+ });
+
+ console.log(
+ `Updated customer ${customer.email} with stripeCustomerId ${stripeCustomer.id}`,
+ );
+ }
+}
+
+main();