diff --git a/packages/api/src/controllers/stripe.ts b/packages/api/src/controllers/stripe.ts index c345323f3d..81ecf6f36a 100644 --- a/packages/api/src/controllers/stripe.ts +++ b/packages/api/src/controllers/stripe.ts @@ -1,4 +1,4 @@ -import Router from "express/lib/router"; +import { Router } from "express"; import { db } from "../store"; import Stripe from "stripe"; import { products } from "../config"; @@ -727,10 +727,22 @@ app.post("/migrate-test-products", async (req, res) => { // If user.newStripeProductId is in testProducts, migrate it to the new product if (testProducts.includes(user.newStripeProductId)) { // get the stripe customer - const customer = await req.stripe.customers.get({ + const customer = await req.stripe.customers.list({ email: user.email, }); + if (customer.data.length === 0) { + console.log(` + Unable to migrate user - customer not found in stripe for user=${user.id} email=${user.email} subscriptionId=${user.stripeCustomerSubscriptionId} + `); + res.status(500).json({ + errors: [ + `Unable to migrate user - customer not found in stripe for user=${user.id} email=${user.email} subscriptionId=${user.stripeCustomerSubscriptionId}`, + ], + }); + return; + } + let payAsYouGoItems = []; let isPayAsYouGoPlan = products[productMapping[user.newStripeProductId]].payAsYouGo; @@ -757,7 +769,7 @@ app.post("/migrate-test-products", async (req, res) => { })); } - if (req.body.dry_run) { + if (!req.body.actually_migrate) { res.status(500).json({ migrating_user: { email: user.email, @@ -797,7 +809,7 @@ app.post("/migrate-test-products", async (req, res) => { // Update user's customer, product, subscription, and payment id in our db await db.user.update(user.id, { - stripeCustomerId: customer.id, + stripeCustomerId: user.stripeCustomerId, stripeProductId: productMapping[user.newStripeProductId], stripeCustomerSubscriptionId: subscription.id, stripeCustomerPaymentMethodId: null,