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