From f72f8c402cf5dad5595a0b8977288809bb77375c Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Mon, 18 Nov 2024 14:10:41 +0000 Subject: [PATCH] Update rest of pay connector to toggle headers --- .../src/__tests__/govuk-pay-api.spec.js | 57 +++++++++++-------- packages/connectors-lib/src/govuk-pay-api.js | 10 ++-- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/packages/connectors-lib/src/__tests__/govuk-pay-api.spec.js b/packages/connectors-lib/src/__tests__/govuk-pay-api.spec.js index c45b6eda9..43f4b3e08 100644 --- a/packages/connectors-lib/src/__tests__/govuk-pay-api.spec.js +++ b/packages/connectors-lib/src/__tests__/govuk-pay-api.spec.js @@ -13,6 +13,12 @@ const headers = { 'content-type': 'application/json' } +const recurringHeaders = { + accept: 'application/json', + authorization: `Bearer ${process.env.GOV_PAY_RECURRING_APIKEY}`, + 'content-type': 'application/json' +} + describe('govuk-pay-api-connector', () => { beforeEach(jest.clearAllMocks) @@ -46,27 +52,12 @@ describe('govuk-pay-api-connector', () => { it('uses the correct API key if recurring arg is set to true', async () => { fetch.mockReturnValue({ ok: true, status: 200 }) await expect(govUkPayApi.createPayment({ cost: 0 }, true)).resolves.toEqual({ ok: true, status: 200 }) - expect(fetch).toHaveBeenCalledWith( - 'http://0.0.0.0/payment', - expect.objectContaining({ - headers: expect.objectContaining({ - authorization: `Bearer ${process.env.GOV_PAY_RECURRING_APIKEY}` - }) - }) - ) - }) - - it('uses the correct API key if recurring arg is set to false', async () => { - fetch.mockReturnValue({ ok: true, status: 200 }) - await expect(govUkPayApi.createPayment({ cost: 0 }, false)).resolves.toEqual({ ok: true, status: 200 }) - expect(fetch).toHaveBeenCalledWith( - 'http://0.0.0.0/payment', - expect.objectContaining({ - headers: expect.objectContaining({ - authorization: `Bearer ${process.env.GOV_PAY_APIKEY}` - }) - }) - ) + expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment', { + body: JSON.stringify({ cost: 0 }), + headers: recurringHeaders, + method: 'post', + timeout: 10000 + }) }) }) @@ -90,6 +81,16 @@ describe('govuk-pay-api-connector', () => { expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', { headers, method: 'get', timeout: 10000 }) expect(consoleErrorSpy).toHaveBeenCalled() }) + + it('uses the correct API key if recurring arg is set to true', async () => { + fetch.mockReturnValue({ ok: true, status: 200, json: () => {} }) + await expect(govUkPayApi.fetchPaymentStatus(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 })) + expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', { + headers: recurringHeaders, + method: 'get', + timeout: 10000 + }) + }) }) describe('fetchPaymentEvents', () => { @@ -107,6 +108,16 @@ describe('govuk-pay-api-connector', () => { await expect(govUkPayApi.fetchPaymentEvents(123)).rejects.toEqual(Error('test event error')) expect(consoleErrorSpy).toHaveBeenCalled() }) + + it('uses the correct API key if recurring arg is set to true', async () => { + fetch.mockReturnValue({ ok: true, status: 200, json: () => {} }) + await expect(govUkPayApi.fetchPaymentEvents(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 })) + expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123/events', { + headers: recurringHeaders, + method: 'get', + timeout: 10000 + }) + }) }) describe('createRecurringPayment', () => { @@ -115,7 +126,7 @@ describe('govuk-pay-api-connector', () => { await expect(govUkPayApi.createRecurringPayment({ cost: 0 })).resolves.toEqual({ ok: true, status: 200 }) expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', { body: JSON.stringify({ cost: 0 }), - headers, + headers: recurringHeaders, method: 'post', timeout: 10000 }) @@ -129,7 +140,7 @@ describe('govuk-pay-api-connector', () => { expect(govUkPayApi.createRecurringPayment({ reference: '123' })).rejects.toEqual(Error('')) expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', { body: JSON.stringify({ reference: '123' }), - headers, + headers: recurringHeaders, method: 'post', timeout: 10000 }) diff --git a/packages/connectors-lib/src/govuk-pay-api.js b/packages/connectors-lib/src/govuk-pay-api.js index b3f10fcc3..7935ee502 100644 --- a/packages/connectors-lib/src/govuk-pay-api.js +++ b/packages/connectors-lib/src/govuk-pay-api.js @@ -18,7 +18,7 @@ const headers = (recurring = false) => ({ export const createRecurringPayment = async preparedPayment => { try { return fetch(process.env.GOV_PAY_RCP_API_URL, { - headers: headers(), + headers: headers(true), method: 'post', body: JSON.stringify(preparedPayment), timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT @@ -56,10 +56,10 @@ export const createPayment = async (preparedPayment, recurring = false) => { * @param paymentId * @returns {Promise} */ -export const fetchPaymentStatus = async paymentId => { +export const fetchPaymentStatus = async (paymentId, recurring = false) => { try { return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}`, { - headers: headers(), + headers: headers(recurring), method: 'get', timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT }) @@ -74,10 +74,10 @@ export const fetchPaymentStatus = async paymentId => { * @param paymentId * @returns {Promise} */ -export const fetchPaymentEvents = async paymentId => { +export const fetchPaymentEvents = async (paymentId, recurring = false) => { try { return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}/events`, { - headers: headers(), + headers: headers(recurring), method: 'get', timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT })