api-beep-onboarding
Version:
Oracle OCI FaaS for Api Beep Onboarding library
133 lines (110 loc) • 4.8 kB
JavaScript
/**
*
* BeePay 2.023-2.024 - Oracle OCI FaaS for Api Beep Onboarding
* Way4 WS runtime SOAP request/response client implementation
*
* Changes:
* jlugo: 2023-dic-01. File creation
* jlugo: 2023-jan-16. Update merchant ID and merchant Amex for Store, to use store procedure for database validation
* jlugo: 2024-apr-25. Moved to a library
* jlugo: 2024-jul-15. Add Way4 request/response to OCI log
*/
/* RGVzYXJyb2xsYWRvIHBvciBKb25hdGhhbiBMdWdv */
import { readFileSync } from "node:fs";
import got from "got";
import { parseStringPromise } from "xml2js";
import { env } from "./utils.js";
/**
* Web Services execution kind of errors
*/
export class WSSoapError {
#error;
constructor(error) {
this.#error = error;
}
get Error() {
return this.#error;
}
}
/**
* SOAP client web services Way4 runtime caller
*/
export class WSSoapClient {
m_cert;
m_debug;
#xml2jsOptions; //--- XML to JSON response transform options
/**
*
* @param {String} cert - WS server certificate /path/cert-filename.p12
* @param {Object} debug - node.js console object
*/
constructor(cert, debug) {
this.m_cert = cert;
this.m_debug = debug;
this.#xml2jsOptions = {
normalizeTags: true,
normalize: true,
explicitRoot: true,
ignoreAttrs: false,
async: true,
explicitArray: false
};
}
/**
*
* @param {String} request - SOAP WS XML request body
* @returns {Promise<Object>} - SOAP WS XML response body
*/
async callSoapWSAsync(request) {
try {
const res = await got(env.baseUrl,
{
method: "POST",
https: {
pfx: readFileSync(this.m_cert),
passphrase: "",
rejectUnauthorized: false
},
headers: {
"user-agent": "Api Beep Onboarding JLugo",
"Content-Type": "application/xml",
"Accept": "application/xml"
},
followRedirect: false,
throwHttpErrors: true,
body: request,
responseType: "text",
encoding: "utf8",
hooks: {
beforeRequest: [
options => {
this.m_debug?.log(options.body.replace(/(\r\n|\n|\r)/gm, ""));
}
],
afterResponse: [
async (response) => {
this.m_debug?.log(response.body.replace(/(\r\n|\n|\r)/gm, ""));
const body = await parseStringPromise(response.body, this.#xml2jsOptions);
if (body["s:envelope"]["s:body"]["sendresponse"]) {
response.body = body["s:envelope"]["s:body"]["sendresponse"]["sendresult"]["response"];
//--- this.m_debug?.log(response.body);
return response;
}
// body["s:envelope"]["s:body"]["fault"].faultcode = s:Client;
// .faultstring: "Invalid request" .
throw new Error("Invalid web services request");
}
]
}
});
//--- Set wsruntime response object
return { ok: true, body: res.body };
}
catch(ex) {
this.m_debug?.log(`APIOB-SOAP01. ${ex.message}`);
//--- Set response object w/error. GOT: throws automatically when response.ok=false, and throwHttpErrors is true
return { ok: false, body: { apiCode: "APIOB-SOAP01", apiError: ex.message } };
}
}
}
export default WSSoapClient;