Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for shipping address #300

Merged
merged 2 commits into from Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion 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 @@ -101,6 +102,13 @@ export default class Attachment {
});
}

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

if (updateTax) {
pricing = pricing.tax({
vat_number: dom.value(elems.vat_number),
Expand Down
5 changes: 4 additions & 1 deletion lib/recurly/pricing/calculations.js
Expand Up @@ -85,6 +85,8 @@ Calculations.prototype.subtotal = function () {
/**
* Calculates tax
*
* tax info precedence: `tax` > `shipping_address` > `address`
*
* @param {Function} done
* @private
*/
Expand All @@ -93,7 +95,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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line determines address precedence. We calculate tax from shipping address first, then address if none is present

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',
'shippingAddress'
];

/**
Expand Down
38 changes: 36 additions & 2 deletions test/pricing/attachment.test.js
Expand Up @@ -47,13 +47,47 @@ describe('Recurly.Pricing.attach', function () {
});
});

describe('when pre-populated with a valid giftcard redemption code', function(){
describe('when given an address', function () {
this.ctx.fixtureOpts = {
plan: 'basic',
country: 'US',
postal_code: '94129'
};

it('sets the address', function (done) {
assert(typeof this.pricing.items.address === 'undefined');
this.pricing.on('set.address', () => {
assert(this.pricing.items.address.country === 'US');
assert(this.pricing.items.address.postal_code === '94129');
done();
});
});
});

describe('when given a shipping address', function () {
this.ctx.fixtureOpts = {
plan: 'basic',
'shipping_address.country': 'US',
'shipping_address.postal_code': '94129'
};

it('sets the shipping address', function (done) {
assert(typeof this.pricing.items.shipping_address === 'undefined');
this.pricing.on('set.shipping_address', () => {
assert(this.pricing.items.shipping_address.country === 'US');
assert(this.pricing.items.shipping_address.postal_code === '94129');
done();
});
});
});

describe('when pre-populated with a valid giftcard redemption code', function () {
this.ctx.fixtureOpts = {
plan: 'basic',
giftcard: 'superGiftcardForMe'
};

it('applies the giftcard to the pricing instance', function(done) {
it('applies the giftcard to the pricing instance', function (done) {
assert(typeof this.pricing.items.gift_card === 'undefined');
this.pricing.on('set.gift_card', () => {
assert(this.pricing.items.gift_card.currency === 'USD');
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('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.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('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.93');
assert.equal(price.next.tax, '1.75');
done();
});
})
});
});

describe('with usage addons', function() {
Expand Down
3 changes: 3 additions & 0 deletions test/support/fixtures.js
Expand Up @@ -65,6 +65,9 @@ const pricing = opts => `
<input type="text" data-recurly="tax_code" value="${fetch(opts, 'tax_code', '')}">
<input type="text" data-recurly="vat_number" value="${fetch(opts, 'vat_number', '')}">

<input type="text" data-recurly="shipping_address.country" value="${fetch(opts, 'shipping_address.country', '')}">
<input type="text" data-recurly="shipping_address.postal_code" value="${fetch(opts, 'shipping_address.postal_code', '')}">

<span data-recurly="total_now"></span>
<span data-recurly="subtotal_now"></span>
<span data-recurly="addons_now"></span>
Expand Down