Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): recompute seat quantity from current subscription state

Richie McIlroy4d57ac17fa10
diff --git a/apps/web/actions/organization/update-seat-quantity.ts b/apps/web/actions/organization/update-seat-quantity.ts
index 01f24126e..942bbb0bf 100644
--- a/apps/web/actions/organization/update-seat-quantity.ts
+++ b/apps/web/actions/organization/update-seat-quantity.ts
@@ -153,6 +153,7 @@ export async function updateSeatQuantity(
validateQuantity(newQuantity);
const { subscription, subscriptionItem, proSeatsUsed, user } =
await getOwnerSubscription(organizationId);
+ const currentQuantity = subscriptionItem.quantity ?? 1;
if (newQuantity < proSeatsUsed) {
throw new Error(
@@ -160,15 +161,30 @@ export async function updateSeatQuantity(
);
}
- await stripe().subscriptions.update(subscription.id, {
- items: [
- {
- id: subscriptionItem.id,
- quantity: newQuantity,
- },
- ],
- proration_behavior: "create_prorations",
- });
+ const isSeatIncrease = newQuantity > currentQuantity;
+ const updatedSubscription = await stripe().subscriptions.update(
+ subscription.id,
+ {
+ items: [
+ {
+ id: subscriptionItem.id,
+ quantity: newQuantity,
+ },
+ ],
+ proration_behavior: isSeatIncrease
+ ? "always_invoice"
+ : "create_prorations",
+ ...(isSeatIncrease
+ ? { payment_behavior: "pending_if_incomplete" as const }
+ : {}),
+ },
+ );
+
+ if (isSeatIncrease && updatedSubscription.pending_update) {
+ throw new Error(
+ "Payment for the added seats could not be completed. Update your payment method and try again.",
+ );
+ }
try {
await db()
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx
index c2aea379f..d75dbfa48 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx
@@ -169,7 +169,7 @@ export function SeatManagementCard() {
<span className="text-sm text-gray-11">
{preview.proratedAmount === 0
? "No prorated adjustment"
- : `${preview.proratedAmount > 0 ? "Prorated charge" : "Prorated credit"}: $${Math.abs(preview.proratedAmount / 100).toFixed(2)} ${preview.currency.toUpperCase()}`}
+ : `${preview.proratedAmount > 0 ? "Due now" : "Prorated credit"}: $${Math.abs(preview.proratedAmount / 100).toFixed(2)} ${preview.currency.toUpperCase()}`}
</span>
) : null}
<Button