/
process.php
127 lines (107 loc) · 4.17 KB
/
process.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
<?php
return function($site, $pages, $page) {
if (r::is('post')) {
// POST request from the Cart page
// Honeypot trap for robots
if (get('subject') != '') go(page('error')->url());
// Start the validation
$invalidFields = '';
// Validate personal details
$fieldsToValidate = [
'firstname' => v::minLength(esc(get('firstname')), 1),
'lastname' => v::minLength(esc(get('lastname')), 1),
'email' => v::email(esc(get('email')))
];
foreach ($fieldsToValidate as $key => $value) {
if ($value === false) {
$invalidFields .= $key.',';
}
}
// Validate mailing address
if ($site->mailing_address()->bool()) {
$fieldsToValidate = [
'address1' => v::minLength(esc(get('address1')), 1),
'city' => v::minLength(esc(get('city')), 1),
'state' => v::minLength(esc(get('state')), 1),
'postcode' => v::minLength(esc(get('postcode'), 1))
];
foreach ($fieldsToValidate as $key => $value) {
if ($value === false) {
$invalidFields .= $key.',';
}
}
}
// Validate terms and conditions checkbox
if ($tc = page('shop/terms-conditions') and $tc->text()->isNotEmpty() and get('tac') !== 'agree') {
$invalidFields .= 'tac,';
}
// Write personal details & mailing address to the transaction file
page(s::get('txn'))->update([
'payer-id' => $user ? $user->username() : '',
'payer-firstname' => esc(get('firstname')),
'payer-lastname' => esc(get('lastname')),
'payer-email' => esc(get('email')),
'shipping-address' => yaml::encode([
'address1' => esc(get('address1')),
'address2' => esc(get('address2')),
'city' => esc(get('city')),
'state' => esc(get('state')),
'country' => esc(get('country')),
'postcode' => esc(get('postcode'))
])
], $site->defaultLanguage()->code());
// Return to cart if there are invalid fields
if ($invalidFields != '') {
go(page('shop/cart')->url().'/invalid'.url::paramSeparator().substr($invalidFields, 0, -1).'#details');
}
// Set up variables
$user = $site->user();
$timestamp = date('U');
// Set transaction status
if (get('txnPaid') == 'true') {
$status = 'paid';
} else {
$status = 'pending';
}
// Add transaction details
$discount = getDiscount();
$decimal_places = decimalPlaces($site->currency_code());
page(s::get('txn'))->update([
'txn-date' => $timestamp,
'txn-currency' => $site->currency_code(),
'status' => $status,
'subtotal' => number_format(cartSubtotal(getItems()),$decimal_places,'.',''),
'discountcode' => s::get('discountcode'),
'discount' => number_format($discount['amount'],$decimal_places,'.',''),
'tax' => number_format(cartTax()['total'],$decimal_places,'.',''),
'taxes' => yaml::encode(cartTax()),
'giftcode' => s::get('giftcode'),
'giftcertificate' => null !== get('giftCertificateAmount') ? number_format(get('giftCertificateAmount'),$decimal_places,'.','') : '0.00'
], $site->defaultLanguage()->code());
// Update the site's giftcard balance
if ($giftCertificateRemaining = get('giftCertificateRemaining')) {
$certificates = $site->gift_certificates()->yaml();
foreach ($certificates as $key => $certificate) {
if (str::upper($certificate['code']) == s::get('giftcode')) {
$certificates[$key]['amount'] = number_format($giftCertificateRemaining,$decimal_places,'.','');
}
}
$site->update([
'gift-certificates' => yaml::encode($certificates)
], $site->defaultLanguage()->code());
}
// Redirect to self with GET, passing along the gateway and order ID as URL parameters
go($page->url().'/gateway'.url::paramSeparator().get('gateway').'/id'.url::paramSeparator().page(s::get('txn'))->txn_id());
} else {
// GET request. Gateway-specific.
// Get the transaction file we just created.
$txn = page('shop/orders/'.param('id'));
// If it's not there, kick back to Cart page
if(!$txn) go(page('shop/cart')->url());
return [
'gateway' => param('gateway'),
'txn' => $txn,
];
}
};
?>