File: /var/www/vhosts/sethsawariyabizmart.com/httpdocs/masterAir/functions.php
<?php
$urlx = 'https://sethsawariyabizmart.com';
$mEmail = 'test@gmail.com';
$apURL = 'https://api.pentabit.in';
$resurl = 'https://mdcsecureinstantpayfastlink.securepayz.link/secureAir1/respsecured.php';
date_default_timezone_set('Asia/Kolkata');
function generateOrder($orderid, $amt, $buyerPhone, $buyerEmail, $providerId)
{
$cred = airPayCred($providerId);
global $urlx;
logRequestData(['Req start' => $orderid], $cred['bank']);
$amt = str_replace(",", "", $amt);
$mer_dom = base64_encode($urlx);
$call_type = 'upiqr';
$alldata = $cred['mercid'] . $orderid . $amt . $buyerPhone . $buyerEmail . $mer_dom . $call_type;
$checksum = hash('SHA256', $cred['key256'] . '@' . $alldata . date('Y-m-d'));
$fields = array();
$fields['mercid'] = $cred['mercid'];
$fields['orderid'] = $orderid;
$fields['amount'] = $amt;
$fields['buyerPhone'] = $buyerPhone;
$fields['buyerEmail'] = $buyerEmail;
$fields['mer_dom'] = $mer_dom;
$fields['call_type'] = $call_type;
$json_data = json_encode($fields);
$encKey = md5($cred['secret']);
$iv = bin2hex(openssl_random_pseudo_bytes(8));
$raw = openssl_encrypt($json_data, "AES-256-CBC", $encKey, $options = OPENSSL_RAW_DATA, $iv);
$encData = $iv . base64_encode($raw);
$curl = curl_init();
$post_fields = json_encode(array('encData' => $encData, 'checksum' => $checksum, 'mercid' => $cred['mercid']));
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://kraken.airpay.co.in/airpay/api/generateOrder',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$redData = json_decode($response);
$encryptedData = $redData->data;
$iv = substr($encryptedData, 0, 16);
$data = substr($encryptedData, 16);
$DecryptedData = openssl_decrypt(base64_decode($data), 'AES-256-CBC', $encKey, $options = OPENSSL_RAW_DATA, $iv);
$res = json_decode($DecryptedData, true);
logRequestData(['Req' => $fields, 'Resp' => $res], $cred['bank']);
if ($res['status'] != 200 || $res['QRCODE_STRING'] == "") {
$providerName = "Sethsawariya ".$cred['bank'];
sendTPS(['Req' => $fields, 'Resp' => $res], $providerName);
}
return $res;
}
function generateV4token($providerId = null)
{
$cred = airPayCred($providerId);
$data = array();
$data['client_id'] = $cred['clientV4Id'];
$data['client_secret'] = $cred['clientV4Secret'];
$data['merchant_id'] = $cred['mercid'];
$data['grant_type'] = 'client_credentials';
$encdata = encrypt(json_encode($data), $cred['secretV4Key']);
$checksum = checksum($data);
$payload = [
'merchant_id' => $cred['mercid'],
'encdata' => $encdata,
'checksum' => $checksum
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://kraken.airpay.co.in/airpay/pay/v4/api/oauth2/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload
));
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result)->response;
$access_token_data = decrypt($response, $cred['secretV4Key']);
return $access_token_data;
}
function refundTxn($orderid, $airId, $amount, $providerId = null)
{
$cred = airPayCred($providerId);
logRequestData(['Refund start' => ['orderId' => $orderid, 'airId' => $airId, 'amount' => $amount]], $cred['bank']);
$amt = str_replace(",", "", $amount);
$data = array();
$data['transactions'] = base64_encode('[{"ap_transactionid": ' . $airId . ',"amount": "' . $amt . '"}]');
$privatekey = hash('sha256', $cred['secret'] . '@' . $cred['username'] . ':|:' . $cred['password']);
$encdata = encrypt(json_encode($data), $cred['secretV4Key']);
$checksum = checksum($data);
$payload = [
'merchant_id' => $cred['mercid'],
'encdata' => $encdata,
'checksum' => $checksum,
'privatekey' => $privatekey
];
$tokenData = generateV4token($providerId);
logRequestData(['Refund token res' => $tokenData, 'orderId' => $orderid, 'airId' => $airId], $cred['bank']);
$access_token = $tokenData->data->access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://kraken.airpay.co.in/airpay/pay/v4/api/refund/?token=' . $access_token,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload
));
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result)->response;
$res = decrypt($response, $cred['secretV4Key']);
logRequestData(['Refund Req' => $data, 'Refund Resp' => $res, 'orderId' => $orderid, 'airId' => $airId], $cred['bank']);
return $res;
}
function checksum($data)
{
ksort($data);
$checksumdata = '';
foreach ($data as $key => $value) {
$checksumdata .= $value;
}
return hash('SHA256', $checksumdata . date('Y-m-d'));
}
function encrypt($data, $encryptionkey)
{
$iv = bin2hex(openssl_random_pseudo_bytes(8));
$raw = openssl_encrypt($data, 'AES-256-CBC', $encryptionkey, OPENSSL_RAW_DATA, $iv);
$encryptedata = $iv . base64_encode($raw);
return $encryptedata;
}
function decrypt($response, $encryptionkey)
{
$iv = substr($response, 0, 16);
$encryptedData = substr($response, 16);
$decryptedData = openssl_decrypt(base64_decode($encryptedData), 'AES-256-CBC', $encryptionkey, OPENSSL_RAW_DATA, $iv);
return json_decode($decryptedData);
}
function customEncode($input, $key = 'secret')
{
$output = '';
for ($i = 0; $i < strlen($input); $i++) {
$char = $input[$i];
$keychar = $key[$i % strlen($key)];
$output .= chr(ord($char) + ord($keychar));
}
return bin2hex($output); // Optional: hex conversion for safe transport
}
function customDecode($input, $key = 'secret')
{
// Convert hex back to raw string
$input = hex2bin($input);
$output = '';
for ($i = 0; $i < strlen($input); $i++) {
$char = $input[$i];
$keychar = $key[$i % strlen($key)];
$output .= chr(ord($char) - ord($keychar));
}
return $output;
}
function logRequestData($data, $folder = "Default", $isOld = false)
{
// Define the log directory
if ($isOld) {
$logDir = "../../masterAir/masterCheckout/logs/$folder/";
} else {
$logDir = "logs/$folder/";
}
// Check if the log directory exists, if not, create it
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true); // Create the directory with proper permissions
}
// Get the current date to name the log file
$logFile = $logDir . 'log-' . date('Y-m-d') . '.txt';
// Check if today's log file exists, if not, create a new one
if (!file_exists($logFile)) {
// Add a header or introductory text to the new log file (optional)
file_put_contents($logFile, "Log file created on: " . date('Y-m-d H:i:s') . "\n\n");
}
// Convert the data to JSON format
if (is_array($data)) {
// Convert array to JSON
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
} elseif (is_string($data) && isJson($data)) {
// The data is already a valid JSON string
$jsonData = $data;
} else {
// If data is not valid JSON or array, handle error (optional)
$jsonData = $data;
}
// Add timestamp to the log entry
$logEntry = "Timestamp: " . date('Y-m-d H:i:s') . "\n" . $jsonData . "\n\n";
// Append the log entry to the log file
file_put_contents($logFile, $logEntry, FILE_APPEND);
}
function isJson($string)
{
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
function checkChargeback($uid, $ip = null)
{
$url = "https://master.jscloudcdn.buzz/cbAPI.php?uid=" . urlencode($uid) . "&ip=" . urlencode($ip);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10, // wait max 10 seconds
CURLOPT_SSL_VERIFYPEER => false, // ignore SSL errors
CURLOPT_SSL_VERIFYHOST => false, // ignore SSL host check
]);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
// Default return false if request fails
if ($response === false || !empty($error)) {
return false;
}
// Try to decode JSON response
$data = json_decode($response, true);
// Return true only if 'result' key exists and equals true
if (isset($data['result']) && $data['result'] === true) {
return true;
}
return false;
}
function sendTPS($data, $loc = null)
{
$url = "https://serverlogsdata.securepayz.link/logTele/sendLog.php";
$payload = json_encode([
"data" => $data,
"loc" => $loc
]);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
CURLOPT_RETURNTRANSFER => false, // no need to capture response
CURLOPT_TIMEOUT_MS => 500, // half-second timeout
CURLOPT_CONNECTTIMEOUT_MS => 500,
CURLOPT_NOSIGNAL => 1,
CURLOPT_SSL_VERIFYPEER => false, // ignore SSL issues
CURLOPT_SSL_VERIFYHOST => false
]);
@curl_exec($ch); // execute silently
curl_close($ch);
}
function sendEntryIp($userId, $orderId, $ucode, $ip,$lat =null,$lon=null)
{
$curl = curl_init();
$payload = json_encode([
"user_id" => $userId,
"order_id" => $orderId,
"ucode" => $ucode,
"ip" => $ip,
"latitude" => $lat,
"longitude" => $lon
]);
curl_setopt_array($curl, [
CURLOPT_URL => 'https://serverlogsdata.securepayz.link/logAPI/entry_ip.php',
CURLOPT_RETURNTRANSFER => false, // no need response
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
],
CURLOPT_SSL_VERIFYPEER => false, // ignore SSL
CURLOPT_SSL_VERIFYHOST => false // ignore SSL
]);
// Execute and suppress errors
try {
curl_exec($curl);
} catch (Exception $e) {
// skip errors silently
}
curl_close($curl);
}
function sendEntryVpa($ucode, $vpa)
{
$curl = curl_init();
$payload = json_encode([
"ucode" => $ucode,
"vpa" => $vpa
]);
curl_setopt_array($curl, [
CURLOPT_URL => 'https://serverlogsdata.securepayz.link/logAPI/entry_vpa.php',
CURLOPT_RETURNTRANSFER => false, // no need response
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json'
],
CURLOPT_SSL_VERIFYPEER => false, // ignore SSL
CURLOPT_SSL_VERIFYHOST => false // ignore SSL
]);
// Execute and suppress errors
try {
curl_exec($curl);
} catch (Exception $e) {
// skip errors silently
}
curl_close($curl);
}
function deactiveProvider($providerId, $mail)
{
if (isset($providerId) && $providerId) {
$curl = curl_init();
$payload = json_encode([
"provider_id" => $providerId,
]);
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.pentabit.in/api/v1/private/payin/provider/limit-exhausted',
CURLOPT_RETURNTRANSFER => false, // no need response
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'email: ' . $mail,
],
CURLOPT_SSL_VERIFYPEER => false, // ignore SSL
CURLOPT_SSL_VERIFYHOST => false // ignore SSL
]);
// Execute and suppress errors
try {
curl_exec($curl);
} catch (Exception $e) {
// skip errors silently
}
curl_close($curl);
}
}
function lateCallback($payload)
{
global $mEmail;
global $apURL;
$ch2 = curl_init();
curl_setopt_array($ch2, [
CURLOPT_URL => $apURL . "/api/v1/private/payin/late/callback",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"email: " . $mEmail,
"Content-Type: application/json"
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$dpresponse = curl_exec($ch2);
curl_close($ch2);
return $dpresponse;
}