Commit 7e81f47
authored
* refactor(billing): slim BillingSubscription schema (delegate periodEnd to Stripe API)
Drop currentPeriodEnd and cancelAtPeriodEnd from Mongoose model and Zod schema.
Add stripeEventId field (tiebreaker for same-second event ordering, V5 P1 #2).
Migration 20260503000200 unsets the removed fields from existing documents.
* refactor(billing): fetchSubscriptionDetails helper + getSubscription merge Stripe details
New fetchSubscriptionDetails() fetches currentPeriodEnd, cancelAtPeriodEnd, status,
nextRenewalDate from Stripe API on-demand (+50ms on GET /subscription, 0ms on quota gate).
getSubscription() merges cached local fields with live Stripe details for UI consumers.
Falls back gracefully when Stripe is not configured or subscription is free.
* refactor(billing): unified webhook handlers — drop period fields, pass eventId
handleSubscriptionUpdated: drop currentPeriodEnd/cancelAtPeriodEnd from $set fields.
All 4 handlers now call updateIfEventNewer(id, created, eventId, fields).
handleInvoicePaymentSucceeded: add event param + use updateIfEventNewer (V5 P1 #1).
handleSubscriptionDeleted: force meter rotate to free on cancel (V5 P1 #3).
* fix(billing): event tiebreaker via eventId for same-second deliveries (V5 P1 #2)
updateIfEventNewer now takes eventId as 3rd param. Same-second events are ordered
by lex string comparison of evt_ IDs, preventing the 2nd event from being silently
skipped when both events have the same Unix second timestamp.
Stores stripeEventId alongside stripeEventCreatedAt in the document.
* refactor(billing): drop billing.refund.service — inline Stripe refund in admin controller
billing.refund.service.js had a single function (refundCharge) used only by the admin
controller. Inline it directly to eliminate an unnecessary service indirection layer.
Idempotency key pattern and argument validation are preserved unchanged.
* test(billing): update tests for PR2 — new signatures, V5 P1 coverage, inline refund
- subscription.schema: drop cancelAtPeriodEnd/currentPeriodEnd tests, add stripeEventId
- subscription.repository: updateIfEventNewer now 4-arg; add same-second tiebreaker test
- webhook.integration: add eventId arg to all updateIfEventNewer assertions; V5 P1 #3 tests
- webhook.subscription: updateIfEventNewer 4-arg; handleInvoicePaymentSucceeded with event
- billing.service: updateIfEventNewer 4-arg; add forceRotateForPlanChange mock
- billing.checkout: fetchSubscriptionDetails tests + updated getSubscription (Stripe merge)
- billing.admin.integration: rewritten for inline Stripe (no refund service mock)
- billing.refund.service: rewritten as admin controller refund tests
* fix(billing): payment_succeeded markers + admin refund key + reason guard
- webhook: always advance stripeEventCreatedAt/stripeEventId on
invoice.payment_succeeded, even when pastDueSince is already null;
prevents stale out-of-order replays from being applied against a
markers state that was never advanced on routine invoices
- admin: replace fixed idempotency key with randomUUID suffix so
multiple partial refunds on the same charge are not collapsed within
Stripe's 24 h idempotency window
- reason validation already enforced upstream via AdminRefundRequest
Zod schema (z.enum); no controller change required
1 parent 3df3104 commit 7e81f47
17 files changed
Lines changed: 690 additions & 238 deletions
File tree
- modules/billing
- controllers
- migrations
- models
- repositories
- services
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | | - | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| 17 | + | |
13 | 18 | | |
14 | 19 | | |
15 | 20 | | |
16 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
17 | 47 | | |
18 | 48 | | |
19 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
Lines changed: 8 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | 40 | | |
48 | 41 | | |
49 | 42 | | |
| |||
87 | 80 | | |
88 | 81 | | |
89 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | 35 | | |
37 | 36 | | |
38 | 37 | | |
| |||
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
45 | | - | |
46 | 44 | | |
47 | 45 | | |
48 | 46 | | |
| |||
Lines changed: 19 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
| 219 | + | |
| 220 | + | |
219 | 221 | | |
220 | 222 | | |
221 | | - | |
| 223 | + | |
| 224 | + | |
222 | 225 | | |
223 | 226 | | |
224 | 227 | | |
225 | 228 | | |
226 | | - | |
| 229 | + | |
227 | 230 | | |
228 | 231 | | |
229 | 232 | | |
230 | 233 | | |
231 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
232 | 247 | | |
233 | | - | |
| 248 | + | |
234 | 249 | | |
235 | 250 | | |
236 | 251 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
247 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
248 | 274 | | |
249 | | - | |
| 275 | + | |
250 | 276 | | |
251 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
252 | 285 | | |
253 | 286 | | |
254 | 287 | | |
255 | 288 | | |
256 | 289 | | |
| 290 | + | |
257 | 291 | | |
258 | 292 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
216 | | - | |
217 | 215 | | |
218 | 216 | | |
219 | 217 | | |
220 | | - | |
| 218 | + | |
221 | 219 | | |
222 | 220 | | |
223 | 221 | | |
| |||
305 | 303 | | |
306 | 304 | | |
307 | 305 | | |
308 | | - | |
| 306 | + | |
309 | 307 | | |
310 | 308 | | |
311 | 309 | | |
| |||
314 | 312 | | |
315 | 313 | | |
316 | 314 | | |
317 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
318 | 325 | | |
319 | 326 | | |
320 | 327 | | |
| |||
340 | 347 | | |
341 | 348 | | |
342 | 349 | | |
343 | | - | |
| 350 | + | |
344 | 351 | | |
345 | 352 | | |
346 | 353 | | |
| |||
359 | 366 | | |
360 | 367 | | |
361 | 368 | | |
| 369 | + | |
362 | 370 | | |
| 371 | + | |
363 | 372 | | |
364 | 373 | | |
365 | 374 | | |
366 | | - | |
| 375 | + | |
367 | 376 | | |
368 | 377 | | |
369 | 378 | | |
370 | 379 | | |
371 | 380 | | |
372 | 381 | | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
380 | 396 | | |
381 | 397 | | |
382 | 398 | | |
| |||
0 commit comments