<?php

    
	if($response_code == '01' || $response_code == '00')
	{

		//Payment Successful, You can Update Status to Paid here on Database;
				
		$user_id = get_invoice_to_using_txnref($orderId); //get the user id from 
		

		//get the invoice details
		$iid = get_invoice_id_using_txnref($orderId);
		
		
		 $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
		 $invoice_amount =	$total_amount;

		//	 $credit = get_invoiced_credit($iid); //get invoice credit
			 
		//	 $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
		
		 //get invoice instalment or fulpayment
		 //$payment_mode = get_invoice_payment_mode($iid);
			//show payment mode
		 //$show_payment_mode = show_payment_mode($payment_mode);
		
		
		
		//get user programme details
		//$user = get_user_id();//get user
		//$level = '100'; //get user level
		$programme_type = get_user_programme_type_applied_for($invoice_to); //programme type
		$school = get_user_school_applied_for($invoice_to); //school

		$department = get_user_department_applied_for($invoice_to); //department
		$programme  = get_user_programme_applied_for($invoice_to);//programmeme
		
		$full_name = get_user_fullname($invoice_to);
		$email = get_user_email($invoice_to);
		$phone_no = get_user_phone_no($invoice_to);
	    
		
		//collect matric number an dother student info
		$appno = get_user_application_number($invoice_to);
		$mode_of_entry = get_applicant_mode_of_entry($invoice_to);
		
		 //get current session 
		 $session = get_current_session_4($id = 5); //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
		 
		 //get remita commission charges
		 $remita_share_fix = '300';
		 $vat_fix = '15';
		 
		 $extra_tot_m = $remita_share_fix + $vat_fix;
		 
		 
		 
		 //$applicant_level = 'I'; //aplicant level 
	  
	  	 $application_number = get_user_application_number($invoice_to);
		 
		 
		 


		 if(!applicant_id_exist_payments_4($invoice_to, $session)) //in future add session to match exactly the candidate
		 {
		 		
                   $cid = get_user_cart_id($id);
                     
                    //when Prence
                     if( $programme_type == 1){
                      	$mode = 1;
                       //when NCE Reg or LVSP
                     }elseif ($programme_type == 2 ) {
                     	//when LVSP
                        if ( $cid == 2) {
                          $mode = 2;
                          $session = 3;
                        }else{
                              $mode = 1; //for now
                        }

                     }

                     

				
				/*$mode = 1; //programme mode 1 -  full time, 2 part time*/
				
				
				$payment_year = date('Y');//payment year

				$qry44="INSERT INTO `applicants_nce_fee_payments`
				(`mode`,`programme_type_id`,`department_id`,`programme_id`,`session`,`semester`,`applicant_id`, `payment_method`, `payment_status`,`invoice_id`, `amount`, `added_by`,`date_added`) 
				 VALUES ('$mode','$programme_type','$department','$programme','$session','$semester','$invoice_to','3','1','$iid','$invoice_amount','ghost',NOW())";
				
				 $rst44 =  mysqli_query($dbc, $qry44);
				
				if($rst44)//mysqli_affected_rows($dbc)
				{
				  //if done finally collect all info for receipt
				  
				  
				  
				   //query
					$q441 = "UPDATE invoices SET payment_status = 1 WHERE invoice_id = '$iid'"; //update invoice to paid where payment status is 1
					$r441 = mysqli_query($dbc, $q441);
					
					if(mysqli_affected_rows($dbc))
					//if($r441)
					{//end of if update is successful
					  
					   
					  // echo '<br/><br/> <a target="_blank" href="create_app_payment_slip.php?qlk='.md5(8).'&me='.$invoice_to.'" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="Click here to Print / Download Payment Receipt"><i class="glyphicon glyphicon-print"> </i> Print / Download Receipt</a>';
					   //clear some sessions
					   $mg  = '';
						if ($invoice_for == 0) {
							$mg = ', your Application Fees Payment was successful.'; //application fee
						} else {
							$mg = ', your Screening Fees Payment was successful.'; //application fee //screening fee
						}
		
		
				   		//get user phone number
						$phone_no = get_user_phone_no($invoice_to);
						$fname = get_user_first_name($invoice_to);
						//attach sms message
						$sms_msg = 'Dear '.$fname.$mg; //Please login with valid credentials to continue your application process.-';

						//send_sms($phone_no,$sms_msg.' '); //send sms to user


					   $applicant_fee_payments_entered = true;
					   
					   echo '<li class="list-group-item list-group-item-success"><h3> Payment Successfully processed</h3></li><br>';
		  
					}
					else
					{
					 	echo   $msg =   'li class="list-group-item list-group-item-danger"><h3>  &nbsp;&nbsp;SERVER ERROR: Please try again.(01)</h3></li><br>';
					 // header("Location: dashboard.php?act=view_invoices&error=".urlencode($msg)."&qlk=".md5(8)."&auth=".sha1(md5(2))."&iid=".$iid);
		  
					}//end of if invoice is updated
			  
				  
				  
				}
				else
				{
				   //else tell the admin to reprint
					
					echo $msg =  '<li class="list-group-item list-group-item-danger"><h3> System Error! We are sorry for inconvenience.Please Retry again. Thank you. (02)</h3></li><br>';
					//header("Location: dashboard.php?act=view_invoices&error=".urlencode($msg)."&qlk=".md5(8)."&auth=".sha1(md5(2))."&iid=".$iid);
				}
				
		 }//end of if applicnt id exist in payment
		 else
		 {
					  // echo '<br/><br/> <a target="_blank" href="create_app_payment_slip.php?qlk='.md5(8).'&me='.$invoice_to.'" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="Click here to Print / Download Payment Receipt"><i class="glyphicon glyphicon-print"> </i> Print / Download Receipt</a>';
					  
			$q441 = mysqli_query($dbc,"UPDATE invoices SET payment_status = 1       WHERE invoice_id = '$iid'");
			
			echo '<li class="list-group-item list-group-item-info"><h3>Payment already processed</h3></li><br>';


		 }
		 
		 
			
		
		
		$transaction_type = 0; //applicants
		$transaction_for = ''; 

		if ($invoice_for == 0) {
			$transaction_for = 0; //application fee
		} else {
			$transaction_for = 1; //screening fee
		}
		
		
		$payment_method = 3; //3 for remita pay bank branch payment
		
		
		$iid2 = $iid;//pass invoice id
		$title2 = $title;
		$desc2 = $description;
		
	   
		//get payment status using 
		$payment_status = get_payment_status_by_resp_code($response_code);
		//$amt = $amt / 100;
		$amt = $tot;
		
		
		
		
		
		if($rrr != '')
		{
		
		
		  if($rrr != '' && $orderId != '')
		  {
		  

		
				$qry23 ="UPDATE  `remita_webpay_transaction_log`  SET  
				 `payment_method` = '$payment_method', `payment_status` = '$payment_status', `response_code` = '$response_code', `response_description` = '$response_message',
				 `total_paid_by_buyer` = '$invoice_amount',`extra_charges_by_merchant` = '$extra_tot_m',
				  `transaction_id` = '$rrr',  `email` = '$email', `transaction_date` =NOW()
				 
				 WHERE transaction_reference = '$orderId'";
				   
				$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
				{
					echo $msg6 =   '&nbsp;&nbsp;SERVER ERROR: Transaction Log has issues, we are sorry for the incovenience. Error(06)';
						  
				   //header("Location: dashboard.php?act=view_invoices3&error=".urlencode($msg)."&qlk=".md5(8)."&iid=".$iid);
			
				}


		  }//en dof if check
		  
		}//end of RRR aint empty

		 
		
		
	}//end of if response code is successull
	
?>		
		
