Skip to content

Commit

Permalink
billing: fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelecerati committed Sep 1, 2023
1 parent 3f56974 commit a83040f
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions packages/api/src/controllers/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,17 @@ app.post("/hacker/migration/pay-as-you-go", async (req, res) => {
price: item.id,
}));

let usageReport = await reportUsageForUser(
req,
user,
req.body.token,
false,
req.body.staging === true ? true : false
);
let usageReport;

if (req.body.usageReport) {
usageReport = await reportUsageForUser(
req,
user,
req.body.token,
false,
req.body.staging === true ? true : false
);
}

if (!req.body.actually_migrate) {
res.json({
Expand All @@ -462,31 +466,21 @@ app.post("/hacker/migration/pay-as-you-go", async (req, res) => {
return;
}

let subscriptionDate = subscription.current_period_start;
let billingCycleAnchor = subscription.current_period_end;

let deletedSubscription = await req.stripe.subscriptions.del(
user.stripeCustomerSubscriptionId
subscription = await req.stripe.subscriptions.update(
user.stripeCustomerSubscriptionId,
{
billing_cycle_anchor: "now",
cancel_at_period_end: false,
items: [
...items.data.map((item) => ({
price: item.id,
})),
...payAsYouGoItems,
],
expand: ["latest_invoice.payment_intent"],
}
);

migration.push(deletedSubscription);

subscription = await req.stripe.subscriptions.create({
cancel_at_period_end: false,
customer: user.stripeCustomerId,
backdate_start_date:
req.body.staging === true ? 1690934400 : subscriptionDate,
billing_cycle_anchor:
req.body.staging === true ? 1693612800 : billingCycleAnchor,
items: [
...items.data.map((item) => ({
price: item.id,
})),
...payAsYouGoItems,
],
expand: ["latest_invoice.payment_intent"],
});

migration.push(
`User ${user.email} has been updated to ${subscription.id}`
);
Expand Down Expand Up @@ -535,13 +529,17 @@ app.post("/hacker/migration/pay-as-you-go", async (req, res) => {
return;
}

let usageReport = await reportUsageForUser(
req,
user,
req.body.token,
true,
req.body.staging === true ? true : false
);
let usageReport;

if (req.body.usageReport) {
usageReport = await reportUsageForUser(
req,
user,
req.body.token,
true,
req.body.staging === true ? true : false
);
}

migration.push(
`User ${user.email} has been subscribed to pay as you go plans`
Expand Down

0 comments on commit a83040f

Please sign in to comment.