<?php
        include_once('../config.php');    
        
             
            function TokenCall ()
            {
                /*$Remail =  "dailypay2024@gmail.com";
                $Rd =  "ztw35i";  
                $access_token = '{"email":"'.$Remail.'","password":"'.$Rd.'"}'; 
                $ch = curl_init('https://api.pay2all.in/token');                                             
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                curl_setopt($ch, CURLOPT_POSTFIELDS, $access_token);                                                                  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                'x-tera-tenant-region: IN', 
                'Content-Type: application/json', 
                'Content-Length: ' . strlen($access_token))                                                                       
                );       																		 
                $response = curl_exec($ch); 
                $json_response = json_decode($response);   
                $RechargeAccess_token = $json_response->access_token; 
                return $RechargeAccess_token;  */
            }  
            
            
            function makeRechargeUsingAPI($data_string)
            {    
                     
                    global $RechargeAccess_token;     
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, 'https://erp.pay2all.in/api/v1/payment/recharge');
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    $headers = array();
                    $headers[] = "Accept: application/json";
                    $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                    $headers[] = "Content-Type: application/json";
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch); 
                    return $result;
                    die;
            }
            
            
            function makeRechargeUsingBillPay($data_string)
            {    
                    global $RechargeAccess_token;    
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, 'https://api.pay2all.in/v1/payment/recharge');
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    $headers = array();
                    $headers[] = "Accept: application/json";
                    $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                    $headers[] = "Content-Type: application/json";
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch); 
                    return $result;
                    die;
            }
            
            
            
            if(isset($_POST['userverify']))
            {    
             
              global $RechargeAccess_token;    
                 
                $ary=array(); 
                $mobile=trim($_POST['mobile_no']);
                //$amount=trim($_POST['amount']);
                $provider_id=trim($_POST['operator']);
                $RechargeParams=explode("-",$_POST['RechargeParams']);
                $FinalRechargeParams=$_SESSION['RechargeParams'];
                $_SESSION['PostRechargeParams']=$_POST['RechargeParams'];
                 
                
                if(count($FinalRechargeParams) > 0)
                {
                    foreach($FinalRechargeParams as $key=>$dtm )
                    {
                        $OptionString .='"'.$dtm->name.'":'.'"'.$RechargeParams[$key].'",';
                    }
                }
                else
                    $OptionString ='';   
                 
                
                $data_string = '{"number": "'.$mobile.'","provider_id": "'.$provider_id.'",'.$OptionString.'"api_id": "27"}';
                 
                
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://api.pay2all.in/v1/payment/verification');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                curl_setopt($ch, CURLOPT_POST, 1);
                $headers = array();
                $headers[] = "Accept: application/json";
                $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                $headers[] = "Content-Type: application/json";
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                $result = curl_exec($ch); 
                $Data = json_decode($result);
                 
                
                if(($Data->status==0 || $Data->status==1) && ($Data->status_id==1))
                { 
                    $ary['amount'] =   $Data->amount;
                    $ary['status'] =   $Data->status;
                    $ary['status_id'] =   $Data->status_id;  
                    $_SESSION['billFullref'] = $result; 
                    echo json_encode($ary);
                    die;
                }
                else
                {
                    $ary['status'] =   $Data->status;
                    $ary['message'] =   $Data->message;
                    $ary['status_id'] =  0; 
                    echo json_encode($ary);
                    die;
                }      
            }
            
            
             if(isset($_POST['userproviderBill']))
            {    
                
                    global $RechargeAccess_token;    
                    
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,'https://erp.pay2all.in/api/bbps/biller/'.$_POST['provider']);  
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                    $headers = array();
                    $headers[] = "Accept: application/json";
                    $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                    $headers[] = "Content-Type: application/json";
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch);    
                    $funData = json_decode($result); 
                    echo json_encode($funData->biller[0]->customerParams); 
                    //echo '[{"paramName":"Loan Account Number","dataType":"NUMERIC","isOptional":"false","minLength":"16","maxLength":"16","regEx":"^\\d{16}$"},{"paramName":"Pledge Value","dataType":"NUMERIC","isOptional":"false","minLength":"4","maxLength":"10","regEx":"^\\d{4,10}$"}]';
                   // die;
                
            }
            
            if(isset($_POST['userproviderBill_2']))
            {    
                 
             
                    $ary['billerId'] = $_POST['provider']; 
                    $strng1 = explode(",",$_POST['strng1']);
                    $strng2 = explode(",",$_POST['strng2']);
                    
                    for($i=0;$i<$_POST['Total'];$i++)
                    { 
                        $ary['customerParams'][$i]['name'] = $strng1[$i];
                        $ary['customerParams'][$i]['value'] = $strng2[$i];
                    }
                  
                    $data = json_encode($ary);
                    
                    $_SESSION['SlugProvider'] = $data; 
				    global $RechargeAccess_token;  
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,'https://erp.pay2all.in/api/bbps/fetch');
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    $headers = array();
                    $headers[] = "Accept: application/json";
                    $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                    $headers[] = "Content-Type: application/json";
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch); 
                    echo  $result;
                    die;
	
            }
            
