-
Notifications
You must be signed in to change notification settings - Fork 2
/
call-dwp-benefit-checker-soap-service.js
70 lines (64 loc) · 2.68 KB
/
call-dwp-benefit-checker-soap-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const axios = require('axios')
const util = require('util')
const parseStringAsync = util.promisify(require('xml2js').parseString)
const xpath = require('xml2js-xpath')
const log = require('../log')
const config = require('../../../config')
// Creating HTTP request rather than using SOAP client as node SOAP clients are unreliable
module.exports = function (visitorDwpBenefitCheckerData) {
log.info(`call-dwp-benefit-checker-soap-service DWP_BENEFIT_CHECKER_ENABLED: ${config.DWP_BENEFIT_CHECKER_ENABLED}`)
if (config.DWP_BENEFIT_CHECKER_ENABLED !== 'true') {
return Promise.resolve({
visitorId: visitorDwpBenefitCheckerData.visitorId,
result: 'NOT-RUN',
})
}
const options = {
method: 'POST',
url: config.DWP_BENEFIT_CHECKER_URL,
rejectUnauthorized: false, // Would require injecting valid certificate subject to change
data: getSoapBenefitCheckerRequestBody(visitorDwpBenefitCheckerData),
headers: {
'content-type': 'text/xml',
SOAPAction: config.DWP_BENEFIT_CHECKER_URL,
},
}
return axios(options)
.then(function (responseBody) {
return parseStringAsync(responseBody.data)
.then(function (xml) {
const result = xpath.find(xml, '//ns2:benefitCheckerStatus')
if (result && result[0] && result[0]._) {
const status = result[0]._.toString().toUpperCase()
return {
visitorId: visitorDwpBenefitCheckerData.visitorId,
result: status,
}
}
throw new Error(`Could not parse response: ${responseBody}`)
})
.catch(function (error) {
log.error('Error parsing XML', error)
})
})
.catch(function (error) {
log.error('Error calling Benefit checker', error)
})
}
function getSoapBenefitCheckerRequestBody(data) {
return `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<benefitCheckerRequest xmlns="https://lsc.gov.uk/benefitchecker/service/1.0/API_1.0_Check">
<lscServiceName>${config.DWP_BENEFIT_CHECKER_LSCSERVICENAME}</lscServiceName>
<clientOrgId>${config.DWP_BENEFIT_CHECKER_CLIENTORGID}</clientOrgId>
<clientUserId>${config.DWP_BENEFIT_CHECKER_CLIENTUSERID}</clientUserId>
<clientReference>${config.DWP_BENEFIT_CHECKER_CLIENTREFERENCE}</clientReference>
<nino>${data.nino}</nino>
<surname>${data.surname}</surname>
<dateOfBirth>${data.dateOfBirth}</dateOfBirth>
<dateOfAward></dateOfAward>
</benefitCheckerRequest>
</soapenv:Body>
</soapenv:Envelope>`
}