<?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 ? $_POST : $_GET;


$data['mode']      = filter_var($data['mode'], FILTER_SANITIZE_NUMBER_INT);
$data['Panchayat'] = 1;
$data['Panchayat'] = 1;

$data_customer['name']      = $data['name'];
$data_customer['Address']   = $data['Address'];
$data_customer['Mobile']    = $data['Mobile'];
$data_customer['Panchayat'] = $data['Panchayat'];
$data_customer['Ward']      = $data['Ward'];



$data_header['donation']                 = $data['donation'];
$data_header['sponsorship']              = $data['sponsorship'];
$data_header['challengeid']              = $data['challengeid'];
$data_header['clubid']                   = $data['clubid'];
$data_header['hideidendity']             = $data['hideidendity'];
$data_header['uniquedid']                = $data['uniquedid'];
$data_header['customerid']               = $data['customerid'];
$data_header['orderforothers']           = $data['orderforothers'];
$data_header['name']                     = $data['custname'];
$data_header['address']                  = $data['custaddress'];
$data_header['mobile']                   = $data['custmobile'];
$data_header['Amount']                   = $data['Amount'];
$data_header['Received']                 = $data['Received'];
$data_header['volunteer']                = $data['volunteer'];
$data_header['is_Razor_payment_gateway'] = 2;





$ReceivedAmount = trim($data['Amount']);

$donation = $data['donation'];

$clubid = $data_header['clubid'];

if ($clubid > 0) {
    //$panchayat=$data_customer['Panchayat'];

    //$dataward = findQuery("select * from tbl_ward where panchayat_id=$panchayat and ward_name='NA'");
    //$data_customer['Ward']=$dataward[0]['id'];
}



if ($data_customer['Panchayat'] > 0) {
    $Panchayat = $data['Panchayat'];


    $dataassem                 = findQuery("select * from tbl_panchayat where status=0 and id=$Panchayat");
    $data_customer['Assembly'] = $dataassem[0]['assembly_id'];
    $assembly                  = $data_customer['Assembly'];

    $datadis                   = findQuery("select * from tbl_assembly where status=0 and id=$assembly");
    $data_customer['District'] = $datadis[0]['district'];
}




$id_customer = saveArray('tbl_customer', $data_customer, 0);

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



if ($checking) {
    $orderId = rand(10, 10000000);
    $orderId .= $id_customer;

    $data_header['is_Razor_payment_gateway'] = 2;

    $data_header['orderid'] = $orderId;
}





$data_header['customerid'] = $id_customer;

$id_header = saveArray('tbl_transaction', $data_header, 0);

$isRazorpaymentgateway = 2;




if ($id_header > 0) {
    $message = 'True';
    $data2   = $id_customer;
} else {
    $message = 'False';
    $data2   = 0;
}


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
    }


    $name = $data['name'];

    $checklanguage = containsNonEnglishCharacters($name);

    if ($checklanguage == 0) {
        $name = $data['name'];
    } else {
        $name = 'name';
    }
    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


    $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);


    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);
    }

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










return;
?>	   


