<?php
//調用sql鏈接
include 'conf/conn.php';
require "./paypal/PaypalIPN.php";

$ipn = new PaypalIPN();
$ipn->usePHPCerts();
if (in_sandbox()) {
	$ipn->useSandbox();
}

$verified = $ipn->verifyIPN();
if ($verified) {

	//調用sql鏈接
	include 'conf/conn.php';

	//封裝數據入變量
	$paypal_id = $_POST['custom'];
	$total = $_POST['mc_gross'];
	$time = date("Y-m-d H:i:s");
	//獲取可用的收據號
	$query = "select record from `receipt` order by `receipt`.`record` DESC limit 1";
	$result = mysqli_query($conn, $query);
	$row = mysqli_fetch_assoc($result);

	$receipt = "SRR" . date("y") . "-" . sprintf("%'05d", ++$row["record"]);
	//更新訂單信息
	$query = "UPDATE `orders` SET `amount` = '$total', `time` = '$time' , `status` = 'doing',`receipt_number` = '$receipt' WHERE `orders`.`paypal id` = '$paypal_id';";
	$result = mysqli_query($conn, $query);
	//調用訂單數據
	$query = "select * from orders where `paypal id`='$paypal_id' limit 1";
	$result = mysqli_query($conn, $query);
	$row = mysqli_fetch_assoc($result);
	$username = mysqli_real_escape_string($conn, $row["username"]);
	$customer_first_name = mysqli_real_escape_string($conn, $row["customer Fname"]);
	$customer_last_name = mysqli_real_escape_string($conn, $row["customer Lname"]);
	$customer_email = mysqli_real_escape_string($conn, $row["customer email"]);
	$customer_phone = mysqli_real_escape_string($conn, $row["customer phone"]);
	$customer_address = mysqli_real_escape_string($conn, $row["customer address"]);
	$company_name = mysqli_real_escape_string($conn, $row["company name"]);
	$recipient_first_name = mysqli_real_escape_string($conn, $row["recipient Fname"]);
	$recipient_last_name = mysqli_real_escape_string($conn, $row["recipient Lname"]);
	$recipient_email = mysqli_real_escape_string($conn, $row["email"]);
	$recipient_phone = mysqli_real_escape_string($conn, $row["phone"]);
	$recipient_address = mysqli_real_escape_string($conn, $row["address"]);
	$delivery_date = mysqli_real_escape_string($conn, $row["delivery date"]);
	$delivery_time = mysqli_real_escape_string($conn, $row["delivery time"]);
	$remarks = mysqli_real_escape_string($conn, $row["remarks"]);
	//調用coupon數據
	if ($row["coupon_code"] != "") {
		$coupon_code = $row["coupon_code"];

		$query = "select * from `coupon` where code='$coupon_code'";
		$result = mysqli_query($conn, $query);
		$row = mysqli_fetch_assoc($result);

		$coupon_name = mysqli_real_escape_string($conn, $row["coupon_name"]);
		$usage_count = $row["usage_count"];
		$discount_rate = $row["discount_rate"];
	} else {
		$discount_rate = 1;
	}
	//調用購物數據
	//電郵商品名處理函數
	function letter_change($data) {
		//Letter chocolate currently containing '<br><div>' tags,bypass if this is not a letter chocolate product
		if (stripos($data, "<br><div") === false) {
			return $data;
		}

		$data = htmlentities($data, ENT_QUOTES, "UTF-8");

		$data = str_replace('&hearts;', "♥", $data);
		$data = str_replace('&amp;', "&", $data);
		$data = str_replace('&lt;br&gt;', "<br>", $data);

		$pattern = '/^(.*?)(?:<br>)/';
		$replace = '<p>$1</p><table><tr>';
		$data = preg_replace($pattern, $replace, $data, 1);

		$data = str_replace('&lt;div class=&quot;Dark&quot;&gt;', "<td><img style='max-width:100%; max-height:100%;' src ='http://www.sayvour.com.hk/img/letter/d_", $data);
		$data = str_replace('&lt;div class=&quot;White&quot;&gt;', "<td><img style='max-width:100%; max-height:100%;' src ='http://www.sayvour.com.hk/img/letter/w_", $data);
		$data = str_replace('&lt;div class=&quot;Pink&quot;&gt;', "<td><img style='max-width:100%; max-height:100%;' src ='http://www.sayvour.com.hk/img/letter/p_", $data);
		$data = str_replace('&lt;div class=&quot;Brown&quot;&gt;', "<td><img style='max-width:100%; max-height:100%;' src ='http://www.sayvour.com.hk/img/letter/b_", $data);
		$data = str_replace('&lt;/div&gt;', ".png' align ='left'></td>", $data);
		$data = str_replace('_?.', "_s1.", $data);
		$data = str_replace('_/.', "_s2.", $data);
		$data = str_replace('_:.', "_s3.", $data);
		$data = str_replace("_&#10084;", '_❤️️', $data);
		$data = str_replace("_&#127801;", '_🌹', $data);
		$data = str_replace("&#9825;", "_♡", $data);
		$data = str_replace('><br><', "></tr><tr><", $data);

		$data .= "</tr></table>";

		return $data;
	}
	//歸0計數器
	$count_value = 0;

	$sub_total = 0;

	$query = "select * from `bought_item` where `paypal_id` = '$paypal_id'";
	$result = mysqli_query($conn, $query);

	while ($row = mysqli_fetch_assoc($result)) {
		//取貨方式判定
		if ($row["product_name"] == "Delivery Fee" or $row["product_name"] == ",運費") {
			$delivery_methods = "SF Express Refrigerated Delivery Service";
			$extra_check = $count_value;
			$extra_fee = $row["price"];
		} elseif ($row["product_name"] == "Plastic Bag Charge" or $row["product_name"] == ",膠袋費") {
			$delivery_methods = "Self pick up";
			$extra_check = $count_value;
			$extra_fee = $row["price"];
		} else {
			$sub_total += $row["price"] * $row["amount"];
		}

		$product_name[$count_value] = letter_change($row["product_name"]);
		$product_price[$count_value] = $row["price"];
		$product_amount[$count_value] = $row["amount"];
		$count_value++;
	}

	$order_amount = $count_value;
	$order_total = $sub_total + $extra_fee;
	//封裝數據入變量結束

	//新增coupon使用記錄
	if ($discount_rate < 1) {
		$usage_count--;
		$query = "UPDATE `coupon` SET `usage_count` = '$usage_count' WHERE `coupon`.`code` = '$coupon_code';";
		$result = mysqli_query($conn, $query);

		$query = "INSERT INTO `used_coupon` (`date`, `username`, `receipt_no`, `code`) VALUES ('$time', '$username', '$paypal_id', '$coupon_code')";
		$result = mysqli_query($conn, $query);
	}
	//新增coupon使用記錄結束

	//寄送訂單電郵
	$maillist = '';
	//歸0計數器
	$count_value = 0;
	//準備電郵訂單詳細
	while ($count_value < $order_amount) {
		//跳過附加費
		if ($product_name[$count_value] == ",運費" or $product_name[$count_value] == ",膠袋費" or $product_name[$count_value] == "Plastic Bag Charge" or $product_name[$count_value] == "Delivery Fee") {
			$count_value++;
			continue;
		}
		$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[$count_value] . "
<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$
" . $product_price[$count_value] . "
<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'>
" . $product_amount[$count_value] . "
<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$" . $product_price[$count_value] * $product_amount[$count_value] . "
</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>";
		$count_value++;
	}
	$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>
";
	if ($discount_rate < 1) {
		$maillist .= "
<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'>
The coupon code “$coupon_name” has been successfully applied
<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'>
</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'>
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'>
" . (100 - $discount_rate * 100) . "%
</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$" . $sub_total . "
</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 {
		$maillist .= "
<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$" . $sub_total .
			"</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>";
	}

	//hide extra check delivery charge at 2021-02-05
	$maillist .= "
	<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'>
	" . $product_name[$extra_check] . "
	<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$" . $product_price[$extra_check] * $product_amount[$extra_check] . "
	</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 .= "
	// <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'>
	// " . $product_name[$extra_check] . "
	// <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$" . " 待定 " . "
	// </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>";

	//發送電郵
	require_once 'phpmailer/class.phpmailer.php';
	$mail = new PHPMailer();
	//$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;

	// $mail->addReplyTo("info@amigospa.com.hk", "Amigo Spa & Beauty");
	$mail->addReplyTo("info@artech-system.com", "Tommy Care");
	$mail->AddAddress($customer_email);

	//Won't send email to follow person if this is a dev environment.
	if (in_retail()) {
		// $mail->AddBCC("daniel@at-creative.com");
		// // $mail->AddBCC("edmondcheng@at-creative.com");
		// $mail->AddBCC("info@sayvour.com.hk");
		// $mail->AddBCC("marketing@sayvour.com.hk");
		// $mail->AddBCC("catherine@sayvour.com.hk");
		// $mail->AddBCC("sayvourhongkong@gmail.com");
	}

	$mail->IsHTML(true);
	$mail->setFrom("info@artech-system.com", "Tommy Care");
	$mail->Subject = "Thank you for your order from Tommy Care";
	$body = "
<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>
</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 Tommy Care</h1>
</div>
<div style='Margin-left:20px;Margin-right:20px'>
<p style='Margin-top:0;Margin-bottom:0;text-transform: capitalize'>Dear&nbsp;" . $customer_first_name . "</p><p style='Margin-top:20px;Margin-bottom:20px'>Thank you for your recent order on " . $time . ". 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 : " . $paypal_id . "    Receipt no : " . $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></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$" . $order_total . "</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'>
<strong>Order Details:</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_first_name . " " . $customer_last_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'>
<div style='line-height:10px;font-size:1px'>&nbsp;</div>
</div>
<div style='Margin-left:20px;Margin-right:20px'>
<strong>Delivery Details:</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_first_name . " " . $recipient_last_name . "</td></tr>
<tr><td>Email:</td><td> " . $recipient_email . "</td></tr>
<tr><td>Phone:</td><td> " . $recipient_phone . "</td></tr>";

	if ($delivery_methods == "SF Express Refrigerated Delivery Service") {
		$body .= "
          <tr><td valign='top'>Delivery Methods:</td><td> " . $delivery_methods . "</td></tr>
          <tr><td valign='top'>Address:</td><td> " . $recipient_address . "</td></tr>
          <tr><td valign='top'>Delivery Time:</td><td> " . $delivery_date . " " . $delivery_time . "</td></tr>
        ";
	} else {
		$body .= "
          <tr><td valign='top'>Delivery Methods:</td><td> " . $delivery_methods . "</td></tr>
          <tr><td valign='top'>Pick Up Time:</td><td> " . $delivery_date . "</td></tr>
        ";
	}

	$body .= "
<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>";

	if ($delivery_methods == "SF Express Refrigerated Delivery Service") {
		$body .= "
Refrigerated Delivery: All our chocolates are made to order and will be delivered to your designated address according to your schedule.
		";
	}

	$body .= "
Should you have further enquiry, please feel free to contact us at +852 2951 0005.<br><br><br>
Thank you once again for supporting Tommy Care. </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'>

</div>
<div style='Margin-left:20px;Margin-right:20px;Margin-top:20px'>
<p style='Margin-top:0;Margin-bottom:20px'>Tommy Care</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'>



</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>
  ";
	$mail->Body = $body;
	$mail->Send();
	//寄送訂單郵件結束

	//刪除購物車記錄
	$query = "DELETE FROM `cart` WHERE `cart`.`username` = '$username'";
	$result = mysqli_query($conn, $query);

} else {
	$current_time = date("Y-m-d H:i:s");
	throw new Exception('Fail to verify paypal ipn message on' . current_time . ' : ' . implode(',', $_POST));
}

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

?>