-
Notifications
You must be signed in to change notification settings - Fork 138
/
subscription.test.js
654 lines (616 loc) · 21.8 KB
/
subscription.test.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import assert from 'assert';
import { initRecurly } from '../../support/helpers';
describe('Recurly.Pricing.Subscription', function () {
beforeEach(function () {
this.recurly = initRecurly();
this.pricing = this.recurly.Pricing.Subscription();
});
describe('with taxation', () => {
it('should not append tax elements if there is no tax', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.done(function (price) {
assert.equal(price.taxes.length, 0);
assert.equal(price.now.tax, '0.00');
assert.equal(price.next.tax, '0.00');
done();
});
});
it('should not apply tax if plan is tax exempt and not usst', function (done) {
this.pricing
.plan('tax_exempt', { quantity: 1 })
.address({
country: 'GB'
})
.done(function (price) {
assert.equal(price.taxes.length, 0);
assert.equal(price.now.tax, '0.00');
assert.equal(price.next.tax, '0.00');
done();
});
});
it('should apply a giftcard discount to the post tax price', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: '94110' // tax literal for test
})
.giftcard("hundred-dollar-card")
.done(function (price) {
assert.equal(price.now.gift_card, '23.91');
assert.equal(price.next.gift_card, '21.74');
assert.equal(price.now.total, '0.00');
assert.equal(price.next.total, '0.00');
done();
});
});
it('should append US tax elements in the US', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: '94110' // tax literal for test
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
});
it('should append VAT tax elements if in the EU', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'DE'
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'vat');
assert.equal(price.taxes[0].rate, '0.015');
assert.equal(price.now.tax, '0.33');
assert.equal(price.next.tax, '0.30');
done();
});
});
it('should append US state tax, similar to 2015 VAT regulations', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: '94129' // tax literal for test
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.taxes[0].region, 'CA');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
});
it('should append VAT tax elements if in the EU and region for 2015 type rules', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'GB'
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'vat');
assert.equal(price.taxes[0].region, 'GB');
assert.equal(price.taxes[0].rate, '0.2');
assert.equal(price.now.tax, '4.40');
assert.equal(price.next.tax, '4.00');
done();
});
});
it('should append 20 pct. of 49.00 to have a correct tax of $9.80', function (done) {
this.pricing
.plan('free-trial', { quantity: 1 })
.address({
country: 'GB'
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'vat');
assert.equal(price.taxes[0].region, 'GB');
assert.equal(price.taxes[0].rate, '0.2');
assert.equal(price.now.tax, '0.40');
assert.equal(price.next.tax, '9.80');
done();
});
});
describe('with a shipping address', function () {
it('calculates tax from the shipping address', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.shippingAddress({
country: 'US',
postal_code: '94129'
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
});
it('calculates tax from the shipping address when a billing address is given', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'DE',
postal_code: 'XXX-XXX'
})
.shippingAddress({
country: 'US',
postal_code: '94129'
})
.done(function (price) {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
})
});
describe('given specific tax amounts', () => {
it('requires the now and next amounts be given as finite numbers', function () {
assert.throws(() => this.pricing.tax({ amount: 'invalid' }), /Invalid 'amount'/);
assert.throws(() => this.pricing.tax({ amount: { now: 'invalid' } }), /Invalid 'amount.now'/);
assert.throws(() => this.pricing.tax({ amount: { now: 20 } }), /Invalid 'amount.next'/);
assert.throws(() => this.pricing.tax({ amount: { now: 20, next: 'invalid' } }), /Invalid 'amount.next'/);
});
it('applies the specific tax amounts as provided, ignoring built-in tax calculations', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: '94129'
})
.tax({
amount: {
now: 20,
next: 10
}
})
.done(price => {
assert.strictEqual(this.pricing.items.tax.amount.now, 20);
assert.strictEqual(this.pricing.items.tax.amount.next, 10);
assert.strictEqual(price.now.tax, '20.00');
assert.strictEqual(price.next.tax, '10.00');
done();
});
});
});
});
describe('with addons', () => {
describe('with fixed addons', () => {
it('updates the price accordingly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf')
.done(price => {
assert.equal(price.now.addons, '1.00');
assert.equal(price.next.addons, '1.00');
assert.equal(price.now.total, '22.99');
assert.equal(price.next.total, '20.99');
done();
});
});
describe('when an addon quantity is updated', () => {
it('updates the quantity and price accordingly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf', { quantity: 1 })
.then(() => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 1);
})
.addon('snarf', { quantity: 2 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 2);
assert.equal(price.now.addons, '2.00');
assert.equal(price.next.addons, '2.00');
assert.equal(price.now.total, '23.99');
assert.equal(price.next.total, '21.99');
done();
});
});
});
describe('when an addon quantity is updated to zero', () => {
it('removes the addon from the pricing instance', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf', { quantity: 2 })
.then(() => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 2);
})
.addon('snarf', { quantity: 0 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 0);
assert.equal(price.now.addons, '0.00');
assert.equal(price.next.addons, '0.00');
assert.equal(price.now.total, '21.99');
assert.equal(price.next.total, '19.99');
done();
});
});
});
describe('when the plan quantity is zero', () => {
it('calculates to zero', function (done) {
this.pricing
.plan('basic', { quantity: 0 })
.addon('snarf', { quantity: 2 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'snarf');
assert.equal(this.pricing.items.addons[0].quantity, 2);
assert.equal(price.now.addons, '0.00');
assert.equal(price.next.addons, '0.00');
assert.equal(price.now.total, '0.00');
assert.equal(price.next.total, '0.00');
done();
});
});
});
});
describe('with usage addons', () => {
it('should not apply the usage cost to the price', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.addon('snarf')
.addon('with_usage')
.done(function (price) {
assert.equal(price.now.addons, '1.00');
assert.equal(price.next.addons, '1.00');
assert.equal(price.now.total, '22.99');
assert.equal(price.next.total, '20.99');
done();
});
});
});
describe('with tiered addons', () => {
it('should apply the tiered cost to the addon price', function (done) {
this.pricing
.plan('tiered-plan', { quantity: 1 })
.addon('tiered')
.then(() => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'tiered');
assert.equal(this.pricing.items.addons[0].quantity, 1);
})
.addon('tiered', { quantity: 6 })
.done(price => {
assert.equal(this.pricing.items.addons.length, 1);
assert.equal(this.pricing.items.addons[0].code, 'tiered');
assert.equal(this.pricing.items.addons[0].quantity, 6);
// 3 * 2 + 2 * 4 = 14
assert.strictEqual(price.now.addons, '14.00');
assert.strictEqual(price.next.addons, '14.00');
assert.strictEqual(price.now.total, '35.99');
assert.strictEqual(price.next.total, '33.99');
done();
});
});
});
});
describe('with gift cards', function () {
it('should apply a valid gift card correctly to price.now', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard("super-gift-card")
.done(function (price) {
assert.equal(price.now.total, '1.99');
assert.equal(price.next.total, '19.99');
done();
});
});
it('should return the value of gift card used in price.now and price.next', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard("super-gift-card")
.done(function (price) {
assert.equal(price.now.gift_card, '20.00');
assert.equal(price.next.gift_card, '0.00');
done();
});
});
it('should use leftover gift card balance in price.next', function (done) {
this.pricing
.plan('basic', { quantity: 4 })
.address({
country: 'US'
})
.giftcard("hundred-dollar-card")
.done(function (price) {
assert.equal(price.now.gift_card, '81.96');
assert.equal(price.next.gift_card, '18.04');
assert.equal(price.now.total, '0.00');
assert.equal(price.next.total, '61.92');
done();
});
});
it('should never set a total lower than 0.00', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard("hundred-dollar-card")
.done(function (price) {
assert.equal(price.now.gift_card, '21.99');
assert.equal(price.next.gift_card, '19.99');
assert.equal(price.now.total, '0.00');
assert.equal(price.next.total, '0.00');
done();
});
});
it('emits an error event when a gift card is not found', function (done) {
this.pricing
.on('error.gift_card', function (err) {
assert(err.code === 'not-found');
done();
})
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard('invalid');
});
it('emits an event when the gift card is set', function (done) {
this.pricing
.on('set.gift_card', function (giftcard) {
assert(giftcard.currency === 'USD');
assert(giftcard.unit_amount === 20);
done();
})
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard('super-gift-card');
});
it('emits an event when the gift card is unset', function (done) {
this.pricing
.on('unset.gift_card', function () {
done();
})
.plan('basic', { quantity: 1 })
.address({
country: 'US'
})
.giftcard('australian-card');
});
it('emits an error event when the giftcard currency doesnt match the config currency', function (done) {
this.pricing
.on('error.gift_card', function (err) {
assert(err.code === 'gift-card-currency-mismatch');
done();
})
.plan('basic', { quantity: 1 })
.address({
country: 'AUD' // I set AUD here because it's the config currency not address currency that matters. config currency is USD.
})
.giftcard('australian-card');
});
it('emits an unset event when a giftcard is cleared and removes giftcard from the pricing', function (done) {
let emitted = false;
this.pricing
.on('unset.gift_card', () => emitted = true)
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.giftcard('super-gift-card')
.then((giftcard) => assert(giftcard.unit_amount === 20))
.giftcard('')
.then((giftcard) => assert(!giftcard))
.done(function (price) {
assert.equal(price.now.gift_card, undefined);
assert.equal(price.now.total,"21.99");
assert(emitted, true);
done();
});
});
});
describe('with coupons', function () {
it('should apply a multi-use coupon correctly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop')
.done(function (price) {
assert.equal(price.now.discount, '20.00');
assert.equal(price.next.discount, '19.99');
done();
});
});
it('should apply a single-use coupon correctly', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop-single-use')
.done(function (price) {
assert.equal(price.now.discount, '20.00');
assert.equal(price.next.discount, '0.00');
done();
});
});
it('should apply a trial extension coupon to a plan with a trial period', function (done) {
this.pricing
.plan('free-trial', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop-free-trial')
.done(function (price) {
assert.equal(price.now.plan, '0.00');
assert.equal(price.now.addons, '0.00');
assert.equal(price.now.discount, '0.00');
assert.equal(price.next.discount, '0.00');
done();
});
});
it(`should apply a trial period when a trial extension coupon
is applied to a plan without a trial period`, function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop-free-trial')
.done(function (price) {
assert.equal(price.now.plan, '0.00');
assert.equal(price.now.addons, '0.00');
assert.equal(price.now.discount, '0.00');
assert.equal(price.next.discount, '0.00');
done();
});
});
it('emits an error event when a coupon is not found', function (done) {
this.pricing
.on('error.coupon', function (err) {
assert(err.code === 'not-found');
done();
})
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop-invalid');
});
it('emits an unset event when a coupon is cleared', function (done) {
let emitted = false;
this.pricing
.on('unset.coupon', () => emitted = true)
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop')
.then((coupon) => assert(coupon.code === 'coop'))
.coupon('')
.then((coupon) => assert(!coupon))
.done(function (price) {
assert.equal(price.now.discount, '0.00');
assert(emitted, true);
done();
});
});
it('does nothing when the same coupon is set again', function (done) {
const fail = event => {
assert.fail(`${event} emitted`, `${event} should not be emitted`);
};
this.pricing
.plan('basic', { quantity: 1 })
.address({
country: 'US',
postal_code: 'NoTax'
})
.coupon('coop')
.then(() => {
this.pricing.on('error.coupon', () => fail('error.coupon'));
this.pricing.on('set.coupon', () => fail('set.coupon'));
this.pricing.on('unset.coupon', () => fail('unset.coupon'));
})
.coupon('coop')
.then(coupon => {
assert.equal(coupon.code, 'coop');
assert.equal(this.pricing.items.coupon.code, 'coop');
done();
})
.done();
});
describe('when the plan has a free trial period with a set-up fee', () => {
it('applies single-use discounts to the setup fee', function () {
return this.pricing
.plan('free-trial')
.coupon('coop-single-use')
.reprice()
.then(price => {
assert.strictEqual(price.now.discount, '2.00');
assert.strictEqual(price.next.discount, '0.00');
assert.strictEqual(price.now.total, '0.00');
assert.strictEqual(price.next.total, '49.00');
});
});
});
describe('when the plan has a free trial period and no set-up fee', () => {
it('applies single-use discounts to the price next period', function () {
return this.pricing
.plan('free-trial-no-setup')
.coupon('coop-single-use')
.reprice()
.then(price => {
assert.strictEqual(price.now.discount, '0.00');
assert.strictEqual(price.next.discount, '20.00');
assert.strictEqual(price.now.total, '0.00');
assert.strictEqual(price.next.total, '29.00');
});
});
});
describe('when the plan is changed', () => {
it('removes coupons which are incompatible with the new plan', function (done) {
this.pricing
.plan('basic', { quantity: 1 })
.coupon('coop-pct-plan-basic')
.then(coupon => {
assert.equal(coupon.code, 'coop-pct-plan-basic');
assert.equal(this.pricing.items.coupon.code, 'coop-pct-plan-basic');
})
.reprice()
.then(price => {
assert.equal(price.now.discount, '3.00');
assert.equal(price.next.discount, '3.00');
assert.equal(price.now.total, '18.99');
assert.equal(price.next.total, '16.99');
})
.plan('basic-2', { quantity: 1 })
.done(price => {
assert.equal(this.pricing.items.coupon, undefined);
assert.equal(price.now.discount, '0.00');
assert.equal(price.next.discount, '0.00');
assert.equal(price.now.total, '95.09');
assert.equal(price.next.total, '90.09');
done();
});
});
});
});
});