@defra-fish/recurring-payments-job 1.58.0-rc.11 → 1.58.0-rc.13
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra-fish/recurring-payments-job",
|
|
3
|
-
"version": "1.58.0-rc.
|
|
3
|
+
"version": "1.58.0-rc.13",
|
|
4
4
|
"description": "Rod Licensing Recurring Payments Job",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@defra-fish/business-rules-lib": "1.58.0-rc.
|
|
40
|
-
"@defra-fish/connectors-lib": "1.58.0-rc.
|
|
39
|
+
"@defra-fish/business-rules-lib": "1.58.0-rc.13",
|
|
40
|
+
"@defra-fish/connectors-lib": "1.58.0-rc.13",
|
|
41
41
|
"commander": "^7.2.0",
|
|
42
42
|
"moment-timezone": "^0.5.34"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "1c82e1fb1fba05fd3b37a2e28048c78b2b045e01"
|
|
45
45
|
}
|
|
@@ -14,7 +14,9 @@ jest.mock('@defra-fish/connectors-lib', () => ({
|
|
|
14
14
|
}))
|
|
15
15
|
}
|
|
16
16
|
}))
|
|
17
|
-
jest.mock('../services/govuk-pay-service.js')
|
|
17
|
+
jest.mock('../services/govuk-pay-service.js', () => ({
|
|
18
|
+
sendPayment: jest.fn()
|
|
19
|
+
}))
|
|
18
20
|
|
|
19
21
|
describe('recurring-payments-processor', () => {
|
|
20
22
|
beforeEach(() => {
|
|
@@ -58,6 +60,8 @@ describe('recurring-payments-processor', () => {
|
|
|
58
60
|
it('prepares the data for found recurring payments', async () => {
|
|
59
61
|
const referenceNumber = Symbol('reference')
|
|
60
62
|
salesApi.getDueRecurringPayments.mockReturnValueOnce([getMockDueRecurringPayment(referenceNumber)])
|
|
63
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
64
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
61
65
|
|
|
62
66
|
await processRecurringPayments()
|
|
63
67
|
|
|
@@ -106,6 +110,9 @@ describe('recurring-payments-processor', () => {
|
|
|
106
110
|
]
|
|
107
111
|
}
|
|
108
112
|
|
|
113
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
114
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
115
|
+
|
|
109
116
|
await processRecurringPayments()
|
|
110
117
|
|
|
111
118
|
expect(salesApi.createTransaction).toHaveBeenCalledWith(expectedData)
|
|
@@ -127,6 +134,9 @@ describe('recurring-payments-processor', () => {
|
|
|
127
134
|
]
|
|
128
135
|
})
|
|
129
136
|
|
|
137
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
138
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
139
|
+
|
|
130
140
|
await processRecurringPayments()
|
|
131
141
|
|
|
132
142
|
expect(salesApi.createTransaction).toHaveBeenCalledWith(
|
|
@@ -153,6 +163,9 @@ describe('recurring-payments-processor', () => {
|
|
|
153
163
|
licenceStartTime: 15
|
|
154
164
|
})
|
|
155
165
|
|
|
166
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
167
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
168
|
+
|
|
156
169
|
await processRecurringPayments()
|
|
157
170
|
|
|
158
171
|
expect(salesApi.createTransaction).toHaveBeenCalledWith(
|
|
@@ -170,6 +183,9 @@ describe('recurring-payments-processor', () => {
|
|
|
170
183
|
licenceStartDate: '2020-03-14'
|
|
171
184
|
})
|
|
172
185
|
|
|
186
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
187
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
188
|
+
|
|
173
189
|
await processRecurringPayments()
|
|
174
190
|
|
|
175
191
|
expect(salesApi.createTransaction).toHaveBeenCalledWith(
|
|
@@ -204,6 +220,9 @@ describe('recurring-payments-processor', () => {
|
|
|
204
220
|
id: transactionId
|
|
205
221
|
})
|
|
206
222
|
|
|
223
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
224
|
+
sendPayment.mockResolvedValueOnce(mockPaymentResponse)
|
|
225
|
+
|
|
207
226
|
const expectedData = {
|
|
208
227
|
amount: 5000,
|
|
209
228
|
description: 'The recurring card payment for your rod fishing licence',
|
|
@@ -229,6 +248,8 @@ describe('recurring-payments-processor', () => {
|
|
|
229
248
|
mockGetDueRecurringPayments.push(getMockDueRecurringPayment(reference))
|
|
230
249
|
})
|
|
231
250
|
salesApi.getDueRecurringPayments.mockReturnValueOnce(mockGetDueRecurringPayments)
|
|
251
|
+
const mockPaymentResponse = { payment_id: 'test-payment-id' }
|
|
252
|
+
sendPayment.mockResolvedValue(mockPaymentResponse)
|
|
232
253
|
|
|
233
254
|
const expectedData = []
|
|
234
255
|
references.forEach(reference => {
|
|
@@ -3,6 +3,8 @@ import { SERVICE_LOCAL_TIME } from '@defra-fish/business-rules-lib'
|
|
|
3
3
|
import { salesApi } from '@defra-fish/connectors-lib'
|
|
4
4
|
import { sendPayment } from './services/govuk-pay-service.js'
|
|
5
5
|
|
|
6
|
+
const payments = []
|
|
7
|
+
|
|
6
8
|
export const processRecurringPayments = async () => {
|
|
7
9
|
if (process.env.RUN_RECURRING_PAYMENTS?.toLowerCase() === 'true') {
|
|
8
10
|
console.log('Recurring Payments job enabled')
|
|
@@ -10,6 +12,7 @@ export const processRecurringPayments = async () => {
|
|
|
10
12
|
const response = await salesApi.getDueRecurringPayments(date)
|
|
11
13
|
console.log('Recurring Payments found: ', response)
|
|
12
14
|
await Promise.all(response.map(record => processRecurringPayment(record)))
|
|
15
|
+
console.log('Recurring Payments processed:', payments)
|
|
13
16
|
} else {
|
|
14
17
|
console.log('Recurring Payments job disabled')
|
|
15
18
|
}
|
|
@@ -19,7 +22,7 @@ const processRecurringPayment = async record => {
|
|
|
19
22
|
const referenceNumber = record.expanded.activePermission.entity.referenceNumber
|
|
20
23
|
const agreementId = record.entity.agreementId
|
|
21
24
|
const transaction = await createNewTransaction(referenceNumber)
|
|
22
|
-
takeRecurringPayment(agreementId, transaction)
|
|
25
|
+
await takeRecurringPayment(agreementId, transaction)
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
const createNewTransaction = async referenceNumber => {
|
|
@@ -35,10 +38,14 @@ const createNewTransaction = async referenceNumber => {
|
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
const takeRecurringPayment = (agreementId, transaction) => {
|
|
41
|
+
const takeRecurringPayment = async (agreementId, transaction) => {
|
|
39
42
|
const preparedPayment = preparePayment(agreementId, transaction)
|
|
40
43
|
console.log('Requesting payment:', preparedPayment)
|
|
41
|
-
sendPayment(preparedPayment)
|
|
44
|
+
const payment = await sendPayment(preparedPayment)
|
|
45
|
+
payments.push({
|
|
46
|
+
agreementId,
|
|
47
|
+
paymentId: payment.payment_id
|
|
48
|
+
})
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
const processPermissionData = async referenceNumber => {
|
|
@@ -5,6 +5,22 @@ jest.mock('@defra-fish/connectors-lib')
|
|
|
5
5
|
|
|
6
6
|
describe('govuk-pay-service', () => {
|
|
7
7
|
describe('sendPayment', () => {
|
|
8
|
+
const preparedPayment = { id: '1234' }
|
|
9
|
+
|
|
10
|
+
it('sendPayment should return response from createPayment in json format', async () => {
|
|
11
|
+
const mockPreparedPayment = { id: 'test-payment-id' }
|
|
12
|
+
const mockResponse = { status: 'success', paymentId: 'abc123' }
|
|
13
|
+
|
|
14
|
+
const mockFetchResponse = {
|
|
15
|
+
json: jest.fn().mockResolvedValue(mockResponse)
|
|
16
|
+
}
|
|
17
|
+
govUkPayApi.createPayment.mockResolvedValue(mockFetchResponse)
|
|
18
|
+
|
|
19
|
+
const result = await sendPayment(mockPreparedPayment)
|
|
20
|
+
|
|
21
|
+
expect(result).toEqual(mockResponse)
|
|
22
|
+
})
|
|
23
|
+
|
|
8
24
|
it('should send provided payload data to Gov.UK Pay', async () => {
|
|
9
25
|
govUkPayApi.createPayment.mockResolvedValue({
|
|
10
26
|
ok: true,
|
|
@@ -19,5 +35,30 @@ describe('govuk-pay-service', () => {
|
|
|
19
35
|
await sendPayment(payload)
|
|
20
36
|
expect(govUkPayApi.createPayment).toHaveBeenCalledWith(payload, true)
|
|
21
37
|
})
|
|
38
|
+
|
|
39
|
+
it('should throw an error when the GOV.UK Pay connector raises an error', async () => {
|
|
40
|
+
govUkPayApi.createPayment.mockImplementationOnce(() => {
|
|
41
|
+
throw new Error('Oops!')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await sendPayment(preparedPayment)
|
|
46
|
+
} catch (e) {
|
|
47
|
+
expect(e.message).toBe('Oops!')
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should log error message when the GOV.UK Pay API raises an error', async () => {
|
|
52
|
+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
53
|
+
govUkPayApi.createPayment.mockImplementationOnce(() => {
|
|
54
|
+
throw new Error()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
await sendPayment(preparedPayment)
|
|
59
|
+
} catch (error) {
|
|
60
|
+
expect(consoleSpy).toHaveBeenCalledWith('Error creating payment', preparedPayment.id)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
22
63
|
})
|
|
23
64
|
})
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { govUkPayApi } from '@defra-fish/connectors-lib'
|
|
2
2
|
|
|
3
|
-
export const sendPayment = preparedPayment => {
|
|
4
|
-
|
|
3
|
+
export const sendPayment = async preparedPayment => {
|
|
4
|
+
try {
|
|
5
|
+
const response = await govUkPayApi.createPayment(preparedPayment, true)
|
|
6
|
+
return await response.json()
|
|
7
|
+
} catch (e) {
|
|
8
|
+
console.error('Error creating payment', preparedPayment.id)
|
|
9
|
+
throw e
|
|
10
|
+
}
|
|
5
11
|
}
|