Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Use subscription period to compute prorations

Richie McIlroy8d142b94e823
diff --git a/apps/web/actions/organization/update-seat-quantity.ts b/apps/web/actions/organization/update-seat-quantity.ts
index cbdf42af5..c905870a9 100644
--- a/apps/web/actions/organization/update-seat-quantity.ts
+++ b/apps/web/actions/organization/update-seat-quantity.ts
@@ -81,7 +81,7 @@ export async function previewSeatChange(
newQuantity: number,
) {
validateQuantity(newQuantity);
- const { owner, subscriptionItem, proSeatsUsed } =
+ const { owner, subscription, subscriptionItem, proSeatsUsed } =
await getOwnerSubscription(organizationId);
const customerId = owner.stripeCustomerId;
const subscriptionId = owner.stripeSubscriptionId;
@@ -96,6 +96,8 @@ export async function previewSeatChange(
);
}
+ const currentQuantity = subscriptionItem.quantity ?? 1;
+
const previewParams = {
customer: customerId,
subscription: subscriptionId,
@@ -115,11 +117,21 @@ export async function previewSeatChange(
.autoPagingToArray({ limit: 1000 })
: preview.lines.data;
- const currentQuantity = subscriptionItem.quantity ?? 1;
- const proratedAmount = previewLines.reduce((total, line) => {
+ let proratedAmount = previewLines.reduce((total, line) => {
if (!line.proration) return total;
return total + line.amount;
}, 0);
+
+ if (proratedAmount === 0 && newQuantity !== currentQuantity) {
+ const currentPeriodEnd = subscription.current_period_end;
+ proratedAmount = previewLines.reduce((total, line) => {
+ if (line.period.end <= currentPeriodEnd) {
+ return total + line.amount;
+ }
+ return total;
+ }, 0);
+ }
+
const nextPaymentDate = preview.period_end;
return {