<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
require_once('../lib/dbclass.php');

$data = [];
extract($_POST);
$data = $_POST;

$Transid        = $_REQUEST['id'];
$ReceivedAmount = $_REQUEST['received'];



$checking = findQuery('SELECT * FROM `settings`');



if ($checking) {
    $isRazorpaymentgateway = $checking[0]['isRazorpaymentgateway'];

    if ($isRazorpaymentgateway > 1) {
        $orderId = rand(10, 10000000);
        $orderId .= $Transid;

        $is_Razor_payment_gateway = 2;

        $data_header['orderid'] = $orderId;
    } elseif ($isRazorpaymentgateway == 1) {
        $is_Razor_payment_gateway = 1;
    } else {
        $is_Razor_payment_gateway = 0;
    }
}


updateQuery("UPDATE tbl_transaction SET is_Razor_payment_gateway=$is_Razor_payment_gateway,orderid='$orderId' where customerid='$Transid'");



if ($isRazorpaymentgateway > 1) {
    function containsNonEnglishCharacters($string)
    {
        // Regular expression for matching non-English characters
        if (preg_match('/[^\x20-\x7E]/', $string)) {
            return 1; // Contains non-English characters
        }

        return 0; // Contains only English characters
    }


    $cust = findQuery("SELECT * FROM `tbl_customer` where id=$Transid");

    $name = $cust[0]['name'];

    $checklanguage = containsNonEnglishCharacters($name);

    if ($checklanguage == 0) {
        $name = $cust[0]['name'];
    } else {
        $name = $Transid;
    }
    function hashCalculate($salt, $input)
    {
        /* Columns used for hash calculation, Donot add or remove values from $hash_columns array */
        $hash_columns = ['address_line_1', 'address_line_2', 'amount', 'api_key', 'city', 'country', 'currency', 'description', 'email', 'mode', 'name', 'order_id', 'phone', 'return_url', 'state', 'udf1', 'udf2', 'udf3', 'udf4', 'udf5', 'zip_code',];
        /*Sort the array before hashing*/
        sort($hash_columns);

        $hash_data = $salt;

        foreach ($hash_columns as $column) {
            if (isset($input[$column])) {
                if (strlen($input[$column]) > 0) {
                    $hash_data .= '|' . trim($input[$column]);
                }
            }
        }

        $hash = strtoupper(hash('sha512', $hash_data));

        return $hash;
    }
    $salt = SALT; // from DBClass


    $message = 'True';


    if ($data['mode'] > 0) {
        $hash           = '';
        $pendingorders1 = [
         'address_line_1' => 'ad1',
         'address_line_2' => 'ad2',
         'amount'         => $ReceivedAmount,  // Assuming $data['Amount'] is defined
         'api_key'        => APIKEY, // from DBClass
         'city'           => CITY,
         'country'        => 'IND',
         'currency'       => 'INR',
         'description'    => DESCRIPTION,
         'email'          => EMAIL,
         'mode'           => 'LIVE',
         'name'           => $name,  // Assuming $data['name'] is defined
         'order_id'       => $orderId,   // Assuming $orderId is defined
         'phone'          => PHONE,
         'return_url'     => 'http://localhost:8888/paymentresponse',
         'state'          => 'kerala',
         'zip_code'       => ZIPCODE,
];
        $hash = hashCalculate($salt, $pendingorders1);
    } else {
        $hash          = '';
        $pendingorders = [
         'address_line_1' => 'ad1',
         'address_line_2' => 'ad2',
         'amount'         => $ReceivedAmount,  // Assuming $data['Amount'] is defined
         'api_key'        => APIKEY, // from DBClass
         'city'           => CITY,
         'country'        => 'IND',
         'currency'       => 'INR',
         'description'    => DESCRIPTION,
         'email'          => EMAIL,
         'mode'           => 'ios_sdk|LIVE',
         'name'           => $name,  // Assuming $data['name'] is defined
         'order_id'       => $orderId,   // Assuming $orderId is defined
         'phone'          => PHONE,
         'return_url'     => 'http://localhost:8888/paymentresponse',
         'state'          => 'kerala',
         'zip_code'       => ZIPCODE,
];
        $hash = hashCalculate($salt, $pendingorders);
    }

    echo json_encode(['Status' => $message, 'data' => 1,'order_id' => $orderId,'hash' => $hash,'name' => $name]);
} else {
    echo json_encode(['Status' => $message, 'data' => $data2]);
}










return;
?>	   


