<?php 
 
include 'remita_constants.php'; //include som other constants


 

 
 //collect invoice id
 if(isset($_POST['iid'],$_POST['qlk']))
 {
	 
	 //collect it
	 $iid = mysqli_real_escape_string($dbc, $_POST['iid']);
	 
	 //pass iid to a reference
	 $_SESSION['iidar'] = $iid;
	 
	 
	 //collect application number
	 $appno = get_user_application_number($user_id);
	 
	 
	 
	 //invoice to
	 $invoice_type  = get_invoice_type($iid);//get invoice type
	 $invoice_to = get_invoice_invoice_to($iid);
	 $pay_to = get_invoice_pay_to($iid); //get who to pay the invoce to
	 $invoice_for = get_invoice_for($iid); //get what the invoice is made for 
	 $title = get_invoice_title($iid); // get the invocie title
	 $description = get_invoice_desc($iid); //get the invoice desc
	 $amount = get_invoice_amount($iid); //get the invocie amount
	 $total_amount = get_invoice_total_amount($iid); //get invoice total amount this is the amount billed client
	 $credit = get_invoiced_credit($iid); //get invoice credit
	 
	 //get invoice instalment or fulpayment
	 $payment_mode = get_invoice_payment_mode($iid);
	 //show payment mode
	 $show_payment_mode = show_payment_mode($payment_mode);
	 
	 
	 $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
	 
 
	 //get current session 
	 //$session = get_current_session($id = 1); //get current session
	 //$session_title = get_session_title($session); //get session title
	 
	 //get current semester
	 // $semester = get_current_semester($id = 1); //get current semester
	 //$semester_title = get_semester_title($semester); //get semester title
	 
	 //candidate details
	 $fullname = get_user_fullname($invoice_to);
	 $email = get_user_email($invoice_to);
	 $phone_no = get_user_phone_no($invoice_to);

	 

	 
	 //for interswitch
	 $txnref = mysqli_real_escape_string($dbc, $_POST['txnref']);


//  //$total_amountt =  $total_amount * 100;
    $total_amountt = $_POST['amount'];
	//$total_amountt = '2100';
  
   //pass it to session
   $_SESSION['amnttr'] = $total_amountt;
//   
//   
//    //splitting of the money
	  $jdlab_share = '150'; //jd share 
	  $school_share = $total_amountt  - $jdlab_share;
//   
//   
//   
   //remita share 
   $remita_share = '0';
   
   $remita_share_fix = '300';
   $vat_fix = '15';
   
   //final school share 
   $ss = $school_share - $remita_share;
   
   
   
$totalAmount = $total_amountt;//"1000";
$timesammp=DATE("dmyHis");		
$orderID = $txnref;//$timesammp;
$payerName = $fullname;//$_POST["name"];
$payerEmail = $email;//$_POST["email"];
$payerPhone = $phone_no;$_POST["phone"];
$responseurl = PATH . "/dashboard.php?act=regfee_response&qlk=c9f0f895fb98ab9159f51fd0297e236d";
//$responseurl="http://www.clickschools.online/index.php?remita/payment_response";
$hash_string = MERCHANTID . SERVICETYPEID . $orderID . $totalAmount . $responseurl . APIKEY;
$hash = hash('sha512', $hash_string);
$itemtimestamp = $timesammp;
$itemid1="COEMINNA NCE REGISTRATION FEE";//"itemid1";
$itemid2="PROCESSING FEE / VAT";//"34444".$itemtimestamp;
$itemid3= "8694".$itemtimestamp;
$beneficiaryName="Niger State College of Education Minna";
$beneficiaryName2="JD Lab Education Collection Account";
$beneficiaryName3="Remita";
$beneficiaryAccount="6020067886";
$beneficiaryAccount2="6020067886";
$beneficiaryAccount3="6020067886";
$bankCode="011";
$bankCode2="050";
$bankCode3="070";
$beneficiaryAmount = $ss;//"900";
$beneficiaryAmount2 = $jdlab_share; //"50";
$beneficiaryAmount3 = $remita_share; //"50";
$deductFeeFrom=1;
$deductFeeFrom2=0;
$deductFeeFrom3=0;
//The JSON data.
$content = '{"merchantId":"'. MERCHANTID
.'"'.',"serviceTypeId":"'.SERVICETYPEID
.'"'.",".'"totalAmount":"'.$totalAmount
.'","hash":"'. $hash
.'"'.',"orderId":"'.$orderID
.'"'.",".'"responseurl":"'.$responseurl
.'","payerName":"'. $payerName
.'"'.',"payerEmail":"'.$payerEmail
.'"'.",".'"payerPhone":"'.$payerPhone
.'","lineItems":[
{"lineItemsId":"'.$itemid1.'","beneficiaryName":"'.$beneficiaryName.'","beneficiaryAccount":"'.$beneficiaryAccount.'","bankCode":"'.$bankCode.'","beneficiaryAmount":"'.$beneficiaryAmount.'","deductFeeFrom":"'.$deductFeeFrom.'"},
{"lineItemsId":"'.$itemid2.'","beneficiaryName":"'.$beneficiaryName2.'","beneficiaryAccount":"'.$beneficiaryAccount2.'","bankCode":"'.$bankCode2.'","beneficiaryAmount":"'.$beneficiaryAmount2.'","deductFeeFrom":"'.$deductFeeFrom2.'"},
{"lineItemsId":"'.$itemid3.'","beneficiaryName":"'.$beneficiaryName3.'","beneficiaryAccount":"'.$beneficiaryAccount3.'","bankCode":"'.$bankCode3.'","beneficiaryAmount":"'.$beneficiaryAmount3.'","deductFeeFrom":"'.$deductFeeFrom3.'"}
]}';
$curl = curl_init(GATEWAYURL);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
//	curl_setopt($curl, CURLOPT_URL,$curl);
		//$proxy_ip = '192.9.200.10'; //proxy IP here //////
	
//$proxy_port = 3128; //proxy port from your proxy list //////
			//curl_setopt($curl, CURLOPT_PROXYPORT, $proxy_port); //////
			//curl_setopt($curl, CURLOPT_PROXYTYPE, 'HTTP'); //////
	
//curl_setopt($curl, CURLOPT_PROXY, $proxy_ip); //////
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$jsonData = substr($json_response, 6, -1);
$response = json_decode($jsonData, true);
var_dump($response);
$statuscode = $response['statuscode'];
$statusMsg = $response['status'];
//if($statuscode=='025'){
//$rrr = trim($response['RRR']);
//$new_hash_string = MERCHANTID . $rrr . APIKEY;
//$new_hash = hash('sha512', $new_hash_string);
//echo '<html>
//<head>
//<link rel="stylesheet" href="css/bootstrap.min.css">
//<link rel="stylesheet" href="css/bootstrap-dark.min.css">
//</head>
//<body>
//  <div class="container">
//	<div class="row">
//    <div class="col-xs-12 col-md-9 col-lg-7">
//
//<form action="'.GATEWAYRRRPAYMENTURL.'" method="POST">
//<input id="merchantId" name="merchantId" value="'.MERCHANTID.'" type="hidden"/>
//<input id="rrr" name="rrr" value="'.$rrr.'" type="hidden"/>
//<input id="responseurl" name="responseurl" value="'.$responseurl.'" type="hidden"/>
//<input id="hash" name="hash" value="'.$new_hash.'" type="hidden"/>
//
//</div>
// <div class="form-group">
//	<div class="col-sm-8 col-sm-offset-4">
//		<input type="submit" class="btn btn-sm btn-primary" name="submit" value="Submit" />
//	</div>
//</div>
//	</form>
//</div>
//</div>
//</div>
//</body>
//</html>';
//}
//else{
//echo "Error Generating RRR - " .$statusMsg;
//}


    


 


 
 
 
  //echo $_SERVER['HTTP_USER_AGENT'];
 
 ////##### insert to the remita transaction log
 
              $transaction_type = 1; //applicants fresh student
			  $transaction_for = 1; //registration fee fresh
			  $payment_method = 1; //1 for remita pay via card payment , 2 - remita pay via bank branch payment
			  
			  
			  $iid2 = $iid;//pass invoice id
			  $title2 = $title;
			  $desc2 = $description;
			  
			 
			  //get payment status using 
			 // $payment_status = get_payment_status_by_resp_code($statt);
			  //$amt = $amt / 100;
			  //$amt = $tot;
			  
			  
			  
			    if(!r_trf_exist($txnref, $invoice_to) && $txnref != '')
			    {
				

			  
					  $qry23="INSERT INTO `remita_webpay_transaction_log` (`invoice_id`, `transaction_type`, `user_id`, `transaction_for`,
					   `transaction_reference`, `title`, `description`, `amount`, `date_added`, `account`) 
						 
						VALUES ('".$iid2."','$transaction_type','".$invoice_to."','$transaction_for','$txnref','".$title2."','".$desc2."','$total_amountt',NOW(),'$beneficiaryAccount')";
						 
					  $rst23 =  mysqli_query($dbc, $qry23);
				  
					  //if(mysqli_affected_rows($dbc))
					  if($rst23)
					  {//end of if update is successful
					  
							 
							 
							 //clear some sessions
							 
							 //unset($_SESSION['iid']);
							 //unset($_SESSION['amnt']);
							 
							 
							
					  
					  
					  //do nothing for now
					  
					  }
					  else
					  {
						   $msg =   '&nbsp;&nbsp;SERVER ERROR: Transaction Log has issues, we are sorry for the incovenience.';
								
						   header("Location: dashboard.php?act=view_invoices3&error=".urlencode($msg)."&qlk=".md5(8)."&iid=".$iid);
				  
					  }

 
				}//en dof if check
				
			  
 
 ?>
 <!--<div style="height:20px; displat:block;"></div>-->
  <ul class="breadcrumb">
    
        <li><a href="dashboard.php?act=payments&qlk=<?php echo md5(8); ?>">Payments</a> <span class="divider"></span> </li>
        <li><a href="dashboard.php?act=view_invoices3&qlk=<?php echo md5(8); ?>&auth=<?php echo sha1(md5(2))?>&iid=<?php echo $iid; ?>">View Invoice</a> <span class="divider"></span> </li>

        <li class="active" > Confirmation Page <span class="divider"></span> </li>
        
        
        
    </ul>
  
  
<div class="well"><!-- well starts -->


      <div class="row" style="padding-left:0px; padding-right:0px;">
      
                      <ul class="list-group">
                          <li class="list-group-item list-group-item-danger">
                              <h5>PLEASE CONFIRM YOUR PAYMENT DETAILS, IF THEY ARE ALL CORRECT?</h5>
                          </li>
                      </ul>
                      
                      <div class="col-md-12">
                      
                      
                       <p><span style="color:#foo" > *</span> Please note that the processing fee is <strong>N315</strong> only (VAT Inclusive)</p><br/><br/> 
                       
                            
                      <h4><span class="glyphicon glyphicon-pushpin"></span> Your Reference: (<span style="color:#F00"><?php echo $txnref; ?></span>) </h4>
                      <p style="color:#F00"> <strong>Please copy this  reference somewhere incase of transaction challenges </strong></p>
                          <div class="form-actions pull-right" style="margin-top:10px; padding-left:0px; padding-right:0px;">
                          			
                          			
                                    
                                    <?php if($payment_status == '1'){ ?>
                                    <div ><input type="submit" name="" class="btn btn-info" value="Paid"  data-toggle="tooltip" data-placement="top" title="You have made payment for this invoice" disabled="disabled"/></div>
                                    <?php }else{ ?>
                                    <div class="pull-right">
<!--                                     <input type='image' src='http://voguepay.com/images/buttons/make_payment_blue.png' alt='Submit' height="35" width="160" />
-->                                   
                                       <a href="#" class="btn btn-lg btn-info" data-toggle="modal" data-target="#myModallR"> YES, PAY!</a>


										<!-- <button type="submit"  class="btn btn-info"  data-toggle="tooltip" data-placement="top" title="Click here to Pay"> YES, PAY!</button>-->
         
                                    </div>
                                    <?php } ?>
                                    
                                    
                                   
                                   
                                   
                                   
                                   <!-- </form>--><!-- end of form for the inst -->

                                  	   
                                 	
                              </div>
                      
      
                    </div><!-- /.col md 12 -->          
                     
                      <div class="col-md-12">
                      	
                        <div class="col-md-4 help-block">
                      		<p class="help-block">Invoice # <span class="badge badge-success"><?php echo $iid ;?></span></p>
                            Invoice Date: <?php echo get_invoice_date($iid) ;?><br>
                            Due Date: <?php echo get_invoice_due_date($iid) ;?><br>
                            Expire Date / Time: <?php echo get_invoice_exp_date($iid) ;?>
                            
                        </div><!-- /.col  md 4-->
                        
                        <div class="col-md-4">
                        	<p class="help-block">Invoice To </p>
                            <?php echo get_user_fullname($invoice_to); ?><br><?php echo '<b>APPLICATION No.: '.$appno.'</b><br/>'; ?>
                            <?php echo $email; ?><br>
                            <?php echo get_user_address($invoice_to); ?><br>
                            <?php echo show_state(get_user_state_id($invoice_to)); ?><br>
                             <?php //echo show_country(get_user_country_id($invoice_to)); ?>.<br>
                         
                        
                      	</div><!-- /.col  md 4-->
                      	
                        
                        <div class="col-md-4">
                        
                       		<p class="help-block">Pay To </p>
                            <?php echo $pay_to; ?><br>
                            <span class="label label-info"> Payment Mode: </span> &nbsp; <strong><?php echo $show_payment_mode; ?></strong>

                           
                      	</div><!-- /.col  md 4-->
                      	
                      	
                        
                      
                      </div><!-- /.col  md 12-->
                      
                         <div style="height:100px"></div><!-- giv space -->
                     
                        <div class="col-md-12">
                       		
                            
                            
                            
                            <li class="list-group-item help-block" style="min-height:60px;">
                            	 <span>Total Amount: 
                                </span> 
                                <span class="pull-right text-success" style="font-size:35px; font-weight:bold;"><?php echo 'N'.number_format($total_amountt + $remita_share_fix + $vat_fix ,2).'K'; ?> 
                                </span>
                               
                            </li>
                            
                            
                         </div>
                         
                        <div class="clearfix"></div>

                         <br/>
                         
                    
      
      </div><!-- /.col md 12 -->
      
      
      
		
	
</div><!-- /.well -->	

<div class="col-md-12">
       
       <br/>
          
        <hr> 
       
       </div>
         
        
        <div class="col-md-12">
              <div class="col-sm-8  help-block">     
                    
                     
                     
                     <img src="images/remita-payment-logo-horizonal-opt.png" class="img-responsive" />
                  
                  </div>    
              
                <div class="col-sm-4  help-block">     
                    <h4>For Support, Email or Call : </h4>
                    <h5><span class="glyphicon glyphicon-envelope"></span> eportals@coeminna.edu.ng </h5>
                     <h5><span class="glyphicon glyphicon-phone"></span> +234- 08142411717 OR 07068401380 </h5>
                     
                     
                     <img src="images/support.png" class="pull-right" />
                  
                  </div>
         </div>
         
         
         
         
         
         


<!--  Modal  -->
<div class="modal fade" id="myModallR" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Remita Reponse / Payment Type</h4>
      </div>
      <div class="modal-body">
            
                <?php
					
					if($statuscode=='025'){
					$rrr = trim($response['RRR']);
					$new_hash_string = MERCHANTID . $rrr . APIKEY;
					$new_hash = hash('sha512', $new_hash_string);
 
				
				
				?> 
			 
                
                <form action="<?php echo GATEWAYRRRPAYMENTURL; ?>" method="POST">
                <input id="merchantId" name="merchantId" value="<?php echo MERCHANTID; ?>" type="hidden"/>
                <input id="rrr" name="rrr" value="<?php echo $rrr; ?>" type="hidden"/>
                <input id="responseurl" name="responseurl" value="<?php echo $responseurl; ?>" type="hidden"/>
                <input id="hash" name="hash" value="<?php echo $new_hash;?>" type="hidden"/>


                 <!--   <label class="control-label">Please Choose Payment Type</label>
                       
                       
                        <select name="paymenttype" class="form-control">
                            <option value=""> -- Select --</option>
                            <option value="REMITA_PAY"> Remita Account Transfer</option>
                            <option value="Interswitch"> Verve Card</option>
                            <option value="UPL"> Visa</option>
                            <option value="UPL"> MasterCard</option>
                            <option value="PocketMoni"> PocketMoni</option>
                            <option value="RRRGEN"> POS</option>
                            <option value="ATM"> ATM</option>
                            <option value="BANK_BRANCH">BANK BRANCH</option>
                            <option value="BANK_INTERNET">BANK INTERNET</option>
                        </select>
                 -->
                 
                 
                 <h4>Are you sure you want to proceed with the payments ?</h4>
                
                
                <?php
                
				
				}
                else
				{
							 
						echo  '<div class="alert alert-danger alert-dismissable"> 
					  				<button class="close" aria-hidden="true" data-dismiss="alert" type="button">x</button>
					  					&nbsp;&nbsp; <i class="glyphicon glyphicon-exclamation-sign"></i>  Error Generating RRR -'.$statusMsg.' 
					 		  </div>';
                }
                
                
                ?>
                

                  
		
			</div><!-- /body -->
			
      		
            <div class="modal-footer">
            
            <?php 
			 if($statuscode=='025')
			 {
				 
				 ?>
            
             		<input type="submit" class="btn btn-sm btn-success" name="submit" value="YES" />
                    
                    
                  <?php 
				  
				  
			 }
			 
			 ?>


            <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
          
            
           </div>
               
          
                
               
            </form>
        
          
               
       </div>
	</div>
</div>
<!------------------------------- modal box for query ------------------------------------>
         
         
         
         
           	

<?php

 }//end of iid exist
else
{
	//do nothing for now
}
?>
<?php ?>