Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Routes } from '@/utils/constants';
import { legacySubscriptions, prices, subscriptions, toSubscription, users } from '@onlook/db';
import { createBillingPortalSession, createCheckoutSession, createCustomer, isTierUpgrade, PriceKey, releaseSubscriptionSchedule, SubscriptionStatus, updateSubscription, updateSubscriptionNextPeriod } from '@onlook/stripe';
import { createBillingPortalSession, createCheckoutSession, createCustomer, isTierUpgrade, PriceKey, releaseSubscriptionSchedule, SubscriptionStatus, updateSubscriptionNextPeriod, upgradeSubscription } from '@onlook/stripe';
import { and, eq, isNull } from 'drizzle-orm';
import { headers } from 'next/headers';
import { z } from 'zod';
Expand Down Expand Up @@ -162,7 +162,7 @@ export const subscriptionRouter = createTRPCRouter({
const isUpgrade = isTierUpgrade(currentPrice, newPrice);
if (isUpgrade) {
// If the new price is higher, we invoice the customer immediately.
await updateSubscription({
await upgradeSubscription({
subscriptionId: stripeSubscriptionId,
subscriptionItemId: stripeSubscriptionItemId,
priceId: stripePriceId,
Expand Down
62 changes: 62 additions & 0 deletions packages/stripe/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,68 @@ export const updateSubscription = async ({
});
};

export const upgradeSubscription = async ({
subscriptionId,
subscriptionItemId,
priceId,
}: {
subscriptionId: string;
subscriptionItemId: string;
priceId: string;
}) => {
const stripe = createStripeClient();
const currentSubscription = await stripe.subscriptions.retrieve(subscriptionId);
if (!currentSubscription) {
throw new Error('Subscription not found');
}

const currentPrice = currentSubscription.items.data[0]?.price.id;
if (currentPrice === priceId) {
throw new Error('New price is the same as the current price');
}

const currentPriceAmount = currentSubscription.items.data[0]?.price.unit_amount;
if (!currentPriceAmount) {
throw new Error('Current price amount not found');
}

const updatedSubscription = await stripe.subscriptions.update(subscriptionId, {
items: [
{
id: subscriptionItemId,
price: priceId,
},
],
// We don't want to prorate the price difference because it would be based on time remaining in the current period
proration_behavior: 'none',
});

const newPriceAmount = updatedSubscription.items.data[0]?.price.unit_amount;
if (!newPriceAmount) {
throw new Error('New price amount not found');
}

const priceDifferenceAmount = newPriceAmount - currentPriceAmount;

// Create a one-off invoice item for the price difference if the new price is higher
if (priceDifferenceAmount > 0) {
await stripe.invoiceItems.create({
customer: updatedSubscription.customer as string,
amount: priceDifferenceAmount,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The currency field was removed from the invoiceItems.create call. Stripe typically requires a currency for invoice items; consider reinstating it.

Suggested change
amount: priceDifferenceAmount,
currency: 'usd',

currency: updatedSubscription.currency || 'usd',
description: 'Price upgrade difference',
});

// Create invoice immediately
const invoice = await stripe.invoices.create({
customer: updatedSubscription.customer as string,
auto_advance: true,
});
}

return updatedSubscription;
};

export const updateSubscriptionNextPeriod = async ({
subscriptionId,
priceId,
Expand Down