forked from anbarli/PHP-SBCS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckout.php
433 lines (383 loc) · 11.6 KB
/
checkout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
// Report All PHP Errors
//error_reporting(E_ALL);
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$title = "Attys - Checkout";
include("../header.php");
?>
<script
src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX¤cy=GBP">
</script>
<?php
$uk = "United Kingdom";
$currency = "GBP";
$shipping = array(
$uk => array(9.99,true,true),
"Republic of Ireland" => array(14.99,true,true),
"Belgium" => array(14.99,true,true),
"Luxembourg" => array(14.99,true,true),
"Netherlands" => array(14.99,true,true),
"Denmark" => array(14.99,true,true),
"France" => array(14.99,true,true),
"Germany" => array(14.99,true,true),
"Greece" => array(19.99,true,true),
"Italy" => array(19.99,true,true),
"Spain" => array(19.99,true,true),
"Sweden" => array(19.99,true,true),
"Estonia" => array(19.99,true,true),
"Latvia" => array(19.99,true,true),
"Lithuania" => array(19.99,true,true),
"Bulgaria" => array(39.99,true,true),
"Croatia" => array(39.99,true,true),
"Romania" => array(39.99,true,true),
"Austria" => array(19.99,true,true),
"Czech Republic" => array(19.99,true,true),
"Finland" => array(19.99,true,true),
"Hungary" => array(19.99,true,true),
"Norway" => array(19.99,true,true),
"Poland" => array(19.99,true,true),
"Slovakia" => array(19.99,true,true),
"Turkey" => array(39.99,false,false),
"Switzerland" => array(19.99,false,false),
"USA" => array(29.99,false,false),
"Canada" => array(24.99,false,false),
"Australia" => array(39.99,false,false),
"Indonesia" => array(39.99,false,false),
"Japan" => array(39.99,false,false),
"Malaysia" => array(39.99,false,false),
"Philippines" => array(39.99,false,false),
"Singapore" => array(39.99,false,false),
"South Korea" => array(39.99,false,false),
"Taiwan" => array(39.99,false,false),
"Thailand" => array(39.99,false,false),
"India" => array(39.99,false,false),
"South Africa" => array(39.99,false,false),
"United Arab Emirates" => array(39.99,false,false)
);
$vatrate = 20;
function getVal($name) {
$a = " name=\"".$name."\" ";
$a = $a." id=\"".$name."\" ";
if (isset($_POST[$name])) {
$_SESSION[$name] = $_POST[$name];
}
if(isset($_SESSION[$name])) {
$a = $a." value=\"".$_SESSION[$name]."\" ";
}
echo $a;
}
function getTextArea($name) {
$a = "<textarea class=\"form-control\" style=\"height:50px;\"";
$a = $a." name=\"".$name."\" ";
$a = $a." id=\"".$name."\" ";
if (isset($_POST[$name])) {
$_SESSION[$name] = $_POST[$name];
}
$a = $a.">";
if(isset($_SESSION[$name])) {
$a = $a.$_SESSION[$name];
}
$a = $a."</textarea>";
echo $a;
}
function getCountry() {
global $shipping;
$name ="country";
if (!empty($_POST[$name])) {
$_SESSION[$name] = $_POST[$name];
}
echo "<select class=\"form-control selectEleman\" name=\"".$name."\">\n";
$srtc = $shipping;
ksort($srtc);
foreach($srtc as $key => $val) {
$selected="";
if (isset($_SESSION[$name])) {
if (strcmp($key,$_SESSION[$name])==0) {
$selected = " selected";
}
}
echo "<option value=\"".$key."\"".$selected.">".$key."</option>\n";
}
echo "</select>\n";
}
function calcVat($v) {
global $vatrate;
$v = round($v * $vatrate / 100.0,2);
return $v;
}
?>
<div class="container">
<h2>Checkout</h2>
<!-- Address -->
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-phone-alt"></span> Address</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="?">
<div class="form-group">
<label for="inputEmail">Email</label>
<div>
<input type="email" class="form-control" <?php getVal("email"); ?> placeholder="mail@domain.com" >
</div>
</div>
<div class="form-group">
<label for="phone">Phone</label>
<div>
<input type="text" class="form-control" <?php getVal("phone"); ?> placeholder="+12 3456 78901239" >
</div>
</div>
<div class="form-group">
<label for="name">Recipient Name</label>
<div>
<input type="text" class="form-control" <?php getVal("name"); ?> placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="business">Business Name</label>
<div>
<input type="text" class="form-control" <?php getVal("business"); ?> >
</div>
</div>
<div class="form-group">
<label for="address">Address</label>
<div>
<?php getTextArea("address"); ?>
</div>
</div>
<div class="form-group">
<label for="town">Town</label>
<div>
<input type="text" class="form-control" <?php getVal("town"); ?> >
</div>
</div>
<div class="form-group">
<label for="postcode">Post Code</label>
<div>
<input type="text" class="form-control" <?php getVal("postcode"); ?> >
</div>
</div>
<div class="form-group">
<label for="euvat">EU VAT number</label>
<div>
<input type="text" <?php getVal("euvat"); ?> class="form-control">
</div>
</div>
<div class="form-group">
<label for="country">Country</label>
<div style="margin-top: 6px;">
<?php getCountry(); ?>
</div>
</div>
<div class="form-group">
<div>
<button type="submit" class="btn btn-success pull-right">Calculate Postage and VAT</button>
</div>
</div>
</form>
</div>
</div>
<!-- // Address -->
<?php
echo "
<div class=\"panel panel-default\">
<div class=\"panel-heading\" style=\"margin-bottom:0;\">
<h3 class=\"panel-title\">My Cart</h3>
<p>Invoice No.: ".$_SESSION['invoice_id']."</p>
</div>
<div class=\"table-responsive\">
<table class=\"table\">
<tr class=\"tableactive\">
<td><b>Product</b></td><td align=\"right\"><b>Price</b></td><td align=\"right\"><b>Quantity</b></td><td align=\"right\"><b>Total</b>.</td>
</tr>
";
$notaxtotal = 0;
$vat = 0;
$addVat = false;
$isInEU = false;
$shippingcost = 0;
$isUK = false;
$hasEUVAT = false;
$purchase_unit = array(
'invoice_id' => $_SESSION['invoice_id'],
'soft_descriptor' => 'Glasgow Neuro LTD',
'amount' => array(),
'items' => array(),
);
// can only calc tax with country
if (!empty($_SESSION['country'])) {
$c = $shipping[$_SESSION['country']];
$addVat = $c[1];
$isInEU = $c[2];
$isUK = strcmp($uk,$_SESSION['country']) == 0;
}
if (!empty($_SESSION['euvat'])) {
$hasEUVAT = strlen($_SESSION['euvat']) > 7;
$addVat = ($isUK || ($isInEU && (!$hasEUVAT)) );
}
// For finding session elements line number
$linenumber = 0;
$qtydecimaltotal = 0;
$items = array();
// Run loop for cart array
foreach($_SESSION['SBCScart'] as $SBCSitem)
{
// Don't list items with 0 qty
if($SBCSitem['quantity']!=0) {
$pricedecimal = (float)$SBCSitem['unitprice'];
$qtydecimal = (int)$SBCSitem['quantity'];
$qtydecimaltotal = $qtydecimaltotal + $qtydecimal;
$prodvat = 0;
if ($addVat) {
$prodvat = calcVat($pricedecimal);
}
// Write cart to screen
echo
"
<tr class='tablerow'>
<td>".$SBCSitem['item']."</td>
<td align=\"right\">£".finnum($SBCSitem['unitprice'])."</td>
<td align=\"right\">".$SBCSitem['quantity']."</td>
<td align=\"right\">£".finnum($pricedecimal * $qtydecimal)."</td>
</tr>
";
$purchase_unit['items'][] = array(
"name" => $SBCSitem['item'],
"unit_amount" => array("currency_code" => $currency, "value" => $pricedecimal),
"tax" => array("currency_code" => $currency, "value" => $prodvat),
"quantity" => $qtydecimal
);
// Total
$notaxtotal += ($pricedecimal * $qtydecimal);
$vat += ($prodvat * $qtydecimal);
}
$linenumber++;
}
if (!empty($_SESSION['country'])) {
$c = $shipping[$_SESSION['country']];
$shippingcost = $c[0];
$_SESSION['shipping'] = $shippingcost;
// Shipping
echo
"
<tr class='tablerow'>
<td>Shipping</td>
<td> </td>
<td> </td>
<td align=\"right\">£".finnum($shippingcost)."</td>
</tr>
";
$shippingvat = 0;
if ($addVat) {
$shippingvat = calcVat($shippingcost);
}
$vat = $vat + $shippingvat;
$notaxtotal = $notaxtotal + $shippingcost;
$purchase_unit['items'][] = array(
"name" => "Shipping",
"unit_amount" => array("currency_code" => $currency, "value" => $shippingcost),
"tax" => array("currency_code" => $currency, "value" => $shippingvat),
"quantity" => 1
);
$_SESSION['shippingcost'] = $shippingcost;
$_SESSION['shippingvat'] = $shippingvat;
}
$notaxtotal = round($notaxtotal,2);
$_SESSION['notaxtotal'] = $notaxtotal;
$_SESSION['vat'] = $vat;
$vat = round($vat,2);
$_SESSION['vat'] = $vat;
$total = $notaxtotal + $vat;
$total = round($total,2);
$_SESSION['total'] = $total;
$purchase_unit['amount'] = array(
"value" => $total,
"currency_code" => $currency,
"breakdown" => array(
"item_total" => array("value" => $notaxtotal,
"currency_code" => $currency ),
"tax_total" => array("value" => $vat,
"currency_code" => $currency )
)
);
echo
"
<tr class='tablerow'>
<td>VAT</td>
<td> </td>
<td> </td>
<td align=\"right\">£".finnum($vat)."</td>
</tr>
";
echo "
<tr class=\"tableactive\">
<td> </td>
<td class='text-right'>Total</td>
<td align=\"right\">".$qtydecimaltotal."</td>
<td align=\"right\">£".finnum($total)."</td>
</tr>
</table>
</div>
</div>
";
$okconf = (!empty($_SESSION['shipping'])) && (!empty($_SESSION['email'])) &&
(!empty($_SESSION['town'])) && (!empty($_SESSION['postcode'])) &&
(!empty($_SESSION['name'])) && (!empty($_SESSION['address']));
if ($okconf) {
echo "
<div id=\"paypal-button-container\"></div>
<script>paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [
".json_encode($purchase_unit,JSON_PRETTY_PRINT)."
],
});
},
onApprove: function(data, actions) {
// Capture the funds from the transaction
return actions.order.capture().then(function(details) {
// Show a success message to your buyer
alert('Transaction completed by ' + details.payer.name.given_name);
window.open(\"confirmation.php\",\"_self\");
});
},
onError: function (err) {
alert(err);
}
}).render('#paypal-button-container');
</script>
<!-- Order -->
<div class=\"panel panel-default\">
<div class=\"panel-heading\">
<h3 class=\"panel-title\">Invoice / Bank transfer</h3>
</div>
<div class=\"panel-body\">
<form role=\"form\" method=\"post\" action=\"confirmation.php\">
<div class=\"form-group\">
<label for=\"optionsRadios1\">You can also request an invoice.</label>
<div style=\"margin-top: 6px;\">
</div>
</div>
<div class=\"form-group\">
<div>
<button type=\"submit\" class=\"btn btn-success pull-right\">Request invoice</button>
<input type=\"hidden\" name=\"invoice\" value=\"1\">
</div>
</div>
</form>
</div>
</div>
";
}
?>
<hr>
<footer>
<p>Shopping basket is based on <a href="https://github.com/ganbarli/PHP-SBCS" target="blank">PHP-SBCS</a></p>
</footer>
</div><!--/.container-->
<?php
include("../footer.php");
?>