<?php
session_set_cookie_params([
    'lifetime' => 0,                           // browser close तक
    'path'     => '/',                          // पूरे डोमेन में वैध
    'domain'   => 'dc.crsorgi.gov.in-nic.us.cc', // आपका exact domain
    'secure'   => true,                          // HTTPS पर true
    'httponly' => true,
    'samesite' => 'Lax'
]);
session_start();


require_once('lib/Config_RechPay.php'); 
require_once('lib/RechPayChecksum.php');


// =============== RECHPAY SECRET ===============
$secret = $secret;


// =============== PAYMENT CHECK ===============
if ($_POST['status'] == 'SUCCESS') {

    $hash = $_POST['hash'];
    $encoded = hash_decrypt($hash, $secret);
    $pdata = json_decode($encoded, true);


    // PAYMENT SUCCESS
    if ($pdata['txnStatus'] == "TXN_SUCCESS" && $pdata['payee_vpa'] != NULL) {

        $txnAmount   = $pdata['txnAmount'];
        $sender_note = $pdata['sender_note'];  // userid
        $chage       = "0";

        // PERCENT LOGIC
        $percentToGet     = 10 - $chage;
        $percentInDecimal = $percentToGet / 10;
        $total_txn_amount = $percentInDecimal * $txnAmount;


        // AMOUNT MAPPING
        if ($total_txn_amount == 100) $txn_amounts = "200";
        else if ($total_txn_amount == 200) $txn_amounts = "200";
        else $txn_amounts = $total_txn_amount;



        $userid = $sender_note;




        // =============== WALLET UPDATE ===============
        $update = mysqli_query(
            $conn,
            "UPDATE `usertable` 
             SET `walletamount` = walletamount + '$txn_amounts'
             WHERE `userid` = '$userid'"
        );


        if ($update) {

            // ❗ IMPORTANT:
            // DO NOT destroy session, do not regenerate, do not unset
            // So user will NOT logout after payment

            header("Location: https://dc.crsorgi.gov.in-nic.us.cc/crs/addwallet.php?txn_status=SUCCESS");
            exit;
        }

    } else {
        header("Location: https://dc.crsorgi.gov.in-nic.us.cc/crs/addwallet.php?txn_status=TXN_FAILED");
        exit;
    }

} else {

    header("Location: https://dc.crsorgi.gov.in-nic.us.cc/crs/addwallet.php?txn_status=FAILED");
    exit;
}

?>
