<?php
include_once('../config.php');  
include_once('../recharges/recharge_api.php');  
include_once(ABS_PATH.'/custom_function/default.php');  
 
if(!isset($_SESSION['userId']))
{ 
header("Location: ../");
die;
} 
                
                
                            
                                
                            
                    $metdtToken  =  $RechargeAccess_token;
                    if(isset($_POST['FormDataSubmitAgn']))
                    {
                          
                         
                         
                                    $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                                    $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                                    $stmt->execute();
                                    $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);   
                                    
                                    
                                    $jsonar = json_decode($_SESSION['SlugProvider']);   
                                    $ary['client_id'] = mt_rand(100000, 999999);
                                    $client_id = $ary['client_id'];
                                    $ary['mobile_number'] = $userNam->phm;
                                    $ary['billerId'] = $jsonar->billerId;  
                                    $ary['reference_id'] = $_POST['fref'];
                                    $ary['amount'] = round($_POST['AMMT']).".00";
                                    foreach($jsonar->customerParams as $k=>$mp)
                                         $ary['customerParams'][$k] = $mp;
                                     
                                    $data =  json_encode($ary); 
                                    $_POST['amount']  = round($_POST['AMMT']);
                			        $mobile =  $userNam->phm;
                			        $provider_id =$jsonar->billerId;  
                			        $amount = $_POST['amount']; 
                			        $payment_txnid  = mt_rand(100000, 999999); 
                                    
                                    $ch = curl_init();
                                    curl_setopt($ch, CURLOPT_URL,'https://erp.pay2all.in/api/bbps/payment');
                                    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 ".$metdtToken;
                                    $headers[] = "Content-Type: application/json";
                                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                                    $result = curl_exec($ch);  
                                    $_SESSION['SlugProviderR'] = $result; 
                                    unlink($_SESSION['SlugProvider']);   
                                    $Data = json_decode($result);   
                                    $orderid= "NA";
                                    
                                    $response_date=  date('Y-m-d H:i:s');
                                    $stmt =  $conn->prepare("insert into recharge_response (request,response,response_date,txnId,cusTid) values(:request,:response,:response_date,:txnId,:cusTid)");
                                    $stmt->bindValue(':request', $data, PDO::PARAM_STR);
                                    $stmt->bindValue(':response', $result, PDO::PARAM_STR);
                                    $stmt->bindValue(':response_date', $response_date, PDO::PARAM_STR);
                                    $stmt->bindValue(':txnId', $payment_txnid, PDO::PARAM_STR);
                                    $stmt->bindValue(':cusTid',  $_SESSION['userId'] , PDO::PARAM_STR);  
                                    $stmt->execute(); 
                                    
                                    if($Data->utr!='')
                                        $UTR = $Data->utr;
                                    else
                                        $UTR =  "NA";
                                    
                                    if($Data->status_id==1)
                                    {
                                    $recharge_status='Success';
                                    $status=1;
                                    $_SESSION['recharge_success'.$rand_no]=1;    
                                    $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                                    }
                                    else if( ($Data->status_id==2) || ($Data->status_id==4))
                                    {
                                    $recharge_status='Fail';
                                    $status=0;
                                    $_SESSION['recharge_success'.$rand_no]=2;
                                    $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                                    }
                                    else if($Data->status_id==3)
                                    {
                                    $recharge_status='Pending'; 
                                    $status=2;
                                    $_SESSION['recharge_success'.$rand_no]=3;
                                    $_SESSION['Regis'] = "Recharge ".$recharge_status; 
                                    }
                                    
                                    if(($status==1) || ($status==2))  
                                    {  
                                    $stmt =  $conn->prepare("insert into UI__Recharges (operator,amount,mobile,user_id,cust_id,type,status,payee_txn_id,user_type,url,orderid,report_id,last_update,client_id,utr) values(:operator,:amount,:mobile,:user_id,:cust_id,:type,:status,:payee_txn_id,:user_type,:url,:orderid,:report_id,:last_update,:client_id,:utr)");            
                                    $stmt->bindValue(':operator', $provider_id, PDO::PARAM_STR);
                                    $stmt->bindValue(':amount', $amount, PDO::PARAM_STR);
                                    $stmt->bindValue(':mobile', $mobile, PDO::PARAM_STR);
                                    $stmt->bindValue(':user_id', 1, PDO::PARAM_STR);
                                    $stmt->bindValue(':cust_id', $_SESSION['userId'], PDO::PARAM_STR);
                                    $stmt->bindValue(':type', 1, PDO::PARAM_INT);
                                    $stmt->bindValue(':status', $status, PDO::PARAM_INT);
                                    $stmt->bindValue(':payee_txn_id', $payment_txnid, PDO::PARAM_STR);
                                    $stmt->bindValue(':user_type', 1, PDO::PARAM_INT);
                                    $stmt->bindValue(':url', "NA", PDO::PARAM_STR);
                                    $stmt->bindValue(':orderid', $orderid, PDO::PARAM_STR);
                                    $stmt->bindValue(':report_id', $Data->report_id, PDO::PARAM_STR);
                                    $stmt->bindValue(':last_update', date("d-M-Y H:i"), PDO::PARAM_STR);
                                    $stmt->bindValue(':client_id', $client_id, PDO::PARAM_STR);
                                    $stmt->bindValue(':utr', $UTR, PDO::PARAM_STR);
                                    $stmt->execute();
                                    $last_id  = $conn->lastInsertId();  
                                    
                                    
                                    
                                    if( ($status==1) || ($status==2)) 
                                    {
                                    $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept,recId,wtype,decucationType) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept,:recId,1,1)"); 
                                    $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                                    $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                                    $stmt->bindValue(':amt', $amount, PDO::PARAM_STR);
                                    $stmt->bindValue(':orgAmt', "NA" , PDO::PARAM_STR); 
                                    $stmt->bindValue(':order_id', $orderid , PDO::PARAM_STR); 
                                    $stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
                                    $stmt->bindValue(':status',  $recharge_status, PDO::PARAM_STR); 
                                    $stmt->bindValue(':accept', $status , PDO::PARAM_STR);      
                                    $stmt->bindValue(':recId', $last_id , PDO::PARAM_STR);    
                                    $stmt->execute();         
                                    LvBal($_SESSION['userId'],$amount,2,$_POST['UrlslugnM']." Recharged",$mobile);    
                                    
                                    
                                    
                                    $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                                    $stmt->bindValue(':type',   8 , PDO::PARAM_STR);   
                                    $stmt->bindValue(':bank',  $_POST['UrlslugnM']." Recharge" , PDO::PARAM_STR);   
                                    $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                                    $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                                    $stmt->bindValue(':amountPaid', $amount   , PDO::PARAM_STR); 
                                    $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                                    $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                                    $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                                    $stmt->bindValue(':custId', $_SESSION['userId']  , PDO::PARAM_STR);  
                                    $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                                    $stmt->bindValue(':Topay',  $mobile , PDO::PARAM_STR); 
                                    $stmt->bindValue(':TopayName',  $mobile  , PDO::PARAM_STR);     
                                    $stmt->execute(); 
                                    AddFundNew($_SESSION['userId'],5,"Prepaid Recharge Bonus");
                                    }
                                    
                                    
                                    $subject = "Recharge ".$recharge_status." On ".$SITE_NAME;  
                                    $template_type = 1;
                                    $attachment = '';
                                     
                                    $message="
                                    <tr>
                                    <td align='left' valign='top'>
                                    <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for making recharge with us. Your Details are as follows </h5> 
                                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> Mobile: $mobile , Amount Rs : $amount </h5>
                                    
                                    </td>
                                    </tr>";     
                                    SendMail($Email,$subject,$message,$template_type,$attachment);
                                    echo $_SESSION['LastIntId'];
                                    }
                                    else
                                        echo 0;
                            
                            die;  
                    } 
                        
                            
    include_once("../view/dashboard/elenexct2.php");
?>