Skip to content

Commit

Permalink
Adds support for shipping address
Browse files Browse the repository at this point in the history
- Adds pricing class support for shipping address
- Adds attachment procedure for shipping address
- Establlishes that shipping address holds precedence for billing address in tax calculations

Signed-off-by: Christopher Rogers <chrissrogers@gmail.com>
  • Loading branch information
chrissrogers committed Oct 6, 2016
1 parent 27b9078 commit 3d03169
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
20 changes: 14 additions & 6 deletions lib/recurly/pricing/attachment.js
Expand Up @@ -59,11 +59,12 @@ export default class Attachment {
const target = event.target || event.srcElement;
const targetName = dom.data(target, 'recurly');
const updating = name => event === INIT_RUN || targetName === name;
const updateAddress = updating('country') || updating('postal_code');
const updateAddon = elems.addon && updating('addon');
const updateAddress = updating('country') || updating('postal_code');
const updateCurrency = updating('currency');
const updateCoupon = elems.coupon && (updating('coupon') || updating('plan'));
const updateGiftcard = elems.gift_card && updating('gift_card');
const updateShippingAddress = updating('shipping_address.country') || updating('shipping_address.postal_code');
const updateTax = updating('vat_number') || updating('tax_code');

let pricing = this.pricing.plan(dom.value(elems.plan), { quantity: dom.value(elems.plan_quantity) });
Expand Down Expand Up @@ -94,11 +95,18 @@ export default class Attachment {
pricing = pricing.giftcard(dom.value(elems.gift_card).trim()).then(null, ignoreNotFound);
}

if (updateAddress) {
pricing = pricing.address({
country: dom.value(elems.country),
postal_code: dom.value(elems.postal_code)
});
if (updateShippingAddress || updateAddress) {
if ('shipping_address.country' in elems || 'shipping_address.postal_code' in elems) {
pricing = pricing.shippingAddress({
country: dom.value(elems['shipping_address.country']),
postal_code: dom.value(elems['shipping_address.postal_code'])
});
} else {
pricing = pricing.address({
country: dom.value(elems.country),
postal_code: dom.value(elems.postal_code)
});
}
}

if (updateTax) {
Expand Down
3 changes: 2 additions & 1 deletion lib/recurly/pricing/calculations.js
Expand Up @@ -93,7 +93,8 @@ Calculations.prototype.tax = function (done) {
this.price.now.tax = 0;
this.price.next.tax = 0;

var taxInfo = merge({}, this.items.address);
const address = this.items.shipping_address || this.items.address;
const taxInfo = merge({}, address);

merge(taxInfo, this.items.tax);

Expand Down
18 changes: 17 additions & 1 deletion lib/recurly/pricing/index.js
Expand Up @@ -14,7 +14,8 @@ const PROPERTIES = [
'coupon',
'address',
'currency',
'gift_card'
'gift_card',
'shipping_address'
];

/**
Expand Down Expand Up @@ -341,6 +342,21 @@ export default class Pricing extends Emitter {
return new PricingPromise(updateFactory(this, 'address', address), this).nodeify(done);
}

/**
* Updates shipping address
*
* @param {Object} address
* @param {String} address.country
* @param {String|Number} address.postal_code
* @param {String} address.vat_number
* @param {Function} [done] callback
* @public
*/

shippingAddress (address, done) {
return new PricingPromise(updateFactory(this, 'shipping_address', address), this).nodeify(done);
}

/**
* Updates tax info
*
Expand Down
3 changes: 2 additions & 1 deletion lib/recurly/pricing/promise.js
Expand Up @@ -19,7 +19,8 @@ const pricingMethods = [
'reset',
'remove',
'reprice',
'giftcard'
'giftcard',
'shipping_address'
];

/**
Expand Down
40 changes: 40 additions & 0 deletions test/pricing/pricing.test.js
Expand Up @@ -139,6 +139,46 @@ describe('Recurly.Pricing', function () {
done();
});
});

describe('with a shipping address', function () {
it('calculates tax from the shipping address', function (done) {
this.pricing
.plan('intermediate', { quantity: 1 })
.shippingAddress({
country: 'US',
postal_code: '94110'
})
.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.93');
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('intermediate', { quantity: 1 })
.address({
country: 'DE',
postal_code: 'XXX-XXX'
})
.shippingAddress({
country: 'US',
postal_code: '94110'
})
.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.93');
assert.equal(price.next.tax, '1.75');
done();
});
})
});
});

describe('with usage addons', function() {
Expand Down

0 comments on commit 3d03169

Please sign in to comment.