@playmoweb/payline-typescript-sdk
Version:
Payline Typescript SDK
83 lines • 2.76 kB
JavaScript
import soap from "soap";
import path from "path";
import { fileURLToPath } from "url";
import { ConsolePaylineLogger } from "./extensions/payline-logger.js";
import { PaylineUtils } from "./payline-utils.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DEFAULT_HOMOLOGATION_WSDL = path.join(__dirname, "../config/wsdls/homologation/WebPaymentAPI.xml");
const DEFAULT_PRODUCTION_WSDL = path.join(__dirname, "../config/wsdls/production/WebPaymentAPI.xml");
/**
* Payline service
*/
class Payline {
merchantId;
accessKey;
contractNumber;
wsdl;
options;
production = false;
debugMode = false;
logger;
clientInitialization;
constructor(merchantId, accessKey, contractNumber, options = {}, production = false, debugMode = false) {
this.merchantId = merchantId;
this.accessKey = accessKey;
this.contractNumber = contractNumber;
this.options = options;
this.production = production;
this.debugMode = debugMode;
this.logger = new ConsolePaylineLogger(debugMode);
if (production) {
this.wsdl = DEFAULT_PRODUCTION_WSDL;
}
else {
this.wsdl = DEFAULT_HOMOLOGATION_WSDL;
}
}
setWsdl(wsdl) {
this.wsdl = wsdl;
}
getContractNumber() {
return this.contractNumber;
}
/**
* Get the soap client with this configuration
*/
getClient() {
if (this.clientInitialization) {
return this.clientInitialization;
}
this.clientInitialization = new Promise((resolve, reject) => {
soap.createClient(this.wsdl, this.options, (err, client) => {
if (err) {
reject(err);
}
else {
resolve(client);
}
});
})
.then((client) => {
client.setSecurity(new soap.BasicAuthSecurity(this.merchantId, this.accessKey));
client.on("request", (xml) => this.logger.log("request", xml));
client.on("response", (xml) => this.logger.log("response", xml));
return client;
});
return this.clientInitialization;
}
/**
* Execute a soap method using a client and a payload.
*/
execAndCatch(method, payload) {
return this.getClient()
.then(client => {
return new Promise((resolve, reject) => {
client[method](payload, (err, response) => err ? reject(err) : resolve(response));
});
})
.then((response) => response)
.catch(err => PaylineUtils.parseErrors(err));
}
}
export { Payline };
//# sourceMappingURL=payline.js.map