diff --git a/lib/balanced.js b/lib/balanced.js index 78c53fd..a2c8d44 100644 --- a/lib/balanced.js +++ b/lib/balanced.js @@ -76,7 +76,14 @@ function associate_to_customer(customer) { balanced.registerType('card', { debit: transaction_create('debit'), - credit: transaction_create('credit'), + // function below confirms card is creditable before attempting + credit: function() { + if (this.can_credit) { + transaction_create('credit') + } else { + throw new BalancedError('FundingInstrumentNotCreditable'); + } + }, hold: transaction_create('card_hold'), associate_to_customer: associate_to_customer, @@ -190,3 +197,9 @@ balanced.registerType('dispute', { events: '_', transaction: '_' }); + +function BalancedError(message) { + this.name = this.constructor.name; + this.message = message; + this.stack = (new Error()).stack; +} diff --git a/test/test.js b/test/test.js index 5f8061f..1da713f 100644 --- a/test/test.js +++ b/test/test.js @@ -380,17 +380,21 @@ test('credit_card', function (cb, assert, marketplace, debit_card) { }); }); -test('credit_card_can_credit_false', function (cb, assert, marketplace, debit_card) { - balanced.marketplace.credits.create({ - amount: 250000, - destination: fixtures.card_non_creditable - }).then(function (credit) { +test('credit_card_can_credit_false', function(cb, assert, marketplace) { + balanced.marketplace.cards.create({ + 'number': '4111111111111111', + 'expiration_month': '05', + 'expiration_year': '2016' + }).then(function (card) { + card.credit({ + "amount": 1000, + "description": "Credit" + }).then(function (customer) { assert(false); - }, function(err) { - var error = JSON.parse(err.message).errors[0]; - assert(error.status_code == 409); - assert(error.category_code == 'funding-destination-not-creditable'); + }, function (err) { + assert(err.message == 'FundingInstrumentNotCreditable'); cb(); + }); }); });