maxelpaytest-js
Version:
Use MaxelPay for secure crypto payments.
42 lines (36 loc) • 1.35 kB
JavaScript
import CryptoJS from 'crypto-js';
const maxelpayCheckout = async (payload, api_key, secret_key, environment) => {
const encryption = (secret_key, payload) => {
const endpoint = `https://api.maxelpay.com/v1/${environment}/merchant/order/checkout`;
const key = CryptoJS.enc.Utf8.parse(secret_key);
const ivHex = CryptoJS.enc.Utf8.parse(secret_key.substr(0, 16));
const encrypted = CryptoJS.AES.encrypt(payload, key, {
iv: ivHex,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
};
payload = JSON.stringify(payload);
const encrypt_payload = encryption(secret_key, payload);
const body = {
data: encrypt_payload
};
const headers = {
"api-key": api_key,
"Content-Type": "application/json"
};
try {
const response = await fetch(endpoint, {
method: "POST",
headers: headers,
body: JSON.stringify(body)
});
const data = await response.json(); // Await here to wait for response.json() to resolve
return data; // Return the data after the API call is completed
} catch (error) {
console.error('Error in API call:', error);
throw error;
}
};
export default maxelpayCheckout;