diff --git a/app/controllers/web-payments/payment-auth-request.controller.js b/app/controllers/web-payments/payment-auth-request.controller.js index c094cb381..15f19b079 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-axios.client') +const connectorClient = require('../../services/clients/connector.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 = async (req, res, next) => { +module.exports = (req, res, next) => { const { chargeData, chargeId, params } = req const charge = normalise.charge(chargeData, chargeId) const { wallet } = params @@ -40,24 +40,24 @@ module.exports = async (req, res, next) => { payload } - try { - const result = await connectorClient({ correlationId: req.headers[CORRELATION_HEADER] }).chargeAuthWithWallet(chargeOptions, getLoggingFields(req)) + 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 } + }) - setSessionVariable(req, `ch_${(chargeId)}.webPaymentAuthResponse`, { - statusCode: result.status, - ...result.data && result.data.error_identifier && { errorIdentifier: result.data.error_identifier } + // Always return 200 - the redirect checks if there are any errors + res.status(200) + 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 + .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}` }) }) - 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 e03c38f41..66b91ad0b 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', async () => { + it('should set payload in the session and return handle payment url', done => { const res = { status: sinon.spy(), send: sinon.spy() @@ -118,15 +118,17 @@ describe('The web payments auth request controller', () => { nock(process.env.CONNECTOR_HOST) .post(`/v1/frontend/charges/${chargeId}/wallets/google`) .reply(200) - - 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 + 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() + } + ) }) it('should set error identifier in the session for declined transaction, if it is present in the response body ' + - 'and return handle payment url', async () => { + 'and return handle payment url', done => { const res = { status: sinon.spy(), send: sinon.spy() @@ -142,13 +144,16 @@ describe('The web payments auth request controller', () => { .post(`/v1/frontend/charges/${chargeId}/wallets/google`) .reply(200, { error_identifier: 'AUTHORISATION_REJECTED' }) - 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 + 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() + } + ) }) - it('should not set payload in the session and return handle payment url if error', async () => { + it('should not set payload in the session and return handle payment url if error', done => { const res = { status: sinon.spy(), send: sinon.spy() @@ -159,14 +164,16 @@ describe('The web payments auth request controller', () => { nock(process.env.CONNECTOR_HOST) .post(`/v1/frontend/charges/${chargeId}/wallets/apple`) .replyWithError('oops') - - 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 + 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() + } + ) }) - it('should call the `next` function when the Google Pay normalise function throws an error', async () => { + it('should call the `next` function when the Google Pay normalise function throws an error', done => { const res = { status: sinon.spy(), send: sinon.spy() @@ -180,8 +187,10 @@ describe('The web payments auth request controller', () => { throw error } - await requirePaymentAuthRequestController(mockNormaliseThrowException, mockCookies)(req, res, next) + requirePaymentAuthRequestController(mockNormaliseThrowException, mockCookies)(req, res, next) sinon.assert.calledWith(next, error) + + done() }) }) })