import * as _ from 'lodash'; import * as soap from 'soap'; import * as parser from 'xml2js'; import * as path from 'path'; import { logger } from './logger'; export default class PurolatorAPI { private environment: string = this.params.environment; private debug: boolean = this.params.debug === 'true' ? true : false; private key: string = this.params.key; private password: string = this.params.password; private account: string = this.params.account; private isProduction: boolean = this.params.environment === 'production' ? true : false; private endpoint: string = this.isProduction ? 'https://webservices.purolator.com/PWS/V1' : 'https://devwebservices.purolator.com/PWS/V1'; constructor( private params: any ) { } public consolidate(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.5', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Consolidate shipment' } }; this.request(params, 'Consolidate', 'Shipping', headers, (err, res) => { return cb(err, res); }); } public createReturnShipment(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Create return shipment' } }; this.request(params, 'CreateReturnsManagementShipment', 'ReturnsManagement', headers, (err, res) => { return cb(err, res); }); } public createShipment(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.6', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Create shipment' } }; this.request(params, 'CreateShipment', 'Shipping', headers, (err, res) => { return cb(err, res); }); } public getDeliveryDetails(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get delivery details' } }; this.request(params, 'GetDeliveryDetails', 'Tracking', headers, (err, res) => { return cb(err, res); }); } public getDocuments(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get documents' } }; this.request(params, 'GetDocuments', 'ShippingDocuments', headers, (err, res) => { return cb(err, res); }); } public getQuickEstimate(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.4', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get quick estimate' } }; this.request(params, 'GetQuickEstimate', 'Estimating', headers, (err, res) => { return cb(err, res); }); } public getFullEstimate(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.4', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get full estimate' } }; this.request(params, 'GetFullEstimate', 'Estimating', headers, (err, res) => { return cb(err, res); }); } public getServicesOptions(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get services options' } }; this.request(params, 'GetServicesOptions', 'ServiceAvailability', headers, (err, res) => { return cb(err, res); }); } public getServiceRules(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get service rule' } }; this.request(params, 'GetServiceRules', 'ServiceAvailability', headers, (err, res) => { return cb(err, res); }); } public getShipmentManifestDocument(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Get manifest' } }; this.request(params, 'GetShipmentManifestDocument', 'ShippingDocuments', headers, (err, res) => { return cb(err, res); }); } public schedulePickup(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Schedule pickup' } }; this.request(params, 'SchedulePickUp', 'PickUp', headers, (err, res) => { return cb(err, res); }); } public trackPackagesByPin(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Track by pin' } }; this.request(params, 'TrackPackagesByPin', 'Tracking', headers, (err, res) => { return cb(err, res); }); } public trackPackagesByRef(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Track by ref' } }; this.request(params, 'TrackPackagesByReference', 'Tracking', headers, (err, res) => { return cb(err, res); }); } public validateCityPostalCodeZip(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Validate city postal code zip' } }; this.request(params, 'ValidateCityPostalCodeZip', 'ServiceAvailability', headers, (err, res) => { return cb(err, res); }); } public validatePickup(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Validate pickup' } }; this.request(params, 'ValidatePickUp', 'PickUp', headers, (err, res) => { return cb(err, res); }); } public validateShipment(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.6', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Validate Shipment' } }; this.request(params, 'ValidateShipment', 'Shipping', headers, (err, res) => { return cb(err, res); }); } public validateReturnShipment(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.3', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Validate return shipment' } }; this.request(params, 'ValidateReturnsManagementShipment', 'ReturnsManagement', headers, (err, res) => { return cb(err, res); }); } public voidPickup(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.2', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Void pickup' } }; this.request(params, 'VoidPickUp', 'PickUp', headers, (err, res) => { return cb(err, res); }); } public voidShipment(params: any, cb: any) { const headers = { 'ns1:RequestContext': { 'ns1:Version': '1.5', 'ns1:Language': 'en', 'ns1:GroupID': 'xxx', 'ns1:RequestReference': 'Void shipment' } }; this.request(params, 'VoidShipment', 'Shipping', headers, (err, res) => { return cb(err, res); }); } private request(params: any, fn: string, wsdl: string, headers: any, cb) { var wsdlOptions = { endpoint: `${this.endpoint}/${wsdl}/${wsdl}Service.asmx` }; soap .createClient(path.join(__dirname, 'wsdl', `${wsdl}Service.wsdl`), wsdlOptions, (err, client) => { if (err) { return cb(err, null); } client.wsdl.definitions.xmlns.ns1 = 'http://purolator.com/pws/datatypes/v1'; client.wsdl.xmlnsInEnvelope = client.wsdl._xmlnsMap(); client.addSoapHeader(headers); client.setSecurity(new soap.BasicAuthSecurity(this.key, this.password)); client[fn](params, (err, result) => { if (this.debug) { parser.parseString(client.lastRequest, { explicitArray: false }, (err, res) => { logger.info(`${fn} ok`, res); }); } if (err) { return this.handleResponseError(err, cb); } return cb(err, result); }); }); } private handleResponseError(err: any, cb: any) { try { return cb(err.root.Envelope.Body.Fault, null); } catch (e) { if (this.debug) { logger.error(err); } return cb(err, null); } } }