<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Public Bank Payment Gateway</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

</head>

<style>

* {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}

input[type="text"] {
width:150px;
}

#wrapper {
width:573px;
padding-left:100px;
margin:0 auto;
}

#Table1 {
margin-top:20px;
}

.example {
font-size:11px;
font-style:italic;
}

.hide {
	display:none;
	visibility:hidden;
	height:0px;
}

.status {
	margin:20px 0px;
	margin-left:145px;
	font-size:18px;
}

.submit {
	margin-left:178px;
}

</style>

<body>

<?php
		// require("class.phpmailer.php");
		// require 'PHPMailerAutoload.php';
		// $mx				= new PHPMailer;
		//
		// $mx->isSMTP();
		// $mx->Host       = "mail.vivalife.com.my";
		// $mx->SMTPDebug  = 2;
		// $mx->SMTPAuth   = true;
		// $mx->Port       = 587;
		// $mx->Username   = "admin@vivalife.com.my";
		// $mx->Password   = "&Avi$#vA0608";
		//
		// $mx->setFrom('order@vivalife.com.my', 'Vivalife');
		// $mx->AddReplyTo('order@vivalife.com.my','Vivalife');
		// $mx->IsHTML(true);
		// $mx->Subject	= "Order Confirmation from Vivalife.com.my E-Store";
		// $mx->Body		= 'checking 1';
		// $mx->AddAddress('allangan85@gmail.com');
		// $mx->Send();
		// exit();
?>





<?php

/*$host="localhost"; // Host name

$username="ecomm"; // Mysql username

$password="Viad749fa"; // Mysql password

$db_name="vivalife_com_my_ecomm"; // Database name*/



// Connect to server and select database.

//mysql_connect("$host","$username","$password")or die("cannot connect");

$link = mysqli_connect("localhost","admin_vivalife_v3","owb90W?1","admin_vivalife_v3")or die("cannot connect");

mysqli_select_db($link,"admin_vivalife_v3")or die("cannot select DB");



date_default_timezone_set('Asia/Kuala_Lumpur');



//define

//$result = $_POST['result'];

$result = '0065432100000000000000P1478099880808000000000100';

$order_id_salt = htmlentities($_GET['order_id']);

//$order_id = substr($order_id_salt, 10);

$order_id = 2166;



$response = substr($result, 0, 2);

$auth = substr($result, 2, 6);

$invoice = substr($result, 8, 20);

$pan = substr($result, -20, -16);

$expire_date = substr($result, -16, -12);





//echo $result;





/*echo '<br>';

echo 'Time : '.date('H:i:s a');

echo '<br>';

echo 'Authorization Code :#'.$auth.'# Total string : '.strlen($auth);

echo '<br>';

echo 'Response Code :#'.$response.'# Total string : '.strlen($response);

echo '<br>';

echo 'Invoice Number :#'.$invoice.'# Total string : '.strlen($invoice);

echo '<br>';

echo 'Result String :#'.$result.'# Total string : '.strlen($result);

echo '<br>';

$my_value =$result;

for($i=0;$i<=strlen($my_value);$i++){

echo $my_value[$i].",";}*/



// check status



if($response == "00") {



	//echo "Approved";



		$order_id_salt = htmlentities($_GET['order_id']);

		//$order_id = substr($order_id_salt, 10);

		$order_id = 2166;



		$query9 = "SELECT * FROM oc_order WHERE order_id='$order_id'";

		$result9 = mysqli_query($link,$query9) or die(mysqli_error($link));

		$row9 = mysqli_fetch_array($result9);

		//echo '<pre>'; print_r($row9); echo '</pre>';

		/** query customer table **/



		$customer_id = $row9['customer_id'];



		$query20 = "SELECT * FROM oc_customer WHERE customer_id='$customer_id'";

		$result20 = mysqli_query($link,$query20) or die(mysqli_error($link));

		$row20 = mysqli_fetch_array($result20);

		//echo '<pre>'; print_r($row20); echo '</pre>';



		$distID = $row20['DistID'];

		/** end query customer table **/







		/** query coupon total table **/





		//$query20a="SELECT * FROM oc_order_total WHERE order_id = '$order_id' AND code LIKE 'coupon'";

		$query20a = "SELECT * FROM oc_order_total WHERE order_id = '$order_id' AND code LIKE 'coupon'";

		$result20a = mysqli_query($link,$query20a) or die(mysqli_error($link));

		$row20a = mysqli_fetch_array($result20a);

		print_r('<br/>');
		print_r('<pre>');
		print_r($row20a);
		print_r('</pre>');







		if($row20a['alCouponCode'] == "" || empty($row20a['alCouponCode'])) {
			$couponCode = 0;
			$couponAmount = 0;
		} else {
			$couponCode = $row20a['alCouponCode'];
			$couponAmount = $row20a['value'];
		}

		/** end query coupon total table **/







	//update database

		//$sql="UPDATE `order` SET invoice_pbb='$invoice', authcode='$auth', pan_pbb='$pan', expirydate_pbb='$expire_date' WHERE order_id=108";

		$result_q = htmlentities('0065432100000000000000P1478099880808000000000100');

		//$result_q = htmlentities($_POST['result']);



		$order_id_salt_q = htmlentities($_GET['order_id']);

		//$order_id_q = substr($order_id_salt_q, 10);

		$order_id_q = 2166;



		$card = htmlentities($_GET['card']);

		$auth_q = substr($result_q, 2, 6);

		$pan_q = substr($result_q, -20, -16);

		$expire_date_q = substr($result_q, -16, -12);

		$invoice_q = substr($result_q, 8, 20);



		$sql="UPDATE oc_order SET invoice_pbb='$invoice_q',authcode='$auth_q', pan_pbb='$pan_q', expirydate_pbb='$expire_date_q', card='$card',order_status_id='15' WHERE order_id=".$order_id_q."";











		$sql2="INSERT INTO oc_coupon_history SET coupon_id='$couponCode', order_id='$order_id_q', customer_id='$customer_id', amount = '$couponAmount', date_added = NOW()";

















$order_id9 = $row9['order_id'];

$total9 = $row9['total'];

$user9 = $row9['firstname'];

$email9 = $row9['email'];





$productQuery="SELECT product_id,quantity,order_product_id FROM oc_order_product WHERE order_id = '" . (int)$order_id9 . "'";

$product_result = mysqli_query($link,$productQuery) or die(mysqli_error($link));

while($product=mysqli_fetch_array($product_result))

{

	$resubQuery="SELECT subtract FROM oc_product WHERE product_id='".(int)$product['product_id']."'";

	$resub_query_result = mysqli_query($link,$resubQuery) or die(mysqli_error($link));

	if (mysqli_num_rows($resub_query_result))

	{

		$resub_query_row=mysqli_fetch_array($resub_query_result);

		$resub_value = $resub_query_row['subtract'];

		if ($resub_value)

		{

			$updateQuery="UPDATE oc_product SET quantity = (quantity - " . (int)$product['quantity'].") WHERE product_id = '".(int)$product['product_id']."'";

			mysqli_query($link,$updateQuery) or die(mysqli_error($link));



			$optionQuery="SELECT product_option_value_id FROM oc_order_option WHERE order_id = '" . (int)$order_id9 . "' AND order_product_id = '" . (int)$product['order_product_id']."'";

			$option_query_result = mysqli_query($link,$optionQuery) or die(mysqli_error($link));

			while($option=mysqli_fetch_array($option_query_result))

			{

				$updateOptionQuery="UPDATE oc_product_option_value SET quantity = (quantity - " . (int)$product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'";

				mysqli_query($link,$updateOptionQuery) or die(mysqli_error($link));

			}

		}

	}

}

$order_id9 = $row9['order_id'];

$date_added = $row9['date_added'];

$payment_method = $row9['payment_method'];

$shipping_method = $row9['shipping_method'];

$telephone = $row9['telephone'];

$ip = $row9['ip'];

$comment = $row9['comment'];



$total9 = $row9['total'];

$user9 = $row9['firstname'];

$email9 = $row9['email'];



$payment_company = $row9['payment_company'];

$payment_firstname = $row9['payment_firstname'];

$payment_lastname = $row9['payment_lastname'];

$payment_address_1 = $row9['payment_address_1'];

$payment_address_2 = $row9['payment_address_2'];

$payment_city = $row9['payment_city'];

$payment_postcode = $row9['payment_postcode'];

$payment_zone = $row9['payment_zone'];

$payment_country = $row9['payment_country'];



$shipping_company = $row9['shipping_company'];

$shipping_firstname = $row9['shipping_firstname'];

$shipping_lastname = $row9['shipping_lastname'];

$shipping_address_1 = $row9['shipping_address_1'];

$shipping_address_2 = $row9['shipping_address_2'];

$shipping_city = $row9['shipping_city'];

$shipping_postcode = $row9['shipping_postcode'];

$shipping_zone = $row9['shipping_zone'];

$shipping_country = $row9['shipping_country'];





/*$message =



"Hello $user9, \n



Thank you for ordering from us. Here are your order details.\n



Order ID: $order_id9 \n

Total Amount: RM $total9



You can login to your account to check your order status via vivalife.com.my/estore.

We will process your order shortly.\n





Thank You

Vivalife.com.my/estore

";*/







/*	 $headers = "From: Vivalife <customer@vivalife.com.my>" . "\r\n" .

"Bcc: alan_gts85@yahoo.com";*/



	//mail($email9, "Order Confirmation from Vivalife.com.my E-Store", $message, $headers);



	require("class.phpmailer.php");

	require 'PHPMailerAutoload.php';

	/*$productQueryEmail="SELECT product_id,quantity,order_product_id FROM oc_order_product WHERE order_id = '" . (int)$order_id9 . "'";

	$product_result_email = mysql_query($productQueryEmail) or die(mysqli_error($link));

	while($productemail=mysql_fetch_array($product_result_email))

	{



	}*/

	$mx				= new PHPMailer;



	$mx->isSMTP();

	$mx->Host       = "mail.vivalife.com.my"; // SMTP server example

	$mx->SMTPDebug  = 0;                      // enables SMTP debug information (for testing)

	$mx->SMTPAuth   = true;                  // enable SMTP authentication

	$mx->Port       = 587;                    // set the SMTP port for the GMAIL server

	$mx->Username   = "admin@vivalife.com.my"; // SMTP account username example

	$mx->Password   = "&Avi$#vA0608";        // SMTP account password example



	$mx->setFrom('order@vivalife.com.my', 'Vivalife');

	$mx->AddReplyTo('order@vivalife.com.my','Vivalife');



	$mx->addBCC("order@vivalife.com.my", "Vivalife");

	//$mx->addBCC("allangan85@gmail.com");

	$mx->IsHTML(true);

	$mx->Subject	= "Order Confirmation from Vivalife.com.my E-Store";



	$mx->Body		= '



	<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/DTD/strict.dtd">

		<html>

		 <head>

		  <title>Vivalife</title>

		 </head>



 	<body style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000;">







<div style="width: 680px;"><a href="http://vivalife.com.my/estore/" title="VIVA Life Science"><img src="http://vivalife.com.my/estore/image/data/logo_03.gif" alt="VIVA Life Science" style="margin-bottom: 20px; border: none;" /></a>

  <p style="margin-top: 0px; margin-bottom: 20px;">Thank you for your interest in VIVA Life Science products. Your order has been received and will be processed once payment has been confirmed.</p>





  <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">

    <thead>

      <tr>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;" colspan="2">Order Details</td>

      </tr>

    </thead>

    <tbody>

      <tr>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">





		<b>DistID:</b> '.$distID.'<br />



		<b>Order ID:</b> '.$order_id9.'<br />





          <b>Date Added:</b> '.$date_added.'<br />

          <b>Payment Method:</b> '.$payment_method.'<br />

          <b>Shipping Method:</b> '.$shipping_method.'

         </td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;"><b>Email:</b> '.$email9.'<br />

          <b>Telephone:</b> '.$telephone.'<br />

          <b>IP Address:</b> '.$ip.'<br /></td>

      </tr>

    </tbody>

  </table>

  <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">

    <thead>

      <tr>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">Comment:</td>

      </tr>

    </thead>

    <tbody>

      <tr>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">'.$comment.'</td>

      </tr>

    </tbody>

  </table>



  <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">

    <thead>

      <tr>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">Payment Address</td>



        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">Shipping Address</td>



      </tr>

    </thead>

    <tbody>

      <tr>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">'.$payment_company.'<br>'.$payment_firstname.' '.$payment_lastname.'<br>'.$payment_address_1.'<br>'.$payment_address_2.'<br>'.$payment_city.' '.$payment_postcode.'<br>'.$payment_zone.' '.$payment_country.'</td>



        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">'.$shipping_company.'<br>'.$shipping_firstname.' '.$shipping_lastname.'<br>'.$shipping_address_1.'<br>'.$shipping_address_2.'<br>'.$shipping_city.' '.$shipping_postcode.'<br>'.$shipping_zone.' '.$shipping_country.'</td>



      </tr>

    </tbody>

  </table>

  <table style="border-collapse: collapse; width: 100%; border-top: 1px solid #DDDDDD; border-left: 1px solid #DDDDDD; margin-bottom: 20px;">

    <thead>

      <tr>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">Product</td>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: left; padding: 7px; color: #222222;">Model</td>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">Quantity</td>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">Price</td>

        <td style="font-size: 12px; border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; background-color: #EFEFEF; font-weight: bold; text-align: right; padding: 7px; color: #222222;">Total</td>

      </tr>

    </thead>

    <tbody>

	';

	$productQueryEmail="SELECT * FROM oc_order_product WHERE order_id = '" . (int)$order_id9 . "'";

	$product_resultEmail = mysqli_query($link,$productQueryEmail) or die(mysqli_error($link));

	while($productEmail=mysqli_fetch_array($product_resultEmail))

	{

     $mx->Body		.= '

      <tr>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">'.$productEmail['name'].'

          </td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: left; padding: 7px;">'.$productEmail['model'].'</td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;">'.$productEmail['quantity'].'</td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;">RM '.number_format($productEmail['price'],2).'</td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;">RM '.number_format($productEmail['total'],2).'</td>

      </tr>

	  ';

      }



	  $mx->Body		.= '



    </tbody>

    <tfoot>

	';



    $productTotalEmail="SELECT * FROM oc_order_total WHERE order_id = '" . (int)$order_id9 . "' ORDER BY sort_order ASC";

	$product_TotalEmail = mysqli_query($link,$productTotalEmail) or die(mysqli_error($link));

	while($productTotal=mysqli_fetch_array($product_TotalEmail)) {



	 $mx->Body		.= '

      <tr>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;" colspan="4"><b>'.$productTotal['title'].':</b></td>

        <td style="font-size: 12px;	border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; text-align: right; padding: 7px;">'.$productTotal['text'].'</td>

      </tr>

	 ';

	 }





	  $mx->Body		.= '

    </tfoot>

  </table>

  <p style="margin-top: 0px; margin-bottom: 20px;">Please reply to this email if you have any questions.</p>



</div>









    </body>

		</html>';



	//$mx->AddAddress('allangan85@gmail.com');

	$mx->AddAddress($email9);

	$mx->Send();





//send the message, check for errors

/*if (!$mx->send()) {

    echo "Mailer Error: " . $mx->ErrorInfo;

} else {

    echo "Message sent!";

}*/







		$queryResult = mysqli_query($link,$sql);

		if (!$queryResult) die(mysqli_error($link));



		if ($queryResult === false)

		  echo "ERROR Please contact admin";

		else

		  echo "";





		  $queryResult2 = mysqli_query($link,$sql2);

		if (!$queryResult2) die(mysqli_error($link));



		if ($queryResult2 === false)

		  echo "ERROR Please contact admin";

		else

		  echo "";















} else {



	//echo "Decline";

	echo '<script>

		window.setTimeout(function() {

        window.location.href = "index.php?route=checkout/cart";

        }, 55000);

		</script>

        <script>

		$(document).ready(function() {

		$("#hide").addClass("hide");

		});

		</script>';



}



/*function status($auth) {

  	if($response !== 00) {

		echo "Decline";

		?>

		<script>

		window.setTimeout(function() {

        window.location.href = 'index.php?route=checkout/cart';

        }, 55000);

		</script>

        <script>

		$(document).ready(function() {

		$('#hide').addClass('hide');

		});

		</script>

	<?php

    }

	else {



   }*/

?>



<div id="wrapper"><!--wrapper-->



	<img src="image/public_bank_logo.jpg" />



  <?php /*?>  <div class="status">

    Status : <?php echo status($auth) ?>

    </div><?php */?>



    <div class="status">

    	Status: <?php if($response == "00") {



	echo "Approved";

		} else {

			echo "Declined";

		}

		?>

    </div>





	<form action="http://vivalife.com.my/estore/index.php?route=payment/worldpay/callback" method="post" id="autopost" style="display:none;">

    <table width="500" border="0" style="margin-top:10px;">

      <tr>

        <td >Order ID</td>

        <td>: <input type="text" value="<?php echo $order_id ?>" name="order_id"  /></td>

      </tr>

      <tr>

        <td width="111" >Response Code</td>

        <td>: <input type="text" value="<?php echo $response ?>" name="response"  /></td>

      </tr>

      <tr>

        <td >Authorization  Code</td>

        <td>: <input type="text" value="<?php echo $auth ?>" name="auth"  /></td>

      </tr>

      <tr>

        <td>Invoice  No</td>

        <td>: <input type="text" value="<?php echo $invoice ?>" name="invoice"  /></td>

      </tr>

      <tr>

        <td>PAN</td>

        <td>: <input type="text" value="<?php echo $pan ?>" name="pan"  /></td>

      </tr>

      <tr>

        <td>Expiry  Date </td>

        <td>: <input type="text" value="<?php echo $expire_date ?>" name="expire_date"  /></td>

      </tr>





    </table>

    <div>

    <input  type="submit" value="Proceed" class="submit"  />

    </div>

    </form>



<script type='text/javascript'>



jQuery(document).ready(function($) {

$("#autopost").submit();

});

</script>

</div><!--wrapper-->



</body>

</html>
