Skip to content

Commit

Permalink
Merge pull request #3892 from alphagov/pp-12853-remove-requestretry-path
Browse files Browse the repository at this point in the history
Remove code path for Apple Pay validation with request retry
  • Loading branch information
marcotranchino authored Aug 12, 2024
2 parents 8c60a96 + 2312a43 commit 59f360b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 337 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"filename": "app/controllers/web-payments/apple-pay/merchant-validation.controller.js",
"hashed_secret": "1348b145fa1a555461c1b790a2f66614781091e9",
"is_verified": false,
"line_number": 19
"line_number": 16
}
],
"test/controllers/web-payments/apple-pay/normalise-apple-pay-payload.test.js": [
Expand Down Expand Up @@ -389,5 +389,5 @@
}
]
},
"generated_at": "2024-08-09T08:40:47Z"
"generated_at": "2024-08-09T13:55:54Z"
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

const request = require('requestretry') // to be removed once axios is in use
const logger = require('../../../utils/logger')(__filename)
const { getLoggingFields } = require('../../../utils/logging-fields-helper')
const axios = require('axios')
const { HttpsProxyAgent } = require('hpagent')
const proxyUrl = process.env.HTTPS_PROXY
const applePayMerchantValidationViaAxios = process.env.APPLE_PAY_MERCHANT_VALIDATION_VIA_AXIOS === 'true'


function getCertificateMultiline (cert) {
return `-----BEGIN CERTIFICATE-----
Expand Down Expand Up @@ -54,104 +51,41 @@ module.exports = async (req, res) => {
return res.sendStatus(400)
}

const httpsAgent = new HttpsProxyAgent({
proxy: proxyUrl,
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key
})

const axiosInstance = axios.create({ httpsAgent, proxy: false });

if (proxyUrl) {
logger.info('Using proxy URL')
} else {
logger.info('No proxy URL. Warning: this would only happen when running locally without egress proxy.')
}

const options = applePayMerchantValidationViaAxios ?
{
url: url,
method: 'post',
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key,
headers: { 'Content-Type': 'application/json' },
data: {
merchantIdentifier: merchantIdentityVars.merchantIdentifier,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
},
httpsAgent: httpsAgent
} :
{
url: url,
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key,
method: 'post',
body: {
merchantIdentifier: merchantIdentityVars.merchantIdentifier,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
},
json: true
}
logger.info('Generating Apple Pay session via axios and https proxy agent (hpagent)')

if (applePayMerchantValidationViaAxios) {
if (proxyUrl) {
logger.info('Generating Apple Pay session via axios and https proxy agent (hpagent)')

const data = {
merchantIdentifier: merchantIdentityVars.merchantIdentifier,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
}
const data = {
merchantIdentifier: merchantIdentityVars.merchantIdentifier,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
}

try {
const response = await axiosInstance.post(url, data, { headers: { 'Content-Type': 'application/json; charset=utf-8' } })
const httpsAgent = new HttpsProxyAgent({
proxy: proxyUrl,
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key
})

logger.info('Apple Pay session successfully generated via axios and https proxy agent (hpagent)')
res.status(200).send(response.data)
} catch (error) {
logger.info('Error generating Apple Pay session with axios and https proxy agent (hpagent)', {
...getLoggingFields(req),
error: error.message,
status: error.response ? error.response.status : 'No status'
})
logger.info('Apple Pay session via axios and https proxy agent (hpagent) failed', 'Apple Pay Error')
res.status(500).send('Apple Pay Error')
}
} else {
logger.info('Generating Apple Pay session via axios and https proxy agent (hpagent) (NO PROXY)')
try {
const response = await axios(options)
const axiosInstance = axios.create({ httpsAgent, proxy: false });

logger.info('Apple Pay session successfully generated via axios and https proxy agent (hpagent) (NO PROXY)')
res.status(200).send(response.data)
} catch (error) {
logger.info('Error generating Apple Pay session (NO PROXY)', {
...getLoggingFields(req),
error: error.message
})
logger.info('Apple Pay session via axios and https proxy agent (hpagent) with no proxy failed', 'Apple Pay Error')
res.status(500).send('Apple Pay Error')
}
}
try {
const response = await axiosInstance.post(url, data, { headers: { 'Content-Type': 'application/json; charset=utf-8' } })

} else {
logger.info('Generating Apple Pay session via request retry')
request(options, (err, response, body) => {
if (err) {
logger.info('Error generating Apple Pay session', {
...getLoggingFields(req),
error: err,
response: response,
body: body
})
logger.info('Apple Pay session via request retry failed', body)
return res.status(500).send(body)
}
logger.info('Apple Pay session successfully generated via request retry')
res.status(200).send(body)
logger.info('Apple Pay session successfully generated via axios and https proxy agent (hpagent)')
res.status(200).send(response.data)
} catch (error) {
logger.info('Error generating Apple Pay session with axios and https proxy agent (hpagent)', {
...getLoggingFields(req),
error: error.message,
status: error.response ? error.response.status : 'No status'
})
logger.info('Apple Pay session via axios and https proxy agent (hpagent) failed', 'Apple Pay Error')
res.status(500).send('Apple Pay Error')
}
}
22 changes: 0 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"punycode": "1.4.1",
"randomstring": "^1.3.0",
"request": "^2.88.2",
"requestretry": "^7.1.0",
"serve-favicon": "2.5.0",
"staticify": "5.0.x",
"throng": "5.0.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe('Validate with Apple the merchant is legitimate', () => {
process.env.STRIPE_APPLE_PAY_MERCHANT_ID = stripeMerchantId
process.env.STRIPE_APPLE_PAY_MERCHANT_ID_CERTIFICATE = stripeCertificate
process.env.STRIPE_APPLE_PAY_MERCHANT_ID_CERTIFICATE_KEY = stripeKey
process.env.APPLE_PAY_MERCHANT_VALIDATION_VIA_AXIOS = 'true'

sendSpy = sinon.spy()
res = {
Expand All @@ -47,41 +46,6 @@ describe('Validate with Apple the merchant is legitimate', () => {
}
})

describe('when running locally with no proxy', () => {
it('should return a payload for a Worldpay payment if Merchant is valid', async () => {
const axiosStub = sinon.stub().resolves(appleResponse)
const controller = getControllerWithMocks(axiosStub)

const req = {
body: {
url,
paymentProvider: 'worldpay'
}
}
await controller(req, res)

sinon.assert.calledOnce(axiosStub)
const axiosCallArg = axiosStub.getCall(0).args[0]

sinon.assert.match(axiosCallArg, {
url: url,
method: 'post',
cert: sinon.match(cert => cert.includes(worldpayCertificate)),
key: sinon.match(key => key.includes(worldpayKey)),
headers: { 'Content-Type': 'application/json' },
data: {
merchantIdentifier: worldpayMerchantId,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: merchantDomain
}
})

sinon.assert.calledWith(res.status, 200)
sinon.assert.calledWith(sendSpy, appleResponse.data)
})
})

describe('when there is a proxy', () => {
beforeEach(() => {
process.env.HTTPS_PROXY = 'https://fakeproxy.com'
Expand Down
Loading

0 comments on commit 59f360b

Please sign in to comment.