Skip to content

Commit af957d7

Browse files
committed
update based on feedback
1 parent 1ba8e8f commit af957d7

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

apps/web/template/next.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ const nextConfig = {
44
devIndicators: {
55
buildActivity: false,
66
},
7+
eslint: {
8+
ignoreDuringBuilds: true,
9+
},
10+
typescript: {
11+
ignoreBuildErrors: true,
12+
},
713
};
814
export default nextConfig;

apps/web/template/next.config.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/stripe/src/functions.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,18 @@ export const upgradeSubscription = async ({
124124
throw new Error('Subscription not found');
125125
}
126126

127-
const currentPrice = currentSubscription.items.data[0]?.price.id;
127+
const currentItem = currentSubscription.items.data.find((item) => item.id === subscriptionItemId);
128+
if (!currentItem) {
129+
throw new Error('Subscription item not found');
130+
}
131+
132+
const currentPrice = currentItem.price.id;
128133
if (currentPrice === priceId) {
129134
throw new Error('New price is the same as the current price');
130135
}
131136

132-
const currentPriceAmount = currentSubscription.items.data[0]?.price.unit_amount;
133-
if (!currentPriceAmount) {
137+
const currentPriceAmount = currentItem.price.unit_amount;
138+
if (currentPriceAmount == null) {
134139
throw new Error('Current price amount not found');
135140
}
136141

@@ -145,27 +150,38 @@ export const upgradeSubscription = async ({
145150
proration_behavior: 'none',
146151
});
147152

148-
const newPriceAmount = updatedSubscription.items.data[0]?.price.unit_amount;
149-
if (!newPriceAmount) {
153+
const newItem =
154+
updatedSubscription.items.data.find((i) => i.id === subscriptionItemId)
155+
?? updatedSubscription.items.data[0];
156+
if (!newItem) {
157+
throw new Error('Subscription item not found on updated subscription');
158+
}
159+
const newPriceAmount = newItem.price?.unit_amount;
160+
if (newPriceAmount == null) {
150161
throw new Error('New price amount not found');
151162
}
152163

153-
const priceDifferenceAmount = newPriceAmount - currentPriceAmount;
164+
const quantity = newItem.quantity ?? 1;
165+
const priceDifferenceAmount = (newPriceAmount - currentPriceAmount) * quantity;
154166

155167
// Create a one-off invoice item for the price difference if the new price is higher
156168
if (priceDifferenceAmount > 0) {
157169
await stripe.invoiceItems.create({
158170
customer: updatedSubscription.customer as string,
159171
amount: priceDifferenceAmount,
160-
currency: updatedSubscription.currency || 'usd',
161-
description: 'Price upgrade difference',
172+
description: 'Onlook subscription upgrade',
162173
});
163174

164175
// Create invoice immediately
165176
const invoice = await stripe.invoices.create({
166177
customer: updatedSubscription.customer as string,
167178
auto_advance: true,
168179
});
180+
181+
if (!invoice.id) {
182+
throw new Error('Invoice not created');
183+
}
184+
await stripe.invoices.pay(invoice.id);
169185
}
170186

171187
return updatedSubscription;

0 commit comments

Comments
 (0)