Skip to content

Commit

Permalink
Add toCents support
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Metral committed Dec 22, 2014
1 parent 9c812ca commit 243bf90
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
18 changes: 14 additions & 4 deletions dist/paypal-invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ var currencyProperties = {
USD: {
symbol: '$',
decimals: 2,
round: BigNum.ROUND_HALF_UP
round: BigNum.ROUND_HALF_UP,
iso4217: 840
},
GBP: {
symbol: '£',
decimals: 2,
round: BigNum.ROUND_HALF_UP
round: BigNum.ROUND_HALF_UP,
iso4217: 826
},
AUD: {
symbol: '$',
decimals: 2,
round: BigNum.ROUND_HALF_UP
round: BigNum.ROUND_HALF_UP,
iso4217: 36
}
};

Expand All @@ -56,6 +59,10 @@ module.exports = {
newNumber: function (v) {
return new BigNum(v);
},
toCents: function (currency, amount) {
var decimals = currencyProperties[currency].decimals;
return new BigNum(10).pow(decimals).times(amount);
},
properties: currencyProperties
};

Expand Down Expand Up @@ -303,7 +310,10 @@ var InvoiceTotals = function (invoice) {

InvoiceTotals.prototype = {
_round: function (amt) {
return currency.round(this.invoice.currencyCode, amt);
return currency.round(this.invoice.currencyCode, amt);
},
toCents: function (amt) {
return currency.toCents(this.invoice.currencyCode, amt);
},
generateRoundedTaxDetailsFromTaxes: function (taxes) {
var roundedTaxDetails = {};
Expand Down
2 changes: 1 addition & 1 deletion dist/paypal-invoice.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ module.exports = {
newNumber: function (v) {
return new BigNum(v);
},
toCents: function (currency, amount) {
var decimals = currencyProperties[currency].decimals;
return new BigNum(10).pow(decimals).times(amount);
},
properties: currencyProperties
};

5 changes: 4 additions & 1 deletion lib/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ var InvoiceTotals = function (invoice) {

InvoiceTotals.prototype = {
_round: function (amt) {
return currency.round(this.invoice.currencyCode, amt);
return currency.round(this.invoice.currencyCode, amt);
},
toCents: function (amt) {
return currency.toCents(this.invoice.currencyCode, amt);
},
generateRoundedTaxDetailsFromTaxes: function (taxes) {
var roundedTaxDetails = {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "paypal-invoice",
"version": "0.1.4",
"version": "0.1.6",
"description": "PayPal Invoicing for Node.js",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions test/currencyTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ describe('lib/invoiceTests', function () {
var num = $$('123.45');
assert.equal(JSON.stringify({amt:num}), '{"amt":"123.45"}');
});

it('should handle USD toCents properly', function () {
var i = new Invoice('USD');
var item = new Invoice.Item(1, '3.99', 'UID', null);
i.addItem(item);
var tot = i.calculate();
assert.equal('399', tot.toCents(tot.total).toString());
});
});

0 comments on commit 243bf90

Please sign in to comment.