| Total Complexity | 48 |
| Total Lines | 384 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like pagantisNotify 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 pagantisNotify, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class pagantisNotify |
||
| 27 | { |
||
| 28 | /** @var mixed $pagantisOrder */ |
||
| 29 | protected $pagantisOrder; |
||
| 30 | |||
| 31 | /** @var $string $origin */ |
||
|
|
|||
| 32 | public $origin; |
||
| 33 | |||
| 34 | /** @var $string */ |
||
| 35 | public $order; |
||
| 36 | |||
| 37 | /** @var mixed $oscommerceOrderId */ |
||
| 38 | protected $oscommerceOrderId = ''; |
||
| 39 | |||
| 40 | /** @var mixed $cfg */ |
||
| 41 | protected $cfg; |
||
| 42 | |||
| 43 | /** @var Client $orderClient */ |
||
| 44 | protected $orderClient; |
||
| 45 | |||
| 46 | /** @var Order $oscommerceOrder */ |
||
| 47 | protected $oscommerceOrder; |
||
| 48 | |||
| 49 | /** @var mixed $pagantisOrderId */ |
||
| 50 | protected $pagantisOrderId = ''; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * pagantisNotify constructor. |
||
| 55 | */ |
||
| 56 | public function __construct() |
||
| 57 | { |
||
| 58 | $this->oscommerceOrderId = '7e93cd07dbeba82f6d77965f9bec2c7a'; |
||
| 59 | $this->getPagantisOrderId(); |
||
| 60 | $result = ($this->pagantisOrderId); |
||
| 61 | $result = unserialize($result); |
||
| 62 | foreach ($result as $var => $content) { |
||
| 63 | $GLOBALS[$var] = unserialize($content); |
||
| 64 | tep_session_register($var); |
||
| 65 | } |
||
| 66 | |||
| 67 | header("Location: http://oscommerce-dev.docker:8095/checkout_process.php"); |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | public static function __callStatic($name, $arguments) |
||
| 72 | { |
||
| 73 | // TODO: Implement __callStatic() method. |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Validation vs PagantisClient |
||
| 78 | * |
||
| 79 | * @return array|Array_ |
||
| 80 | * @throws Exception |
||
| 81 | */ |
||
| 82 | public function processInformation() |
||
| 83 | { |
||
| 84 | require_once(__ROOT__.'/pagantis/vendor/autoload.php'); |
||
| 85 | try { |
||
| 86 | $this->checkConcurrency(); |
||
| 87 | $this->getMerchantOrder(); |
||
| 88 | $this->getPagantisOrderId(); |
||
| 89 | $this->getPagantisOrder(); |
||
| 90 | $this->checkOrderStatus(); |
||
| 91 | $this->checkMerchantOrderStatus(); |
||
| 92 | $this->validateAmount(); |
||
| 93 | $this->processMerchantOrder(); |
||
| 94 | } catch (\Exception $exception) { |
||
| 95 | $jsonResponse = new JsonExceptionResponse(); |
||
| 96 | $jsonResponse->setMerchantOrderId($this->oscommerceOrderId); |
||
| 97 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 98 | $jsonResponse->setException($exception); |
||
| 99 | $response = $jsonResponse->toJson(); |
||
| 100 | $this->insertLog($exception); |
||
| 101 | } |
||
| 102 | try { |
||
| 103 | if (!isset($response)) { |
||
| 104 | $this->confirmPagantisOrder(); |
||
| 105 | $jsonResponse = new JsonSuccessResponse(); |
||
| 106 | $jsonResponse->setMerchantOrderId($this->oscommerceOrderId); |
||
| 107 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 108 | } |
||
| 109 | } catch (\Exception $exception) { |
||
| 110 | $this->rollbackMerchantOrder(); |
||
| 111 | $jsonResponse = new JsonExceptionResponse(); |
||
| 112 | $jsonResponse->setMerchantOrderId($this->oscommerceOrderId); |
||
| 113 | $jsonResponse->setPagantisOrderId($this->pagantisOrderId); |
||
| 114 | $jsonResponse->setException($exception); |
||
| 115 | $jsonResponse->toJson(); |
||
| 116 | $this->insertLog($exception); |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
| 120 | $jsonResponse->printResponse(); |
||
| 121 | } else { |
||
| 122 | return $jsonResponse; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * COMMON FUNCTIONS |
||
| 128 | */ |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @throws Exception |
||
| 132 | */ |
||
| 133 | private function checkConcurrency() |
||
| 134 | { |
||
| 135 | $this->getQuoteId(); |
||
| 136 | $this->checkConcurrencyTable(); |
||
| 137 | $this->unblockConcurrency(); |
||
| 138 | $this->blockConcurrency(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @throws MerchantOrderNotFoundException |
||
| 143 | */ |
||
| 144 | private function getMerchantOrder() |
||
| 145 | { |
||
| 146 | if (!isset($this->oscommerceOrder)) { |
||
| 147 | throw new MerchantOrderNotFoundException(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @throws NoIdentificationException |
||
| 153 | */ |
||
| 154 | private function getPagantisOrderId() |
||
| 155 | { |
||
| 156 | $query = "select pmt_order_id from ".TABLE_PAGANTIS_ORDERS." where os_order_id='".$this->oscommerceOrderId."'"; |
||
| 157 | $resultsSelect = tep_db_query($query); |
||
| 158 | while ($orderRow = tep_db_fetch_array($resultsSelect)) { |
||
| 159 | $this->pagantisOrderId = $orderRow['pmt_order_id']; |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($this->pagantisOrderId == '') { |
||
| 163 | throw new NoIdentificationException(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @throws OrderNotFoundException |
||
| 169 | */ |
||
| 170 | private function getPagantisOrder() |
||
| 171 | { |
||
| 172 | try { |
||
| 173 | $publicKey = trim(MODULE_PAYMENT_PAGANTIS_PK); |
||
| 174 | $secretKey = trim(MODULE_PAYMENT_PAGANTIS_SK); |
||
| 175 | $this->orderClient = new \Pagantis\OrdersApiClient\Client($publicKey, $secretKey); |
||
| 176 | $this->pagantisOrder = $this->orderClient->getOrder($this->pagantisOrderId); |
||
| 177 | } catch (\Exception $e) { |
||
| 178 | throw new OrderNotFoundException(); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @throws AlreadyProcessedException |
||
| 184 | * @throws WrongStatusException |
||
| 185 | */ |
||
| 186 | private function checkOrderStatus() |
||
| 187 | { |
||
| 188 | try { |
||
| 189 | $this->checkPagantisStatus(array('AUTHORIZED')); |
||
| 190 | } catch (\Exception $e) { |
||
| 191 | if ($this->oscommerceOrderId!='') { |
||
| 192 | throw new AlreadyProcessedException(); |
||
| 193 | } else { |
||
| 194 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 195 | $status = $this->pagantisOrder->getStatus(); |
||
| 196 | } else { |
||
| 197 | $status = '-'; |
||
| 198 | } |
||
| 199 | throw new WrongStatusException($status); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @throws AlreadyProcessedException |
||
| 206 | */ |
||
| 207 | private function checkMerchantOrderStatus() |
||
| 208 | { |
||
| 209 | if (!$this->oscommerceOrder->info['order_status']!=='1') { |
||
| 210 | throw new AlreadyProcessedException(); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @throws AmountMismatchException |
||
| 216 | */ |
||
| 217 | private function validateAmount() |
||
| 218 | { |
||
| 219 | $pagantisAmount = $this->pagantisOrder->getShoppingCart()->getTotalAmount(); |
||
| 220 | $wcAmount = intval(strval(100 * $this->oscommerceOrder->get_total())); |
||
| 221 | if ($pagantisAmount != $wcAmount) { |
||
| 222 | throw new AmountMismatchException($pagantisAmount, $wcAmount); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @throws Exception |
||
| 228 | */ |
||
| 229 | private function processMerchantOrder() |
||
| 230 | { |
||
| 231 | $this->saveOrder(); |
||
| 232 | $this->updateBdInfo(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return false|string |
||
| 237 | * @throws UnknownException |
||
| 238 | */ |
||
| 239 | private function confirmPagantisOrder() |
||
| 240 | { |
||
| 241 | try { |
||
| 242 | $this->pagantisOrder = $this->orderClient->confirmOrder($this->pagantisOrderId); |
||
| 243 | } catch (\Exception $e) { |
||
| 244 | throw new UnknownException($e->getMessage()); |
||
| 245 | } |
||
| 246 | |||
| 247 | $jsonResponse = new JsonSuccessResponse(); |
||
| 248 | return $jsonResponse->toJson(); |
||
| 249 | } |
||
| 250 | /** |
||
| 251 | * UTILS FUNCTIONS |
||
| 252 | */ |
||
| 253 | |||
| 254 | /** STEP 1 CC - Check concurrency */ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @throws QuoteNotFoundException |
||
| 258 | */ |
||
| 259 | private function getQuoteId() |
||
| 260 | { |
||
| 261 | $this->oscommerceOrderId = $_GET['order_id']; |
||
| 262 | if ($this->oscommerceOrderId == '') { |
||
| 263 | throw new QuoteNotFoundException(); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Check if concurrency table exists |
||
| 269 | */ |
||
| 270 | private function checkConcurrencyTable() |
||
| 271 | { |
||
| 272 | $checkTable = tep_db_query("SHOW TABLES LIKE '".TABLE_PAGANTIS_CONCURRENCY."'"); |
||
| 273 | if (tep_db_num_rows($checkTable) == 0) { |
||
| 274 | $sql = "CREATE TABLE IF NOT EXISTS ".TABLE_PAGANTIS_CONCURRENCY." ( |
||
| 275 | id int NOT NULL, |
||
| 276 | `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||
| 277 | UNIQUE KEY id(id))"; |
||
| 278 | tep_db_query($sql); |
||
| 279 | } |
||
| 280 | return; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Unlock the concurrency |
||
| 285 | * |
||
| 286 | * @param null $orderId |
||
| 287 | * @throws Exception |
||
| 288 | */ |
||
| 289 | private function unblockConcurrency($orderId = null) |
||
| 290 | { |
||
| 291 | try { |
||
| 292 | if ($orderId == null) { |
||
| 293 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where timestamp<".(time() - 5); |
||
| 294 | tep_db_query($query); |
||
| 295 | } elseif ($this->$orderId!='') { |
||
| 296 | $query = "delete from ".TABLE_PAGANTIS_CONCURRENCY." where id=".$this->$orderId; |
||
| 297 | tep_db_query($query); |
||
| 298 | } |
||
| 299 | } catch (Exception $exception) { |
||
| 300 | throw new ConcurrencyException(); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @throws \Exception |
||
| 306 | */ |
||
| 307 | private function blockConcurrency() |
||
| 308 | { |
||
| 309 | try { |
||
| 310 | $query = "INSERT INTO ".TABLE_PAGANTIS_CONCURRENCY." (id) VALUES ('$this->oscommerceOrderId')"; |
||
| 311 | tep_db_query($query); |
||
| 312 | } catch (Exception $exception) { |
||
| 313 | throw new ConcurrencyException(); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** STEP 2 GMO - Get Merchant Order */ |
||
| 318 | /** STEP 3 GPOI - Get Pagantis OrderId */ |
||
| 319 | /** STEP 4 GPO - Get Pagantis Order */ |
||
| 320 | /** STEP 5 COS - Check Order Status */ |
||
| 321 | /** |
||
| 322 | * @param $statusArray |
||
| 323 | * |
||
| 324 | * @throws \Exception |
||
| 325 | */ |
||
| 326 | private function checkPagantisStatus($statusArray) |
||
| 327 | { |
||
| 328 | $pagantisStatus = array(); |
||
| 329 | foreach ($statusArray as $status) { |
||
| 330 | $pagantisStatus[] = constant("\Pagantis\OrdersApiClient\Model\Order::STATUS_$status"); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 334 | $payed = in_array($this->pagantisOrder->getStatus(), $pagantisStatus); |
||
| 335 | if (!$payed) { |
||
| 336 | if ($this->pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 337 | $status = $this->pagantisOrder->getStatus(); |
||
| 338 | } else { |
||
| 339 | $status = '-'; |
||
| 340 | } |
||
| 341 | throw new WrongStatusException($status); |
||
| 342 | } |
||
| 343 | } else { |
||
| 344 | throw new OrderNotFoundException(); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | /** STEP 6 CMOS - Check Merchant Order Status */ |
||
| 348 | /** STEP 7 VA - Validate Amount */ |
||
| 349 | /** STEP 8 PMO - Process Merchant Order */ |
||
| 350 | /** |
||
| 351 | * @throws \Exception |
||
| 352 | */ |
||
| 353 | private function saveOrder() |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Save the merchant order_id with the related identification |
||
| 371 | */ |
||
| 372 | private function updateBdInfo() |
||
| 373 | { |
||
| 374 | global $wpdb; |
||
| 375 | |||
| 376 | $this->checkDbTable(); |
||
| 377 | $tableName = $wpdb->prefix.self::ORDERS_TABLE; |
||
| 378 | |||
| 379 | $wpdb->update( |
||
| 380 | $tableName, |
||
| 381 | array('wc_order_id'=>$this->oscommerceOrderId), |
||
| 382 | array('id' => $this->oscommerceOrderId), |
||
| 383 | array('%s'), |
||
| 384 | array('%d') |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** STEP 9 CPO - Confirmation Pagantis Order */ |
||
| 389 | private function rollbackMerchantOrder() |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $exceptionMessage |
||
| 396 | * |
||
| 397 | * @throws \Zend_Db_Exception |
||
| 398 | */ |
||
| 399 | private function insertLog($exception) |
||
| 410 | } |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | $pgNotify = new pagantisNotify(); |
||
| 415 | $pgNotify->processInformation(); |