Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/balanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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;
}
22 changes: 13 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down