diff --git a/app/controllers/web-payments/payment-auth-request.controller.js b/app/controllers/web-payments/payment-auth-request.controller.js index 15f19b079..c094cb381 100644 --- a/app/controllers/web-payments/payment-auth-request.controller.js +++ b/app/controllers/web-payments/payment-auth-request.controller.js @@ -4,14 +4,14 @@ const logger = require('../../utils/logger')(__filename) const logging = require('../../utils/logging') const { getLoggingFields } = require('../../utils/logging-fields-helper') -const connectorClient = require('../../services/clients/connector.client') +const connectorClient = require('../../services/clients/connector-axios.client') const normaliseApplePayPayload = require('./apple-pay/normalise-apple-pay-payload') const normaliseGooglePayPayload = require('./google-pay/normalise-google-pay-payload') const { CORRELATION_HEADER } = require('../../../config/correlation-header') const { setSessionVariable } = require('../../utils/cookies') const normalise = require('../../services/normalise-charge') -module.exports = (req, res, next) => { +module.exports = async (req, res, next) => { const { chargeData, chargeId, params } = req const charge = normalise.charge(chargeData, chargeId) const { wallet } = params @@ -40,24 +40,24 @@ module.exports = (req, res, next) => { payload } - return connectorClient({ correlationId: req.headers[CORRELATION_HEADER] }).chargeAuthWithWallet(chargeOptions, getLoggingFields(req)) - .then(data => { - setSessionVariable(req, `ch_${(chargeId)}.webPaymentAuthResponse`, { - statusCode: data.statusCode, - ...data.body && data.body.error_identifier && { errorIdentifier: data.body.error_identifier } - }) + try { + const result = await connectorClient({ correlationId: req.headers[CORRELATION_HEADER] }).chargeAuthWithWallet(chargeOptions, getLoggingFields(req)) - // Always return 200 - the redirect checks if there are any errors - res.status(200) - res.send({ url: `/handle-payment-response/${wallet}/${chargeId}` }) + setSessionVariable(req, `ch_${(chargeId)}.webPaymentAuthResponse`, { + statusCode: result.status, + ...result.data && result.data.error_identifier && { errorIdentifier: result.data.error_identifier } }) - .catch(err => { - logger.error(`Error while trying to authorise ${wallet} Pay payment`, { - ...getLoggingFields(req), - error: err - }) - res.status(200) - // Always return 200 - the redirect handles the error - res.send({ url: `/handle-payment-response/${wallet}/${chargeId}` }) + + // Always return 200 - the redirect checks if there are any errors + res.status(200) + res.send({ url: `/handle-payment-response/${wallet}/${chargeId}` }) + } catch (err) { + logger.error(`Error while trying to authorise ${wallet} Pay payment`, { + ...getLoggingFields(req), + error: err }) + res.status(200) + // Always return 200 - the redirect handles the error + res.send({ url: `/handle-payment-response/${wallet}/${chargeId}` }) + } } diff --git a/test/controllers/web-payments/payment-auth-request.controller.test.js b/test/controllers/web-payments/payment-auth-request.controller.test.js index 66b91ad0b..e03c38f41 100644 --- a/test/controllers/web-payments/payment-auth-request.controller.test.js +++ b/test/controllers/web-payments/payment-auth-request.controller.test.js @@ -104,7 +104,7 @@ describe('The web payments auth request controller', () => { return proxyquire('../../../app/controllers/web-payments/payment-auth-request.controller.js', proxyquireMocks) } - it('should set payload in the session and return handle payment url', done => { + it('should set payload in the session and return handle payment url', async () => { const res = { status: sinon.spy(), send: sinon.spy() @@ -118,17 +118,15 @@ describe('The web payments auth request controller', () => { nock(process.env.CONNECTOR_HOST) .post(`/v1/frontend/charges/${chargeId}/wallets/google`) .reply(200) - requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res).then(() => { - expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line - expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line - expect(mockCookies.setSessionVariable.calledWith(req, `ch_${chargeId}.webPaymentAuthResponse`, expectedBodySavedInSession)).to.be.ok // eslint-disable-line - done() - } - ) + + await requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res) + expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line + expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line + expect(mockCookies.setSessionVariable.calledWith(req, `ch_${chargeId}.webPaymentAuthResponse`, expectedBodySavedInSession)).to.be.ok // eslint-disable-line }) it('should set error identifier in the session for declined transaction, if it is present in the response body ' + - 'and return handle payment url', done => { + 'and return handle payment url', async () => { const res = { status: sinon.spy(), send: sinon.spy() @@ -144,16 +142,13 @@ describe('The web payments auth request controller', () => { .post(`/v1/frontend/charges/${chargeId}/wallets/google`) .reply(200, { error_identifier: 'AUTHORISATION_REJECTED' }) - requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res).then(() => { - expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line - expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line - expect(mockCookies.setSessionVariable.calledWith(req, `ch_${chargeId}.webPaymentAuthResponse`, expectedBodySavedInSession)).to.be.ok // eslint-disable-line - done() - } - ) + await requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res) + expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line + expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line + expect(mockCookies.setSessionVariable.calledWith(req, `ch_${chargeId}.webPaymentAuthResponse`, expectedBodySavedInSession)).to.be.ok // eslint-disable-line }) - it('should not set payload in the session and return handle payment url if error', done => { + it('should not set payload in the session and return handle payment url if error', async () => { const res = { status: sinon.spy(), send: sinon.spy() @@ -164,16 +159,14 @@ describe('The web payments auth request controller', () => { nock(process.env.CONNECTOR_HOST) .post(`/v1/frontend/charges/${chargeId}/wallets/apple`) .replyWithError('oops') - requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res).then(() => { - expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line - expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line - expect(mockCookies.setSessionVariable.called).to.be.false // eslint-disable-line - done() - } - ) + + await requirePaymentAuthRequestController(mockNormalise, mockCookies)(req, res) + expect(res.status.calledWith(200)).to.be.ok // eslint-disable-line + expect(res.send.calledWith({ url: `/handle-payment-response/google/${chargeId}` })).to.be.ok // eslint-disable-line + expect(mockCookies.setSessionVariable.called).to.be.false // eslint-disable-line }) - it('should call the `next` function when the Google Pay normalise function throws an error', done => { + it('should call the `next` function when the Google Pay normalise function throws an error', async () => { const res = { status: sinon.spy(), send: sinon.spy() @@ -187,10 +180,8 @@ describe('The web payments auth request controller', () => { throw error } - requirePaymentAuthRequestController(mockNormaliseThrowException, mockCookies)(req, res, next) + await requirePaymentAuthRequestController(mockNormaliseThrowException, mockCookies)(req, res, next) sinon.assert.calledWith(next, error) - - done() }) }) })