<?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;



$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'];
}

$data_customer['Photo'] = '';

if ($data['isphoto']) {
    if ($data['isphoto'] == 1) {
        $target_dir       = 'images/';
        $random           = rand(0, 999999);
        $filename         = sha1($image . $random);
        $target_file_name = $target_dir . $filename;
        $img_name         = basename($_FILES['image']['name']);

        if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file_name)) {
        } else {
            $message = 'False';
            $data2[] = ['error' => 'Some Error Occured While Uploading Photo'];
            echo json_encode(['Status' => $message, 'data' => $data2]);

            return;
        }







        $data_customer['Photo'] = $filename;
    }
}


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

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



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

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

        $data_header['is_Razor_payment_gateway'] = 2;

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





$data_header['customerid'] = $id_customer;
$id_header                 = saveArray('tbl_transaction', $data_header, 0);

if ($donation == 0) {
    $data_detail['qty'] = $data['qty'];
    $price_per_item     = 500;
    $Amount             = $data['Amount'];
    $quantity           = floor($Amount / $price_per_item);
    $id                 = 0;

    if ($quantity > 0) {
        $data_detail['hdrid']  = $id_header;
        $data_detail['item']   = 1;
        $data_detail['qty']    = $data_detail['qty'];
        $data_detail['Totqty'] = $data_detail['qty'];
        $data_detail['rate']   = 500;
        $data_detail['amount'] = $data_detail['qty'] * 500;


        $id = saveArray('tbl_transactiondetails', $data_detail, 0);
    }
}




if ($id > 0 || $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 = '7f850b261dfa7105ce670612a350e6c2d3494f5c'; //Pass your SALT here


    $pendingorders = [
    'address_line_1' => 'ad1',
    'address_line_2' => 'ad2',
    'amount'         => $ReceivedAmount,  // Assuming $data['Amount'] is defined
    'api_key'        => '1a8d0e55-afaa-45f1-9c47-b75868edcb9e',
    'city'           => 'tirur',
    'country'        => 'IND',
    'currency'       => 'INR',
    'description'    => 'Test',
    'email'          => 'customercare.smreader@gmail.com',
    'mode'           => 'ios_sdk|LIVE',
    'name'           => $name,  // Assuming $data['name'] is defined
    'order_id'       => $orderId,   // Assuming $orderId is defined
    'phone'          => '9597403366',
    'return_url'     => 'http://localhost:8888/paymentresponse',
    'state'          => 'kerala',
    'zip_code'       => '630501',
];
    $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'        => '1a8d0e55-afaa-45f1-9c47-b75868edcb9e',
         'city'           => 'tirur',
         'country'        => 'IND',
         'currency'       => 'INR',
         'description'    => 'Test',
         'email'          => 'customercare.smreader@gmail.com',
         'mode'           => 'LIVE',
         'name'           => $name,  // Assuming $data['name'] is defined
         'order_id'       => $orderId,   // Assuming $orderId is defined
         'phone'          => '9597403366',
         'return_url'     => 'http://localhost:8888/paymentresponse',
         'state'          => 'kerala',
         'zip_code'       => '630501',
];
        $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;
?>	   


