79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
const crypto = require("crypto");
|
|
const soap = require("soap");
|
|
|
|
const config = {
|
|
KEY: "Your KEY",
|
|
IV: "Your IV",
|
|
username: "Your username",
|
|
password: "Your password",
|
|
WebServiceUrl:
|
|
"https://services.asanpardakht.net/paygate/merchantservices.asmx?WSDL",
|
|
merchantConfigurationID: "YourConfigurationID",
|
|
};
|
|
|
|
function addPadding(string, blocksize = 32) {
|
|
const pad = blocksize - (string.length % blocksize);
|
|
return string + String.fromCharCode(pad).repeat(pad);
|
|
}
|
|
|
|
function stripPadding(string) {
|
|
const pad = string.charCodeAt(string.length - 1);
|
|
return string.slice(0, -pad);
|
|
}
|
|
|
|
function encrypt(string = "") {
|
|
const key = Buffer.from(config.KEY, "base64");
|
|
const iv = Buffer.from(config.IV, "base64");
|
|
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
|
|
let encrypted = cipher.update(addPadding(string), "utf8", "base64");
|
|
encrypted += cipher.final("base64");
|
|
return encrypted;
|
|
}
|
|
|
|
function decrypt(string = "") {
|
|
const key = Buffer.from(config.KEY, "base64");
|
|
const iv = Buffer.from(config.IV, "base64");
|
|
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
|
|
let decrypted = decipher.update(string, "base64", "utf8");
|
|
decrypted += decipher.final("utf8");
|
|
return stripPadding(decrypted);
|
|
}
|
|
|
|
async function encryptWS(string = "") {
|
|
try {
|
|
const client = await soap.createClientAsync(
|
|
"https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL"
|
|
);
|
|
const args = {
|
|
aesKey: config.KEY,
|
|
aesVector: config.IV,
|
|
toBeEncrypted: string,
|
|
};
|
|
const result = await client.EncryptInAESAsync(args);
|
|
return result[0].EncryptInAESResult;
|
|
} catch (err) {
|
|
console.error("Error in EncryptWS:", err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function decryptWS(string = "") {
|
|
try {
|
|
const client = await soap.createClientAsync(
|
|
"https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL"
|
|
);
|
|
const args = {
|
|
aesKey: config.KEY,
|
|
aesVector: config.IV,
|
|
toBeDecrypted: string,
|
|
};
|
|
const result = await client.DecryptInAESAsync(args);
|
|
return result[0].DecryptInAESResult;
|
|
} catch (err) {
|
|
console.error("Error in DecryptWS:", err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
module.exports = { config, encrypt, decrypt, encryptWS, decryptWS };
|