<?php
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
include "includes/operations.php";
include "stripe/init.php";

	//print_r($_POST); exit;
	$customer_name = $_POST['customer_name'];
	$customer_email = $_POST['customer_email'];
	$customer_phone = $_POST['customer_phone'];
	$order_price = $_POST['total'];
	$ip_address = $_POST['ip_address'];
	$shipping_cost = $_POST['shipping_cost'];
	$user_state = $_POST['user_state'];
	$user_city = $_POST['user_city'];
	$user_postalcode = $_POST['user_postalcode'];
	$order_address = $_POST['order_address'];
	$order_date = date('Y-m-d');
	$order_id = rand(130,1309800);
	$timestamp = time();
	
	$gen_code = $customer_email.'_'.$order_id;
	$download_passcode = base64_encode($gen_code);
	
	$final_code = preg_replace('/\s+/', '', $download_passcode);
	
	$order_status = 'https://candycleansechallenge.com/tracking-order.php?id='.$order_id;
	
	
	$res = mysqli_query($con, "SELECT secret_key FROM system_configuration");
	$lang = mysqli_fetch_array($res);
	$sk_key = $lang['secret_key'];
	
	
	\Stripe\Stripe::setApiKey($sk_key);
	$token = $_POST['stripeToken'];
	
	$charge = \Stripe\Charge::create([
		'amount' => $order_price*100,
		'currency' => 'usd',
		'description' => 'Candy Cleanse',
		'source' => $token,
	]);
	
	if ($charge) {
	
		$query = "
			INSERT into orders SET
				order_id = $order_id,
				customer_name = ?,
				customer_email = ?,
				customer_phone = ?,
				user_state = ?,
				user_city = ?,
				user_postalcode = ?,
				order_address = ?,
				shipping_cost = '$shipping_cost',
				total_price = '$order_price',
				order_date = '$order_date',
				download_passcode = '$final_code'
		";
		$stmt = mysqli_prepare($con, $query);
		mysqli_stmt_bind_param($stmt, 'sssssss',$customer_name,$customer_email,$customer_phone,$user_state,$user_city,$user_postalcode,$order_address);
		mysqli_stmt_execute($stmt);
		$item_id=mysqli_insert_id($con);
		
		for ($i=0;$i<sizeof($_POST['prod_id']);$i++) {
			$prod_id = $_POST['prod_id'][$i];
			$prod_quantity = $_POST['prod_quantity'][$i];
			mysqli_query($con, "INSERT INTO order_details SET order_id='$item_id', product_id='$prod_id', product_quantity='$prod_quantity'");
		}
		
		mysqli_query($con, "DELETE FROM cart WHERE cust_ip='$ip_address' AND date_added='$order_date'");
		
		$to = $customer_email;
		$from = 'info@candycleansechallenge.com';
		$subject = 'Thanks for purchasing';
	
		$message = '
			<html>
				<body class="em_body" style="margin: 0px auto; padding: 0px;" bgcolor="#efefef">
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="em_full_wrap" bgcolor="#efefef">
					<tbody>
						<tr>
							<td align="center" valign="top">
								<table align="center" border="0" cellpadding="0" cellspacing="0" class="em_main_table" style="width: 650px; table-layout: fixed;" width="650" bgcolor="#ffffff">
									<!-- Start Pre header section -->
									<tbody style="background: url(images/bg.PNG) no-repeat;width:100px;background-position: center center;background-size: 600px;">
										<tr>
											<td align="center" style="background-color: #efefef; border-bottom: 3px solid white;">
												<table bgcolor="#efefef" border="0" cellpadding="0" cellspacing="0" class="em_wrapper" width="100%">
													<tbody>
														<tr>
															<td align="center" style="font-family: Helvetica, Arial, sans-serif; font-size: 10px; color: #8e8e8e; line-height: 20px; padding: 5px 0;">
																&nbsp;
															</td>
														</tr>
													</tbody>
												</table>
											</td>
										</tr>
										<!-- End Pre header section -->
										<tr>
											<td align="center" valign="top" style="padding: 20px 20px;">
												<a data-link-name="Candy Cleanse" href="https://webriders.co/demos/candy-weightloss" target="_blank" title="Candy Cleanse">
													<img src="https://webriders.co/demos/candy-weightloss/images/logo.PNG" width="211" alt="Candy Cleanse" border="0" style="border-radius: 15px !important;float:center;max-width: 180px;height: auto; background: #ffffff; display: block; border: 0px;" />
												</a>
											</td>
										</tr>
										<tr>
											<td align="left" valign="top" style="font-family: Helvetica, Arial, sans-serif; font-size: 16px; line-height: 26px; color: #1c1c1c;">
												<!-- Start EMail Body -->
												<table>
													<tbody>
														<tr>
															<td align="left" valign="top" style="font-family: Helvetica, Arial, sans-serif; font-size: 15px; line-height: 27px; color: #1c1c1c; padding: 20px 30px 8px 30px; font-weight: bold;">
																Dear '.$customer_name.',
															</td>
														</tr>
														<tr>
															<td align="left" class="Pad_LR20" valign="top" style="font-family: Helvetica, Arial, sans-serif; font-size: 15px; line-height: 26px; color: #1c1c1c; padding: 8px 40px 15px 30px;">
																<p>
																	Thank you for your Candy Cleanse purchase. We know you’re going to love the effects on your body!
																</p>
																<p>
																	Track Order here: '.$order_status.'
																</p>
																<p>
																	Your shipment will go out within 2 business days. You’ll receive an email confirmation once shipped.
																</p>
																
																<p>
																	Thank your for your purchase and investment in yourself! We look forward to your transformation pictures.
																</p>
																<p>
																	If you’re posting on social be sure to use <b>#CandyCleanse</b> and tag <b>@CoachStormy</b> on Instagram for a possible feature!
																</p>
																<p>
																	<b>Here’s to your health!</b>
																</p>
																<p>
																	<b>The Candy Cleanse Team</b>
																</p>
															</td>
														</tr>
														</tr>
													</tbody>
												</table>
											</td>
										</tr>
										<tr>
											<td align="center" style="background-color: #f7f7f7;">
												<table bgcolor="#f7f7f7" border="0" cellpadding="0" cellspacing="0" class="em_wrapper" width="100%">
													<tbody>
														<tr>
															<td align="center" style="padding: 10px;" valign="top" class="Pad_LR22">
																<table align="center" border="0" cellpadding="0" width="600" cellspacing="0" style="width: 600px;" class="em_wrapper">
																	<tbody>
																		<tr>
																			<td align="center">
																				<table width="100%" border="0" cellspacing="0" cellpadding="0">
																					<tbody>
																						<tr>
																							<td
																								align="center"
																								valign="top"
																								style="font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 16px; color: black; padding: 8px 0px 0px 0px; text-align: left;"
																							>
																								<center><p>Copyright © - 2024 Stormy Wellington, Inc. All Rights Reserved.</p></center>
																								
																							</td>
																						</tr>
																					</tbody>
																				</table>
																			</td>
																		</tr>
																	</tbody>
																</table>
															</td>
														</tr>
													</tbody>
												</table>
											</td>
										</tr>
									</tbody>
								</table>
							</td>
						</tr>
					</tbody>
				</table>
			</body>
		</html>';
		
		$headers .= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
		$headers .= 'From: ' . $from . "\r\n";
		mail($to,$subject,$message,$headers);
		
		$to_1 = 'info@candycleansechallenge.com';
		$from = $customer_email;
		$subject_1 = 'Order Alert! You have a new Candy Cleanse order';   
        $message_1 = '
        <html>
			<body>
				<table cellspacing="0" style="padding:15px;border: 2px dashed #edb509; width: 100%;border-collapse: collapse;"> 
					<tr> 
						<th style="padding:20px; !important;">Order Number:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$order_id.'</td> 
					</tr>
					<tr> 
						<th style="padding:20px; !important;">Order Price :</th><td style="padding: 8px;border: 1px solid #ddd;">$'.$order_price.'</td> 
					</tr>
					<tr> 
						<th style="padding:20px; !important;">Name:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$customer_name.'</td> 
					</tr>
					<tr style="background-color: #e0e0e0;"> 
						<th style="padding:20px; !important;">Email:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$customer_email.'</td> 
					</tr> 
					<tr> 
						<th style="padding:20px; !important;">Phone:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$customer_phone.'</td> 
					</tr> 
					<tr> 
						<th style="padding:20px; !important;">State:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$user_state.'</td> 
					</tr> 
					<tr> 
						<th style="padding:20px; !important;">Town/City:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$user_city.'</td> 
					</tr> 
					<tr> 
						<th style="padding:20px; !important;">Postal Code:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$user_postalcode.'</td> 
					</tr> 
					<tr> 
						<th style="padding:20px; !important;">Shipping Address:</th><td style="padding: 8px;border: 1px solid #ddd;">'.$order_address.'</td> 
					</tr> 
					
				</table> 
			</body>
		</html>';
		
		$headers_1 .= "MIME-Version: 1.0\r\n";
		$headers_1 .= "Content-Type: text/html; charset=UTF-8\r\n";
		$headers_1 .= 'From: ' . $from . "\r\n";
		$headers_1 .= 'Cc: ab.bakr97@gmail.com' . "\r\n";
		mail($to_1,$subject_1,$message_1,$headers_1);
		
		header("location:success.php?token=$final_code");
		exit();
	}
	else {
		header("location:failed.php");
		exit();
	}


?>