Total Complexity | 69 |
Total Lines | 587 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like WcPagantisNotify often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WcPagantisNotify, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class WcPagantisNotify extends WcPagantisGateway |
||
23 | { |
||
24 | /** Concurrency tablename */ |
||
25 | const CONCURRENCY_TABLE = 'pagantis_concurrency'; |
||
26 | |||
27 | /** Seconds to expire a locked request */ |
||
28 | const CONCURRENCY_TIMEOUT = 5; |
||
29 | |||
30 | /** @var mixed $pagantisOrder */ |
||
31 | protected $pagantisOrder; |
||
32 | |||
33 | /** @var $string $origin */ |
||
|
|||
34 | public $origin; |
||
35 | |||
36 | /** @var $string */ |
||
37 | public $order; |
||
38 | |||
39 | /** @var mixed $woocommerceOrderId */ |
||
40 | protected $woocommerceOrderId = ''; |
||
41 | |||
42 | /** @var mixed $cfg */ |
||
43 | protected $cfg; |
||
44 | |||
45 | /** @var Client $orderClient */ |
||
46 | protected $orderClient; |
||
47 | |||
48 | /** @var WC_Order $woocommerceOrder */ |
||
49 | protected $woocommerceOrder; |
||
50 | |||
51 | /** @var mixed $pagantisOrderId */ |
||
52 | protected $pagantisOrderId = ''; |
||
53 | |||
54 | /** @var $string */ |
||
55 | protected $product; |
||
56 | |||
57 | /** @var $string */ |
||
58 | protected $urlTokenVerification = null; |
||
59 | |||
60 | /** |
||
61 | * Validation vs PagantisClient |
||
62 | * |
||
63 | * @return JsonExceptionResponse|JsonSuccessResponse |
||
64 | * @throws ConcurrencyException |
||
65 | */ |
||
66 | public function processInformation() |
||
67 | { |
||
68 | try { |
||
69 | require_once(__ROOT__.'/vendor/autoload.php'); |
||
70 | try { |
||
71 | if ($_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['origin'] == 'notification') { |
||
72 | return $this->buildResponse(); |
||
73 | } |
||
74 | |||
75 | |||
76 | $this->checkConcurrency(); |
||
77 | $this->getProductType(); |
||
78 | $this->getMerchantOrder(); |
||
79 | $this->getPagantisOrderId(); |
||
80 | $this->getPagantisOrder(); |
||
81 | $checkAlreadyProcessed = $this->checkOrderStatus(); |
||
82 | if ($checkAlreadyProcessed) { |
||
83 | return $this->buildResponse(); |
||
84 | } |
||
85 | $this->validateAmount(); |
||
86 | if ($this->checkMerchantOrderStatus()) { |
||
87 | $this->processMerchantOrder(); |
||
88 | } |
||
89 | } catch (\Exception $exception) { |
||
90 | $this->insertLog($exception); |
||
91 | |||
92 | return $this->buildResponse($exception); |
||
93 | } |
||
94 | |||
95 | try { |
||
96 | $this->confirmPagantisOrder(); |
||
97 | |||
98 | return $this->buildResponse(); |
||
99 | } catch (\Exception $exception) { |
||
100 | $this->rollbackMerchantOrder(); |
||
101 | $this->insertLog($exception); |
||
102 | |||
103 | return $this->buildResponse($exception); |
||
104 | } |
||
105 | } catch (\Exception $exception) { |
||
106 | $this->insertLog($exception); |
||
107 | return $this->buildResponse($exception); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * COMMON FUNCTIONS |
||
113 | */ |
||
114 | |||
115 | /** |
||
116 | * @throws ConcurrencyException |
||
117 | * @throws QuoteNotFoundException |
||
118 | */ |
||
119 | private function checkConcurrency() |
||
120 | { |
||
121 | $this->woocommerceOrderId = $_GET['order-received']; |
||
122 | if ($this->woocommerceOrderId == '') { |
||
123 | throw new QuoteNotFoundException(); |
||
124 | } |
||
125 | |||
126 | $this->unblockConcurrency(); |
||
127 | $this->blockConcurrency($this->woocommerceOrderId); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * getProductType |
||
132 | */ |
||
133 | private function getProductType() |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @throws MerchantOrderNotFoundException |
||
144 | */ |
||
145 | private function getMerchantOrder() |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * |
||
157 | * @throws NoIdentificationException |
||
158 | */ |
||
159 | private function getPagantisOrderId() |
||
160 | { |
||
161 | global $wpdb; |
||
162 | |||
163 | $this->setUrlToken(); |
||
164 | |||
165 | $this->checkDbTable(); |
||
166 | $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; |
||
167 | $order_id = $wpdb->get_var("SELECT order_id FROM $tableName WHERE token='{$this->getUrlToken()}' "); |
||
168 | $this->pagantisOrderId = $order_id; |
||
169 | |||
170 | if ($this->pagantisOrderId == '') { |
||
171 | throw new NoIdentificationException(); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @throws OrderNotFoundException |
||
177 | */ |
||
178 | private function getPagantisOrder() |
||
179 | { |
||
180 | try { |
||
181 | $this->cfg = get_option('woocommerce_pagantis_settings'); |
||
182 | $this->cfg = get_option('woocommerce_pagantis_settings'); |
||
183 | if ($this->isProduct4x()) { |
||
184 | $publicKey = $this->cfg['pagantis_public_key_4x']; |
||
185 | $secretKey = $this->cfg['pagantis_private_key_4x']; |
||
186 | } else { |
||
187 | $publicKey = $this->cfg['pagantis_public_key']; |
||
188 | $secretKey = $this->cfg['pagantis_private_key']; |
||
189 | } |
||
190 | |||
191 | $this->orderClient = new Client($publicKey, $secretKey); |
||
192 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
193 | } catch (\Exception $e) { |
||
194 | throw new OrderNotFoundException(); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | * @throws WrongStatusException |
||
201 | */ |
||
202 | private function checkOrderStatus() |
||
203 | { |
||
204 | try { |
||
205 | $this->checkPagantisStatus(array('AUTHORIZED')); |
||
206 | } catch (\Exception $e) { |
||
207 | if ($this->pagantisOrder instanceof Order) { |
||
208 | $status = $this->pagantisOrder->getStatus(); |
||
209 | } else { |
||
210 | $status = '-'; |
||
211 | } |
||
212 | |||
213 | if ($status === Order::STATUS_CONFIRMED) { |
||
214 | return true; |
||
215 | } |
||
216 | throw new WrongStatusException($status); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return bool |
||
222 | */ |
||
223 | private function checkMerchantOrderStatus() |
||
224 | { |
||
225 | //Order status reference => https://docs.woocommerce.com/document/managing-orders/ |
||
226 | $validStatus = array('on-hold', 'pending', 'failed', 'processing', 'completed'); |
||
227 | $isValidStatus = apply_filters( |
||
228 | 'woocommerce_valid_order_statuses_for_payment_complete', |
||
229 | $validStatus, |
||
230 | $this |
||
231 | ); |
||
232 | |||
233 | if (!$this->woocommerceOrder->has_status($isValidStatus)) { // TO CONFIRM |
||
234 | $logMessage = "WARNING checkMerchantOrderStatus." . |
||
235 | " Merchant order id:".$this->woocommerceOrder->get_id(). |
||
236 | " Merchant order status:".$this->woocommerceOrder->get_status(). |
||
237 | " Pagantis order id:".$this->pagantisOrder->getStatus(). |
||
238 | " Pagantis order status:".$this->pagantisOrder->getId(); |
||
239 | |||
240 | $this->insertLog(null, $logMessage); |
||
241 | $this->woocommerceOrder->add_order_note($logMessage); |
||
242 | $this->woocommerceOrder->save(); |
||
243 | return false; |
||
244 | } |
||
245 | |||
246 | return true; //TO SAVE |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @throws AmountMismatchException |
||
251 | */ |
||
252 | private function validateAmount() |
||
253 | { |
||
254 | $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); |
||
255 | $wcAmount = intval(strval(100 * $this->woocommerceOrder->get_total())); |
||
256 | if ($pagantisAmount != $wcAmount) { |
||
257 | throw new AmountMismatchException($pagantisAmount, $wcAmount); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @throws Exception |
||
263 | */ |
||
264 | private function processMerchantOrder() |
||
265 | { |
||
266 | $this->saveOrder(); |
||
267 | $this->updateBdInfo(); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return false|string |
||
272 | * @throws UnknownException |
||
273 | */ |
||
274 | private function confirmPagantisOrder() |
||
275 | { |
||
276 | try { |
||
277 | $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
278 | } catch (\Exception $e) { |
||
279 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
280 | if ($this->pagantisOrder->getStatus() !== Order::STATUS_CONFIRMED) { |
||
281 | throw new UnknownException($e->getMessage()); |
||
282 | } else { |
||
283 | $logMessage = 'Concurrency issue: Order_id '.$this->pagantisOrderId.' was confirmed by other process'; |
||
284 | $this->insertLog(null, $logMessage); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | $jsonResponse = new JsonSuccessResponse(); |
||
289 | return $jsonResponse->toJson(); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * UTILS FUNCTIONS |
||
294 | */ |
||
295 | /** STEP 1 CC - Check concurrency */ |
||
296 | /** |
||
297 | * Check if orders table exists |
||
298 | */ |
||
299 | private function checkDbTable() |
||
300 | { |
||
301 | global $wpdb; |
||
302 | $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; |
||
303 | |||
304 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { |
||
305 | $charset_collate = $wpdb->get_charset_collate(); |
||
306 | $sql= "CREATE TABLE IF NOT EXISTS $tableName |
||
307 | (id INT, |
||
308 | order_id varchar(60), |
||
309 | wc_order_id varchar(60), |
||
310 | token varchar(32) NOT NULL, |
||
311 | ADD PRIMARY KEY (id,order_id) |
||
312 | )$charset_collate"; |
||
313 | |||
314 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
315 | dbDelta($sql); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Check if logs table exists |
||
321 | */ |
||
322 | private function checkDbLogTable() |
||
323 | { |
||
324 | global $wpdb; |
||
325 | $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; |
||
326 | |||
327 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { |
||
328 | $charset_collate = $wpdb->get_charset_collate(); |
||
329 | $sql = "CREATE TABLE $tableName ( id int NOT NULL AUTO_INCREMENT, log text NOT NULL, |
||
330 | createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (id)) $charset_collate"; |
||
331 | |||
332 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
333 | dbDelta($sql); |
||
334 | } |
||
335 | return; |
||
336 | } |
||
337 | |||
338 | /** STEP 2 GMO - Get Merchant Order */ |
||
339 | /** STEP 3 GPOI - Get Pagantis OrderId */ |
||
340 | /** STEP 4 GPO - Get Pagantis Order */ |
||
341 | /** STEP 5 COS - Check Order Status */ |
||
342 | |||
343 | /** |
||
344 | * @param $statusArray |
||
345 | * |
||
346 | * @throws \Exception |
||
347 | */ |
||
348 | private function checkPagantisStatus($statusArray) |
||
367 | } |
||
368 | } |
||
369 | |||
370 | /** STEP 6 CMOS - Check Merchant Order Status */ |
||
371 | /** STEP 7 VA - Validate Amount */ |
||
372 | /** STEP 8 PMO - Process Merchant Order */ |
||
373 | /** |
||
374 | * @throws \Exception |
||
375 | */ |
||
376 | private function saveOrder() |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Save the merchant order_id with the related identification |
||
406 | */ |
||
407 | private function updateBdInfo() |
||
408 | { |
||
409 | global $wpdb; |
||
410 | |||
411 | $this->checkDbTable(); |
||
412 | $tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE; |
||
413 | |||
414 | $wpdb->update( |
||
415 | $tableName, |
||
416 | array('wc_order_id'=>$this->woocommerceOrderId), |
||
417 | array('token' => $this->getUrlToken(),'order_id' => $this->pagantisOrderId), |
||
418 | array( '%s'), |
||
419 | array( '%s', '%s' ) |
||
420 | ); |
||
421 | } |
||
422 | |||
423 | /** STEP 9 CPO - Confirmation Pagantis Order */ |
||
424 | private function rollbackMerchantOrder() |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @param null $exception |
||
431 | * @param null $message |
||
432 | */ |
||
433 | private function insertLog($exception = null, $message = null) |
||
434 | { |
||
435 | global $wpdb; |
||
436 | |||
437 | $this->checkDbLogTable(); |
||
438 | $logEntry = new LogEntry(); |
||
439 | if ($exception instanceof \Exception) { |
||
440 | $logEntry = $logEntry->error($exception); |
||
441 | } else { |
||
442 | $logEntry = $logEntry->info($message); |
||
443 | } |
||
444 | |||
445 | $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; |
||
446 | $wpdb->insert($tableName, array('log' => $logEntry->toJson())); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param null $orderId |
||
451 | * |
||
452 | * @throws ConcurrencyException |
||
453 | */ |
||
454 | private function unblockConcurrency($orderId = null) |
||
455 | { |
||
456 | global $wpdb; |
||
457 | $tableName = $wpdb->prefix.PG_CONCURRENCY_TABLE_NAME; |
||
458 | if ($orderId == null) { |
||
459 | $query = "DELETE FROM $tableName WHERE createdAt<(NOW()- INTERVAL ".self::CONCURRENCY_TIMEOUT." SECOND)"; |
||
460 | } else { |
||
461 | $query = "DELETE FROM $tableName WHERE order_id = $orderId"; |
||
462 | } |
||
463 | $resultDelete = $wpdb->query($query); |
||
464 | if ($resultDelete === false) { |
||
465 | throw new ConcurrencyException(); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @param $orderId |
||
471 | * |
||
472 | * @throws ConcurrencyException |
||
473 | */ |
||
474 | private function blockConcurrency($orderId) |
||
475 | { |
||
476 | global $wpdb; |
||
477 | $tableName = $wpdb->prefix.PG_CONCURRENCY_TABLE_NAME; |
||
478 | $insertResult = $wpdb->insert($tableName, array('order_id' => $orderId)); |
||
479 | if ($insertResult === false) { |
||
480 | if ($this->getOrigin() == 'Notify') { |
||
481 | throw new ConcurrencyException(); |
||
482 | } else { |
||
483 | $query = sprintf( |
||
484 | "SELECT TIMESTAMPDIFF(SECOND,NOW()-INTERVAL %s SECOND, createdAt) as rest FROM %s WHERE %s", |
||
485 | self::CONCURRENCY_TIMEOUT, |
||
486 | $tableName, |
||
487 | "order_id=$orderId" |
||
488 | ); |
||
489 | $resultSeconds = $wpdb->get_row($query); |
||
490 | $restSeconds = isset($resultSeconds) ? ($resultSeconds->rest) : 0; |
||
491 | $secondsToExpire = ($restSeconds>self::CONCURRENCY_TIMEOUT) ? self::CONCURRENCY_TIMEOUT : $restSeconds; |
||
492 | sleep($secondsToExpire+1); |
||
493 | |||
494 | $logMessage = sprintf( |
||
495 | "User waiting %s seconds, default seconds %s, bd time to expire %s seconds", |
||
496 | $secondsToExpire, |
||
497 | self::CONCURRENCY_TIMEOUT, |
||
498 | $restSeconds |
||
499 | ); |
||
500 | $this->insertLog(null, $logMessage); |
||
501 | } |
||
502 | } |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @param null $exception |
||
507 | * |
||
508 | * |
||
509 | * @return JsonExceptionResponse|JsonSuccessResponse |
||
510 | * @throws ConcurrencyException |
||
511 | */ |
||
512 | private function buildResponse($exception = null) |
||
513 | { |
||
514 | $this->unblockConcurrency($this->woocommerceOrderId); |
||
515 | |||
516 | if ($exception == null) { |
||
517 | $jsonResponse = new JsonSuccessResponse(); |
||
518 | } else { |
||
519 | $jsonResponse = new JsonExceptionResponse(); |
||
520 | $jsonResponse->setException($exception); |
||
521 | } |
||
522 | |||
523 | $jsonResponse->setMerchantOrderId($this->woocommerceOrderId); |
||
524 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
525 | |||
526 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
527 | $jsonResponse->printResponse(); |
||
528 | } else { |
||
529 | return $jsonResponse; |
||
530 | } |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * GETTERS & SETTERS |
||
535 | */ |
||
536 | |||
537 | /** |
||
538 | * @return mixed |
||
539 | */ |
||
540 | public function getOrigin() |
||
541 | { |
||
542 | return $this->origin; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @param mixed $origin |
||
547 | */ |
||
548 | public function setOrigin($origin) |
||
549 | { |
||
550 | $this->origin = $origin; |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * @return bool |
||
555 | */ |
||
556 | private function isProduct4x() |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * @return mixed |
||
563 | */ |
||
564 | public function getProduct() |
||
565 | { |
||
566 | return $this->product; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * @param mixed $product |
||
571 | */ |
||
572 | public function setProduct($product) |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * @return mixed |
||
579 | */ |
||
580 | public function getWoocommerceOrderId() |
||
581 | { |
||
582 | return $this->woocommerceOrderId; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * @return mixed |
||
587 | */ |
||
588 | private function getUrlToken() |
||
589 | { |
||
590 | return $this->urlTokenVerification; |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * |
||
595 | */ |
||
596 | private function setUrlToken() |
||
609 | } |
||
610 | |||
611 | } |
||
612 |