backend-manager 5.0.103 → 5.0.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/CLAUDE.md +113 -24
  3. package/README.md +8 -0
  4. package/TODO-PAYMENT-v2.md +5 -2
  5. package/package.json +1 -1
  6. package/src/cli/commands/deploy.js +2 -4
  7. package/src/cli/commands/emulator.js +30 -1
  8. package/src/cli/commands/test.js +33 -2
  9. package/src/manager/events/firestore/payments-webhooks/on-write.js +17 -3
  10. package/src/manager/events/firestore/payments-webhooks/transitions/index.js +6 -0
  11. package/src/manager/libraries/payment/processors/paypal.js +587 -0
  12. package/src/manager/libraries/{payment-processors → payment/processors}/stripe.js +86 -18
  13. package/src/manager/libraries/{payment-processors → payment/processors}/test.js +15 -8
  14. package/src/manager/routes/payments/cancel/processors/paypal.js +30 -0
  15. package/src/manager/routes/payments/cancel/processors/stripe.js +1 -1
  16. package/src/manager/routes/payments/cancel/processors/test.js +4 -6
  17. package/src/manager/routes/payments/intent/post.js +3 -3
  18. package/src/manager/routes/payments/intent/processors/paypal.js +150 -0
  19. package/src/manager/routes/payments/intent/processors/stripe.js +3 -5
  20. package/src/manager/routes/payments/intent/processors/test.js +7 -8
  21. package/src/manager/routes/payments/portal/processors/paypal.js +24 -0
  22. package/src/manager/routes/payments/portal/processors/stripe.js +1 -1
  23. package/src/manager/routes/payments/refund/post.js +85 -0
  24. package/src/manager/routes/payments/refund/processors/paypal.js +117 -0
  25. package/src/manager/routes/payments/refund/processors/stripe.js +103 -0
  26. package/src/manager/routes/payments/refund/processors/test.js +98 -0
  27. package/src/manager/routes/payments/webhook/processors/paypal.js +137 -0
  28. package/src/manager/schemas/payments/refund/post.js +18 -0
  29. package/src/test/test-accounts.js +46 -0
  30. package/templates/backend-manager-config.json +20 -24
  31. package/test/events/payments/journey-payments-cancel.js +3 -3
  32. package/test/events/payments/journey-payments-failure.js +1 -1
  33. package/test/events/payments/journey-payments-one-time.js +1 -1
  34. package/test/events/payments/journey-payments-plan-change.js +4 -4
  35. package/test/events/payments/journey-payments-suspend.js +3 -3
  36. package/test/events/payments/journey-payments-trial.js +2 -2
  37. package/test/fixtures/paypal/order-approved.json +62 -0
  38. package/test/fixtures/paypal/order-completed.json +110 -0
  39. package/test/fixtures/paypal/subscription-active.json +76 -0
  40. package/test/fixtures/paypal/subscription-cancelled.json +50 -0
  41. package/test/fixtures/paypal/subscription-suspended.json +65 -0
  42. package/test/helpers/payment/paypal/parse-webhook.js +539 -0
  43. package/test/helpers/payment/paypal/to-unified-one-time.js +382 -0
  44. package/test/helpers/payment/paypal/to-unified-subscription.js +820 -0
  45. package/test/helpers/{stripe-parse-webhook.js → payment/stripe/parse-webhook.js} +4 -4
  46. package/test/helpers/{stripe-to-unified-one-time.js → payment/stripe/to-unified-one-time.js} +8 -6
  47. package/test/helpers/{stripe-to-unified.js → payment/stripe/to-unified-subscription.js} +40 -33
  48. package/test/routes/payments/refund.js +174 -0
  49. package/src/manager/libraries/payment-processors/resolve-price-id.js +0 -19
  50. package/src/manager/routes/forms/delete.js +0 -37
  51. package/src/manager/routes/forms/get.js +0 -46
  52. package/src/manager/routes/forms/post.js +0 -45
  53. package/src/manager/routes/forms/public/get.js +0 -37
  54. package/src/manager/routes/forms/put.js +0 -52
  55. package/src/manager/schemas/forms/delete.js +0 -6
  56. package/src/manager/schemas/forms/get.js +0 -6
  57. package/src/manager/schemas/forms/post.js +0 -9
  58. package/src/manager/schemas/forms/public/get.js +0 -6
  59. package/src/manager/schemas/forms/put.js +0 -10
  60. /package/src/manager/libraries/{payment-processors → payment}/order-id.js +0 -0
@@ -0,0 +1,820 @@
1
+ /**
2
+ * Test: PayPal toUnifiedSubscription()
3
+ * Unit tests for the PayPal library's raw subscription → unified subscription transformation
4
+ *
5
+ * Tests the pure function directly — no emulator, no Firestore, no HTTP
6
+ * Mirrors stripe/to-unified-subscription.js for consistent coverage
7
+ */
8
+ const PayPal = require('../../../../src/manager/libraries/payment/processors/paypal.js');
9
+
10
+ // Real PayPal sandbox fixtures
11
+ const FIXTURE_ACTIVE = require('../../../fixtures/paypal/subscription-active.json');
12
+ const FIXTURE_CANCELLED = require('../../../fixtures/paypal/subscription-cancelled.json');
13
+ const FIXTURE_SUSPENDED = require('../../../fixtures/paypal/subscription-suspended.json');
14
+
15
+ // Mock config matching the BEM template (new flat price structure)
16
+ const MOCK_CONFIG = {
17
+ payment: {
18
+ products: [
19
+ { id: 'basic', name: 'Basic', type: 'subscription', limits: { requests: 100 } },
20
+ {
21
+ id: 'plus', name: 'Plus', type: 'subscription',
22
+ prices: { monthly: 9.99, annually: 99.99 },
23
+ paypal: { productId: 'PROD-plus' },
24
+ },
25
+ {
26
+ id: 'pro', name: 'Pro', type: 'subscription',
27
+ prices: { monthly: 29.99, annually: 299.99 },
28
+ paypal: { productId: 'PROD-pro' },
29
+ },
30
+ ],
31
+ },
32
+ };
33
+
34
+ function toUnifiedSubscription(rawSubscription, options) {
35
+ return PayPal.toUnifiedSubscription(rawSubscription, { config: MOCK_CONFIG, ...options });
36
+ }
37
+
38
+ module.exports = {
39
+ description: 'PayPal toUnifiedSubscription() transformation',
40
+ type: 'group',
41
+
42
+ tests: [
43
+ // ─── Status mapping ───
44
+
45
+ {
46
+ name: 'status-active',
47
+ async run({ assert }) {
48
+ const result = toUnifiedSubscription({ status: 'ACTIVE' });
49
+ assert.equal(result.status, 'active', 'PayPal ACTIVE → unified active');
50
+ },
51
+ },
52
+
53
+ {
54
+ name: 'status-approved',
55
+ async run({ assert }) {
56
+ const result = toUnifiedSubscription({ status: 'APPROVED' });
57
+ assert.equal(result.status, 'active', 'PayPal APPROVED → unified active');
58
+ },
59
+ },
60
+
61
+ {
62
+ name: 'status-suspended',
63
+ async run({ assert }) {
64
+ const result = toUnifiedSubscription({ status: 'SUSPENDED' });
65
+ assert.equal(result.status, 'suspended', 'PayPal SUSPENDED → unified suspended');
66
+ },
67
+ },
68
+
69
+ {
70
+ name: 'status-cancelled',
71
+ async run({ assert }) {
72
+ const result = toUnifiedSubscription({ status: 'CANCELLED' });
73
+ assert.equal(result.status, 'cancelled', 'PayPal CANCELLED → unified cancelled');
74
+ },
75
+ },
76
+
77
+ {
78
+ name: 'status-expired',
79
+ async run({ assert }) {
80
+ const result = toUnifiedSubscription({ status: 'EXPIRED' });
81
+ assert.equal(result.status, 'cancelled', 'PayPal EXPIRED → unified cancelled');
82
+ },
83
+ },
84
+
85
+ {
86
+ name: 'status-approval-pending',
87
+ async run({ assert }) {
88
+ const result = toUnifiedSubscription({ status: 'APPROVAL_PENDING' });
89
+ assert.equal(result.status, 'cancelled', 'PayPal APPROVAL_PENDING → unified cancelled');
90
+ },
91
+ },
92
+
93
+ {
94
+ name: 'status-unknown-defaults-to-cancelled',
95
+ async run({ assert }) {
96
+ const result = toUnifiedSubscription({ status: 'SOME_FUTURE_STATUS' });
97
+ assert.equal(result.status, 'cancelled', 'Unknown status → cancelled');
98
+ },
99
+ },
100
+
101
+ // ─── Product resolution ───
102
+
103
+ {
104
+ name: 'product-resolves-from-plan-product-id',
105
+ async run({ assert }) {
106
+ const result = toUnifiedSubscription({
107
+ _plan: { product_id: 'PROD-plus' },
108
+ });
109
+ assert.equal(result.product.id, 'plus', 'Should resolve to plus');
110
+ assert.equal(result.product.name, 'Plus', 'Should have correct name');
111
+ },
112
+ },
113
+
114
+ {
115
+ name: 'product-resolves-pro-from-plan-product-id',
116
+ async run({ assert }) {
117
+ const result = toUnifiedSubscription({
118
+ _plan: { product_id: 'PROD-pro' },
119
+ });
120
+ assert.equal(result.product.id, 'pro', 'Should resolve to pro');
121
+ assert.equal(result.product.name, 'Pro', 'Should have correct name');
122
+ },
123
+ },
124
+
125
+ {
126
+ name: 'product-falls-back-to-basic-on-unknown-product',
127
+ async run({ assert }) {
128
+ const result = toUnifiedSubscription({
129
+ _plan: { product_id: 'PROD-nonexistent' },
130
+ });
131
+ assert.equal(result.product.id, 'basic', 'Unknown product → basic');
132
+ assert.equal(result.product.name, 'Basic', 'Unknown product → Basic name');
133
+ },
134
+ },
135
+
136
+ {
137
+ name: 'product-falls-back-to-basic-on-missing-plan',
138
+ async run({ assert }) {
139
+ const result = toUnifiedSubscription({});
140
+ assert.equal(result.product.id, 'basic', 'No _plan → basic');
141
+ },
142
+ },
143
+
144
+ {
145
+ name: 'product-falls-back-to-basic-without-config',
146
+ async run({ assert }) {
147
+ const result = PayPal.toUnifiedSubscription(
148
+ { _plan: { product_id: 'PROD-plus' } },
149
+ {},
150
+ );
151
+ assert.equal(result.product.id, 'basic', 'No config → basic');
152
+ },
153
+ },
154
+
155
+ // ─── Frequency resolution ───
156
+
157
+ {
158
+ name: 'frequency-month-from-plan',
159
+ async run({ assert }) {
160
+ const result = toUnifiedSubscription({
161
+ _plan: {
162
+ billing_cycles: [
163
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
164
+ ],
165
+ },
166
+ });
167
+ assert.equal(result.payment.frequency, 'monthly', 'MONTH → monthly');
168
+ },
169
+ },
170
+
171
+ {
172
+ name: 'frequency-year-from-plan',
173
+ async run({ assert }) {
174
+ const result = toUnifiedSubscription({
175
+ _plan: {
176
+ billing_cycles: [
177
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'YEAR', interval_count: 1 } },
178
+ ],
179
+ },
180
+ });
181
+ assert.equal(result.payment.frequency, 'annually', 'YEAR → annually');
182
+ },
183
+ },
184
+
185
+ {
186
+ name: 'frequency-week-from-plan',
187
+ async run({ assert }) {
188
+ const result = toUnifiedSubscription({
189
+ _plan: {
190
+ billing_cycles: [
191
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'WEEK', interval_count: 1 } },
192
+ ],
193
+ },
194
+ });
195
+ assert.equal(result.payment.frequency, 'weekly', 'WEEK → weekly');
196
+ },
197
+ },
198
+
199
+ {
200
+ name: 'frequency-day-from-plan',
201
+ async run({ assert }) {
202
+ const result = toUnifiedSubscription({
203
+ _plan: {
204
+ billing_cycles: [
205
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'DAY', interval_count: 1 } },
206
+ ],
207
+ },
208
+ });
209
+ assert.equal(result.payment.frequency, 'daily', 'DAY → daily');
210
+ },
211
+ },
212
+
213
+ {
214
+ name: 'frequency-null-when-missing',
215
+ async run({ assert }) {
216
+ const result = toUnifiedSubscription({});
217
+ assert.equal(result.payment.frequency, null, 'Missing plan → null');
218
+ },
219
+ },
220
+
221
+ {
222
+ name: 'frequency-from-inline-plan',
223
+ async run({ assert }) {
224
+ // PayPal ?fields=plan returns plan inline
225
+ const result = toUnifiedSubscription({
226
+ plan: {
227
+ billing_cycles: [
228
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'YEAR', interval_count: 1 } },
229
+ ],
230
+ },
231
+ });
232
+ assert.equal(result.payment.frequency, 'annually', 'Inline plan year → annually');
233
+ },
234
+ },
235
+
236
+ {
237
+ name: 'frequency-prefers-plan-over-inline',
238
+ async run({ assert }) {
239
+ const result = toUnifiedSubscription({
240
+ _plan: {
241
+ billing_cycles: [
242
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
243
+ ],
244
+ },
245
+ plan: {
246
+ billing_cycles: [
247
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'YEAR', interval_count: 1 } },
248
+ ],
249
+ },
250
+ });
251
+ assert.equal(result.payment.frequency, 'monthly', '_plan takes priority over inline plan');
252
+ },
253
+ },
254
+
255
+ // ─── Trial resolution ───
256
+
257
+ {
258
+ name: 'trial-claimed-when-plan-has-trial-cycle',
259
+ async run({ assert }) {
260
+ const result = toUnifiedSubscription({
261
+ start_time: '2024-01-01T00:00:00Z',
262
+ _plan: {
263
+ billing_cycles: [
264
+ {
265
+ tenure_type: 'TRIAL',
266
+ frequency: { interval_unit: 'DAY', interval_count: 1 },
267
+ total_cycles: 14,
268
+ },
269
+ {
270
+ tenure_type: 'REGULAR',
271
+ frequency: { interval_unit: 'MONTH', interval_count: 1 },
272
+ },
273
+ ],
274
+ },
275
+ });
276
+ assert.equal(result.trial.claimed, true, 'Plan with trial cycle → claimed');
277
+ assert.ok(result.trial.expires.timestampUNIX > 0, 'Trial expires should be set');
278
+ },
279
+ },
280
+
281
+ {
282
+ name: 'trial-not-claimed-when-no-trial-cycle',
283
+ async run({ assert }) {
284
+ const result = toUnifiedSubscription({
285
+ _plan: {
286
+ billing_cycles: [
287
+ {
288
+ tenure_type: 'REGULAR',
289
+ frequency: { interval_unit: 'MONTH', interval_count: 1 },
290
+ },
291
+ ],
292
+ },
293
+ });
294
+ assert.equal(result.trial.claimed, false, 'No trial cycle → not claimed');
295
+ },
296
+ },
297
+
298
+ {
299
+ name: 'trial-not-claimed-when-no-plan',
300
+ async run({ assert }) {
301
+ const result = toUnifiedSubscription({});
302
+ assert.equal(result.trial.claimed, false, 'No plan → not claimed');
303
+ },
304
+ },
305
+
306
+ {
307
+ name: 'trial-claimed-with-no-start-time-has-epoch-expiry',
308
+ async run({ assert }) {
309
+ const result = toUnifiedSubscription({
310
+ _plan: {
311
+ billing_cycles: [
312
+ {
313
+ tenure_type: 'TRIAL',
314
+ frequency: { interval_unit: 'DAY', interval_count: 1 },
315
+ total_cycles: 7,
316
+ },
317
+ ],
318
+ },
319
+ });
320
+ assert.equal(result.trial.claimed, true, 'Trial cycle exists → claimed');
321
+ assert.equal(result.trial.expires.timestampUNIX, 0, 'No start_time → epoch expiry');
322
+ },
323
+ },
324
+
325
+ {
326
+ name: 'trial-month-based-cycle',
327
+ async run({ assert }) {
328
+ const result = toUnifiedSubscription({
329
+ start_time: '2024-01-01T00:00:00Z',
330
+ _plan: {
331
+ billing_cycles: [
332
+ {
333
+ tenure_type: 'TRIAL',
334
+ frequency: { interval_unit: 'MONTH', interval_count: 1 },
335
+ total_cycles: 1,
336
+ },
337
+ ],
338
+ },
339
+ });
340
+ assert.equal(result.trial.claimed, true, 'Monthly trial → claimed');
341
+ assert.ok(result.trial.expires.timestampUNIX > 0, 'Should have computed trial end');
342
+ },
343
+ },
344
+
345
+ // ─── Cancellation resolution ───
346
+
347
+ {
348
+ name: 'cancellation-when-status-cancelled',
349
+ async run({ assert }) {
350
+ const result = toUnifiedSubscription({
351
+ status: 'CANCELLED',
352
+ status_update_time: '2024-06-15T12:00:00Z',
353
+ });
354
+ assert.equal(result.cancellation.pending, false, 'Already cancelled → not pending');
355
+ assert.ok(result.cancellation.date.timestampUNIX > 0, 'Should have cancellation date');
356
+ },
357
+ },
358
+
359
+ {
360
+ name: 'cancellation-cancelled-no-update-time',
361
+ async run({ assert }) {
362
+ const result = toUnifiedSubscription({
363
+ status: 'CANCELLED',
364
+ });
365
+ assert.equal(result.cancellation.pending, false, 'Already cancelled → not pending');
366
+ assert.equal(result.cancellation.date.timestampUNIX, 0, 'No update time → epoch');
367
+ },
368
+ },
369
+
370
+ {
371
+ name: 'cancellation-none-when-active',
372
+ async run({ assert }) {
373
+ const result = toUnifiedSubscription({
374
+ status: 'ACTIVE',
375
+ });
376
+ assert.equal(result.cancellation.pending, false, 'Active → not pending');
377
+ assert.equal(result.cancellation.date.timestampUNIX, 0, 'No cancellation date');
378
+ },
379
+ },
380
+
381
+ // ─── Expiration resolution ───
382
+
383
+ {
384
+ name: 'expires-from-next-billing-time',
385
+ async run({ assert }) {
386
+ const result = toUnifiedSubscription({
387
+ billing_info: { next_billing_time: '2024-07-01T00:00:00Z' },
388
+ });
389
+ assert.ok(result.expires.timestampUNIX > 0, 'Should have expiration');
390
+ },
391
+ },
392
+
393
+ {
394
+ name: 'expires-defaults-to-epoch-when-missing',
395
+ async run({ assert }) {
396
+ const result = toUnifiedSubscription({});
397
+ assert.equal(result.expires.timestampUNIX, 0, 'Missing billing_info → epoch');
398
+ },
399
+ },
400
+
401
+ // ─── Start date resolution ───
402
+
403
+ {
404
+ name: 'start-date-from-start-time',
405
+ async run({ assert }) {
406
+ const result = toUnifiedSubscription({ start_time: '2024-01-15T00:00:00Z' });
407
+ assert.ok(result.payment.startDate.timestampUNIX > 0, 'Should have start date from start_time');
408
+ },
409
+ },
410
+
411
+ {
412
+ name: 'start-date-from-create-time-fallback',
413
+ async run({ assert }) {
414
+ const result = toUnifiedSubscription({ create_time: '2024-01-10T00:00:00Z' });
415
+ assert.ok(result.payment.startDate.timestampUNIX > 0, 'Should have start date from create_time');
416
+ },
417
+ },
418
+
419
+ {
420
+ name: 'start-date-defaults-to-epoch-when-missing',
421
+ async run({ assert }) {
422
+ const result = toUnifiedSubscription({});
423
+ assert.equal(result.payment.startDate.timestampUNIX, 0, 'Missing start_time → epoch');
424
+ },
425
+ },
426
+
427
+ // ─── Payment metadata ───
428
+
429
+ {
430
+ name: 'payment-processor-always-paypal',
431
+ async run({ assert }) {
432
+ const result = toUnifiedSubscription({});
433
+ assert.equal(result.payment.processor, 'paypal', 'Processor should always be paypal');
434
+ },
435
+ },
436
+
437
+ {
438
+ name: 'payment-resource-id-from-subscription-id',
439
+ async run({ assert }) {
440
+ const result = toUnifiedSubscription({ id: 'I-ABC123' });
441
+ assert.equal(result.payment.resourceId, 'I-ABC123', 'resourceId should be subscription ID');
442
+ },
443
+ },
444
+
445
+ {
446
+ name: 'payment-resource-id-null-when-missing',
447
+ async run({ assert }) {
448
+ const result = toUnifiedSubscription({});
449
+ assert.equal(result.payment.resourceId, null, 'Missing ID → null resourceId');
450
+ },
451
+ },
452
+
453
+ {
454
+ name: 'payment-order-id-from-custom-id',
455
+ async run({ assert }) {
456
+ const result = toUnifiedSubscription({ custom_id: 'uid:user-abc,orderId:1234-5678' });
457
+ assert.equal(result.payment.orderId, '1234-5678', 'orderId should come from custom_id');
458
+ },
459
+ },
460
+
461
+ {
462
+ name: 'payment-order-id-null-when-missing',
463
+ async run({ assert }) {
464
+ const result = toUnifiedSubscription({});
465
+ assert.equal(result.payment.orderId, null, 'Missing custom_id → null orderId');
466
+ },
467
+ },
468
+
469
+ {
470
+ name: 'payment-event-metadata-passed-through',
471
+ async run({ assert }) {
472
+ const result = toUnifiedSubscription({}, { eventName: 'BILLING.SUBSCRIPTION.ACTIVATED', eventId: 'WH-123' });
473
+ assert.equal(result.payment.updatedBy.event.name, 'BILLING.SUBSCRIPTION.ACTIVATED', 'Event name passed through');
474
+ assert.equal(result.payment.updatedBy.event.id, 'WH-123', 'Event ID passed through');
475
+ },
476
+ },
477
+
478
+ {
479
+ name: 'payment-event-metadata-null-when-missing',
480
+ async run({ assert }) {
481
+ const result = toUnifiedSubscription({});
482
+ assert.equal(result.payment.updatedBy.event.name, null, 'Missing event name → null');
483
+ assert.equal(result.payment.updatedBy.event.id, null, 'Missing event ID → null');
484
+ },
485
+ },
486
+
487
+ // ─── Price resolution ───
488
+
489
+ {
490
+ name: 'price-resolves-from-config',
491
+ async run({ assert }) {
492
+ const result = toUnifiedSubscription({
493
+ _plan: {
494
+ product_id: 'PROD-plus',
495
+ billing_cycles: [
496
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
497
+ ],
498
+ },
499
+ });
500
+ assert.equal(result.payment.price, 9.99, 'Price should match config for plus/monthly');
501
+ },
502
+ },
503
+
504
+ {
505
+ name: 'price-resolves-annually',
506
+ async run({ assert }) {
507
+ const result = toUnifiedSubscription({
508
+ _plan: {
509
+ product_id: 'PROD-pro',
510
+ billing_cycles: [
511
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'YEAR', interval_count: 1 } },
512
+ ],
513
+ },
514
+ });
515
+ assert.equal(result.payment.price, 299.99, 'Price should match config for pro/annually');
516
+ },
517
+ },
518
+
519
+ {
520
+ name: 'price-zero-when-product-unknown',
521
+ async run({ assert }) {
522
+ const result = toUnifiedSubscription({
523
+ _plan: { product_id: 'PROD-nonexistent' },
524
+ });
525
+ assert.equal(result.payment.price, 0, 'Unknown product → price 0');
526
+ },
527
+ },
528
+
529
+ // ─── Custom ID parsing ───
530
+
531
+ {
532
+ name: 'custom-id-parses-uid-and-order-id',
533
+ async run({ assert }) {
534
+ const result = toUnifiedSubscription({
535
+ custom_id: 'uid:user-123,orderId:ord-456',
536
+ });
537
+ assert.equal(result.payment.orderId, 'ord-456', 'orderId from custom_id');
538
+ },
539
+ },
540
+
541
+ {
542
+ name: 'custom-id-handles-colons-in-values',
543
+ async run({ assert }) {
544
+ const result = toUnifiedSubscription({
545
+ custom_id: 'uid:user:with:colons,orderId:ord-789',
546
+ });
547
+ assert.equal(result.payment.orderId, 'ord-789', 'orderId parsed correctly');
548
+ },
549
+ },
550
+
551
+ {
552
+ name: 'custom-id-handles-empty-string',
553
+ async run({ assert }) {
554
+ const result = toUnifiedSubscription({ custom_id: '' });
555
+ assert.equal(result.payment.orderId, null, 'Empty custom_id → null');
556
+ },
557
+ },
558
+
559
+ // ─── Full unified shape ───
560
+
561
+ {
562
+ name: 'full-active-subscription-shape',
563
+ async run({ assert }) {
564
+ const result = toUnifiedSubscription({
565
+ id: 'I-FULL-TEST',
566
+ status: 'ACTIVE',
567
+ custom_id: 'uid:user-full,orderId:ord-full',
568
+ start_time: '2024-01-01T00:00:00Z',
569
+ billing_info: { next_billing_time: '2024-02-01T00:00:00Z' },
570
+ _plan: {
571
+ product_id: 'PROD-pro',
572
+ billing_cycles: [
573
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
574
+ ],
575
+ },
576
+ }, { eventName: 'BILLING.SUBSCRIPTION.ACTIVATED', eventId: 'WH-FULL' });
577
+
578
+ // Verify all top-level keys exist
579
+ assert.ok(result.product, 'Should have product');
580
+ assert.ok(result.status, 'Should have status');
581
+ assert.ok(result.expires, 'Should have expires');
582
+ assert.ok(result.trial, 'Should have trial');
583
+ assert.ok(result.cancellation, 'Should have cancellation');
584
+ assert.ok(result.payment, 'Should have payment');
585
+
586
+ // Verify values
587
+ assert.equal(result.product.id, 'pro', 'Product should be pro');
588
+ assert.equal(result.status, 'active', 'Status should be active');
589
+ assert.equal(result.trial.claimed, false, 'Trial should not be claimed');
590
+ assert.equal(result.cancellation.pending, false, 'Should not be pending cancellation');
591
+ assert.equal(result.payment.processor, 'paypal', 'Processor should be paypal');
592
+ assert.equal(result.payment.resourceId, 'I-FULL-TEST', 'Resource ID should match');
593
+ assert.equal(result.payment.frequency, 'monthly', 'Frequency should be monthly');
594
+ assert.equal(result.payment.orderId, 'ord-full', 'Order ID should match');
595
+ assert.equal(result.payment.updatedBy.event.name, 'BILLING.SUBSCRIPTION.ACTIVATED', 'Event name should match');
596
+ },
597
+ },
598
+
599
+ {
600
+ name: 'empty-subscription-gets-safe-defaults',
601
+ async run({ assert }) {
602
+ const result = toUnifiedSubscription({});
603
+
604
+ assert.equal(result.product.id, 'basic', 'Empty → basic product');
605
+ assert.equal(result.status, 'cancelled', 'Empty → cancelled (no status field)');
606
+ assert.equal(result.trial.claimed, false, 'Empty → trial not claimed');
607
+ assert.equal(result.cancellation.pending, false, 'Empty → not pending');
608
+ assert.equal(result.payment.processor, 'paypal', 'Empty → still paypal');
609
+ assert.equal(result.payment.orderId, null, 'Empty → null orderId');
610
+ assert.equal(result.payment.resourceId, null, 'Empty → null resourceId');
611
+ assert.equal(result.payment.frequency, null, 'Empty → null frequency');
612
+ },
613
+ },
614
+
615
+ // ─── Combination / edge-case scenarios ───
616
+
617
+ {
618
+ name: 'combo-active-with-trial',
619
+ async run({ assert }) {
620
+ const result = toUnifiedSubscription({
621
+ id: 'I-TRIAL-ACTIVE',
622
+ status: 'ACTIVE',
623
+ start_time: '2024-01-01T00:00:00Z',
624
+ _plan: {
625
+ product_id: 'PROD-plus',
626
+ billing_cycles: [
627
+ {
628
+ tenure_type: 'TRIAL',
629
+ frequency: { interval_unit: 'DAY', interval_count: 1 },
630
+ total_cycles: 14,
631
+ },
632
+ {
633
+ tenure_type: 'REGULAR',
634
+ frequency: { interval_unit: 'MONTH', interval_count: 1 },
635
+ },
636
+ ],
637
+ },
638
+ });
639
+
640
+ assert.equal(result.status, 'active', 'Active with trial → active');
641
+ assert.equal(result.trial.claimed, true, 'Trial should be claimed');
642
+ assert.equal(result.product.id, 'plus', 'Should resolve product');
643
+ assert.equal(result.payment.frequency, 'monthly', 'Regular cycle → monthly');
644
+ },
645
+ },
646
+
647
+ {
648
+ name: 'combo-suspended-subscription',
649
+ async run({ assert }) {
650
+ const result = toUnifiedSubscription({
651
+ id: 'I-SUSPENDED',
652
+ status: 'SUSPENDED',
653
+ custom_id: 'uid:user-sus,orderId:ord-sus',
654
+ _plan: {
655
+ product_id: 'PROD-pro',
656
+ billing_cycles: [
657
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'YEAR', interval_count: 1 } },
658
+ ],
659
+ },
660
+ });
661
+
662
+ assert.equal(result.status, 'suspended', 'SUSPENDED → suspended');
663
+ assert.equal(result.product.id, 'pro', 'Should resolve product');
664
+ assert.equal(result.payment.frequency, 'annually', 'Year → annually');
665
+ assert.equal(result.cancellation.pending, false, 'Not cancelled, just suspended');
666
+ },
667
+ },
668
+
669
+ {
670
+ name: 'combo-cancelled-with-update-time',
671
+ async run({ assert }) {
672
+ const result = toUnifiedSubscription({
673
+ id: 'I-CANCEL',
674
+ status: 'CANCELLED',
675
+ status_update_time: '2024-06-15T18:30:00Z',
676
+ custom_id: 'uid:user-cancel,orderId:ord-cancel',
677
+ _plan: {
678
+ product_id: 'PROD-plus',
679
+ billing_cycles: [
680
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
681
+ ],
682
+ },
683
+ });
684
+
685
+ assert.equal(result.status, 'cancelled', 'CANCELLED → cancelled');
686
+ assert.equal(result.cancellation.pending, false, 'Already cancelled → not pending');
687
+ assert.ok(result.cancellation.date.timestampUNIX > 0, 'Should have cancellation date');
688
+ assert.equal(result.payment.orderId, 'ord-cancel', 'Order ID preserved');
689
+ },
690
+ },
691
+
692
+ {
693
+ name: 'combo-expired-subscription',
694
+ async run({ assert }) {
695
+ const result = toUnifiedSubscription({
696
+ id: 'I-EXPIRED',
697
+ status: 'EXPIRED',
698
+ _plan: {
699
+ product_id: 'PROD-pro',
700
+ billing_cycles: [
701
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
702
+ ],
703
+ },
704
+ });
705
+
706
+ assert.equal(result.status, 'cancelled', 'EXPIRED → cancelled');
707
+ assert.equal(result.product.id, 'pro', 'Should still resolve product');
708
+ },
709
+ },
710
+
711
+ // ─── Unified shape matches Stripe output ───
712
+
713
+ {
714
+ name: 'shape-matches-stripe-output-keys',
715
+ async run({ assert }) {
716
+ const result = toUnifiedSubscription({
717
+ id: 'I-SHAPE',
718
+ status: 'ACTIVE',
719
+ custom_id: 'uid:u1,orderId:o1',
720
+ start_time: '2024-01-01T00:00:00Z',
721
+ billing_info: { next_billing_time: '2024-02-01T00:00:00Z' },
722
+ _plan: {
723
+ product_id: 'PROD-plus',
724
+ billing_cycles: [
725
+ { tenure_type: 'REGULAR', frequency: { interval_unit: 'MONTH', interval_count: 1 } },
726
+ ],
727
+ },
728
+ });
729
+
730
+ // Top-level keys
731
+ const topKeys = Object.keys(result).sort();
732
+ assert.deepEqual(topKeys, ['cancellation', 'expires', 'payment', 'product', 'status', 'trial'], 'Should have same top-level keys as Stripe unified');
733
+
734
+ // Product shape
735
+ assert.ok(result.product.id, 'product.id exists');
736
+ assert.ok(result.product.name, 'product.name exists');
737
+
738
+ // Expires shape
739
+ assert.isType(result.expires.timestamp, 'string', 'expires.timestamp is string');
740
+ assert.isType(result.expires.timestampUNIX, 'number', 'expires.timestampUNIX is number');
741
+
742
+ // Trial shape
743
+ assert.isType(result.trial.claimed, 'boolean', 'trial.claimed is boolean');
744
+ assert.ok(result.trial.expires, 'trial.expires exists');
745
+
746
+ // Cancellation shape
747
+ assert.isType(result.cancellation.pending, 'boolean', 'cancellation.pending is boolean');
748
+ assert.ok(result.cancellation.date, 'cancellation.date exists');
749
+
750
+ // Payment shape
751
+ assert.equal(result.payment.processor, 'paypal', 'payment.processor');
752
+ assert.ok('orderId' in result.payment, 'payment.orderId exists');
753
+ assert.ok('resourceId' in result.payment, 'payment.resourceId exists');
754
+ assert.ok('frequency' in result.payment, 'payment.frequency exists');
755
+ assert.ok('price' in result.payment, 'payment.price exists');
756
+ assert.ok(result.payment.startDate, 'payment.startDate exists');
757
+ assert.ok(result.payment.updatedBy, 'payment.updatedBy exists');
758
+ assert.ok(result.payment.updatedBy.event, 'payment.updatedBy.event exists');
759
+ assert.ok(result.payment.updatedBy.date, 'payment.updatedBy.date exists');
760
+ },
761
+ },
762
+
763
+ // ─── Real PayPal sandbox fixtures ───
764
+
765
+ {
766
+ name: 'fixture-active-produces-valid-shape',
767
+ async run({ assert }) {
768
+ const result = toUnifiedSubscription(FIXTURE_ACTIVE);
769
+
770
+ assert.equal(result.status, 'active', 'ACTIVE fixture → active');
771
+ assert.equal(result.payment.processor, 'paypal', 'Processor is paypal');
772
+ assert.equal(result.payment.resourceId, 'I-MTPRX0B9LV4R', 'Resource ID from fixture');
773
+ assert.equal(result.payment.orderId, 'ord-sub-123', 'orderId from custom_id');
774
+ assert.isType(result.expires.timestamp, 'string', 'expires.timestamp is string');
775
+ assert.isType(result.expires.timestampUNIX, 'number', 'expires.timestampUNIX is number');
776
+ assert.equal(result.trial.claimed, false, 'No trial in fixture');
777
+ assert.equal(result.cancellation.pending, false, 'Not cancelled');
778
+ },
779
+ },
780
+
781
+ {
782
+ name: 'fixture-cancelled-produces-valid-shape',
783
+ async run({ assert }) {
784
+ const result = toUnifiedSubscription(FIXTURE_CANCELLED);
785
+
786
+ assert.equal(result.status, 'cancelled', 'CANCELLED fixture → cancelled');
787
+ assert.equal(result.payment.resourceId, 'I-MTPRX0B9LV4R', 'Resource ID from fixture');
788
+ assert.equal(result.payment.orderId, 'ord-sub-123', 'orderId from custom_id');
789
+ assert.equal(result.cancellation.pending, false, 'Already cancelled');
790
+ },
791
+ },
792
+
793
+ {
794
+ name: 'fixture-suspended-produces-valid-shape',
795
+ async run({ assert }) {
796
+ const result = toUnifiedSubscription(FIXTURE_SUSPENDED);
797
+
798
+ assert.equal(result.status, 'suspended', 'SUSPENDED fixture → suspended');
799
+ assert.equal(result.payment.resourceId, 'I-MTPRX0B9LV4R', 'Resource ID from fixture');
800
+ assert.equal(result.cancellation.pending, false, 'Not cancelled, just suspended');
801
+ },
802
+ },
803
+
804
+ {
805
+ name: 'fixture-all-produce-valid-unified-keys',
806
+ async run({ assert }) {
807
+ const fixtures = [FIXTURE_ACTIVE, FIXTURE_CANCELLED, FIXTURE_SUSPENDED];
808
+ const expectedKeys = ['cancellation', 'expires', 'payment', 'product', 'status', 'trial'];
809
+
810
+ for (let i = 0; i < fixtures.length; i++) {
811
+ const result = toUnifiedSubscription(fixtures[i]);
812
+ const keys = Object.keys(result).sort();
813
+ assert.deepEqual(keys, expectedKeys, `Fixture ${i} should have correct top-level keys`);
814
+ assert.isType(result.payment.updatedBy.date.timestamp, 'string', `Fixture ${i}: updatedBy.date.timestamp is string`);
815
+ assert.isType(result.payment.updatedBy.date.timestampUNIX, 'number', `Fixture ${i}: updatedBy.date.timestampUNIX is number`);
816
+ }
817
+ },
818
+ },
819
+ ],
820
+ };