| Total Complexity | 54 |
| Total Lines | 671 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like pagantis 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 pagantis, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class pagantis |
||
| 14 | { |
||
| 15 | /** @var String $is_guest */ |
||
| 16 | public $is_guest; |
||
| 17 | |||
| 18 | /** @var Array $extraConfig */ |
||
| 19 | public $extraConfig; |
||
| 20 | |||
| 21 | /** @var String $form_action_url */ |
||
| 22 | public $form_action_url; |
||
| 23 | |||
| 24 | /** @var String $base_url */ |
||
| 25 | public $base_url; |
||
| 26 | |||
| 27 | /** @var String $os_order_reference */ |
||
| 28 | public $os_order_reference; |
||
| 29 | |||
| 30 | /** @var notifyController $pgNotify */ |
||
| 31 | public $pgNotify; |
||
| 32 | |||
| 33 | public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing', |
||
| 34 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE', |
||
| 35 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE', |
||
| 36 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
| 37 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
||
| 38 | 'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
| 39 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
| 40 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER', |
||
| 41 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default', |
||
| 42 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default', |
||
| 43 | 'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
||
| 44 | 'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
||
| 45 | 'PAGANTIS_URL_OK'=>'', |
||
| 46 | 'PAGANTIS_URL_KO'=>'', |
||
| 47 | 'PAGANTIS_TITLE_EXTRA' => 'Paga hasta en 12 cómodas cuotas con Paga+Tarde. Solicitud totalmente online y sin papeleos,¡y la respuesta es inmediata!', |
||
| 48 | 'PAGANTIS_PROMOTION' => '' |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | */ |
||
| 54 | public function __construct() |
||
| 55 | { |
||
| 56 | $this->version = '8.0.0'; |
||
|
|
|||
| 57 | $this->code = 'pagantis'; |
||
| 58 | $this->sort_order = 0; |
||
| 59 | $this->description = $this->getDescription(); |
||
| 60 | |||
| 61 | if (strpos($_SERVER[REQUEST_URI], "checkout_payment.php") <= 0) { |
||
| 62 | $this->title = MODULE_PAYMENT_PAGANTIS_TEXT_ADMIN_TITLE; // Payment module title in Admin |
||
| 63 | } else { |
||
| 64 | $this->title = $this->extraConfig['PAGANTIS_TITLE']; // Payment module title in Catalog |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->enabled = ((MODULE_PAYMENT_PAGANTIS_STATUS == 'True') ? true : false); |
||
| 68 | |||
| 69 | $this->extraConfig = $this->getExtraConfig(); |
||
| 70 | |||
| 71 | $this->base_url = dirname( |
||
| 72 | sprintf( |
||
| 73 | "%s://%s%s%s", |
||
| 74 | isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', |
||
| 75 | $_SERVER['SERVER_NAME'], |
||
| 76 | isset($_SERVER['SERVER_PORT']) ? ":" . $_SERVER['SERVER_PORT'] : '', |
||
| 77 | $_SERVER['REQUEST_URI'] |
||
| 78 | ) |
||
| 79 | ); |
||
| 80 | |||
| 81 | $this->form_action_url = $this->base_url . '/ext/modules/payment/pagantis/bypass.php'; |
||
| 82 | } |
||
| 83 | |||
| 84 | /*************** |
||
| 85 | * |
||
| 86 | * CLASS METHODS |
||
| 87 | * |
||
| 88 | **************/ |
||
| 89 | /** |
||
| 90 | * Here you can implement using payment zones (refer to standard PayPal module as reference) |
||
| 91 | */ |
||
| 92 | public function update_status() |
||
| 93 | { |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | /* |
||
| 98 | * Here you may define client side javascript that will verify any input fields you use in the payment method |
||
| 99 | * selection page. Refer to standard cc module as reference (cc.php). |
||
| 100 | */ |
||
| 101 | public function javascript_validation() |
||
| 102 | { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | /* |
||
| 107 | * Llamada cuando el usuario esta en la pantalla de eleccion de tipo de pago |
||
| 108 | * This function outputs the payment method title/text and if required, the input fields. |
||
| 109 | * |
||
| 110 | * Si hay un pedido generado previamente y no confirmado, se borra |
||
| 111 | * Caso de uso: |
||
| 112 | * - el usuario llega a la pantalla de confirmacion |
||
| 113 | * - se genera el pedido (pero no se genera entrada en orders_status_history) |
||
| 114 | * - el usuario decide realizar algun cambio en su compra antes de pasar a pagantis |
||
| 115 | * - entra de nuevo en la pantalla de seleccion de tipo de pago (puede elegir otra forma de pago) |
||
| 116 | * - se comprueba que no exista el pedido generado anteriormente |
||
| 117 | * - se borra el pedido que se habia generado inicialmente. Ya no es valido |
||
| 118 | * |
||
| 119 | */ |
||
| 120 | public function selection() |
||
| 121 | { |
||
| 122 | return array('id' => $this->code, 'module' => $this->title); |
||
| 123 | } |
||
| 124 | |||
| 125 | /* |
||
| 126 | * Use this function implement any checks of any conditions after payment method has been selected. You most probably |
||
| 127 | * don't need to implement anything here. |
||
| 128 | */ |
||
| 129 | public function pre_confirmation_check() |
||
| 132 | } |
||
| 133 | |||
| 134 | /* |
||
| 135 | * Implement any checks or processing on the order information before proceeding to payment confirmation. You most |
||
| 136 | probably don't need to implement anything here. |
||
| 137 | * Llamada cuando el usuario entra en la pantalla de confirmacion |
||
| 138 | * |
||
| 139 | * Se genera el pedido: |
||
| 140 | * - con el estado predefinido para el modulo pagantis |
||
| 141 | * - sin notificacion a cliente ni administrador |
||
| 142 | * - no se borra el carrito asociado al pedido |
||
| 143 | * |
||
| 144 | */ |
||
| 145 | public function confirmation() |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Build the data and actions to process when the "Submit" button is pressed on the order-confirmation screen. |
||
| 152 | * This sends the data to the payment gateway for processing. |
||
| 153 | * (These are hidden fields on the checkout confirmation page) |
||
| 154 | */ |
||
| 155 | public function process_button() |
||
| 156 | { |
||
| 157 | try { |
||
| 158 | include_once('./ext/modules/payment/pagantis/vendor/autoload.php'); |
||
| 159 | global $order, $customer_id, $sendto, $billto, $cart, $languages_id, $currency, $currencies, $shipping, |
||
| 160 | $payment, $comments, $customer_default_address_id, $cartID; |
||
| 161 | $global_vars = array(); |
||
| 162 | $global_vars['customer_id'] = serialize($customer_id); |
||
| 163 | $global_vars['sendTo'] = serialize($sendto); |
||
| 164 | $global_vars['billTo'] = serialize($billto); |
||
| 165 | $global_vars['cart'] = serialize($cart); |
||
| 166 | $global_vars['languages_id'] = serialize($languages_id); |
||
| 167 | $global_vars['currency'] = serialize($currency); |
||
| 168 | $global_vars['currencies'] = serialize($currencies); |
||
| 169 | $global_vars['shipping'] = serialize($shipping); |
||
| 170 | $global_vars['payment'] = serialize($payment); |
||
| 171 | $global_vars['comments'] = serialize($comments); |
||
| 172 | $global_vars['$customer_default_address_id'] = serialize($customer_default_address_id); |
||
| 173 | $global_vars['cartID'] = serialize($cartID); |
||
| 174 | $global_vars['sessiontoken'] = serialize($_SESSION['sessiontoken']); |
||
| 175 | |||
| 176 | if (!isset($order)) { |
||
| 177 | throw new UnknownException("Order not found"); |
||
| 178 | } |
||
| 179 | |||
| 180 | $id_hash = time() . serialize($order->products) . '' . serialize($order->customer) . '' . serialize($order->delivery); |
||
| 181 | $this->os_order_reference = md5($id_hash); |
||
| 182 | $_SESSION['order_id'] = $this->os_order_reference; |
||
| 183 | |||
| 184 | $userAddress = new Address(); |
||
| 185 | $userAddress |
||
| 186 | ->setZipCode($order->billing['postcode']) |
||
| 187 | ->setFullName($order->billing['firstname'] . ' ' . $order->billing['lastname']) |
||
| 188 | ->setCountryCode('ES') |
||
| 189 | ->setCity($order->billing['city']) |
||
| 190 | ->setAddress($order->billing['street_address']) |
||
| 191 | ->setFixPhone($order->customer['telephone']) |
||
| 192 | ->setMobilePhone($order->customer['telephone']); |
||
| 193 | |||
| 194 | $orderBillingAddress = $userAddress; |
||
| 195 | |||
| 196 | $orderShippingAddress = new Address(); |
||
| 197 | $orderShippingAddress |
||
| 198 | ->setZipCode($order->delivery['postcode']) |
||
| 199 | ->setFullName($order->billing['firstname'] . ' ' . $order->billing['lastname']) |
||
| 200 | ->setCountryCode('ES') |
||
| 201 | ->setCity($order->delivery['city']) |
||
| 202 | ->setAddress($order->delivery['street_address']) |
||
| 203 | ->setFixPhone($order->customer['telephone']) |
||
| 204 | ->setMobilePhone($order->customer['telephone']); |
||
| 205 | |||
| 206 | $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
||
| 207 | $orderUser |
||
| 208 | ->setAddress($userAddress) |
||
| 209 | ->setFullName($order->billing['firstname'] . ' ' . $order->billing['lastname']) |
||
| 210 | ->setBillingAddress($orderBillingAddress) |
||
| 211 | ->setEmail($order->customer['email_address']) |
||
| 212 | ->setFixPhone($order->customer['telephone']) |
||
| 213 | ->setMobilePhone($order->customer['telephone']) |
||
| 214 | ->setShippingAddress($orderShippingAddress); |
||
| 215 | |||
| 216 | $previousOrders = $this->getOrders(); |
||
| 217 | foreach ((array)$previousOrders as $k => $previousOrder) { |
||
| 218 | $orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
| 219 | $orderHistory |
||
| 220 | ->setAmount(intval(100 * $previousOrder['value'])) |
||
| 221 | ->setDate(new \DateTime($previousOrder['date_purchased'])); |
||
| 222 | $orderUser->addOrderHistory($orderHistory); |
||
| 223 | } |
||
| 224 | |||
| 225 | $details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
| 226 | $shippingCost = number_format($order->info['shipping_cost'], 2, '.', ''); |
||
| 227 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
| 228 | |||
| 229 | $promotedAmount = 0; |
||
| 230 | foreach ($order->products as $item) { |
||
| 231 | $promotedProduct = $this->isPromoted($item); |
||
| 232 | $product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
| 233 | $product |
||
| 234 | ->setAmount(intval(100 * number_format(($item['final_price'] * $item['qty']), 2))) |
||
| 235 | ->setQuantity(intval($item['qty'])) |
||
| 236 | ->setDescription($item['name']); |
||
| 237 | if ($promotedProduct) { |
||
| 238 | $promotedAmount+=$product->getAmount(); |
||
| 239 | } |
||
| 240 | $details->addProduct($product); |
||
| 241 | } |
||
| 242 | |||
| 243 | $orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
||
| 244 | $orderShoppingCart |
||
| 245 | ->setDetails($details) |
||
| 246 | ->setOrderReference($this->os_order_reference) |
||
| 247 | ->setPromotedAmount(0) |
||
| 248 | ->setTotalAmount(intval($order->info['total'] * 100)) |
||
| 249 | ->setPromotedAmount($promotedAmount); |
||
| 250 | |||
| 251 | $callback_url = $this->base_url.'/ext/modules/payment/pagantis/notify.php'; |
||
| 252 | $checkoutProcessUrl = htmlspecialchars_decode( |
||
| 253 | tep_href_link(FILENAME_CHECKOUT_PROCESS, "order_id=$this->os_order_reference&from=order", 'SSL', true) |
||
| 254 | ); |
||
| 255 | |||
| 256 | $cancelUrl = trim(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL', false)); |
||
| 257 | if ($this->extraConfig['PAGANTIS_URL_KO']!='') { |
||
| 258 | $koUrl = $this->extraConfig['PAGANTIS_URL_KO']; |
||
| 259 | } else { |
||
| 260 | $koUrl = $cancelUrl; |
||
| 261 | } |
||
| 262 | |||
| 263 | $orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
| 264 | $orderConfigurationUrls |
||
| 265 | ->setCancel($cancelUrl) |
||
| 266 | ->setKo($koUrl) |
||
| 267 | ->setAuthorizedNotificationCallback($callback_url) |
||
| 268 | ->setRejectedNotificationCallback($callback_url) |
||
| 269 | ->setOk($checkoutProcessUrl); |
||
| 270 | |||
| 271 | |||
| 272 | $orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
| 273 | $orderChannel |
||
| 274 | ->setAssistedSale(false) |
||
| 275 | ->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE); |
||
| 276 | $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
||
| 277 | $orderConfiguration |
||
| 278 | ->setChannel($orderChannel) |
||
| 279 | ->setUrls($orderConfigurationUrls); |
||
| 280 | |||
| 281 | $metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata(); |
||
| 282 | $metadata = array( |
||
| 283 | 'oscommerce' => PROJECT_VERSION, |
||
| 284 | 'pagantis' => $this->version, |
||
| 285 | 'php' => phpversion() |
||
| 286 | ); |
||
| 287 | foreach ($metadata as $key => $metadatum) { |
||
| 288 | $metadataOrder->addMetadata($key, $metadatum); |
||
| 289 | } |
||
| 290 | $orderApiClient = new \Pagantis\OrdersApiClient\Model\Order(); |
||
| 291 | $orderApiClient |
||
| 292 | ->setConfiguration($orderConfiguration) |
||
| 293 | ->setMetadata($metadataOrder) |
||
| 294 | ->setShoppingCart($orderShoppingCart) |
||
| 295 | ->setUser($orderUser); |
||
| 296 | |||
| 297 | $publicKey = trim(MODULE_PAYMENT_PAGANTIS_PK); |
||
| 298 | $secretKey = trim(MODULE_PAYMENT_PAGANTIS_SK); |
||
| 299 | $orderClient = new \Pagantis\OrdersApiClient\Client($publicKey, $secretKey); |
||
| 300 | $pagantisOrder = $orderClient->createOrder($orderApiClient); |
||
| 301 | if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 302 | $url = $pagantisOrder->getActionUrls()->getForm(); |
||
| 303 | $this->insertRow($this->os_order_reference, $pagantisOrder->getId(), serialize($global_vars)); |
||
| 304 | } else { |
||
| 305 | throw new OrderNotFoundException(); |
||
| 306 | } |
||
| 307 | |||
| 308 | if ($url == "") { |
||
| 309 | throw new UnknownException(_("No ha sido posible obtener una respuesta de Pagantis")); |
||
| 310 | } else { |
||
| 311 | $output = "\n"; |
||
| 312 | $output .= tep_draw_hidden_field("formUrl", $url) . "\n"; |
||
| 313 | $output .= tep_draw_hidden_field("cancelUrl", $cancelUrl) . "\n"; |
||
| 314 | return $output; |
||
| 315 | |||
| 316 | } //TODO IFRAME |
||
| 317 | } catch (\Exception $exception) { |
||
| 318 | tep_redirect($cancelUrl); |
||
| 319 | return; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @throws Exception |
||
| 325 | */ |
||
| 326 | public function before_process() |
||
| 327 | { |
||
| 328 | include_once('./ext/modules/payment/pagantis/notifyController.php'); |
||
| 329 | $this->pgNotify = new notifyController(); |
||
| 330 | $this->pgNotify->setOscommerceOrderId($_GET['order_id']); |
||
| 331 | $this->pgNotify->setOrigin(isset($_GET['from']) ? ($_GET['from']) : 'order'); |
||
| 332 | $this->pgNotify->processInformation(); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Post-processing activities |
||
| 337 | * |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | public function after_process() |
||
| 341 | { |
||
| 342 | $this->pgNotify->confirmInformation(); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function output_error() |
||
| 349 | { |
||
| 350 | return false; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | public function check() |
||
| 357 | { |
||
| 358 | if (!isset($this->_check)) { |
||
| 359 | $query = "select * from ".TABLE_CONFIGURATION." where configuration_key = 'MODULE_PAYMENT_PAGANTIS_STATUS'"; |
||
| 360 | $check_query = tep_db_query($query); |
||
| 361 | $this->_check = tep_db_num_rows($check_query); |
||
| 362 | } |
||
| 363 | $this->installPagantisTables(); |
||
| 364 | return $this->_check; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * This is where you define module's configurations (displayed in admin). |
||
| 369 | */ |
||
| 370 | public function install() |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Create the neccesary tables for the module |
||
| 450 | */ |
||
| 451 | private function installPagantisTables() |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Standard functionality to uninstall the module. |
||
| 503 | */ |
||
| 504 | public function remove() |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Internal list of configuration keys used for configuration of the module |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public function keys() |
||
| 540 | { |
||
| 541 | return array('MODULE_PAYMENT_PAGANTIS_STATUS', |
||
| 542 | 'MODULE_PAYMENT_PAGANTIS_PK', |
||
| 543 | 'MODULE_PAYMENT_PAGANTIS_SK' |
||
| 544 | ); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | private function getOrders() |
||
| 551 | { |
||
| 552 | $this->is_guest = 'true'; |
||
| 553 | if (trim($_SESSION['customer_id']) != '') { |
||
| 554 | $this->is_guest = 'false'; |
||
| 555 | $query = sprintf( |
||
| 556 | "select orders_total.value, orders.date_purchased from orders |
||
| 557 | JOIN orders_status_history ON orders.orders_id=orders_status_history.orders_id |
||
| 558 | JOIN orders_total ON orders.orders_id=orders_total.orders_id |
||
| 559 | where orders.customers_id='%s' and orders_status_history.orders_status_id in ('2','3') |
||
| 560 | and orders_total.class='ot_total'", |
||
| 561 | $_SESSION['customer_id'] |
||
| 562 | ); |
||
| 563 | |||
| 564 | $response = array(); |
||
| 565 | $resultsSelect = tep_db_query($query); |
||
| 566 | while ($orderRow = tep_db_fetch_array($resultsSelect)) { |
||
| 567 | $response[] = $orderRow; |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | return $response; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param $orderId |
||
| 576 | * @param $pagantisOrderId |
||
| 577 | * @param $globalVars |
||
| 578 | */ |
||
| 579 | private function insertRow($orderId, $pagantisOrderId, $globalVars) |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @return array |
||
| 596 | */ |
||
| 597 | private function getExtraConfig() |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param $item |
||
| 615 | * |
||
| 616 | * @return bool |
||
| 617 | */ |
||
| 618 | private function isPromoted($item) |
||
| 619 | { |
||
| 620 | $productId = explode('{', $item['id'], 1); |
||
| 621 | $productId = $productId['0']; |
||
| 622 | |||
| 623 | if ($this->extraConfig['PAGANTIS_PROMOTION'] == '') { |
||
| 624 | $promotedProducts = array(); |
||
| 625 | } else { |
||
| 626 | $promotedProducts = array_values((array)unserialize($this->extraConfig['PAGANTIS_PROMOTION'])); |
||
| 627 | } |
||
| 628 | |||
| 629 | return (in_array($productId, $promotedProducts)); |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @return string |
||
| 634 | */ |
||
| 635 | private function getDescription() |
||
| 647 | } |
||
| 648 | |||
| 649 | |||
| 650 | /** |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | private function installSimulator() |
||
| 667 | } |
||
| 668 | |||
| 669 | private function uninstallSimulator() |
||
| 686 |