惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
量子位
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
IT之家
IT之家
V
Visual Studio Blog
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
D
Docker
V
V2EX

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The Hidden Math of Mid-Cycle Subscription Upgrades
scubaDEV · 2026-06-15 · via DEV Community

Here's a question that sounds trivial and isn't. A customer is on Basic, 3 seats. On day 12 of a 30-day billing cycle they switch to Pro, 2 seats. How much do you charge them today, and what do they renew at next month?

If your first instinct was "new price times new quantity," you've already shipped the bug. That's the renewal amount, not what they owe now. The amount due today is a proration, and getting it wrong means either robbing the customer or robbing yourself — quietly, on every mid-cycle change, forever.

I wrote this logic for a real billing integration, covered it in comments, and I'm glad I did, because six months later I couldn't reconstruct it from memory. Here's the part no payment SDK gives you.

What proration actually means

The customer already paid for the full cycle up front. When they change plans mid-cycle, two things are true:

  • They have unused credit on the old plan for the days remaining.
  • They owe the cost of the new plan for those same remaining days.

What you charge today is the difference between those two. Not the new full price — just the delta for the slice of the cycle they haven't used yet.

So for our example, with 18 of 30 days remaining (60% of the cycle left):

  • Unused Basic credit ≈ 60% of (Basic price × 3 seats)
  • New Pro cost ≈ 60% of (Pro price × 2 seats)
  • Charge today = new cost − unused credit

The next renewal then bills the full Pro × 2 at the normal cycle boundary. Two completely different numbers, and conflating them is the classic mistake.

The four cases, one of which "should never happen"

Once seats enter the picture, a single change can be any of four shapes, and they don't behave the same:

  1. Upgrade + more seats — charge the prorated plan delta on existing seats and the prorated cost of the new seats.
  2. Upgrade + fewer seats — charge the plan delta only on the seats that survive; the removed seats generate credit, not charge.
  3. Downgrade — typically costs 0 today. You don't refund mid-cycle cash; the lower price takes effect next renewal.
  4. First purchase — no proration at all, just a clean charge.

In my code one branch is commented "this should never happen given the UI… but it's supported if needed." That comment is doing real work. Defensive billing code that handles the impossible case is cheaper than a support ticket from the one customer who found a way to trigger it.

The trap that actually bit me: dates

The subtle bug wasn't the money math. It was making the new cycle start exactly where the old one ended.

You take the provider's NextBillingTime, and if you carry the time-of-day along with it, the new cycle and the old one drift apart by hours — enough to double-bill a fraction of a day or leave a gap. The fix was to collapse to a date (drop the time component entirely) so the boundary lines up cleanly. In .NET that's exactly what DateOnly is for. A whole class of off-by-a-few-hours billing weirdness disappears the moment you stop carrying the clock around.

The design call: compute, log, never surprise the user

The last decision was philosophical. Proration is the kind of calculation that, if it's ever wrong, is wrong in a way the customer notices on their card statement. So I didn't expose intermediate failures to the user at all. Every branch produces a computed result object — the amount, the case it took, the dates it used — and logs the inputs aggressively. If a charge ever looks off, I can replay exactly which path ran and why, instead of guessing from a stack trace.

That's the real lesson, more than the arithmetic: for money math, observability is part of the feature. The calculation that you can't reconstruct after the fact is the one that costs you a chargeback.

Takeaway

Mid-cycle subscription changes aren't "new price × quantity." They're a difference of two prorated amounts, branching into four cases, anchored to a date boundary you have to normalize by hand. Write it once, comment the impossible branch, log the inputs, and let DateOnly save you from the clock. Then never touch it again if you can help it.