Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/OTapi/lib/ drwxr-xr-x | |
| Viewing file: Select action/file-type: const axios = require('axios');
const crypto = require('crypto');
const dotenv = require('dotenv');
const xml2js = require('xml2js');
dotenv.config();
const { INSTANCE_KEY, SECRET, OTAPI_BASE } = process.env;
/**
* Generate OTAPI Signature and Timestamp
* @param {string} methodName - The name of the OTAPI method (e.g., "GetCategoryInfo")
* @param {Object} params - Key-value object of parameters (excluding signature & timestamp)
* @param {string} secret - Your OTAPI secret key (provided by OTAPI support)
* @returns {{ signature: string, timestamp: string }}
*/
function generateOtapiSignature(methodName, params, secret) {
// Generate timestamp in UTC (yyyyMMddHHmmss)
const now = new Date();
const pad = (n) => String(n).padStart(2, '0');
const timestamp = `${now.getUTCFullYear()}${pad(now.getUTCMonth() + 1)}${pad(now.getUTCDate())}${pad(now.getUTCHours())}${pad(now.getUTCMinutes())}${pad(now.getUTCSeconds())}`;
// Add timestamp to the parameters object
const fullParams = { ...params, timestamp };
// Sort parameters by name
const sortedKeys = Object.keys(fullParams).sort();
// Concatenate values in order (without URL encoding)
const concatenatedValues = sortedKeys.map(key => fullParams[key]).join('');
// Final string to hash
const stringToHash = `${methodName}${concatenatedValues}${secret}`;
// Generate SHA256 hash in hex format
const signature = crypto.createHash('sha256').update(stringToHash).digest('hex');
return { signature, timestamp };
}
function parseXmlToJson(xmlString) {
return new Promise((resolve, reject) => {
xml2js.parseString(
xmlString,
{ explicitArray: false, mergeAttrs: true }, // Makes result cleaner
(err, result) => {
if (err) reject(err);
else resolve(result);
}
);
});
}
// 🔧 Example usage
module.exports={
callOTAPI:async function (methodName, params = {}) {
INSTANCE_KEY, SECRET, OTAPI_BASE
const result = generateOtapiSignature(methodName, params, SECRET);
// console.log('Timestamp:', result.timestamp);
// console.log('Signature:', result.signature);
// Optionally, construct the full URL
const queryString = Object.entries({ ...params, timestamp: result.timestamp, signature: result.signature })
.map(([key, val]) => `${key}=${encodeURIComponent(val)}`)
.join('&');
const fullUrl = `http://otapi.net/service/${methodName}?${queryString}`;
// console.log('Request URL:', fullUrl);
const resp = await axios.get(fullUrl);
// console.log(resp.data);
if (resp.data.error) throw new Error(resp.data.error.message);
try {
if (resp.headers['content-type']?.includes('xml')) {
const json = await parseXmlToJson(resp.data);
// console.log(json.BatchItemSearchResultAnswer);
return json;
} else {
const json = JSON.parse(data);
return json;
// console.log("json");
}
} catch (err) {
console.log("Error parsing XML:", err);
}
// return fullUrl;
// return resp.data.result;
}
}
// module.exports = { callOTAPI };
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0041 ]-- |