diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/agreed-handler-recurring-payments.spec.js b/packages/gafl-webapp-service/src/handlers/__tests__/agreed-handler-recurring-payments.spec.js index 3e913708f..4355907d1 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/agreed-handler-recurring-payments.spec.js +++ b/packages/gafl-webapp-service/src/handlers/__tests__/agreed-handler-recurring-payments.spec.js @@ -36,7 +36,7 @@ describe('The agreed handler', () => { cache: () => ({ helpers: { transaction: { - get: async () => ({ id: 'id-111', cost: 0 }), + get: async () => ({ cost: 0 }), set: async () => {} }, status: { @@ -90,6 +90,41 @@ describe('The agreed handler', () => { expect(transactionPayload.id).toBe(v4guid) }) + it("doesn't overwrite transaction id if one is already set", async () => { + const setTransaction = jest.fn() + const transactionId = 'abc-123-def-456' + uuidv4.mockReturnValue('def-789-ghi-012') + salesApi.finaliseTransaction.mockReturnValueOnce({ + permissions: [] + }) + const mockRequest = { + cache: () => ({ + helpers: { + status: { + get: async () => ({ + [COMPLETION_STATUS.agreed]: true, + [COMPLETION_STATUS.posted]: true, + [COMPLETION_STATUS.finalised]: false, + [RECURRING_PAYMENT]: false + }), + set: () => {} + }, + transaction: { + get: async () => ({ cost: 0, id: transactionId }), + set: setTransaction + } + } + }) + } + + await agreedHandler(mockRequest, getRequestToolkit()) + + expect(salesApi.finaliseTransaction).toHaveBeenCalledWith( + transactionId, + undefined // prepareApiFinalisationPayload has no mocked return value + ) + }) + it('sends a recurring payment creation request to Gov.UK Pay', async () => { const preparedPayment = Symbol('preparedPayment') prepareRecurringPayment.mockResolvedValueOnce(preparedPayment) diff --git a/packages/gafl-webapp-service/src/handlers/agreed-handler.js b/packages/gafl-webapp-service/src/handlers/agreed-handler.js index 1c0e505d7..a4589dd7e 100644 --- a/packages/gafl-webapp-service/src/handlers/agreed-handler.js +++ b/packages/gafl-webapp-service/src/handlers/agreed-handler.js @@ -241,7 +241,9 @@ const finaliseTransaction = async (request, transaction, status) => { export default async (request, h) => { const status = await request.cache().helpers.status.get() const transaction = await request.cache().helpers.transaction.get() - transaction.id = uuidv4() + if (!transaction.id) { + transaction.id = uuidv4() + } // If the agreed flag is not set to true then throw an exception if (!status[COMPLETION_STATUS.agreed]) { @@ -279,6 +281,7 @@ export default async (request, h) => { // If the transaction has already been finalised then redirect to the order completed page if (!status[COMPLETION_STATUS.finalised]) { + console.log('finalising transaction', request, transaction, status) await finaliseTransaction(request, transaction, status) } else { debug('Transaction %s already finalised, redirect to order complete: %s', transaction.id)