<?php
include "../../conn.php";
include "../../functions2.php";

header('Content-Type: application/json; charset=utf-8');
header('Strict-Transport-Security: max-age=31536000');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Access-Control-Allow-Credentials: true');

$origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
header('Access-Control-Allow-Origin: ' . $origin);
header('Vary: Origin');

date_default_timezone_set("Asia/Kolkata");
$serviceNowTime = date("Y-m-d H:i:s");

/* ---------------- METHOD CHECK ---------------- */
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode([
        'code'=>11,
        'msg'=>'Method not allowed',
        'msgCode'=>12,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

/* ---------------- INPUT ---------------- */
$body = json_decode(file_get_contents("php://input"), true);
if (!is_array($body)) {
    echo json_encode([
        'code'=>7,
        'msg'=>'Invalid JSON',
        'msgCode'=>6,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

/* ---------------- PARAM CHECK ---------------- */
if (
    !isset($body['language']) ||
    !isset($body['random']) ||
    !isset($body['signature']) ||
    !isset($body['timestamp'])
) {
    echo json_encode([
        'code'=>7,
        'msg'=>'Param is Invalid',
        'msgCode'=>6,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

$language  = trim($body['language']);
$random    = trim($body['random']);
$signature = strtoupper(trim($body['signature']));

/* ---------------- SIGNATURE FIX ---------------- */
// $serverSign = strtoupper(md5($language . $random));
// if ($serverSign !== $signature) {
//     echo json_encode([
//         'code'=>5,
//         'msg'=>'Wrong signature',
//         'msgCode'=>3,
//         'serviceNowTime'=>$serviceNowTime
//     ]);
//     exit;
// }

/* ---------------- AUTH ---------------- */
if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
    echo json_encode([
        'code'=>4,
        'msg'=>'No operation permission',
        'msgCode'=>2,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

$token = explode(' ', $_SERVER['HTTP_AUTHORIZATION'])[1] ?? '';
$jwt   = json_decode(is_jwt_valid($token), true);

if ($jwt['status'] !== 'Success') {
    echo json_encode([
        'code'=>4,
        'msg'=>'No operation permission',
        'msgCode'=>2,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

/* ---------------- SESSION CHECK ---------------- */
$ses = $conn->query("SELECT id FROM shonu_subjects WHERE akshinak='$token'");
if ($ses->num_rows !== 1) {
    echo json_encode([
        'code'=>4,
        'msg'=>'No operation permission',
        'msgCode'=>2,
        'serviceNowTime'=>$serviceNowTime
    ]);
    exit;
}

/* ---------------- DATA ---------------- */
$data['typelist'] = [];

/* Wake UP APP */
$data['typelist'][] = [
    'payID'=>2,
    'payTypeID'=>0,
    'payName'=>'Wake UP-APP',
    'paySysName'=>'Online Pay',
    'payNameUrl'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20240324160846bfy7.png',
    'payNameUrl2'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon2_20240324160846gdbv.png',
    'minPrice'=>0,
    'maxPrice'=>0,
    'scope'=>null,
    'typeName'=>'Wake UP-APP',
    'typeNameCode'=>0,
    'maxRechargeRifts'=>0.00,
    'sort'=>9
];

/* UPI QR */
$data['typelist'][] = [
    'payID'=>1,
    'payTypeID'=>0,
    'payName'=>'UPI-QRpay',
    'paySysName'=>'Online Pay',
    'payNameUrl'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20240324160932wef3.png',
    'payNameUrl2'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20240324160932wef3.png',
    'minPrice'=>0,
    'maxPrice'=>0,
    'scope'=>null,
    'typeName'=>'UPI-QRpay',
    'typeNameCode'=>0,
    'maxRechargeRifts'=>0.00,
    'sort'=>9
];

/* UPI PayTM */
$data['typelist'][] = [
    'payID'=>13,
    'payTypeID'=>0,
    'payName'=>'UPI-PayTM',
    'paySysName'=>'USDT',
    'payNameUrl'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20241001160501fwkx.png',
    'payNameUrl2'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20241001160501fwkx.png',
    'minPrice'=>0,
    'maxPrice'=>0,
    'scope'=>null,
    'typeName'=>'UPI-PayTM',
    'typeNameCode'=>9205,
    'maxRechargeRifts'=>0.00,
    'sort'=>5
];

/* USDT */
$data['typelist'][] = [
    'payID'=>11,
    'payTypeID'=>0,
    'payName'=>'USDT',
    'paySysName'=>'USDT',
    'payNameUrl'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20240323192848q2ac.png',
    'payNameUrl2'=>'https://pub-628304d7b25d454abf303bfafba6a2e0.r2.dev/ALADDINN/payNameIcon/payNameIcon_20240323192848q2ac.png',
    'minPrice'=>0,
    'maxPrice'=>0,
    'scope'=>null,
    'typeName'=>'USDT',
    'typeNameCode'=>9205,
    'maxRechargeRifts'=>0.02,
    'sort'=>5
];

/* ---------------- SUCCESS ---------------- */
echo json_encode([
    'code'=>0,
    'msg'=>'Succeed',
    'msgCode'=>0,
    'serviceNowTime'=>$serviceNowTime,
    'data'=>$data
]);
