@defra-fish/connectors-lib 1.55.0 → 1.57.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/package.json +2 -2
- package/src/__tests__/govuk-pay-api.spec.js +40 -2
- package/src/govuk-pay-api.js +9 -9
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ Provides connectivity to the resources/infrastructure used in the rod licensing
|
|
|
18
18
|
| SALES_API_TIMEOUT_MS | Request timeout for the requests to the sales API | no | 20000 (20s) | | |
|
|
19
19
|
| GOV_PAY_API_URL | The GOV.UK Pay API base url | yes | | | |
|
|
20
20
|
| GOV_PAY_APIKEY | GOV pay access identifier | yes | | | |
|
|
21
|
+
| GOV_PAY_RECURRING_APIKEY | GOV pay access identifier for recurring payments | yes | | | |
|
|
21
22
|
| GOV_PAY_REQUEST_TIMEOUT_MS | Timeout in milliseconds for API requests | no | 10000 | | |
|
|
22
23
|
| GOV_PAY_RCP_API_URL | The GOV.UK Pay API url for agreements | yes | |
|
|
23
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra-fish/connectors-lib",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.57.0-rc.0",
|
|
4
4
|
"description": "Shared connectors",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"node-fetch": "^2.6.7",
|
|
42
42
|
"redlock": "^4.2.0"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "7f632526e47173f4d8846cab393db6b487abf398"
|
|
45
45
|
}
|
|
@@ -5,6 +5,7 @@ const fetch = require('node-fetch')
|
|
|
5
5
|
process.env.GOV_PAY_API_URL = 'http://0.0.0.0/payment'
|
|
6
6
|
process.env.GOV_PAY_RCP_API_URL = 'http://0.0.0.0/agreement'
|
|
7
7
|
process.env.GOV_PAY_APIKEY = 'key'
|
|
8
|
+
process.env.GOV_PAY_RECURRING_APIKEY = 'recurringkey'
|
|
8
9
|
|
|
9
10
|
const headers = {
|
|
10
11
|
accept: 'application/json',
|
|
@@ -12,6 +13,12 @@ const headers = {
|
|
|
12
13
|
'content-type': 'application/json'
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
const recurringHeaders = {
|
|
17
|
+
accept: 'application/json',
|
|
18
|
+
authorization: `Bearer ${process.env.GOV_PAY_RECURRING_APIKEY}`,
|
|
19
|
+
'content-type': 'application/json'
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
describe('govuk-pay-api-connector', () => {
|
|
16
23
|
beforeEach(jest.clearAllMocks)
|
|
17
24
|
|
|
@@ -41,6 +48,17 @@ describe('govuk-pay-api-connector', () => {
|
|
|
41
48
|
})
|
|
42
49
|
expect(consoleErrorSpy).toHaveBeenCalled()
|
|
43
50
|
})
|
|
51
|
+
|
|
52
|
+
it('uses the correct API key if recurring arg is set to true', async () => {
|
|
53
|
+
fetch.mockReturnValue({ ok: true, status: 200 })
|
|
54
|
+
await expect(govUkPayApi.createPayment({ cost: 0 }, true)).resolves.toEqual({ ok: true, status: 200 })
|
|
55
|
+
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment', {
|
|
56
|
+
body: JSON.stringify({ cost: 0 }),
|
|
57
|
+
headers: recurringHeaders,
|
|
58
|
+
method: 'post',
|
|
59
|
+
timeout: 10000
|
|
60
|
+
})
|
|
61
|
+
})
|
|
44
62
|
})
|
|
45
63
|
|
|
46
64
|
describe('fetchPaymentStatus', () => {
|
|
@@ -63,6 +81,16 @@ describe('govuk-pay-api-connector', () => {
|
|
|
63
81
|
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', { headers, method: 'get', timeout: 10000 })
|
|
64
82
|
expect(consoleErrorSpy).toHaveBeenCalled()
|
|
65
83
|
})
|
|
84
|
+
|
|
85
|
+
it('uses the correct API key if recurring arg is set to true', async () => {
|
|
86
|
+
fetch.mockReturnValue({ ok: true, status: 200, json: () => {} })
|
|
87
|
+
await expect(govUkPayApi.fetchPaymentStatus(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 }))
|
|
88
|
+
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', {
|
|
89
|
+
headers: recurringHeaders,
|
|
90
|
+
method: 'get',
|
|
91
|
+
timeout: 10000
|
|
92
|
+
})
|
|
93
|
+
})
|
|
66
94
|
})
|
|
67
95
|
|
|
68
96
|
describe('fetchPaymentEvents', () => {
|
|
@@ -80,6 +108,16 @@ describe('govuk-pay-api-connector', () => {
|
|
|
80
108
|
await expect(govUkPayApi.fetchPaymentEvents(123)).rejects.toEqual(Error('test event error'))
|
|
81
109
|
expect(consoleErrorSpy).toHaveBeenCalled()
|
|
82
110
|
})
|
|
111
|
+
|
|
112
|
+
it('uses the correct API key if recurring arg is set to true', async () => {
|
|
113
|
+
fetch.mockReturnValue({ ok: true, status: 200, json: () => {} })
|
|
114
|
+
await expect(govUkPayApi.fetchPaymentEvents(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 }))
|
|
115
|
+
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123/events', {
|
|
116
|
+
headers: recurringHeaders,
|
|
117
|
+
method: 'get',
|
|
118
|
+
timeout: 10000
|
|
119
|
+
})
|
|
120
|
+
})
|
|
83
121
|
})
|
|
84
122
|
|
|
85
123
|
describe('createRecurringPayment', () => {
|
|
@@ -88,7 +126,7 @@ describe('govuk-pay-api-connector', () => {
|
|
|
88
126
|
await expect(govUkPayApi.createRecurringPayment({ cost: 0 })).resolves.toEqual({ ok: true, status: 200 })
|
|
89
127
|
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', {
|
|
90
128
|
body: JSON.stringify({ cost: 0 }),
|
|
91
|
-
headers,
|
|
129
|
+
headers: recurringHeaders,
|
|
92
130
|
method: 'post',
|
|
93
131
|
timeout: 10000
|
|
94
132
|
})
|
|
@@ -102,7 +140,7 @@ describe('govuk-pay-api-connector', () => {
|
|
|
102
140
|
expect(govUkPayApi.createRecurringPayment({ reference: '123' })).rejects.toEqual(Error(''))
|
|
103
141
|
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', {
|
|
104
142
|
body: JSON.stringify({ reference: '123' }),
|
|
105
|
-
headers,
|
|
143
|
+
headers: recurringHeaders,
|
|
106
144
|
method: 'post',
|
|
107
145
|
timeout: 10000
|
|
108
146
|
})
|
package/src/govuk-pay-api.js
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
import fetch from 'node-fetch'
|
|
5
5
|
const GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT = 10000
|
|
6
6
|
|
|
7
|
-
const headers =
|
|
7
|
+
const headers = recurring => ({
|
|
8
8
|
accept: 'application/json',
|
|
9
|
-
authorization: `Bearer ${process.env.GOV_PAY_APIKEY}`,
|
|
9
|
+
authorization: `Bearer ${recurring ? process.env.GOV_PAY_RECURRING_APIKEY : process.env.GOV_PAY_APIKEY}`,
|
|
10
10
|
'content-type': 'application/json'
|
|
11
11
|
})
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ const headers = () => ({
|
|
|
18
18
|
export const createRecurringPayment = async preparedPayment => {
|
|
19
19
|
try {
|
|
20
20
|
return fetch(process.env.GOV_PAY_RCP_API_URL, {
|
|
21
|
-
headers: headers(),
|
|
21
|
+
headers: headers(true),
|
|
22
22
|
method: 'post',
|
|
23
23
|
body: JSON.stringify(preparedPayment),
|
|
24
24
|
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
|
|
@@ -37,10 +37,10 @@ export const createRecurringPayment = async preparedPayment => {
|
|
|
37
37
|
* @param preparedPayment - see the GOV.UK pay API reference for details
|
|
38
38
|
* @returns {Promise<*>}
|
|
39
39
|
*/
|
|
40
|
-
export const createPayment = async preparedPayment => {
|
|
40
|
+
export const createPayment = async (preparedPayment, recurring = false) => {
|
|
41
41
|
try {
|
|
42
42
|
return fetch(process.env.GOV_PAY_API_URL, {
|
|
43
|
-
headers: headers(),
|
|
43
|
+
headers: headers(recurring),
|
|
44
44
|
method: 'post',
|
|
45
45
|
body: JSON.stringify(preparedPayment),
|
|
46
46
|
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
|
|
@@ -56,10 +56,10 @@ export const createPayment = async preparedPayment => {
|
|
|
56
56
|
* @param paymentId
|
|
57
57
|
* @returns {Promise<unknown>}
|
|
58
58
|
*/
|
|
59
|
-
export const fetchPaymentStatus = async paymentId => {
|
|
59
|
+
export const fetchPaymentStatus = async (paymentId, recurring = false) => {
|
|
60
60
|
try {
|
|
61
61
|
return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}`, {
|
|
62
|
-
headers: headers(),
|
|
62
|
+
headers: headers(recurring),
|
|
63
63
|
method: 'get',
|
|
64
64
|
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
|
|
65
65
|
})
|
|
@@ -74,10 +74,10 @@ export const fetchPaymentStatus = async paymentId => {
|
|
|
74
74
|
* @param paymentId
|
|
75
75
|
* @returns {Promise<unknown>}
|
|
76
76
|
*/
|
|
77
|
-
export const fetchPaymentEvents = async paymentId => {
|
|
77
|
+
export const fetchPaymentEvents = async (paymentId, recurring = false) => {
|
|
78
78
|
try {
|
|
79
79
|
return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}/events`, {
|
|
80
|
-
headers: headers(),
|
|
80
|
+
headers: headers(recurring),
|
|
81
81
|
method: 'get',
|
|
82
82
|
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
|
|
83
83
|
})
|