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

Revert "PP-11682 Update update payment auth request controller" #3837

Merged
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
38 changes: 19 additions & 19 deletions app/controllers/web-payments/payment-auth-request.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}` })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
})
})
})
Loading