From e49c1c8c7ae6fa07da545f22dfa04454fd8e11a9 Mon Sep 17 00:00:00 2001 From: jonathancouchman Date: Tue, 20 Apr 2021 16:34:20 +0100 Subject: [PATCH] Adds payment/validate endpoint --- lib/api/payments.js | 39 +++++++++++++++++++++++++++++++++++ test/api/fixtures/payments.js | 12 +++++++++++ test/api/payments.js | 17 +++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/lib/api/payments.js b/lib/api/payments.js index 8e52a7b..bcd307c 100644 --- a/lib/api/payments.js +++ b/lib/api/payments.js @@ -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 diff --git a/test/api/fixtures/payments.js b/test/api/fixtures/payments.js index 3ee9113..3600bd1 100644 --- a/test/api/fixtures/payments.js +++ b/test/api/fixtures/payments.js @@ -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, {}); diff --git a/test/api/payments.js b/test/api/payments.js index 7222f92..0de94d5 100644 --- a/test/api/payments.js +++ b/test/api/payments.js @@ -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); + }); + }); });