Skip to content

Commit

Permalink
Merge pull request #60 from CurrencyCloud/sdkjs-52_payments_validate
Browse files Browse the repository at this point in the history
Adds payment/validate endpoint
  • Loading branch information
jonathancouchman authored Apr 20, 2021
2 parents b2d41b2 + e49c1c8 commit 5c0e307
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/api/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,45 @@ module.exports = {
return promise;
},

/**
* Validate a new payment.
* @param {Object} params Object, which contains parameters of the new payment
* @param {String} params.currency Payment currency, required
* @param {String} params.beneficiaryId Id of the payment beneficiary, required
* @param {Number} params.amount Payment amount, required
* @param {String} params.reason Payment reason, required
* @param {String} params.reference Payment reference, required
* @return {Promise} Promise; if fulfilled returns object, which contains confirmation of successful validation; if rejected returns APIerror.
*/
validate: function (params) {
params = params || {};
if (!params.hasOwnProperty('currency')) {
throw new Error('currency is required');
}
if (!params.hasOwnProperty('beneficiaryId')) {
throw new Error('beneficiaryId is required');
}
if (!params.hasOwnProperty('amount')) {
throw new Error('amount is required');
}
if (!params.hasOwnProperty('reason')) {
throw new Error('reason is required');
}
if (!params.hasOwnProperty('reference')) {
throw new Error('reference is required');
}

var url = '/v2/payments/validate';

var promise = client.request({
url: url,
method: 'POST',
qs: params
});

return promise;
},

/**
* Gets details of a payment.
* @param {Object} params Parameters object
Expand Down
12 changes: 12 additions & 0 deletions test/api/fixtures/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,18 @@ nock('https://devapi.currencycloud.com:443', {"encodedQueryParams": true})
]
});

nock('https://devapi.currencycloud.com:443')
.post('/v2/payments/validate', {
"currency": "GBP",
"amount": "100",
"reason": "validate_unit_test",
"reference": "validate123",
"beneficiary_id": "46ed4827-7b6f-4491-a06f-b548d5a7512d"
})
.reply(200, {
"validation_result": "success"
});

nock('https://devapi.currencycloud.com:443')
.post('/v2/authenticate/close_session')
.reply(200, {});
17 changes: 17 additions & 0 deletions test/api/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,21 @@ describe('payments', function () {
.catch(done);
});
});

describe('validate', function () {
it('successfully validate a payment', function (done) {
currencyCloud.payments.validate({
currency:"GBP",
amount:100,
beneficiaryId:"46ed4827-7b6f-4491-a06f-b548d5a7512d",
reason:"validate_unit_test",
reference:'validate123'})
.then(function (res) {
expect(res).is.not.empty;
expect(res).to.have.property('validationResult').that.eql("success");
done();
})
.catch(done);
});
});
});

0 comments on commit 5c0e307

Please sign in to comment.