· 8 years ago · Jul 05, 2018, 08:28 AM
1<?php
2require_once 'core/init.php';
3
4use PHPMailer\PHPMailer\PHPMailer;
5use PHPMailer\PHPMailer\Exception;
6require 'vendor/autoload.php';
7\Stripe\Stripe::setApiKey(STRIPE_PRIVATE);
8
9
10
11$token = $_POST['stripeToken'];
12$email = $_POST['stripeEmail'];
13$user_id = $_POST['userid'];
14$phone = sanitize($_POST['phone']);
15$final_price = sanitize($_POST['final_price']);
16$final_price = round($final_price,2) *100;
17// $final_price = number_format($final_price,2) / 100;
18$tax= sanitize($_POST['tax']);
19$sub_total = sanitize($_POST['sub_total']);
20$description=sanitize($_POST['description']);
21$cart_id = sanitize($_POST['cart_id']);
22$portes = sanitize($_POST['portes']);
23
24$stripeBillingName = sanitize($_POST['stripeBillingName']);
25$stripeBillingAddressCountry = sanitize($_POST['stripeBillingAddressCountry']);
26// $stripeBillingAddressCountryCode = $_POST['stripeBillingAddressCountryCode'];
27$stripeBillingAddressZip = sanitize($_POST['stripeBillingAddressZip']);
28$stripeBillingAddressLine1 = sanitize($_POST['stripeBillingAddressLine1']);
29$stripeBillingAddressCity = sanitize($_POST['stripeBillingAddressCity']);
30// $stripeBillingAddressState = $_POST['stripeBillingAddressState'];
31
32$stripeShippingName = sanitize($_POST['stripeShippingName']);
33$stripeShippingAddressCountry = sanitize($_POST['stripeShippingAddressCountry']);
34// $stripeShippingAddressCountryCode = $_POST['stripeShippingAddressCountryCode'];
35$stripeShippingAddressZip = sanitize($_POST['stripeShippingAddressZip']);
36$stripeShippingAddressLine1 = sanitize($_POST['stripeShippingAddressLine1']);
37$stripeShippingAddressCity = sanitize($_POST['stripeShippingAddressCity']);
38// $stripeShippingAddressState = $_POST['stripeShippingAddressState'];
39
40$metadata = array(
41 "cart_id" => $cart_id,
42 "tax" => $tax,
43 "sub_total"=>$sub_total,
44);
45
46
47$customer = \Stripe\Customer::create(array(
48 'email' => $email,
49 'source' => $token
50));
51
52$charge = \Stripe\Charge::create(array(
53 'customer' => $customer->id,
54 'amount' => $final_price,
55 'currency' => CURRENCY,
56 'receipt_email' => $email,
57 'description' => $description,
58 'metadata' => $metadata
59));
60//adjust the inventory qnt
61$itemQ =$db->query("SELECT * FROM cart where id = '$cart_id'");
62$itemR = mysqli_fetch_assoc($itemQ);
63$items= json_decode($itemR['items'], true);
64foreach ($items as $item) {
65 $item_id = $item['id'];
66 $productQ = $db->query("SELECT quantity from products where id ='{$item_id}'");
67 $product = mysqli_fetch_assoc($productQ);
68 $newQnt = $product['quantity'] - $item['quantity'];
69 $db->query("UPDATE products set quantity ='{$newQnt}' where id = '{$item_id}'");
70}
71
72// this updates the cart
73$final_price = round($final_price,2) /100;
74$db->query("UPDATE cart SET paid = 1 where id = '{$cart_id}'");
75$db->query("INSERT into transactions (charge_id, cart_id, user_id, billing_name, billing_country, billing_zip, billing_address, billing_city, shipping_name, shipping_country, shipping_zip, shipping_address, shipping_city, sub_total, tax, grand_total, description, txn_type) values ('$charge->id','$cart_id','$user_id','$stripeBillingName','$stripeBillingAddressCountry','$stripeBillingAddressZip','$stripeBillingAddressLine1','$stripeBillingAddressCity','$stripeShippingName','$stripeShippingAddressCountry','$stripeShippingAddressZip','$stripeShippingAddressLine1','$stripeShippingAddressCity','$sub_total','$tax','$final_price','$description','$charge->object')");
76
77$domain = ($_SERVER['HTTP_HOST'] != 'localhost')? '.'.$_SERVER['HTTP_HOST']:false;
78setcookie(CART_COOKIE,'',1,"/",$domain,false);
79// include 'includes/head.php';
80// include 'includes/navigation.php';
81$final_price = round($final_price,2) *100;
82?>
83<h1 class="text-center text-success">Thank You!</h1>
84<p>Your card has been successfully charged <?= money($final_price/100); ?>. You have been emailed a receipt. Please check your spam folder if is not in your inbox. Aditionally you can print this page as a receipt.</p>
85<p>Your receipt number is: <strong><?= $cart_id; ?></strong> </p>
86<p>Your order will be shipped to the address bellow.</p>
87<address class="">
88 <?= $stripeShippingName.', '.$phone; ?><br>
89 <?= $stripeShippingAddressCountry.', '.$stripeShippingAddressCity; ?><br>
90 <?= $stripeShippingAddressLine1.', '.$stripeShippingAddressZip; ?>
91</address>
92
93<?php
94
95
96
97
98// $txnQuery = $db->query("SELECT * FROM transactions where id = '{$cart_id}'");
99// $txn = mysqli_fetch_assoc($txnQuery);
100// $cartid = $txn['cart_id'];
101$cartQuery=$db->query("SELECT * FROM cart where id = '{$cart_id}'");
102$cart = mysqli_fetch_assoc($cartQuery);
103$items = json_decode($cart['items'],true);
104$idArray = array();
105$products = array();
106foreach ($items as $item) {
107 $idArray[]=$item['id'];
108}
109$ids = implode(',',$idArray);
110$productQ = $db->query(
111 "SELECT i.id, i.title as 'title', c.id as 'cid', c.category as 'subcat', p.category as 'cat'
112 from products i
113 left join categories c on i.categories = c.id
114 left join categories p on c.parent = p.id
115 where i.id in ({$ids})
116");
117
118while ($p = mysqli_fetch_assoc($productQ)) {
119 foreach ($items as $item) {
120 if ($item['id'] == $p['id']) {
121 $x = $item;
122 continue;
123 }
124 }
125 $products[]=array_merge($x,$p);
126}
127
128
129
130
131
132
133
134$mail = new PHPMailer(true);
135//Tell PHPMailer to use SMTP
136$mail->isSMTP();
137//Enable SMTP debugging
138// 0 = off (for production use)
139// 1 = client messages
140// 2 = client and server messages
141$mail->SMTPDebug = 2;
142//Ask for HTML-friendly debug output
143$mail->Debugoutput = 'html';
144//Set the hostname of the mail server
145$mail->Host = 'smtp.gmail.com';
146// use
147// $mail->Host = gethostbyname('smtp.gmail.com');
148// if your network does not support SMTP over IPv6
149//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
150$mail->Port = 587;
151//Set the encryption system to use - ssl (deprecated) or tls
152$mail->SMTPSecure = 'tls';
153//Whether to use SMTP authentication
154$mail->SMTPAuth = true;
155//Username to use for SMTP authentication - use full email address for gmail
156$mail->Username = '***************@gmail.com';
157//Password to use for SMTP authentication
158$mail->Password = '***************';
159//Set who the message is to be sent from
160$mail->setFrom('from@example.com', 'First Last');
161//Set an alternative reply-to address
162$mail->addReplyTo('replyto@example.com', 'First Last');
163//Set who the message is to be sent to
164$mail->addAddress($email, 'John Doe');
165//Set the subject line
166$mail->Subject = 'PHPMailer GMail SMTP test';
167//Read an HTML message body from an external file, convert referenced images to embedded,
168//convert HTML into a basic plain-text alternative body
169//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
170//Replace the plain text body with one created manually
171$mail->Body = '
172<html xmlns="http://www.w3.org/1999/xhtml">
173<head>
174<style>
175* {
176 color:white;
177 margin: 0;
178 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
179 box-sizing: border-box;
180 font-size: 14px;
181}
182
183img {
184 max-width: 100%;
185}
186
187body {
188 -webkit-font-smoothing: antialiased;
189 -webkit-text-size-adjust: none;
190 width: 100% !important;
191 height: 100%;
192 line-height: 1.6em;
193 /* 1.6em * 14px = 22.4px, use px to get airier line-height also in Thunderbird, and Yahoo!, Outlook.com, AOL webmail clients */
194 /*line-height: 22px;*/
195}
196
197
198table td {
199 vertical-align: top;
200}
201
202/* -------------------------------------
203 BODY & CONTAINER
204------------------------------------- */
205body {
206 background-color: #f6f6f6;
207}
208
209.body-wrap {
210 background-color: #f6f6f6;
211 width: 100%;
212}
213
214.container {
215 display: block !important;
216 max-width: 600px !important;
217 margin: 0 auto !important;
218 /* makes it centered */
219 clear: both !important;
220}
221
222.content {
223 max-width: 600px;
224 margin: 0 auto;
225 display: block;
226 padding: 20px;
227}
228
229/* -------------------------------------
230 HEADER, FOOTER, MAIN
231------------------------------------- */
232.main {
233 background-color: #4f4f4f;
234 box-shadow: 1px 1px 10px 2px black;
235 border: 1px solid #e9e9e9;
236 border-radius: 3px;
237}
238
239.content-wrap {
240 padding: 20px;
241}
242
243.content-block {
244 padding: 0 0 20px;
245}
246
247.header {
248 width: 100%;
249 margin-bottom: 20px;
250}
251
252.footer {
253 width: 100%;
254 clear: both;
255 color: #999;
256 padding: 20px;
257}
258.footer p, .footer a, .footer td {
259 color: #999;
260 font-size: 12px;
261}
262
263/* -------------------------------------
264 TYPOGRAPHY
265------------------------------------- */
266h1, h2, h3 {
267 font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
268 color: #000;
269 margin: 40px 0 0;
270 line-height: 1.2em;
271 font-weight: 400;
272}
273
274h1 {
275 font-size: 32px;
276 font-weight: 500;
277 /* 1.2em * 32px = 38.4px, use px to get airier line-height also in Thunderbird, and Yahoo!, Outlook.com, AOL webmail clients */
278 /*line-height: 38px;*/
279}
280
281h2 {
282 font-size: 24px;
283 /* 1.2em * 24px = 28.8px, use px to get airier line-height also in Thunderbird, and Yahoo!, Outlook.com, AOL webmail clients */
284 /*line-height: 29px;*/
285}
286
287h3 {
288 font-size: 18px;
289 /* 1.2em * 18px = 21.6px, use px to get airier line-height also in Thunderbird, and Yahoo!, Outlook.com, AOL webmail clients */
290 /*line-height: 22px;*/
291}
292
293h4 {
294 font-size: 14px;
295 font-weight: 600;
296}
297
298p, ul, ol {
299 margin-bottom: 10px;
300 font-weight: normal;
301}
302p li, ul li, ol li {
303 margin-left: 5px;
304 list-style-position: inside;
305}
306
307/* -------------------------------------
308 LINKS & BUTTONS
309------------------------------------- */
310a {
311 color: #348eda;
312 text-decoration: underline;
313}
314
315.btn-primary {
316 text-decoration: none;
317 color: #FFF;
318 background-color: #348eda;
319 border: solid #348eda;
320 border-width: 10px 20px;
321 line-height: 2em;
322 /* 2em * 14px = 28px, use px to get airier line-height also in Thunderbird, and Yahoo!, Outlook.com, AOL webmail clients */
323 /*line-height: 28px;*/
324 font-weight: bold;
325 text-align: center;
326 cursor: pointer;
327 display: inline-block;
328 border-radius: 5px;
329 text-transform: capitalize;
330}
331
332/* -------------------------------------
333 OTHER STYLES THAT MIGHT BE USEFUL
334------------------------------------- */
335.last {
336 margin-bottom: 0;
337}
338
339.first {
340 margin-top: 0;
341}
342
343.aligncenter {
344 text-align: center;
345}
346
347.alignright {
348 text-align: right;
349}
350
351.alignleft {
352 text-align: left;
353}
354
355.clear {
356 clear: both;
357}
358
359/* -------------------------------------
360 ALERTS
361 Change the class depending on warning email, good email or bad email
362------------------------------------- */
363.alert {
364 font-size: 16px;
365 color: #fff;
366 font-weight: 500;
367 padding: 20px;
368 text-align: center;
369 border-radius: 3px 3px 0 0;
370}
371.alert a {
372 color: #fff;
373 text-decoration: none;
374 font-weight: 500;
375 font-size: 16px;
376}
377.alert.alert-warning {
378 background-color: #FF9F00;
379}
380.alert.alert-bad {
381 background-color: #D0021B;
382}
383.alert.alert-good {
384 background-color: #68B90F;
385}
386
387/* -------------------------------------
388 INVOICE
389 Styles for the billing table
390------------------------------------- */
391.invoice {
392 margin: 40px auto;
393 text-align: left;
394 width: 80%;
395}
396.invoice td {
397 padding: 5px 0;
398}
399.invoice .invoice-items {
400 width: 100%;
401}
402.invoice .invoice-items td {
403 border-top: #eee 1px solid;
404}
405.invoice .invoice-items .total td {
406 border-top: 2px solid #333;
407 border-bottom: 2px solid #333;
408 font-weight: 700;
409}
410
411/* -------------------------------------
412 RESPONSIVE AND MOBILE FRIENDLY STYLES
413------------------------------------- */
414@media only screen and (max-width: 640px) {
415 body {
416 padding: 0 !important;
417 }
418
419 h1, h2, h3, h4 {
420 font-weight: 800 !important;
421 margin: 20px 0 5px !important;
422 }
423
424 h1 {
425 font-size: 22px !important;
426 }
427
428 h2 {
429 font-size: 18px !important;
430 }
431
432 h3 {
433 font-size: 16px !important;
434 }
435
436 .container {
437 padding: 0 !important;
438 width: 100% !important;
439 }
440
441 .content {
442 padding: 0 !important;
443 }
444
445 .content-wrap {
446 padding: 10px !important;
447 }
448
449 .invoice {
450 width: 100% !important;
451 }
452}
453</style>
454</head>
455<table class="body-wrap">
456 <tr>
457 <td></td>
458 <td class="container" width="600">
459 <div class="content">
460 <table class="main" width="100%" cellpadding="0" cellspacing="0">
461 <tr>
462 <td class="content-wrap aligncenter">
463 <table width="100%" cellpadding="0" cellspacing="0">
464 <tr>
465 <img src="https://i.imgur.com/urrJz2x.png" alt="">
466 </tr>
467 <tr>
468 <td class="content-block">
469 <h1 class="aligncenter">You paid '.money($final_price/100).'</h1>
470 </td>
471 </tr>
472
473 <tr>
474 <td class="content-block aligncenter">
475 <table class="invoice">
476 <tr>
477 <td>'.$stripeShippingName.'<br>'.$stripeShippingAddressCountry.', '.$stripeShippingAddressCity.'<br>'.$stripeShippingAddressLine1.', '.$stripeShippingAddressZip.'</td>
478 </tr>
479 <tr>
480 <td>
481 <table class="invoice-items" cellpadding="0" cellspacing="0">
482 '
483 foreach($products as $product){'
484 <tr>
485 <td>'.$product['title'].' x'.$product['quantity'].'</td>
486 <td class="alignright">'.money($price['price']).'</td>
487 </tr>
488 '}'
489
490
491 </table>
492 </td>
493 </tr>
494 </table>
495 </td>
496 </tr>
497 <tr>
498 <td class="content-block">
499 <h2 class="aligncenter">Thank You for using CPUROCKS©</h2>
500 </td>
501 </tr>
502
503 </table>
504 </td>
505 </tr>
506 </table>
507 </div>
508 </td>
509 <td></td>
510 </tr>
511</table>
512
513
514</html>
515
516';
517$mail->IsHTML(true);
518$mail->AltBody = 'This is a plain-text message body';
519//send the message, check for errors
520if (!$mail->send()) {
521 echo "Mailer Error: " . $mail->ErrorInfo;
522} else {
523 echo "Message sent!";
524}
525 ?>
526
527
528
529<?php
530
531
532
533include 'includes/footer.php';
534// echo '<h1>Successfully charged '. money($final_price/100) .'!</h1>';
535
536?>