<?php
/*// Send an empty HTTP 200 OK response to acknowledge receipt of the notification
header('HTTP/1.1 200 OK');

// Assign payment notification values to local variables. There are more variables, however, these are the most essential ones. If you passed more variables through your form see https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/ for a full list of variables
$payer_email = $_POST['payer_email'];
$address_country = $_POST['address_country'];
$address_city = $_POST['address_city'];
$address_name = $_POST['address_name'];
$address_state = $_POST['address_state'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payer_business_name = $_POST['payer_business_name'];
$memo = $_POST['memo'];
$item_name = $_POST['item_name1'];
$quantity = $_POST['quantity1'];
$payment_amount = $_POST['mc_gross_1'];
$name = $first_name ." ". $last_name ." ". $payer_business_name;
$address = $address_name ."<br>". $address_street ."<br>". $address_city ." ". $address_state ." ". $address_zip ."<br>". $address_country;

// Mail variables
$to = 'email@example.com';
// Need to set timezone for date() to work
date_default_timezone_set('America/New_York');
$subject = 'Order placed on '.date("F d, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<center><h1>An order has been placed.</h1></center>';
$message .= '<p>Customer email:<br>'.$payer_email.'</p>';
$message .= '<p>Customer name:<br>'.$name.'</p>';
$message .= '<p>Customer address:<br>'.$address.'</p>';
if (!empty($memo)) { $message .= '<p>Message from customer:<br>'.$memo.'</p>';}
$message .= '<p>Item placed:<br>'.$item_name.'</p>';
$message .= '<p>Item quantity:<br>'.$quantity.'</p>';
$message .= '<p>Payment amount:<br>$'.$payment_amount.'</p>';
$message .= '</body></html>';


// Build the required acknowledgement message out of the notification just received
$req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
  $value = urlencode(stripslashes($value));  // Encode these values
  $req  .= "&$key=$value";                   // Add the NV pairs to the acknowledgement
}


// Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

// Open a socket for the acknowledgement request
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// Send the HTTP POST request back to PayPal for validation
fputs($fp, $header . $req);

while (!feof($fp)) {                     // While not EOF
  $res = fgets($fp, 1024);               // Get the acknowledgement response
  if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification

    // IPN is verified
    // Code here
	

$username = htmlspecialchars('paypal');
$password = base64_encode('paypal');
$email = htmlspecialchars('paypal@paypal.com');
$created = date("Y-m-d H:i:s"); 
include('conn.php');
$query = "insert into clients(username,password,email,created,logintime) values ('$username','$password','$email','$created','$created')";$result=mysqli_query($conn,$query)
  }
  else if (strcmp ($res, "INVALID") == 0) { //Response contains INVALID - reject notification

    // Something went wrong -- begin error handling
    // Code here
    mail($to, "Invalid IPN", "Invalid IPN", $headers);

  }
}
fclose($fp);  // Close the file*/

















		//read the post from PayPal system and add 'cmd'
		$req = 'cmd=_notify-validate';

		foreach ($_POST as $key => $value) {
			$value = urlencode(stripslashes($value));
			$req .= "&$key=$value";
		}

		//post back to PayPal system to validate (replaces old headers)
		$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Host: www.paypal.com\r\n";
		$header .= "Connection: close\r\n";
		$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
		$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

		//error connecting to paypal
		if (!$fp) {
			//
		}
			
		//successful connection    
		if ($fp) {
			fputs ($fp, $header . $req);
			
			while (!feof($fp)) {
				$res = fgets ($fp, 1024);
				$res = trim($res); //NEW & IMPORTANT
						
				if (strcmp($res, "VERIFIED") == 0) {
					
					include('conf/conn.php');
					
					$recipient_name = $_POST['address_name'];
					$address_country = $_POST['address_country'];
					$address_city = $_POST['address_city'];
					$address_state = $_POST['address_state'];
					$address_street = $_POST['address_street'];
					$address = $address_street ."<br>". $address_city ." ". $address_state ." ". $address_zip ."<br>". $address_country;
					$phone = $_POST['contact_phone'];
					$paypal_id = $_POST['txn_id'];
					$Tpaypal_id = $_POST['custom'];
					$amount = $_POST['mc_gross'];
					$time = date("Y-m-d H:i:s"); 
					
					$check_query = "select * from orders where `paypal id`='".$Tpaypal_id."' limit 1" ;
					$check = mysqli_query($conn,$check_query);
					$row = mysqli_fetch_assoc($check);
					$record=$row['record'];
					$customer_name = $row['customer Fname']." ".$row['customer Lname'];
					$customer_Fname = $row['customer Fname'];
					$customer_email = $row['customer email'];
					$customer_phone = $row['customer phone'];
					$company_name = $row['company name'];
					$recipient_name = $row['recipient Fname']." ".$row['recipient Lname'];
					$email = $row['email'];
					$username = $row['username'];
					$address = $row['address'];
					$phone = $row['phone'];
					$delivery_time = $row['delivery date'].' '.$row['delivery time'];
					$remarks = $row['remarks'];
					$paypal_id = $row['paypal id'];
					
					if($row["coupon_code"]!=""){
						$query="INSERT INTO `used_coupon` (`record`, `date`, `username`, `receipt_no`, `code`) VALUES (NULL, '".date("Y-m-d H:i:s")."', '".$username."', '".$Tpaypal_id."', '".$row["coupon_code"]."')";
						$result=mysqli_query($conn,$query);
						$query="select * from coupon where code ='".$row["coupon_code"]."'";
						$result=mysqli_query($conn,$query);
						$row = mysqli_fetch_assoc($result);
						$discount_rate=$row["usage_count"];
						$usage_count=$row["usage_count"];
						$usage_count--;
	  					$query="UPDATE `coupon` SET `usage_count` = '".$usage_count."' WHERE `coupon`.`code` = '".$row["code"]."';";
						$result=mysqli_query($conn,$query);
					}else{
						$discount_rate=1;
					}
							
					$query = "INSERT INTO `receipt` (`record`, `paypal_id`) VALUES (NULL, '$record')";
					$result = mysqli_query($conn,$query);
							
					$query = "SELECT * FROM `receipt` WHERE paypal_id = '$record'";
					$result = mysqli_query($conn,$query);
					$row = mysqli_fetch_assoc($result);
					$receipt = sprintf("%'05d", $row["record"]);		
							
					$maillist = '';
					$amount1=0;
					$amount3=0;
					$query = "select * from cart where username='$username'";
						$result = mysqli_query($conn,$query);
						if (mysqli_num_rows($result) > 0) { 
						while($row = mysqli_fetch_assoc($result)) {
					$product_name = str_replace('<div class="Dark">',"<img src ='www.sayvour.com.hk/img/letter/d_",$row["product_name"]);
					$product_name = str_replace('<div class="White">',"<img src ='www.sayvour.com.hk/img/letter/w_",$product_name);
					$product_name = str_replace('<div class="Pink">',"<img src ='www.sayvour.com.hk/img/letter/p_",$product_name);
					$product_name = str_replace('</div>',".png' align ='left'>",$product_name);
					$product_name = str_replace('_?.',"_s1.",$product_name);
					$product_name = str_replace('_/.',"_s2.",$product_name);
					$product_name = str_replace('_:.',"_s3.",$product_name);
					$product_name = str_replace('><br><',"><br><br><br><",$product_name);

						
										$maillist = $maillist."<div style='Margin-left:20px;Margin-right:20px; width:100%;'>
														
											<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
											<tbody>
											<tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='50%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>".$product_name."<u></u><u></u></span></p></td>
											  <td style='WIDTH:15%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
												HK$".round($row["price"]*$discount_rate,1)
												."<u></u><u></u></span></p></td> 
												
												<td style='WIDTH:10%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
												".$row["amount"]
												."<u></u><u></u></span></p></td>
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$";
												
												$price = round($row["price"]*$discount_rate,1)*$row["amount"];
												
												$maillist = $maillist.$price."</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td></tr>
											  
											  </tbody>
											  </table>                                
														</div>				
														
										<div style='Margin-left:20px;Margin-right:20px'>
															<div style='display:block;font-size:2px;line-height:2px;Margin-left:auto;Margin-right:auto;width:100%;background-color:#7cc3c3'>&nbsp;</div>
														</div>";
												$amount3=$amount3+($row["price"]*$row["amount"]);
												$amount1=$amount1+round($row["price"]*$discount_rate,1)*$row["amount"];
														}
												$amount2=round($amount1,1)*1;

											
														
														if($amount/$discount_rate < 500 ){$deliveryfee = $maillist."<div style='Margin-left:20px;Margin-right:20px; width:100%;'>
														
											<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
											<tbody>
											<tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='50%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Delivery Fee<u></u><u></u></span></p></td>
											 <td style='WIDTH:15%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
												HK$38<u></u><u></u></span></p></td>
											  <td style='WIDTH:10%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
												1<u></u><u></u></span></p></td>
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$38</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td></tr>
											  
											  </tbody>
											  </table>                                
														</div>			
														
										<div style='Margin-left:20px;Margin-right:20px'>
															<div style='display:block;font-size:2px;line-height:2px;Margin-left:auto;Margin-right:auto;width:100%;background-color:#7cc3c3'>&nbsp;</div>
														</div>";
										}else{
										$deliveryfee = "											  <tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
												<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Delivery Fee<u></u><u></u></span></p></strong></td>
												
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$0</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
											  
											  </tr>
";
										}
									if($discount_rate!=1){
												
												$discount_rate2=(1-$discount_rate)*100; 
												$discount_rate1="<tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
												<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Discount(%)<u></u><u></u></span></p></strong></td>
												
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>".$discount_rate2."%</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
											  
											  </tr>
											  <tr>
<td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Sub-Total After Discount<u></u><u></u></span></p></strong></td>
<td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
<td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$".$amount2."</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
<td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
</tr>
";}else{$discount_rate1="";}					
											
														
														if($amount/$discount_rate < 500 ){
										$deliveryfee = "											  <tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
												<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Delivery Fee<u></u><u></u></span></p></strong></td>
												
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$38</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
											  
											  </tr>
";
				}else{
	$deliveryfee = "											  <tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
												<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Delivery Fee<u></u><u></u></span></p></strong></td>
												
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$0</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
												
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
											  
											  </tr>
";}
														
	$maillist=$maillist."
<div style='Margin-left:20px;Margin-right:20px; width:100%;'>
<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
<tbody>
<tr>
<td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Sub-Total<u></u><u></u></span></p></strong></td>
<td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
<td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>HK$".$amount3."</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
<td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td>
</tr>
".$discount_rate1.$deliveryfee."</tbody>
</table>
</div>
<div style='Margin-left:20px;Margin-right:20px'>
<div style='display:block;font-size:2px;line-height:2px;Margin-left:auto;Margin-right:auto;width:100%;background-color:#7cc3c3'>&nbsp;</div></div>	        
";													
														}
					
					
					
		require_once('phpmailer/class.phpmailer.php');
		$mail = new PHPMailer(); // create a new object
		//$mail->IsSMTP(); // enable SMTP
		//$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
		//$mail->SMTPAuth = true; // authentication enabled
		//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
		//$mail->Host = "localhost";
		//$mail->Host = "mail.station191.com";
		//$mail->Port = 587; // or 587		
		
		$mail->addReplyTo("info@sayvour.com.hk", "Sayvour");
		$mail->AddBCC("daniel@at-creative.com");
		$mail->AddBCC("info@sayvour.com.hk");
		$mail->AddBCC("catherine@sayvour.com.hk");
		$mail->AddBCC("sayvourhongkong@gmail.com");
		$mail->IsHTML(true);
		//$mail->Username = "kk@at-creative.com";
		//$mail->Password = "artech0088";
		$mail->setFrom("info@sayvour.com.hk","Sayvour");
		$mail->Subject = "Thank you for your order from Sayvour";
		$mail->Body = "                                <!-- top bar -->

										<div id=':lt' class='ii gt adP adO'>
											<div id=':ls' class='a3s aXjCH m156312919a8bb8e9'><u></u>
												<div style='margin:0;padding:0'>
													<div style='min-width:320px;background-color:#c2e3e3' lang='x-wrapper'>
														<div style='Margin:0 auto;max-width:560px;min-width:280px;'>
															<div style='border-collapse:collapse;display:table;width:100%'>
																<div style='Float:left;font-size:12px;line-height:19px;max-width:280px;min-width:140px;padding:10px 0 5px 0;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif'></div>
																<div style='Float:left;font-size:12px;line-height:19px;max-width:280px;min-width:139px;padding:10px 0 5px 0;text-align:right;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif'></div>
															</div>
														</div>
														<div style='Margin:0 auto;max-width:600px;min-width:320px;'>
															<div style='font-size:26px;line-height:32px;Margin-top:6px;Margin-bottom:20px;color:#ffffff;font-family:&quot;PT Serif&quot;,Georgia,serif;Margin-left:20px;Margin-right:20px' align='center'>
																<div style='font-size:0px!important;line-height:0!important' align='center'><img style='min-height:auto;width:100%;border:0;max-width:217px' src='https://ci4.googleusercontent.com/proxy/vUMxJkR_Kw9qDMrAUmEEtS4_hy8v4TztsG8zNDbLJxG0zizdPUmHdqEAaVAuRWqrT0fA9zde7TizwG9QGRXUNgPmnlhNJWJ_gzMCbqpsW3bSMaNbueUnoTh1=s0-d-e1-ft#http://i1.cmail2.com/ei/d/F9/F24/F62/203623/csfinal/sayvour-logo.png' alt='' width='217' class='CToWUd'></div>
															</div>
														</div>
										
										<!-- end top bar -->
										
										<!-- Dear -->
										<div style='background-color:#FFFFFF'>
											<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
												<div style='border-collapse:collapse;display:table;width:100%'>
													<div style='max-width:600px;min-width:320px;text-align:left;FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>
														<div style='Margin-left:20px;Margin-right:20px'>
															<div style='line-height:40px;font-size:1px'>&nbsp;</div>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<h1 style='Margin-top:0;Margin-bottom:20px;font-style:normal;font-weight:normal;color:#499f9e;font-size:24px;line-height:32px;font-family:&quot;PT Serif&quot;,Georgia,serif;text-align:center' lang='x-size-28'>Thank you for your order from Sayvour</h1>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<p style='Margin-top:0;Margin-bottom:0;text-transform: capitalize'>Dear&nbsp;".$customer_Fname."</p><p style='Margin-top:20px;Margin-bottom:20px'>Thank you for your recent order on ". date('Y/m/d').". The payment was well received by PayPal or Credit Card.
															<br>
															
Please find the receipt and delivery details as follows:</p>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<div style='line-height:20px;font-size:1px'>&nbsp;</div>
										</div>
										
										<!-- End Dear -->


										<!-- Order Summary -->
														
										<div style='Margin-left:20px;Margin-right:20px'>
									<p style='Margin-top:0;Margin-bottom:20px;font-size:16px;line-height:24px' lang='x-size-16'><span style='color:#575757'><strong>Order Summary and Receipt </strong></span></p>
								</div>		
								
                <div style='Margin-left:20px;Margin-right:20px; width:100%;'>
                                
					<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
                    <tbody>
					
					<tr>
					<td colspan='3' style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0;margin:10px 0px' valign='top' width='50%'>
                        <p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Order no : ".$Tpaypal_id."    Receipt no : "."SRR".date('y')."-".$receipt."<u></u><u></u></span></p>
						
						</td>
					</tr>
					</tbody></table></div>
					
					
										<div style='Margin-left:20px;Margin-right:20px'>
															<div style='display:block;font-size:2px;line-height:2px;Margin-left:auto;Margin-right:auto;width:100%;background-color:#7cc3c3'>&nbsp;</div>
														</div>
														
														<div style='Margin-left:20px;Margin-right:20px; width:100%;'>
					<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
                    <tbody>
                    <tr>
						
                      <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='50%'>
                        <strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Product Name<u></u><u></u></span></p></strong></td>
                      <td style='WIDTH:15%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
                        <strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Unit Price<u></u><u></u></span></p></strong></td>
					  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
                        <strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Quantity<u></u><u></u></span></p></strong></td>
                      <td style='WIDTH:10%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='25%'>
                        <p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Amount</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
                      <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td></tr>
                      
                      </tbody>
                      </table>                                
								</div>	
														
										<div style='Margin-left:20px;Margin-right:20px'>
															<div style='display:block;font-size:2px;line-height:2px;Margin-left:auto;Margin-right:auto;width:100%;background-color:#7cc3c3'>&nbsp;</div>
														</div>"
														.$maillist.
											"<div style='Margin-left:20px;Margin-right:20px; width:100%;'>
														
											<table style='WIDTH:100%;BORDER-COLLAPSE:collapse;BORDER-SPACING:0; margin:10px 0px' cellspacing='0' cellpadding='0' width='100%' border='0'>
											<tbody>
											<tr>
												
											  <td style='WIDTH:45%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='45%'>
												<strong><p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>Total<u></u><u></u></span></strong></p></td>
											  <td style='WIDTH:20%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='20%'>
												<p class='MsoNormal' style='LINE-HEIGHT:12.75pt'><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
											  <td style='WIDTH:25%;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='22%'>
												<p class='MsoNormal' style='TEXT-ALIGN:right;LINE-HEIGHT:12.75pt' align='right'><strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'>".$amount."</span></strong><span style='FONT-SIZE:9pt;FONT-FAMILY:&quot;Helvetica&quot;,sans-serif;COLOR:#333333'><u></u><u></u></span></p></td>
											  <td style='WIDTH:0.3pt;PADDING-BOTTOM:0in;PADDING-TOP:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in' valign='top' width='1'></td></tr>
											  
											  </tbody>
											  </table>                                
														</div>                								

										<!-- End Order Summary -->                

										<div style='Margin-left:20px;Margin-right:20px'>
											<div style='line-height:20px;font-size:1px'>&nbsp;</div>
										</div>
										<div style='Margin-left:20px;Margin-right:20px'>
											<div style='line-height:5px;font-size:1px'>&nbsp;</div>
										</div>
														
										<!-- Best regards -->  
										  
										
										<div style='Margin-left:20px;Margin-right:20px'>
											<div style='line-height:10px;font-size:1px'>&nbsp;</div>
										</div>
										
										<div style='Margin-left:20px;Margin-right:20px'>
										<strong>Billing Address</strong>
										<table style='Margin-top:0;Margin-bottom:20px;font-size:14px;line-height:21px;text-align:left' lang='x-size-14'>
										<tr><td>Name:</td><td> ".$customer_name."</td></tr>
										<tr><td>Email:</td><td> ".$customer_email."</td></tr>
										<tr><td>Phone:</td><td> ".$customer_phone."</td></tr>
										<tr><td valign='top'>Company Name:</td><td> ".$company_name."</td></tr>
										</table>
										

									   </div>
										<div style='Margin-left:20px;Margin-right:20px'>
										<strong>Delivery Address</strong>
										<table style='Margin-top:0;Margin-bottom:20px;font-size:14px;line-height:21px;text-align:left' lang='x-size-14'>
										<tr><td>Name:</td><td> ".$recipient_name."</td></tr>
										<tr><td>Email:</td><td> ".$email."</td></tr>
										<tr><td>Phone:</td><td> ".$phone."</td></tr>
										<tr><td valign='top'>Address:</td><td> ".$address."</td></tr>
										<tr><td valign='top'>Delivery Time:</td><td> ".$delivery_time."</td></tr>
										<tr><td valign='top'>Remarks:</td><td> ".$remarks."</td></tr>
										</table>
										
										
										<div>
											<p style='Margin-top:0;Margin-bottom:20px;font-size:14px;line-height:21px;text-align:left' lang='x-size-14'>
											Payment Method: PayPal / Credit Card<br>

Refrigerated Delivery: All our chocolates are made to order and will be delivered to your designated address according to your schedule.<br><br>
Should you have further enquiry, please feel free to contact us at +852 2951 0005.<br><br><br>
Thank you once again for supporting Sayvour. We hope that you and your loved ones will savour every mouthful of our delicacies and every moment of receiving the personalised gifts made by us.</p>
										</div>
										

									   </div>                 
										<div style='Margin-left:20px;Margin-right:20px'>
											<div style='line-height:20px;font-size:1px'>&nbsp;</div>
										</div>
											  
										<div style='Margin-left:20px;Margin-right:25px'>
															<p style='Margin-top:0;Margin-bottom:20px;font-size:14px;line-height:21px;text-align:left' lang='x-size-14'>
															Best regards,</p>
														</div>
														<div style='font-size:12px;font-style:normal;font-weight:normal' align='left'>
															<a href='http://www.sayvour.com.hk'><img style='border:0;display:block;min-height:auto;width:100%;max-width:150px; padding-left:20px' alt='' width='249' src='https://ci4.googleusercontent.com/proxy/WhjL_vvSpLarh-zQTnIIub3yktIgmMYyzgWhl_XTAMbJQ9LIpv2A32hCNW_U3MQg3wHDHewv_AnV0ysjc7_dtTBbxdltT6OvngagFLBQ44sF8Vmw8QWrq9G8jgY=s0-d-e1-ft#http://i1.cmail2.com/ei/d/F9/F24/F62/203624/csfinal/Sayvour-logo-b.png' class='CToWUd'></a>
														</div>
														<div style='Margin-left:20px;Margin-right:20px;Margin-top:20px'>
															<p style='Margin-top:0;Margin-bottom:20px'>Sayvour | Say Your Flavour</p>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<div style='line-height:5px;font-size:1px'>&nbsp;</div>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<div style='line-height:5px;font-size:1px'>&nbsp;</div>
														</div>
														<div style='Margin-left:20px;Margin-right:20px'>
															<div style='line-height:5px;font-size:1px'>&nbsp;</div>
														</div>
													</div>
												</div>
											</div>
										</div>				
										
										<!-- End Best regards -->                

										
										<!-- footer  -->
										
										<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
											<div style='border-collapse:collapse;display:table;width:100%'>
												<div style='text-align:left;font-size:12px;line-height:19px;color:#7A7A7A;font-family:Cabin,Avenir,sans-serif;Float:left;max-width:400px;min-width:320px;'>
													<div style='Margin-left:20px;Margin-right:20px;Margin-top:10px;Margin-bottom:10px'>
														<table style='border-collapse:collapse;table-layout:fixed'>
															<tbody>
																<tr>
																	<td style='padding:0;width:26px'><a style='text-decoration:underline;color:#9dc4c4' href='http://kk.cmail2.com/t/d-i-ijlhttd-l-r/' target='_blank'><img style='border:0' src='https://ci4.googleusercontent.com/proxy/cdaOVwx01pydUCeWanZ5I0WsxDjvph7JzEiKY3zimkrOp-LjXcGnE1GadRLWYfIKzlmiYSOtzr-WjjHJdYebPiyRLHmWcDY2Zz_HAol546DebSYvB0BcePndD26qkP8w-AE=s0-d-e1-ft#http://i8.cmail2.com/static/eb/master/13-the-blueprint-3/images/facebook.png' width='26' height='26' class='CToWUd'></a></td>
																	<td style='padding:0 0 0 3px;width:26px'><a style='text-decoration:underline;color:#9dc4c4' href='http://kk.cmail2.com/t/d-i-ijlhttd-l-y/' target='_blank'><img style='border:0' src='https://ci5.googleusercontent.com/proxy/H7Yt85uks7gPb4DmaC8HgjAqYMQhhSna9kYBtoUZr5D3lFKE-IJtZdN1MqyIopBh8YJYDX-igm7ScYklACfkd0Y1wvJ0BbBd3m1bcejcougYGa1dWg76ozQ-vG28HEqFDCbI=s0-d-e1-ft#http://i1.cmail2.com/static/eb/master/13-the-blueprint-3/images/instagram.png' width='26' height='26' class='CToWUd'></a></td>
																</tr>
															</tbody>
														</table>
														<div style='Margin-top:20px'>
															<div>Patisserie Sayvour Limited</div>
														</div>
														<div style='Margin-top:18px'>
															<div>Sayvour is an artisan confectionery specialising in handmade chocolates and pastries by using high quality ingredients from around the world.</div>
														</div>
													</div>
												</div>
												<div style='text-align:left;font-size:12px;line-height:19px;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif;Float:left;max-width:320px;min-width:200px;'>
													<div style='Margin-left:20px;Margin-right:20px;Margin-top:10px;Margin-bottom:10px'></div>
												</div>
											</div>
										</div>
										<div style='Margin:0 auto;max-width:600px;min-width:320px;word-wrap:break-word;word-break:break-word'>
											<div style='border-collapse:collapse;display:table;width:100%'>
												<div style='text-align:left;font-size:12px;line-height:19px;color:#9dc4c4;font-family:Cabin,Avenir,sans-serif;max-width:600px;min-width:320px;'>
												</div>
											</div>
										</div>
										<div style='line-height:40px;font-size:40px'>&nbsp;</div>
									</div>
									<img style='overflow:hidden;display:block!important;min-height:1px!important;width:1px!important;border:0!important;margin:0!important;padding:0!important' src='https://ci4.googleusercontent.com/proxy/-HQ08vlGM2ltqJyXUtWXZlorkMV10CQ0pH8IJ2TQo0JNoLOLepN2GqRqjNvhCsxelytv0dohcw2kweEKYfPeyqw=s0-d-e1-ft#https://kk.cmail2.com/t/d-o-ijlhttd-l/o.gif' width='1' height='1' border='0' alt='' class='CToWUd'>
									<div class='yj6qo'></div>
									<div class='adL'></div>
								</div>
								<div class='adL'></div>
							</div>
						</div>

										<!--End  footer  -->


								";
		$mail->AddAddress($customer_email);
					if(!$mail->Send()){
						error_log("n");
					}else{
						error_log("OK");
					}
					
					$x = '2';
					$y = $_POST['num_cart_items'];
					
					$product_name = str_replace('D.','<div class="Dark">',$_POST['item_name1']);
					$product_name = str_replace('W.','<div class="White">',$product_name);
					$product_name = str_replace('P.','<div class="Pink">',$product_name);
					$product_name = str_replace('_','</div>',$product_name);
					
					$bought_items = $product_name;
					while($x<=$y) {
						$product_name = str_replace('D.','<div class="Dark">',$_POST['item_name'.$x]);
						$product_name = str_replace('W.','<div class="White">',$product_name);
						$product_name = str_replace('P.','<div class="Pink">',$product_name);
						$product_name = str_replace('_','</div>',$product_name);
						
						$b=$product_name;
						global $bought_items;
						$bought_items = $bought_items.'<br>'.$b;
						$x++;
					}  
					
					$paypal_id = $_POST['txn_id'];
					$time = date("Y-m-d H:i:s"); 
					$query = "UPDATE `orders` SET `bought_items` = '$bought_items',`amount` = '$amount', `time` = '$time' , `status` = 'doing',`receipt_number` = '"."SRR".date('y')."-".$receipt."' WHERE `orders`.`paypal id` = '$Tpaypal_id';";
					$result=mysqli_query($conn,$query);					
					
					$query = "DELETE FROM `cart` WHERE `cart`.`username` = '$username'";
					$result=mysqli_query($conn,$query);	
					
				}
			
				
				if (strcmp ($res, "INVALID") == 0) {
					//insert into DB in a table for bad payments for you to process later
					error_log("INVALID");
				}
			}

			fclose($fp);
		}




?>
