1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Handles tasks to be performed on orders, particularly placing and processing/fulfilment. |
5
|
|
|
* Placing, Emailing Reciepts, Status Updates, Printing, Payments - things you do with a completed order. |
6
|
|
|
* |
7
|
|
|
* @package shop |
8
|
|
|
*/ |
9
|
|
|
class OrderProcessor |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Order |
13
|
|
|
*/ |
14
|
|
|
protected $order; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var OrderEmailNotifier |
18
|
|
|
*/ |
19
|
|
|
protected $notifier; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $error; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Static way to create the order processor. |
28
|
|
|
* Makes creating a processor easier. |
29
|
|
|
* |
30
|
|
|
* @param Order $order |
31
|
|
|
*/ |
32
|
6 |
|
public static function create(Order $order) |
33
|
|
|
{ |
34
|
6 |
|
return Injector::inst()->create('OrderProcessor', $order); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Assign the order to a local variable |
39
|
|
|
* |
40
|
|
|
* @param Order $order |
41
|
|
|
*/ |
42
|
8 |
|
public function __construct(Order $order) |
43
|
|
|
{ |
44
|
8 |
|
$this->order = $order; |
45
|
8 |
|
$this->notifier = OrderEmailNotifier::create($order); |
46
|
8 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* URL to display success message to the user. |
50
|
|
|
* Happens after any potential offsite gateway redirects. |
51
|
|
|
* |
52
|
|
|
* @return String Relative URL |
53
|
|
|
*/ |
54
|
4 |
|
public function getReturnUrl() |
55
|
3 |
|
{ |
56
|
1 |
|
return $this->order->Link(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a payment model, and provide link to redirect to external gateway, |
61
|
|
|
* or redirect to order link. |
62
|
|
|
* |
63
|
|
|
* @return string - url for redirection after payment has been made |
64
|
|
|
*/ |
65
|
2 |
|
public function makePayment($gateway, $gatewaydata = array()) |
66
|
|
|
{ |
67
|
|
|
//create payment |
68
|
1 |
|
$payment = $this->createPayment($gateway); |
69
|
1 |
|
if (!$payment) { |
70
|
|
|
//errors have been stored. |
71
|
|
|
return false; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Create a purchase service, and set the user-facing success URL for redirects |
75
|
1 |
|
$service = PurchaseService::create($payment) |
76
|
2 |
|
->setReturnUrl($this->getReturnUrl()); |
77
|
|
|
|
78
|
|
|
// Process payment, get the result back |
79
|
1 |
|
$response = $service->purchase($this->getGatewayData($gatewaydata)); |
80
|
1 |
|
if (GatewayInfo::is_manual($gateway)) { |
81
|
|
|
//don't complete the payment at this stage, if payment is manual |
82
|
|
|
$this->placeOrder(); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
// For an OFFSITE payment, response will now contain a redirect |
86
|
1 |
|
// For an ONSITE payment, ShopPayment::onCapture will have been called, which will have called completePayment |
87
|
|
|
|
88
|
|
|
return $response; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Map shop data to omnipay fields |
93
|
|
|
* |
94
|
|
|
* @param array $customData Usually user submitted data. |
95
|
|
|
* |
96
|
1 |
|
* @return array |
97
|
|
|
*/ |
98
|
1 |
|
protected function getGatewayData($customData) |
99
|
1 |
|
{ |
100
|
|
|
$shipping = $this->order->getShippingAddress(); |
101
|
1 |
|
$billing = $this->order->getBillingAddress(); |
102
|
1 |
|
|
103
|
1 |
|
$numPayments = Payment::get() |
104
|
|
|
->filter(array('OrderID' => $this->order->ID)) |
105
|
1 |
|
->count() - 1; |
106
|
|
|
|
107
|
1 |
|
$transactionId = $this->order->Reference . ($numPayments > 0 ? "-$numPayments" : ''); |
|
|
|
|
108
|
1 |
|
|
109
|
|
|
return array_merge( |
110
|
1 |
|
$customData, |
111
|
1 |
|
array( |
112
|
1 |
|
'transactionId' => $transactionId, |
113
|
1 |
|
'firstName' => $this->order->FirstName, |
|
|
|
|
114
|
1 |
|
'lastName' => $this->order->Surname, |
|
|
|
|
115
|
1 |
|
'email' => $this->order->Email, |
|
|
|
|
116
|
1 |
|
'company' => $this->order->Company, |
|
|
|
|
117
|
1 |
|
'billingAddress1' => $billing->Address, |
|
|
|
|
118
|
1 |
|
'billingAddress2' => $billing->AddressLine2, |
|
|
|
|
119
|
1 |
|
'billingCity' => $billing->City, |
|
|
|
|
120
|
1 |
|
'billingPostcode' => $billing->PostalCode, |
|
|
|
|
121
|
1 |
|
'billingState' => $billing->State, |
|
|
|
|
122
|
1 |
|
'billingCountry' => $billing->Country, |
|
|
|
|
123
|
1 |
|
'billingPhone' => $billing->Phone, |
|
|
|
|
124
|
1 |
|
'shippingAddress1' => $shipping->Address, |
|
|
|
|
125
|
1 |
|
'shippingAddress2' => $shipping->AddressLine2, |
|
|
|
|
126
|
1 |
|
'shippingCity' => $shipping->City, |
|
|
|
|
127
|
1 |
|
'shippingPostcode' => $shipping->PostalCode, |
|
|
|
|
128
|
1 |
|
'shippingState' => $shipping->State, |
|
|
|
|
129
|
|
|
'shippingCountry' => $shipping->Country, |
|
|
|
|
130
|
1 |
|
'shippingPhone' => $shipping->Phone, |
|
|
|
|
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
2 |
|
* Create a new payment for an order |
137
|
|
|
*/ |
138
|
2 |
|
public function createPayment($gateway) |
139
|
|
|
{ |
140
|
|
|
if (!GatewayInfo::is_supported($gateway)) { |
141
|
|
|
$this->error( |
142
|
|
|
_t( |
143
|
|
|
"PaymentProcessor.INVALID_GATEWAY", |
144
|
|
|
"`{gateway}` isn't a valid payment gateway.", |
145
|
|
|
'gateway is the name of the payment gateway', |
146
|
|
|
array('gateway' => $gateway) |
|
|
|
|
147
|
|
|
) |
148
|
|
|
); |
149
|
2 |
|
return false; |
150
|
|
|
} |
151
|
|
|
if (!$this->order->canPay(Member::currentUser())) { |
152
|
|
|
$this->error(_t("PaymentProcessor.CANTPAY", "Order can't be paid for.")); |
153
|
2 |
|
return false; |
154
|
2 |
|
} |
155
|
2 |
|
$payment = Payment::create() |
156
|
2 |
|
->init($gateway, $this->order->TotalOutstanding(), ShopConfig::get_base_currency()); |
157
|
|
|
$this->order->Payments()->add($payment); |
|
|
|
|
158
|
|
|
return $payment; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Complete payment processing |
163
|
|
|
* - send receipt |
164
|
|
|
* - update order status accordingling |
165
|
|
|
* - fire event hooks |
166
|
|
|
*/ |
167
|
|
|
public function completePayment() |
168
|
|
|
{ |
169
|
|
|
if (!$this->order->Paid) { |
|
|
|
|
170
|
|
|
$this->order->extend('onPayment'); //a payment has been made |
171
|
|
|
//place the order, if not already placed |
172
|
|
|
if ($this->canPlace($this->order)) { |
173
|
|
|
$this->placeOrder(); |
174
|
|
|
} else { |
175
|
|
|
if ($this->order->Locale) { |
|
|
|
|
176
|
|
|
ShopTools::install_locale($this->order->Locale); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ( |
181
|
|
|
// Standard order |
182
|
|
|
($this->order->GrandTotal() > 0 && $this->order->TotalOutstanding() <= 0) |
183
|
|
|
// Zero-dollar order (e.g. paid with loyalty points) |
184
|
|
|
|| ($this->order->GrandTotal() == 0 && Order::config()->allow_zero_order_total) |
185
|
|
|
) { |
186
|
|
|
//set order as paid |
187
|
|
|
$this->order->Status = 'Paid'; |
|
|
|
|
188
|
|
|
$this->order->Paid = SS_Datetime::now()->Rfc2822(); |
|
|
|
|
189
|
|
|
$this->order->write(); |
190
|
|
|
foreach ($this->order->Items() as $item) { |
|
|
|
|
191
|
|
|
$item->onPayment(); |
192
|
|
|
} |
193
|
|
|
//all payment is settled |
194
|
|
|
$this->order->extend('onPaid'); |
195
|
|
|
} |
196
|
|
|
if (!$this->order->ReceiptSent) { |
|
|
|
|
197
|
|
|
$this->notifier->sendReceipt(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Determine if an order can be placed. |
204
|
|
|
* |
205
|
4 |
|
* @param boolean $order |
206
|
|
|
*/ |
207
|
4 |
|
public function canPlace(Order $order) |
208
|
|
|
{ |
209
|
|
|
if (!$order) { |
210
|
|
|
$this->error(_t("OrderProcessor.NULL", "Order does not exist.")); |
211
|
|
|
return false; |
212
|
4 |
|
} |
213
|
1 |
|
//order status is applicable |
214
|
1 |
|
if (!$order->IsCart()) { |
215
|
|
|
$this->error(_t("OrderProcessor.NOTCART", "Order is not a cart.")); |
216
|
|
|
return false; |
217
|
3 |
|
} |
218
|
|
|
//order has products |
219
|
|
|
if ($order->Items()->Count() <= 0) { |
|
|
|
|
220
|
|
|
$this->error(_t("OrderProcessor.NOITEMS", "Order has no items.")); |
221
|
|
|
return false; |
222
|
3 |
|
} |
223
|
|
|
|
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Takes an order from being a cart to awaiting payment. |
229
|
|
|
* |
230
|
|
|
* @param Member $member - assign a member to the order |
|
|
|
|
231
|
|
|
* |
232
|
4 |
|
* @return boolean - success/failure |
233
|
|
|
*/ |
234
|
4 |
|
public function placeOrder() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
if (!$this->order) { |
237
|
|
|
$this->error(_t("OrderProcessor.NULL", "A new order has not yet been started.")); |
238
|
4 |
|
return false; |
239
|
1 |
|
} |
240
|
|
|
if (!$this->canPlace($this->order)) { //final cart validation |
241
|
|
|
return false; |
242
|
3 |
|
} |
243
|
3 |
|
|
244
|
3 |
|
if ($this->order->Locale) { |
|
|
|
|
245
|
|
|
ShopTools::install_locale($this->order->Locale); |
|
|
|
|
246
|
|
|
} |
247
|
3 |
|
|
248
|
3 |
|
//remove from session |
249
|
3 |
|
$cart = ShoppingCart::curr(); |
250
|
3 |
|
if ($cart && $cart->ID == $this->order->ID) { |
251
|
|
|
ShoppingCart::singleton()->clear(); |
252
|
3 |
|
} |
253
|
3 |
|
//update status |
254
|
3 |
|
if ($this->order->TotalOutstanding()) { |
255
|
|
|
$this->order->Status = 'Unpaid'; |
|
|
|
|
256
|
|
|
} else { |
257
|
3 |
|
$this->order->Status = 'Paid'; |
|
|
|
|
258
|
3 |
|
} |
259
|
3 |
|
if (!$this->order->Placed) { |
|
|
|
|
260
|
3 |
|
$this->order->Placed = SS_Datetime::now()->Rfc2822(); //record placed order datetime |
|
|
|
|
261
|
3 |
|
if ($request = Controller::curr()->getRequest()) { |
262
|
3 |
|
$this->order->IPAddress = $request->getIP(); //record client IP |
|
|
|
|
263
|
|
|
} |
264
|
3 |
|
} |
265
|
3 |
|
//re-write all attributes and modifiers to make sure they are up-to-date before they can't be changed again |
266
|
3 |
|
$items = $this->order->Items(); |
|
|
|
|
267
|
3 |
|
if ($items->exists()) { |
268
|
3 |
|
foreach ($items as $item) { |
269
|
3 |
|
$item->onPlacement(); |
270
|
3 |
|
$item->write(); |
271
|
3 |
|
} |
272
|
3 |
|
} |
273
|
|
|
$modifiers = $this->order->Modifiers(); |
|
|
|
|
274
|
|
|
if ($modifiers->exists()) { |
275
|
|
|
foreach ($modifiers as $modifier) { |
276
|
|
|
$modifier->write(); |
277
|
|
|
} |
278
|
3 |
|
} |
279
|
2 |
|
//add member to order & customers group |
280
|
|
|
if ($member = Member::currentUser()) { |
281
|
|
|
if (!$this->order->MemberID) { |
|
|
|
|
282
|
2 |
|
$this->order->MemberID = $member->ID; |
|
|
|
|
283
|
2 |
|
} |
284
|
|
|
$cgroup = ShopConfig::current()->CustomerGroup(); |
285
|
|
|
if ($cgroup->exists()) { |
286
|
2 |
|
$member->Groups()->add($cgroup); |
287
|
|
|
} |
288
|
3 |
|
} |
289
|
3 |
|
//allow decorators to do stuff when order is saved. |
290
|
|
|
$this->order->extend('onPlaceOrder'); |
291
|
|
|
$this->order->write(); |
292
|
|
|
|
293
|
3 |
|
//send confirmation if configured and receipt hasn't been sent |
294
|
3 |
|
if ( |
295
|
3 |
|
self::config()->send_confirmation |
296
|
|
|
&& !$this->order->ReceiptSent |
|
|
|
|
297
|
|
|
) { |
298
|
|
|
$this->notifier->sendConfirmation(); |
299
|
|
|
} |
300
|
3 |
|
|
301
|
|
|
//notify admin, if configured |
302
|
|
|
if (self::config()->send_admin_notification) { |
303
|
|
|
$this->notifier->sendAdminNotification(); |
304
|
|
|
} |
305
|
3 |
|
|
306
|
|
|
// Save order reference to session |
307
|
3 |
|
OrderManipulation::add_session_order($this->order); |
308
|
|
|
|
309
|
|
|
return true; //report success |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return Order |
314
|
|
|
*/ |
315
|
|
|
public function getOrder() |
316
|
|
|
{ |
317
|
|
|
return $this->order; |
318
|
1 |
|
} |
319
|
|
|
|
320
|
1 |
|
public function getError() |
321
|
|
|
{ |
322
|
|
|
return $this->error; |
323
|
6 |
|
} |
324
|
|
|
|
325
|
6 |
|
private function error($message) |
326
|
1 |
|
{ |
327
|
|
|
$this->error = $message; |
328
|
3 |
|
} |
329
|
|
|
|
330
|
3 |
|
public static function config() |
331
|
|
|
{ |
332
|
|
|
return new Config_ForClass("OrderProcessor"); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
1 |
|
* @deprecated 2.0 |
337
|
1 |
|
*/ |
338
|
|
|
public function sendEmail($template, $subject, $copyToAdmin = true) |
339
|
|
|
{ |
340
|
|
|
Deprecation::notice('2.0', 'Use OrderEmailNotifier instead'); |
341
|
|
|
return $this->notifier->sendEmail($template, $subject, $copyToAdmin); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @deprecated 2.0 |
346
|
|
|
*/ |
347
|
|
|
public function sendConfirmation() |
348
|
|
|
{ |
349
|
|
|
Deprecation::notice('2.0', 'Use OrderEmailNotifier instead'); |
350
|
|
|
$this->notifier->sendConfirmation(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @deprecated 2.0 |
355
|
|
|
*/ |
356
|
|
|
public function sendReceipt() |
357
|
|
|
{ |
358
|
|
|
Deprecation::notice('2.0', 'Use OrderEmailNotifier instead'); |
359
|
|
|
$this->notifier->sendReceipt(); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @deprecated 2.0 |
364
|
|
|
*/ |
365
|
|
|
public function sendStatusChange($title, $note = null) |
366
|
|
|
{ |
367
|
|
|
Deprecation::notice('2.0', 'Use OrderEmailNotifier instead'); |
368
|
|
|
$this->notifier->sendStatusChange($title, $note); |
369
|
3 |
|
} |
370
|
|
|
} |
371
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.