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

$challenge_startdate = date('Y-m-d');


$datach = findQuery("select * from  `tbl_challenge` where '$challenge_startdate'  between `challenge_startdate` and `challenge_enddate` and status=0");

$sponsorproductid1 = $datach[0]['sponsorproductid1'];
$sponsorproductid2 = $datach[0]['sponsorproductid2'];
$sponsorproductid3 = $datach[0]['sponsorproductid3'];
$sponsorproductid4 = $datach[0]['sponsorproductid4'];
$sponsorproductid5 = $datach[0]['sponsorproductid5'];
$datasponsor       = [];
$prod1             = 0;
$prod2             = 0;
$prod3             = 0;
$prod4             = 0;
$prod5             = 0;

$rate1 = 0;
$rate2 = 0;
$rate3 = 0;
$rate4 = 0;
$rate5 = 0;

if ($sponsorproductid1 > 0) {
    $datasponsor = findQuery("select * from  `tbl_product` where id=$sponsorproductid1");
    $prod1       = $datasponsor[0]['id'];
    $rate1       = $datasponsor[0]['amount'];
}

if ($sponsorproductid2 > 0) {
    $datasponsor = [];
    $datasponsor = findQuery("select * from  `tbl_product` where id=$sponsorproductid2");
    $prod2       = $datasponsor[0]['id'];
    $rate2       = $datasponsor[0]['amount'];
}

if ($sponsorproductid3 > 0) {
    $datasponsor = [];
    $datasponsor = findQuery("select * from  `tbl_product` where id=$sponsorproductid3");
    $prod3       = $datasponsor[0]['id'];
    $rate3       = $datasponsor[0]['amount'];
}

if ($sponsorproductid4 > 0) {
    $datasponsor = [];
    $datasponsor = findQuery("select * from  `tbl_product` where id=$sponsorproductid4");
    $prod4       = $datasponsor[0]['id'];
    $rate4       = $datasponsor[0]['amount'];
}

if ($sponsorproductid5 > 0) {
    $datasponsor = [];
    $datasponsor = findQuery("select * from  `tbl_product` where id=$sponsorproductid5");
    $prod5       = $datasponsor[0]['id'];
    $rate5       = $datasponsor[0]['amount'];
}



$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;
$data_header['subscription']             = $data['subscription'] ? $data['subscription'] : 0;




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

$donation = $data['donation'];



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'];
} else {
    $dataassem                 = findQuery('select * from tbl_assembly where district=0');
    $data_customer['Assembly'] = $dataassem[0]['id'];



    $datapan                    = findQuery('select * from tbl_panchayat where assembly_id=0');
    $data_customer['Panchayat'] = $datapan[0]['id'];

    $datawrd               = findQuery('select * from tbl_ward where panchayat_id=0');
    $data_customer['Ward'] = $datawrd[0]['id'];

    $data_customer['District'] = $data['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) {
    $quantity  = $data['qty'];
    $quantity1 = $data['qty1'];
    $quantity2 = $data['qty2'] ? $data['qty2'] : 0;
    $quantity3 = $data['qty3'] ? $data['qty3'] : 0;
    $quantity4 = $data['qty4'] ? $data['qty4'] : 0;

    $id = 0;

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


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

    if ($quantity1 > 0) {
        $data_detail['hdrid']  = $id_header;
        $data_detail['item']   = $prod2;
        $data_detail['qty']    = $quantity1;
        $data_detail['Totqty'] = $quantity1;
        $data_detail['rate']   = $rate2;
        $data_detail['amount'] = $quantity1 * $rate2;


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

    if ($quantity2 > 0) {
        $data_detail['hdrid']  = $id_header;
        $data_detail['item']   = $prod3;
        $data_detail['qty']    = $quantity2;
        $data_detail['Totqty'] = $quantity2;
        $data_detail['rate']   = $rate3;
        $data_detail['amount'] = $quantity2 * $rate3;


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

    if ($quantity3 > 0) {
        $data_detail['hdrid']  = $id_header;
        $data_detail['item']   = $prod4;
        $data_detail['qty']    = $quantity3;
        $data_detail['Totqty'] = $quantity3;
        $data_detail['rate']   = $rate4;
        $data_detail['amount'] = $quantity3 * $rate4;


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

    if ($quantity4 > 0) {
        $data_detail['hdrid']  = $id_header;
        $data_detail['item']   = $prod5;
        $data_detail['qty']    = $quantity4;
        $data_detail['Totqty'] = $quantity4;
        $data_detail['rate']   = $rate5;
        $data_detail['amount'] = $quantity4 * $rate5;


        $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 = '714cf256d53b2e8ca7f0806d0110a4908dd1c937'; //Pass your SALT here







    $pendingorders = [
     'address_line_1' => 'ad1',
     'address_line_2' => 'ad2',
     'amount'         => $ReceivedAmount,  // Assuming $data['Amount'] is defined
     'api_key'        => '452e698c-ad72-4dde-bb01-b28704fff16f',
     'city'           => 'Calicut',
     '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, $pendingorders);






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










return;
?>	   


