Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

billing: migration fix #1827

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/api/src/controllers/stripe.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading