From e1f178c4927d6e8a347db49a07ef0f626838b896 Mon Sep 17 00:00:00 2001 From: richie serna Date: Wed, 8 Oct 2014 20:27:15 -0700 Subject: [PATCH 1/2] Throw error if credit attempted on non-creditable card --- lib/balanced.js | 14 +++++++++++++- test/test.js | 22 +++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/balanced.js b/lib/balanced.js index 78c53fd..0db3c9c 100644 --- a/lib/balanced.js +++ b/lib/balanced.js @@ -76,7 +76,13 @@ function associate_to_customer(customer) { balanced.registerType('card', { debit: transaction_create('debit'), - credit: transaction_create('credit'), + credit: function() { //confirms card is creditable before attempting + 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 +196,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(); + }); }); }); From f87956b8aa88363cab150b0fa3c1f3e7e00b2623 Mon Sep 17 00:00:00 2001 From: richie serna Date: Thu, 9 Oct 2014 14:44:20 -0700 Subject: [PATCH 2/2] Edit comment --- lib/balanced.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/balanced.js b/lib/balanced.js index 0db3c9c..a2c8d44 100644 --- a/lib/balanced.js +++ b/lib/balanced.js @@ -76,7 +76,8 @@ function associate_to_customer(customer) { balanced.registerType('card', { debit: transaction_create('debit'), - credit: function() { //confirms card is creditable before attempting + // function below confirms card is creditable before attempting + credit: function() { if (this.can_credit) { transaction_create('credit') } else {