UNPKG

1sc-api

Version:
150 lines (118 loc) 4.01 kB
// One Shopping Cart API // -- This is a NodeJS version of the PHP Api Script var when = require('when'), https = require('https'), qs = require('querystring'), xml2js = require('xml2js'); var url = require('url'); var xmlParser = xml2js.ParserString; var OneShopAPI = function(settings) { this.settings = settings || { merchantId: '', merchantKey: '', apiUri: '' }; var urlParts = url.parse(settings.apiUri); this.settings.hostname = urlParts.host; this.settings.path = urlParts.path; this.apiParameters = {}; }; OneShopAPI.prototype.AddApiParameter = function(parameterKey, parameterValue) { this.apiParameters[parameterKey] = parameterValue; }; OneShopAPI.prototype.ClearApiParameters = function() { self.apiParameters = {}; }; OneShopAPI.prototype.XlinkApiRequest = function(xlink, parameters) { parameters = parameters || ''; var requestBody = this.CreateRequestString(); return this.SendHttpRequest(xlink, requestBody); }; OneShopAPI.prototype.ApiRequest = function(path, parameters) { parameters = parameters || ''; var uri = this.settings.path + 'API/' + this.settings.merchantId + path; var requestBody = this.CreateRequestString(); return this.SendHttpRequest(uri, requestBody); }; OneShopAPI.prototype.CreateRequestString = function() { var requestBody = '<Request>' + '<Key>' + this.settings.merchantKey + '</Key>' + this.ParseApiParameters(this.apiParameters) + '</Request>'; return requestBody; }; OneShopAPI.prototype.ParseApiParameters = function() { var self = this; var request_payload = ''; for (var prop in self.apiParameters) { if (!self.apiParameters.hasOwnProperty(prop).join) { request_payload += '<' + prop + '>' + self.apiParameters[prop] + '</' + prop + ">\r\n"; } else { // PHP API has this code, but doesn't have a createRequest command // request_payload += '<' + prop + ">\r\n" + // self.createRequest(self.apiParameters[prop]) + // '</' + prop + ">\r\n"; } } return request_payload; }; OneShopAPI.prototype.GetOrdersList = function() { return this.ApiRequest('/ORDERS/LIST'); }; OneShopAPI.prototype.GetOrderCount = function() { return this.ApiRequest('/ORDERS/COUNT'); }; OneShopAPI.prototype.GetOrderById = function(orderId) { return this.ApiRequest('/ORDERS/' + orderId + '/READ'); }; OneShopAPI.prototype.GetProductsList = function() { return this.ApiRequest('/PRODUCTS/LIST'); }; OneShopAPI.prototype.GetProductById = function(productId) { return this.ApiRequest('/PRODUCTS/' + productId + '/READ'); }; OneShopAPI.prototype.GetClientsList = function() { return this.ApiRequest('/CLIENTS/LIST'); }; OneShopAPI.prototype.GetClientById = function(clientId) { return this.ApiRequest('/CLIENTS/' + clientId + '/READ'); }; OneShopAPI.prototype.GetErrorsList = function() { return this.ApiRequest('/ERRORS/LIST'); }; OneShopAPI.prototype.GerAvailableApiMethods = function() { return this.ApiRequest(''); }; OneShopAPI.prototype.SendHttpRequest = function(uri, requestBody) { var self = this; var defer = when.defer(); var options = { hostname: self.settings.hostname, path: uri, method: 'POST' }; options.port = self.settings.port || 443; var xml = requestBody; options.headers = { 'content-length': xml.length }; //console.log('Request : ', options); var req = https.request(options, function(res) { var retData = ''; res.on('data', function(chunk) { retData += chunk; }); res.on('end', function() { defer.resolve(retData); }); }); req.on('error', function(err) { defer.resolve(err); }); req.write(xml); req.end(); return defer.promise; }; module.exports = OneShopAPI;