<?php
include "../connection/config.php";
include "config.php";
extract($_REQUEST);
   
$dd = query("select session,active from users where mobile='$mobile'");
$d = fetch($dd);

if($d['session'] != $_REQUEST['session']){
    $data['success'] = "0";
    $data['msg'] = "You are not authorized to use this";
    $data['session0'] = $d['session'];
    $data['session1'] = $_REQUEST['session'];

    echo json_encode($data);
    return;
 }

$order_id = md5($mobile.$amount.$d['session']);

 $hash = openssl_encrypt($order_id, "AES-128-ECB", $_REQUEST['hash_key']);

$get_data = fetch(query("select * from gateway_temp where hash='$hash' AND user='$mobile'"));

 //$data['qyery'] = "select * from gateway_temp where hash='$hash' AND user='$mobile'";

$amount = $get_data['amount'];

if(rows(query("select sn from settings where data_key='verify_upi_payment' AND data='1'")) > 0){
query("update users set wallet=wallet+'$amount' where mobile='$mobile'");

query("INSERT INTO `transactions`( `user`, `amount`, `type`, `remark`, `owner`, `created_at`) VALUES ('$mobile','$amount','1','Online Deposit','user','$stamp')");
  
    query("INSERT INTO `auto_deposits`( `mobile`, `amount`, `method`, `pay_id`, `created_at`) VALUES ('$mobile','$amount','".$get_data['type']."','$hash','$stamp')");
  
    $check_refer = query("select code from refers where user='$mobile' AND status='0'");
  if(rows($check_refer) > 0){
    $refer = fetch($check_refer);
    
    $code = $refer['code'];
    
    $get_refer_mobile = fetch(query("select mobile from users where code='$code'"));
    $refer_mobile = $get_refer_mobile['mobile'];
    
    
    $amount = $amount*10/100;
    query("update refers set status='1', amount='$amount' where user='$mobile' AND code='$code'");
	query("update users set wallet=wallet+'$amount' where mobile='$refer_mobile'");

	query("INSERT INTO `transactions`( `user`, `amount`, `type`, `remark`, `owner`, `created_at`) VALUES ('$refer_mobile','$amount','1','Refer earning','user','$stamp')");

    $user_details=mysqli_query($con,"select sn,tocken from users where mobile='$mobile'");
        if(mysqli_num_rows($user_details)>0)
        {
            $row=mysqli_fetch_assoc($user_details);
              
            if($row['tocken']!='')
            {
                $_REQUEST['token']=$row['tocken'];
      
              $msg = array
              (
        	   'body' 	=> 'Congratulation your add '.$amount.' point'.' 🤑🤩🥳😍',
        	   'title'	=> 'PLUS LIFE POINT',
        	   'vibrate' => 1,
        	   'sound' => 1,
        	   'image'=>'https://www.digitalkee.com/online_test/application_admin/assets/product/2096198.jpg'
               );
            	$fields = array
            			(
            				'to'		=> $_REQUEST['token'],
            				'notification'	=> $msg,
            				
            			);
            	$headers = array
            			(
            				//'Authorization: key=' . API_ACCESS_KEY,
            				'Authorization: key= AAAAj2wUFTg:APA91bF7grm_Se13eOkBKGEHa1_bECRlGolYgYLQWW_of1bKvB_gvnFfSdPVwIWmTRKrxhKoiOjtMDQZuo-oDHbjN8Uou9vgQnMeIfFH_c-rk7l8vc9nc-nGL9DTor_Imt6hJs-VgeOZ' ,
            				'Content-Type: application/json'
            			);	
            		$ch = curl_init();
            		curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            		curl_setopt( $ch,CURLOPT_POST, true );
            		curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            		curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            		curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            		curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            		$result = curl_exec($ch );
                	//	echo $result; die();
            		
            		curl_close( $ch );
            }
        }
    
$data['success'] = "1";
echo json_encode($data); exit;
} else {
    
    $amount=$_REQUEST['amount'];
    $mobile=$_REQUEST['mobile'];
    query("INSERT INTO `upi_verification`( `user`, `amount`, `created_at`) VALUES  ('$mobile','$amount','$stamp')");
    $user_details=mysqli_query($con,"select sn,tocken from users where mobile='$mobile'");
        if(mysqli_num_rows($user_details)>0)
        {
            $row=mysqli_fetch_assoc($user_details);
              
            if($row['tocken']!='')
            {
                $_REQUEST['token']=$row['tocken'];
      
              $msg = array
              (
        	   'body' 	=> 'Congratulation your add '.$amount.' point'.' 🤑🤩🥳😍',
        	   'title'	=> 'PLUS LIFE POINT',
        	   'vibrate' => 1,
        	   'sound' => 1,
        	   'image'=>'https://www.digitalkee.com/online_test/application_admin/assets/product/2096198.jpg'
               );
            	$fields = array
            			(
            				'to'		=> $_REQUEST['token'],
            				'notification'	=> $msg,
            				
            			);
            	$headers = array
            			(
            				//'Authorization: key=' . API_ACCESS_KEY,
            				'Authorization: key= AAAAj2wUFTg:APA91bF7grm_Se13eOkBKGEHa1_bECRlGolYgYLQWW_of1bKvB_gvnFfSdPVwIWmTRKrxhKoiOjtMDQZuo-oDHbjN8Uou9vgQnMeIfFH_c-rk7l8vc9nc-nGL9DTor_Imt6hJs-VgeOZ' ,
            				'Content-Type: application/json'
            			);	
            		$ch = curl_init();
            		curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            		curl_setopt( $ch,CURLOPT_POST, true );
            		curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            		curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            		curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            		curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            		$result = curl_exec($ch );
                	//	echo $result; die();
            		
            		curl_close( $ch );
            }
        } 
$data['success'] = "1";
echo json_encode($data); exit;
  }
  
$data['success'] = "0";
}

echo json_encode($data);