Skip to content

Commit

Permalink
Adds additional i18n for ApplePay line item labels
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Rogers <[email protected]>
  • Loading branch information
chrissrogers committed Feb 6, 2018
1 parent eb53346 commit 2e69bcd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var staticConfig = {
autoWatch: true,
browsers: [
'PhantomJS'
// 'ChromeDebug'
// 'IE11 - Win7'
],
singleRun: true,
Expand Down
27 changes: 16 additions & 11 deletions lib/recurly/apple-pay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Emitter from 'component-emitter';
import errors from '../errors';
import {Pricing} from './pricing';
import {normalize} from '../util/normalize';
import decimalize from '../util/decimalize';
import {FIELDS} from './token';

const debug = require('debug')('recurly:apple-pay');
Expand All @@ -19,7 +20,11 @@ const APPLE_PAY_ADDRESS_MAP = {
};

const I18N = {
authorizationLineItemLabel: 'Card Authorization (Temporary)'
authorizationLineItemLabel: 'Card Authorization (Temporary)',
subtotalLineItemLabel: 'Subtotal',
discountLineItemLabel: 'Discount',
taxLineItemLabel: 'Tax',
giftCardLineItemLabel: 'Gift card'
};

/**
Expand Down Expand Up @@ -160,6 +165,11 @@ class ApplePay extends Emitter {
// Initialize with no line items
this.config.lineItems = [];

if ('recurly' in options) this.recurly = options.recurly;
else return this.initError = this.error('apple-pay-factory-only');

if ('i18n' in options) Object.assign(this.config.i18n, options.i18n);

// Listen for pricing changes to update totals and currency
const { pricing } = options;
if (pricing instanceof Pricing) {
Expand All @@ -172,11 +182,6 @@ class ApplePay extends Emitter {
return this.initError = this.error('apple-pay-config-missing', { opt: 'total' });
}

if ('recurly' in options) this.recurly = options.recurly;
else return this.initError = this.error('apple-pay-factory-only');

if ('i18n' in options) Object.assign(this.config.i18n, options.i18n);

// Retrieve remote configuration
this.recurly.request('get', '/apple_pay/info', (err, info) => {
if (err) return this.initError = this.error(err);
Expand Down Expand Up @@ -232,18 +237,18 @@ class ApplePay extends Emitter {
if (!pricing.hasPrice) return;
let taxAmount = pricing.price.now.taxes || pricing.price.now.tax;

lineItems.push(lineItem('Subtotal', pricing.subtotalPreDiscountNow));
lineItems.push(lineItem(this.config.i18n.subtotalLineItemLabel, pricing.subtotalPreDiscountNow));

if (+pricing.price.now.discount) {
lineItems.push(lineItem('Discount', -pricing.price.now.discount));
lineItems.push(lineItem(this.config.i18n.discountLineItemLabel, -pricing.price.now.discount));
}

if (+taxAmount) {
lineItems.push(lineItem('Tax', taxAmount));
lineItems.push(lineItem(this.config.i18n.taxLineItemLabel, taxAmount));
}

if (+pricing.price.now.giftCard) {
lineItems.push(lineItem('Gift card', -pricing.price.now.giftCard));
lineItems.push(lineItem(this.config.i18n.giftCardLineItemLabel, -pricing.price.now.giftCard));
}

this.config.lineItems = lineItems;
Expand Down Expand Up @@ -395,5 +400,5 @@ class ApplePay extends Emitter {
* @return {object}
*/
function lineItem (label = '', amount = 0) {
return { label, amount };
return { label, amount: decimalize(amount) };
}
73 changes: 71 additions & 2 deletions test/apple-pay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ apiTest(function (requestMethod) {

describe('when given options.pricing', function () {
beforeEach(function () {
let pricing = this.pricing = this.recurly.Pricing();
this.applePay = this.recurly.ApplePay(merge({}, validOpts, { pricing }))
let pricing = this.pricing = this.recurly.Pricing.Checkout();
this.applePay = this.recurly.ApplePay(merge({}, validOpts, { pricing }));
});

it('binds a pricing instance', function (done) {
Expand All @@ -131,6 +131,75 @@ apiTest(function (requestMethod) {
done();
});
});

describe('when the pricing instance includes several items', () => {
beforeEach(function (done) {
this.subscription = this.recurly.Pricing.Subscription()
.plan('basic')
.address({ country: 'US', postalCode: '94117' })
.done(() => {
this.pricing
.subscription(this.subscription)
.adjustment({ amount: 100 })
.coupon('coop')
.giftCard('super-gift-card')
.done(() => done());
});
});

it('includes relevant line items', function () {
const subtotal = this.applePay.lineItems[0];
const discount = this.applePay.lineItems[1];
const giftCard = this.applePay.lineItems[2];
const total = this.applePay.totalLineItem;
assert.equal(this.applePay.lineItems.length, 3);
assert.strictEqual(subtotal.label, this.applePay.config.i18n.subtotalLineItemLabel);
assert.strictEqual(discount.label, this.applePay.config.i18n.discountLineItemLabel);
assert.strictEqual(giftCard.label, this.applePay.config.i18n.giftCardLineItemLabel);
assert.strictEqual(subtotal.amount, '121.99');
assert.strictEqual(discount.amount, '-20.00');
assert.strictEqual(giftCard.amount, '-20.00');
assert.strictEqual(total.amount, '81.99');
});

describe('when the line item labels are customized', () => {
beforeEach(function () {
const pricing = this.pricing;
const i18n = this.exampleI18n = {
authorizationLineItemLabel: 'Custom card authorization label',
subtotalLineItemLabel: 'Custom subtotal label',
discountLineItemLabel: 'Custom discount label',
taxLineItemLabel: 'Custom tax label',
giftCardLineItemLabel: 'Custom Gift card label'
};
this.applePay = this.recurly.ApplePay(merge({}, validOpts, { pricing, i18n }));
});

it('displays those labels', function () {
const subtotal = this.applePay.lineItems[0];
const discount = this.applePay.lineItems[1];
const giftCard = this.applePay.lineItems[2];
assert.equal(subtotal.label, this.exampleI18n.subtotalLineItemLabel);
assert.equal(discount.label, this.exampleI18n.discountLineItemLabel);
assert.equal(giftCard.label, this.exampleI18n.giftCardLineItemLabel);
});
});

describe('when the total price is zero', () => {
beforeEach(function (done) {
this.pricing.coupon('coop-fixed-all-500').done(() => done());
});

it('adds an authorization line item', function () {
assert.strictEqual(this.applePay.totalLineItem.amount, '0.00');
this.applePay.begin();
const authorization = this.applePay.lineItems[2];
assert.strictEqual(authorization.label, this.applePay.config.i18n.authorizationLineItemLabel);
assert.strictEqual(authorization.amount, '1.00');
assert.strictEqual(this.applePay.totalLineItem.amount, '1.00');
});
});
});
});

it('requires a valid country', function (done) {
Expand Down

0 comments on commit 2e69bcd

Please sign in to comment.