Plan Upgrade/Downgrade for SaaS Application

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

SaaS: Plan Upgrade and Downgrade

Plan change is one of trickiest billing operations. Stripe handles proportional calculations, but business logic (what happens to data on downgrade) is on developer side.

Upgrade: Immediate Update

export async function upgradeSubscription(
  tenantId: string,
  newPriceId: string
): Promise<void> {
  const subscription = await db.subscription.findUniqueOrThrow({
    where: { tenantId }
  });

  // Stripe auto-recalculates
  // Example: 20 days left of 30, upgrade from $29 to $99
  // Charge = (99 - 29) * 20/30 = $46.67 immediately
  const updatedSub = await stripe.subscriptions.update(
    subscription.stripeSubscriptionId!,
    {
      items: [{
        id: (await stripe.subscriptions.retrieve(
          subscription.stripeSubscriptionId!
        )).items.data[0].id,
        price: newPriceId,
      }],
      proration_behavior: 'create_prorations',
      payment_behavior: 'error_if_incomplete',
    }
  );

  const previewInvoice = await stripe.invoices.retrieveUpcoming({
    customer: subscription.stripeCustomerId,
    subscription: subscription.stripeSubscriptionId!,
    subscription_items: [{
      id: updatedSub.items.data[0].id,
      price: newPriceId,
    }],
    subscription_proration_behavior: 'create_prorations',
  });

  console.log('Charge now:', previewInvoice.amount_due / 100);
}

Preview Amount for UI

export async function POST(request: Request) {
  const { newPriceId } = await request.json();
  const tenant = await getCurrentTenant();
  const subscription = await db.subscription.findUnique({
    where: { tenantId: tenant!.id }
  });

  const preview = await stripe.invoices.retrieveUpcoming({
    customer: subscription!.stripeCustomerId,
    subscription: subscription!.stripeSubscriptionId!,
    subscription_items: [{
      id: (await stripe.subscriptions.retrieve(
        subscription!.stripeSubscriptionId!
      )).items.data[0].id,
      price: newPriceId,
    }],
  });

  return Response.json({
    amountDue: preview.amount_due / 100,
    currency: preview.currency,
    periodEnd: new Date(preview.period_end * 1000),
  });
}

Downgrade: At Period End

export async function scheduleDowngrade(
  tenantId: string,
  newPriceId: string
): Promise<void> {
  const subscription = await db.subscription.findUniqueOrThrow({
    where: { tenantId }
  });

  await validateDowngrade(tenantId, newPriceId);

  const stripeSubscription = await stripe.subscriptions.retrieve(
    subscription.stripeSubscriptionId!
  );

  await stripe.subscriptions.update(subscription.stripeSubscriptionId!, {
    items: [{
      id: stripeSubscription.items.data[0].id,
      price: newPriceId,
    }],
    proration_behavior: 'none',
    billing_cycle_anchor: 'unchanged',
  });

  await db.subscription.update({
    where: { tenantId },
    data: {
      pendingPriceId: newPriceId,
      pendingPlanChange: getPlanFromPrice(newPriceId),
    }
  });

  await sendPlanChangeScheduledEmail(tenantId, {
    currentPlan: subscription.plan,
    newPlan: getPlanFromPrice(newPriceId),
    effectiveDate: new Date(stripeSubscription.current_period_end * 1000),
  });
}

Downgrade Validation

export async function validateDowngrade(
  tenantId: string,
  newPriceId: string
): Promise<void> {
  const newPlan = getPlanFromPrice(newPriceId);
  const limits = PLAN_LIMITS[newPlan];

  const [projectCount, memberCount, storageGb] = await Promise.all([
    db.project.count({ where: { tenantId } }),
    db.tenantUser.count({ where: { tenantId } }),
    calculateStorageUsage(tenantId),
  ]);

  const violations: string[] = [];

  if (projectCount > limits.projects) {
    violations.push(
      `You have ${projectCount} projects. Limit for ${newPlan}: ${limits.projects}. ` +
      `Delete ${projectCount - limits.projects} projects.`
    );
  }

  if (memberCount > limits.members) {
    violations.push(
      `You have ${memberCount} members. Limit for ${newPlan}: ${limits.members}.`
    );
  }

  if (storageGb > limits.storageGb) {
    violations.push(
      `Used ${storageGb.toFixed(1)} GB. Limit for ${newPlan}: ${limits.storageGb} GB.`
    );
  }

  if (violations.length > 0) {
    throw new PlanDowngradeError(violations);
  }
}

UI: Plan Change Page

export function PlanChangeModal({
  currentPlan,
  targetPlan,
  previewAmount,
  isUpgrade,
  onConfirm,
}: PlanChangeModalProps) {
  return (
    <Dialog>
      <DialogHeader>
        <DialogTitle>
          {isUpgrade ? 'Upgrade' : 'Change'} plan: {currentPlan} → {targetPlan}
        </DialogTitle>
      </DialogHeader>

      {isUpgrade ? (
        <div>
          <p>${previewAmount} will be charged from your card immediately.</p>
          <p>This is proportional payment for the remaining period.</p>
        </div>
      ) : (
        <div>
          <p>Current plan active until end of billing period.</p>
          <p>After that switch to <strong>{targetPlan}</strong>.</p>
          {targetPlan === 'FREE' && (
            <Alert>Check limits: {targetPlan} plan supports up to 3 projects.</Alert>
          )}
        </div>
      )}

      <DialogFooter>
        <Button variant="outline" onClick={onClose}>Cancel</Button>
        <Button onClick={onConfirm}>
          {isUpgrade ? 'Upgrade and Pay' : 'Confirm Plan Change'}
        </Button>
      </DialogFooter>
    </Dialog>
  );
}

Upgrade/downgrade implementation with Stripe proreration, validation and UI — 2–3 working days.