<?php

require_once('config/config.php');
require_once('foo/functions.php');
dbconnect();
session_start();

$account = preg_replace('/\s/', '', $_SESSION['user_acc']);
$currency = $_SESSION['currency'];
$sql = "SELECT * FROM `".TRANF_TABLE_NAME."` WHERE user_name='{$_SESSION['user_login']}' AND user_acc='{$_SESSION['user_acc']}' ORDER BY tranf_id DESC LIMIT 1";
$stmt = $pdo->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$beneficiary = $row['rcv_name'];
$rcvbk = $row['rcv_bk'];
$rcvacc = $row['rcv_acc'];
$amount = $row['atmpt_amt'];

$details = "Transfer to $beneficiary - $rcvacc in $rcvbk";
$transdate = date("l jS \of F Y h:i A");
$transcode = get_transcode(10);


	if (SUCCESS_FULL) {		
		// debit the account with the transfer
		trans_debit($_SESSION['user_acc'], $_SESSION['user_login'], $details, $row['atmpt_amt']);
		
		
		// Send EMAIL
		$email = get_email($_SESSION['user_acc']);
		$message = "
		<html>
		<head>
		<title>".WEB_NAME."</title>
		</head>
		<body>
		<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
		<p>FUNDS TRANSFER SUCCESSFULLY COMPLETED:<br><br>
		DATE: ".$transdate." <br>
		TRANSACTION CODE: ".$transcode." <br>	
		SOURCE ACCOUNT: ".$account." <br>
		BENEFICIARY BANK: ".$rcvbk." <br>
		ACCOUNT NUMBER: ".$rcvacc." <br>
		ACCOUNT NAME: ".$beneficiary." <br> 
		AMOUNT ".$currency."".$amount."
		</p><br><br><br>
		===========
		<p>".WEB_NAME."</p>
		</body>
		</html>
		";
		
		// Send the email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
		$subject = "TRANSFER SUCCESSFULLY COMPLETED";
		mail($email,$subject,$message,$headers);
		
		
		if (USE_SMS) {
		//$phone = ltrim(user_phone($_SESSION['user_login']), '+');
		$phone = clean_phone(user_phone($_SESSION['user_login']));
		$sms_cnt = "TRANSFER FROM ".$account." TO ".$beneficiary.", AMOUNT ".$currency."".$amount." COMPLETED SUCCESSFULLY";
		
		// Send SMS
		$url="http://www.easy2text.com/index.php";

		$postfields = array(
			'app' => SMS_APP,
			'ta' => SMS_TA,
			'u' => SMS_USER,
			'p' => SMS_WORD,
			'to' => "$phone",
			'from' => SMS_SENDER,
			'msg' => "$sms_cnt"
		);

		if (!$curld = curl_init()) {
			exit;
		}

		curl_setopt($curld, CURLOPT_POST, true);
		curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
		curl_setopt($curld, CURLOPT_URL,$url);
		curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($curld);
		curl_close ($curld);
		
		}
		log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_SESSION['user_acc'], 'Transfer Succefully Completed');
		complete_transfer($_SESSION['user_login'], '1', '1', $_SESSION['user_acc']);
		redirect('success.php');
	}
	else {
		
		
		// Send EMAIL
		$email = get_email($_SESSION['user_acc']);
		$message = "
		<html>
		<head>
		<title>".WEB_NAME."</title>
		</head>
		<body>
		<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
		<p>FUNDS TRANSFER FAILED:<br><br>
		DATE: ".$account." <br>	
		TRANSACTION CODE: ".$transcode." <br>
		SOURCE ACCOUNT: ".$account." <br>
		BENEFICIARY BANK: ".$rcvbk." <br>
		ACCOUNT NUMBER: ".$rcvacc." <br>
		ACCOUNT NAME: ".$beneficiary." <br> 
		AMOUNT ".$currency."".$amount."
		</p><br><br><br>
		===========
		<p>".WEB_NAME."</p>
		</body>
		</html>
		";
		
		// Send the email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
		$subject = "ONLINE FUNDS TRANSFER FAILED";
		mail($email,$subject,$message,$headers);

		
		
		if (USE_SMS) {
		//$phone = ltrim(user_phone($_SESSION['user_login']), '+');
		$phone = clean_phone(user_phone($_SESSION['user_login']));
		$sms_cnt = "TRANSFER FROM ".$account." TO ".$beneficiary.", AMOUNT ".$currency."".$amount." FAILED";
		
		// Send SMS
		$url="http://www.easy2text.com/index.php";

		$postfields = array(
			'app' => SMS_APP,
			'ta' => SMS_TA,
			'u' => SMS_USER,
			'p' => SMS_WORD,
			'to' => "$phone",
			'from' => SMS_SENDER,
			'msg' => "$sms_cnt"
		);

		if (!$curld = curl_init()) {
			exit;
		}

		curl_setopt($curld, CURLOPT_POST, true);
		curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
		curl_setopt($curld, CURLOPT_URL,$url);
		curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($curld);
		curl_close ($curld);
		
		}
		complete_transfer($_SESSION['user_login'], '1', '0', $_SESSION['user_acc']);
		redirect('error.php?error=' . urlencode(FINAL_ERROR));
	}

?>