From 7412b00704ab3bb83659b314e59ba27a8cd411a4 Mon Sep 17 00:00:00 2001 From: Dafydd Rosser Date: Tue, 28 Apr 2020 09:46:16 +0100 Subject: [PATCH] Add payment fees endpoints --- lib/api/payments.js | 64 +++++++++++++++++++++ package-lock.json | 2 +- test/api/fixtures/payments.js | 65 ++++++++++++++++++++- test/api/payments.js | 103 ++++++++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+), 2 deletions(-) diff --git a/lib/api/payments.js b/lib/api/payments.js index 922fa89..acabf73 100644 --- a/lib/api/payments.js +++ b/lib/api/payments.js @@ -277,5 +277,69 @@ module.exports = { }); return promise; + }, + + getPaymentFees: function (params) { + params = params || {}; + + const url = '/v2/payments/payment_fees'; + + return client.request({ + url: url, + method: 'GET', + qs: params + }); + }, + + unassignPaymentFee: function (params) { + params = params || {}; + + if (!params.hasOwnProperty("accountId")) { + throw new Error("accountId is required"); + } + + const url = "/v2/payments/unassign_payment_fee"; + + return client.request({ + url: url, + method: 'POST', + qs: params + }); + }, + + assignPaymentFee: function (params) { + params = params || {}; + + if (!params.hasOwnProperty("paymentFeeId")) { + throw new Error("paymentFeeId is required"); + } + + if (!params.hasOwnProperty("accountId")) { + throw new Error("accountId is required"); + } + + const url = "/v2/payments/assign_payment_fee"; + + return client.request({ + url: url, + method: 'POST', + qs: params + }); + }, + + assignedPaymentFee: function (params) { + params = params || {}; + + if (!params.hasOwnProperty("accountId")) { + throw new Error("accountId is required"); + } + + const url = "/v2/payments/assigned_payment_fee"; + + return client.request({ + url: url, + method: 'GET', + qs: params + }); } }; diff --git a/package-lock.json b/package-lock.json index 9b22ac4..3113a21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "currency-cloud", - "version": "2.0.1", + "version": "3.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/api/fixtures/payments.js b/test/api/fixtures/payments.js index 47fbff5..37d84dc 100644 --- a/test/api/fixtures/payments.js +++ b/test/api/fixtures/payments.js @@ -1500,6 +1500,69 @@ nock('https://devapi.currencycloud.com:443', {"encodedQueryParams": true}) "fee_amount": "10.00", "fee_currency": "EUR" }); +nock('https://devapi.currencycloud.com:443', {"encodedQueryParams": true}) + .get('/v2/payments/payment_fees') + .reply(200, { + "payment_fees": [ + { + "id": "e7e1b6e5-c596-4ad1-b8d4-a7035185143a", + "name": "Fee Table CAD 5 - 10 - 15", + "currency": "CAD", + "regular_amount": "5.00", + "priority_shared_amount": "10.00", + "priority_ours_amount": "15.00", + "owner_account_id": "" + }, + { + "id": "029e1771-8de7-4ab0-9c19-c14b325c0c9e", + "name": "Fee Table USD 2 - 4 - 12", + "currency": "USD", + "regular_amount": "2.00", + "priority_shared_amount": "4.00", + "priority_ours_amount": "12.00", + "owner_account_id": "" + } + ], + "pagination": { + "total_entries": 2, + "total_pages": 1, + "current_page": 1, + "per_page": 25, + "previous_page": -1, + "next_page": -1, + "order": "created_at", + "order_asc_desc": "asc" + } +}); + +nock("https://devapi.currencycloud.com:443", { + encodedQueryParams: true +}).post("/v2/payments/unassign_payment_fee").reply(200, { + account_id: "245a1ebd-d8a6-416d-bcc1-9de381d22f90" +}); + +nock("https://devapi.currencycloud.com:443", { + encodedQueryParams: true +}).post("/v2/payments/assign_payment_fee").reply(200, { + id: "7c17b546-0435-45f0-9c17-3a4e0f2d3e84", + account_id: "245a1ebd-d8a6-416d-bcc1-9de381d22f90" +}); + +nock("https://devapi.currencycloud.com:443", { + encodedQueryParams: true +}) +.get("/v2/payments/assigned_payment_fee") +.query({account_id: "4e8ca601-80c0-0133-26ca-0022194273c7"}) +.reply(200, { + id: "2bd34951-becc-4b52-b7d1-3f954609d173", + name: "Fee table name", + currency: "EUR", + regular_amount: "4.00", + priority_shared_amount: "5.00", + priority_ours_amount: "6.00", + owner_account_id: "4e8ca601-80c0-0133-26ca-0022194273c7" +}); + nock('https://devapi.currencycloud.com:443') .post('/v2/authenticate/close_session') - .reply(200, {}); + .reply(200, {}); \ No newline at end of file diff --git a/test/api/payments.js b/test/api/payments.js index 01becf7..892c998 100644 --- a/test/api/payments.js +++ b/test/api/payments.js @@ -420,4 +420,107 @@ describe('payments', function () { .catch(done); }); }); + + describe("getPaymentFees", function () { + it("successfully gets payment fees", function () { + currencyCloud.payments.getPaymentFees() + .then(function (res) { + expect(res).to.not.be.empty; + + expect(res).to.have.property("pagination").that.is.not.empty; + expect(res.pagination).to.have.property("totalEntries").that.equals(2); + expect(res.pagination).to.have.property("totalPages").that.equals(1); + expect(res.pagination).to.have.property("currentPage").that.equals(1); + expect(res.pagination).to.have.property("perPage").that.equals(25); + expect(res.pagination).to.have.property("previousPage").that.equals(-1); + expect(res.pagination).to.have.property("nextPage").that.equals(-1); + expect(res.pagination).to.have.property("order").that.equals("created_at"); + expect(res.pagination).to.have.property("orderAscDesc").that.equals("asc"); + + expect(res).to.have.property("paymentFees").that.is.not.empty; + expect(res.paymentFees[0]).to.have.property("id").that.equals("e7e1b6e5-c596-4ad1-b8d4-a7035185143a"); + expect(res.paymentFees[0]).to.have.property("name").that.equals("Fee Table CAD 5 - 10 - 15"); + expect(res.paymentFees[0]).to.have.property("currency").that.equals("CAD"); + expect(res.paymentFees[0]).to.have.property("regularAmount").that.equals("5.00"); + expect(res.paymentFees[0]).to.have.property("prioritySharedAmount").that.equals("10.00"); + expect(res.paymentFees[0]).to.have.property("priorityOursAmount").that.equals("15.00"); + expect(res.paymentFees[0]).to.have.property("ownerAccountId").that.is.empty; + + expect(res.paymentFees[1]).to.have.property("id").that.equals("029e1771-8de7-4ab0-9c19-c14b325c0c9e"); + expect(res.paymentFees[1]).to.have.property("name").that.equals("Fee Table USD 2 - 4 - 12"); + expect(res.paymentFees[1]).to.have.property("currency").that.equals("USD"); + expect(res.paymentFees[1]).to.have.property("regularAmount").that.equals("2.00"); + expect(res.paymentFees[1]).to.have.property("prioritySharedAmount").that.equals("4.00"); + expect(res.paymentFees[1]).to.have.property("priorityOursAmount").that.equals("12.00"); + expect(res.paymentFees[1]).to.have.property("ownerAccountId").that.is.empty; + }); + }); + }); + + describe("unassignPaymentFee", function () { + it("successfully unassigns a payment fee", function () { + currencyCloud.payments.unassignPaymentFee({ + accountId: "accountId123" + }).then(function (res) { + expect(res).to.not.be.empty; + + expect(res).to.have.property("accountId").that.equals("245a1ebd-d8a6-416d-bcc1-9de381d22f90"); + }); + }); + + it("throws an error if the accountId is not provided", function() { + expect(function () { + currencyCloud.payments.unassignPaymentFee(); + }).to.throw("accountId is required"); + }); + }); + + describe("assignPaymentFee", function () { + it("successfully assigns a payment fee to an account", function () { + currencyCloud.payments.assignPaymentFee({ + paymentFeeId: "hello", + accountId: "accountId123" + }) + .then(function (res) { + expect(res).to.not.be.empty; + + expect(res).to.have.property("id").that.equals("7c17b546-0435-45f0-9c17-3a4e0f2d3e84"); + expect(res).to.have.property("accountId").that.equals("245a1ebd-d8a6-416d-bcc1-9de381d22f90"); + }); + }); + + it ("throws an error when the paymentFeeId is missing", function () { + expect(function () { + currencyCloud.payments.assignPaymentFee({ + accountId: "accountId123" + }); + }).to.throw("paymentFeeId is required"); + }); + + it ("throws an error when the accountId is missing", function () { + expect(function () { + currencyCloud.payments.assignPaymentFee({ + paymentFeeId: "hello" + }); + }).to.throw("accountId is required"); + }); + }); + + describe("assignedPaymentFee", function () { + it("successfully gets the assigned payment fee", function () { + currencyCloud.payments.assignedPaymentFee({ + accountId: "4e8ca601-80c0-0133-26ca-0022194273c7" + }).then(function (res) { + expect(res).to.not.be.empty; + + expect(res).to.have.property("id").that.equals("2bd34951-becc-4b52-b7d1-3f954609d173"); + expect(res).to.have.property("name").that.equals("Fee table name"); + expect(res).to.have.property("currency").that.equals("EUR"); + expect(res).to.have.property("regularAmount").that.equals("4.00"); + expect(res).to.have.property("prioritySharedAmount").that.equals("5.00"); + expect(res).to.have.property("priorityOursAmount").that.equals("6.00"); + expect(res).to.have.property("ownerAccountId").that.equals("4e8ca601-80c0-0133-26ca-0022194273c7"); + }); + }); + }); }); \ No newline at end of file