|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Helper class for getting an order throught the checkout process |
|
5
|
|
|
*/ |
|
6
|
|
|
class Checkout |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* 4 different membership schemes: |
|
10
|
|
|
* 1: creation disabled & membership not required |
|
11
|
|
|
* no body can, or is required to, become a member at checkout. |
|
12
|
|
|
* 2: creation disabled & membership required |
|
13
|
|
|
* only existing members can use checkout. |
|
14
|
|
|
* (ideally the entire shop should be disabled in this case) |
|
15
|
|
|
* 3: creation enabled & membership required |
|
16
|
|
|
* everyone must be, or become a member at checkout. |
|
17
|
|
|
* 4: creation enabled & membership not required (default) |
|
18
|
|
|
* it is optional to be, or become a member at checkout. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
11 |
|
public static function member_creation_enabled() |
|
22
|
|
|
{ |
|
23
|
11 |
|
return CheckoutConfig::config()->member_creation_enabled; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public static function membership_required() |
|
27
|
|
|
{ |
|
28
|
1 |
|
return CheckoutConfig::config()->membership_required; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
4 |
|
public static function get($order = null) |
|
32
|
|
|
{ |
|
33
|
4 |
|
if ($order === null) { |
|
34
|
|
|
$order = ShoppingCart::curr(); //roll back to current cart |
|
35
|
|
|
} |
|
36
|
4 |
|
if ($order->exists() && $order->isInDB()) {//check if order can go through checkout |
|
37
|
4 |
|
return new Checkout($order); |
|
38
|
|
|
} |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected $order; |
|
43
|
|
|
|
|
44
|
|
|
protected $message; |
|
45
|
|
|
|
|
46
|
|
|
protected $type; |
|
47
|
|
|
|
|
48
|
14 |
|
public function __construct(Order $order) |
|
49
|
|
|
{ |
|
50
|
14 |
|
$this->order = $order; |
|
51
|
14 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get stored message |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function getMessage() |
|
59
|
|
|
{ |
|
60
|
1 |
|
return $this->message; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get type of stored message |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getMessageType() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->type; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* contact information |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setContactDetails($email, $firstname, $surname) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->order->Email = $email; |
|
|
|
|
|
|
79
|
|
|
$this->order->FirstName = $firstname; |
|
|
|
|
|
|
80
|
|
|
$this->order->Surname = $surname; |
|
|
|
|
|
|
81
|
|
|
$this->order->write(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
//save / set up addresses |
|
85
|
1 |
|
public function setShippingAddress(Address $address) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->order->ShippingAddressID = $address->ID; |
|
|
|
|
|
|
88
|
1 |
|
if (Member::currentUserID()) { |
|
89
|
1 |
|
$this->order->MemberID = Member::currentUserID(); |
|
|
|
|
|
|
90
|
1 |
|
} |
|
91
|
1 |
|
$this->order->write(); |
|
92
|
1 |
|
$this->order->extend('onSetShippingAddress', $address); |
|
93
|
|
|
//update zones and userinfo |
|
94
|
1 |
|
ShopUserInfo::singleton()->setAddress($address); |
|
95
|
1 |
|
Zone::cache_zone_ids($address); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function setBillingAddress(Address $address) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->order->BillingAddressID = $address->ID; |
|
|
|
|
|
|
101
|
1 |
|
if (Member::currentUserID()) { |
|
102
|
1 |
|
$this->order->MemberID = Member::currentUserID(); |
|
|
|
|
|
|
103
|
1 |
|
} |
|
104
|
1 |
|
$this->order->write(); |
|
105
|
1 |
|
$this->order->extend('onSetBillingAddress', $address); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/* |
|
109
|
|
|
* Get a dataobject of payment methods. |
|
110
|
|
|
*/ |
|
111
|
5 |
|
public function getPaymentMethods() |
|
112
|
|
|
{ |
|
113
|
5 |
|
return GatewayInfo::get_supported_gateways(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set payment method |
|
118
|
|
|
*/ |
|
119
|
5 |
|
public function setPaymentMethod($paymentmethod) |
|
120
|
|
|
{ |
|
121
|
5 |
|
$methods = $this->getPaymentMethods(); |
|
122
|
5 |
|
if (!isset($methods[$paymentmethod])) { |
|
123
|
|
|
Session::set("Checkout.PaymentMethod", null); |
|
124
|
|
|
Session::clear("Checkout.PaymentMethod"); |
|
125
|
|
|
return $this->error(_t("Checkout.NoPaymentMethod", "Payment method does not exist")); |
|
126
|
|
|
} |
|
127
|
5 |
|
Session::set("Checkout.PaymentMethod", $paymentmethod); |
|
128
|
5 |
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Gets the selected payment method from the session, |
|
133
|
|
|
* or the only available method, if there is only one. |
|
134
|
|
|
*/ |
|
135
|
5 |
|
public function getSelectedPaymentMethod($nice = false) |
|
136
|
|
|
{ |
|
137
|
5 |
|
$methods = $this->getPaymentMethods(); |
|
138
|
5 |
|
reset($methods); |
|
139
|
5 |
|
$method = count($methods) === 1 ? key($methods) : Session::get("Checkout.PaymentMethod"); |
|
140
|
5 |
|
if ($nice && isset($methods[$method])) { |
|
141
|
|
|
$method = $methods[$method]; |
|
142
|
|
|
} |
|
143
|
5 |
|
return $method; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Checks if member (or not) is allowed, in accordance with configuration |
|
148
|
|
|
*/ |
|
149
|
7 |
|
public function validateMember($member) |
|
150
|
|
|
{ |
|
151
|
7 |
|
if (!CheckoutConfig::config()->membership_required) { |
|
152
|
7 |
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
if (empty($member) || !($member instanceof Member)) { |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
return true; |
|
159
|
|
|
} |
|
160
|
1 |
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Store a new error & return false; |
|
163
|
1 |
|
*/ |
|
164
|
1 |
|
protected function error($message) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->message($message, "bad"); |
|
167
|
1 |
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Store a message to be fed back to user. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $message |
|
174
|
|
|
* @param string $type - good, bad, warning |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function message($message, $type = "good") |
|
177
|
|
|
{ |
|
178
|
|
|
$this->message = $message; |
|
179
|
|
|
$this->type = $type; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.