Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RP job sends payment requests to GOV.UK Pay #2034

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/connectors-lib/src/sales-api-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,13 @@ export const getDueRecurringPayments = async date => exec2xxOrThrow(call(new URL
*/
export const preparePermissionDataForRenewal = async referenceNumber =>
exec2xxOrThrow(call(new URL(`/permissionRenewalData/${referenceNumber}`, urlBase), 'get'))

/**
* Send payment details for processing
*
* @param transaction
* @returns {Promise<*>}
* @throws on a non-2xx response
*/
export const sendPayment = async transaction =>
exec2xxOrThrow(call(new URL('/sendPayment', urlBase), 'post', { body: JSON.stringify(transaction) }))
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { salesApi } from '@defra-fish/connectors-lib'
import { processRecurringPayments } from '../recurring-payments-processor.js'
import { processRecurringPayments, processPayment } from '../recurring-payments-processor.js'

jest.mock('@defra-fish/business-rules-lib')
jest.mock('@defra-fish/connectors-lib', () => ({
Expand All @@ -8,7 +8,9 @@ jest.mock('@defra-fish/connectors-lib', () => ({
preparePermissionDataForRenewal: jest.fn(() => ({
licensee: { countryCode: 'GB-ENG' }
})),
createTransaction: jest.fn()
createTransaction: jest.fn(),
sendPayment: jest.fn(),
processPayment: jest.fn()
}
}))

Expand Down Expand Up @@ -241,5 +243,20 @@ describe('recurring-payments-processor', () => {

expect(salesApi.createTransaction.mock.calls).toEqual(expectedData)
})

it('logs an error and throws it when sendPayment fails', async () => {
const transaction = { id: 'transaction-id' }
const error = new Error('Payment failed')

salesApi.sendPayment.mockImplementationOnce(() => {
throw error
})

const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(jest.fn())

await expect(processPayment(transaction)).rejects.toThrow(error)

expect(consoleLogSpy).toHaveBeenCalledWith('Error sending payment', JSON.stringify(transaction))
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const processRecurringPayment = async record => {
const transactionData = await processPermissionData(referenceNumber)
console.log('Creating new transaction based on', referenceNumber)
try {
const response = await salesApi.createTransaction(transactionData)
const transaction = await salesApi.createTransaction(transactionData)
const response = await processPayment(transaction)
console.log('New transaction created:', response)
} catch (e) {
console.log('Error creating transaction', JSON.stringify(transactionData))
Expand Down Expand Up @@ -56,3 +57,14 @@ const prepareStartDate = permission => {
.utc()
.toISOString()
}

export const processPayment = async transaction => {
try {
const response = await salesApi.sendPayment(transaction)
console.log('Payment sent successfully:', response)
return response
} catch (e) {
console.log('Error sending payment', JSON.stringify(transaction))
throw e
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,106 @@
import dueRecurringPayments from '../recurring-payments.js'
import { getRecurringPayments } from '../../../services/recurring-payments.service.js'
import { getRecurringPayments, processRecurringPayment } from '../../../services/recurring-payments.service.js'
import { preparePayment } from '../../../../../gafl-webapp-service/src/processors/payment.js'
import { sendPayment } from '../../../../../gafl-webapp-service/src/services/payment/govuk-pay-service.js'

jest.mock('../../../services/recurring-payments.service.js', () => ({
getRecurringPayments: jest.fn()
getRecurringPayments: jest.fn(),
processRecurringPayment: jest.fn()
}))

const getMockRequest = ({ date = '2023-10-19' }) => ({
jest.mock('../../../../../gafl-webapp-service/src/processors/payment.js', () => ({
preparePayment: jest.fn()
}))

jest.mock('../../../../../gafl-webapp-service/src/services/payment/govuk-pay-service.js', () => ({
sendPayment: jest.fn()
}))

const getMockRequest = ({ transactionRecord = {}, contact = {}, date = '2023-10-19' } = {}) => ({
payload: { transactionRecord, contact },
params: { date }
})

const getMockResponseToolkit = () => ({
response: jest.fn()
})
const getMockResponseToolkit = () => {
const code = jest.fn()
const response = jest.fn().mockReturnValue({ code })
return { response }
}

describe('recurring payments', () => {
beforeEach(jest.clearAllMocks)

it('handler should return continue response', async () => {
const request = getMockRequest({})
it('handler should return the response from getRecurringPayments', async () => {
const date = '2023-10-19'
const mockResponseData = { some: 'data' }

getRecurringPayments.mockResolvedValue(mockResponseData)

const request = getMockRequest({ date })
const responseToolkit = getMockResponseToolkit()
expect(await dueRecurringPayments[0].handler(request, responseToolkit)).toEqual(responseToolkit.continue)

await dueRecurringPayments[0].handler(request, responseToolkit)

expect(responseToolkit.response).toHaveBeenCalledWith(mockResponseData)
})

it('should call getRecurringPayments with date', async () => {
const date = Symbol('date')
const request = getMockRequest({ date })
await dueRecurringPayments[0].handler(request, getMockResponseToolkit())
const responseToolkit = getMockResponseToolkit()

await dueRecurringPayments[0].handler(request, responseToolkit)

expect(getRecurringPayments).toHaveBeenCalledWith(date)
})
})

describe('POST /processRecurringPayment', () => {
it('should return 404 if no recurringPayment is found', async () => {
processRecurringPayment.mockResolvedValue({ recurringPayment: null })

const request = getMockRequest()
const responseToolkit = getMockResponseToolkit()

await dueRecurringPayments[1].handler(request, responseToolkit)

expect(processRecurringPayment).toHaveBeenCalledWith(request.payload.transactionRecord, request.payload.contact)

expect(responseToolkit.response).toHaveBeenCalledWith({ error: 'No recurring payment found' })
expect(responseToolkit.response().code).toHaveBeenCalledWith(404)
})

it('should process payment and return response', async () => {
const recurringPayment = { id: 'test-recurring-payment' }
const preparedPayment = { id: 'test-prepared-payment' }
const paymentResponse = { id: 'test-payment-response' }

processRecurringPayment.mockResolvedValue({ recurringPayment })
preparePayment.mockReturnValue(preparedPayment)
sendPayment.mockResolvedValue(paymentResponse)

const request = getMockRequest()
const responseToolkit = getMockResponseToolkit()

await dueRecurringPayments[1].handler(request, responseToolkit)

expect(processRecurringPayment).toHaveBeenCalledWith(request.payload.transactionRecord, request.payload.contact)
expect(preparePayment).toHaveBeenCalledWith(request, recurringPayment)
expect(sendPayment).toHaveBeenCalledWith(preparedPayment)

expect(responseToolkit.response).toHaveBeenCalledWith(paymentResponse)
})

it('should return 500 if an error occurs', async () => {
const error = new Error('Test error')
processRecurringPayment.mockRejectedValue(error)

const request = getMockRequest()
const responseToolkit = getMockResponseToolkit()

await dueRecurringPayments[1].handler(request, responseToolkit)

expect(responseToolkit.response).toHaveBeenCalledWith({ error: 'Failed to process recurring payment' })
expect(responseToolkit.response().code).toHaveBeenCalledWith(500)
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getRecurringPayments } from '../../services/recurring-payments.service.js'
import { getRecurringPayments, processRecurringPayment } from '../../services/recurring-payments.service.js'
import { preparePayment } from '../../../../gafl-webapp-service/src/processors/payment.js'
import { sendPayment } from '../../../../gafl-webapp-service/src/services/payment/govuk-pay-service.js'

const HTTP_STATUS_NOT_FOUND = 404
const HTTP_STATUS_INTERNAL_SERVER_ERROR = 500

export default [
{
method: 'GET',
Expand All @@ -8,5 +14,27 @@ export default [
const result = await getRecurringPayments(date)
return h.response(result)
}
},
{
method: 'POST',
path: '/processRecurringPayment',
handler: async (request, h) => {
try {
const { transactionRecord, contact } = request.payload
const { recurringPayment } = await processRecurringPayment(transactionRecord, contact)

if (!recurringPayment) {
return h.response({ error: 'No recurring payment found' }).code(HTTP_STATUS_NOT_FOUND)
}

const preparedPayment = preparePayment(request, recurringPayment)
const paymentResponse = await sendPayment(preparedPayment)

return h.response(paymentResponse)
} catch (error) {
console.error('Error processing recurring payment:', error)
return h.response({ error: 'Failed to process recurring payment' }).code(HTTP_STATUS_INTERNAL_SERVER_ERROR)
}
}
}
]
Loading