| Conditions | 49 | 
| Paths | > 20000 | 
| Total Lines | 333 | 
| Code Lines | 253 | 
| Lines | 12 | 
| Ratio | 3.6 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php  | 
            ||
| 488 | public function hookPayment($params)  | 
            ||
| 489 |     { | 
            ||
| 490 |         if ($this->context->cart->getOrderTotal() < Configuration::get('PAYLATER_MIN_AMOUNT')) { | 
            ||
| 491 | return;  | 
            ||
| 492 | }  | 
            ||
| 493 | $currency_id = $params['cart']->id_currency;  | 
            ||
| 494 | $currency = new Currency((int) $currency_id);  | 
            ||
| 495 |         if (in_array($currency->iso_code, $this->limited_currencies) == false) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 496 | return false;  | 
            ||
| 497 | }  | 
            ||
| 498 | |||
| 499 | $customer = new Customer((int)$params['cart']->id_customer);  | 
            ||
| 500 | //prevent opening payment method if no credentials introduced  | 
            ||
| 501 |         if (Configuration::get('PAYLATER_ENVIRONMENT') == 0) { | 
            ||
| 502 |             if (trim(Configuration::get('PAYLATER_ACCOUNT_ID_TEST')) == false || | 
            ||
| 503 |                trim(Configuration::get('PAYLATER_ACCOUNT_KEY_TEST')) == false ) { | 
            ||
| 504 | return false;  | 
            ||
| 505 | }  | 
            ||
| 506 |         } else { | 
            ||
| 507 |             if (trim(Configuration::get('PAYLATER_ACCOUNT_ID_LIVE')) == false || | 
            ||
| 508 |             trim(Configuration::get('PAYLATER_ACCOUNT_KEY_LIVE')) == false) { | 
            ||
| 509 | return false;  | 
            ||
| 510 | }  | 
            ||
| 511 | }  | 
            ||
| 512 | |||
| 513 | $cart_products = $this->context->cart->getProducts();  | 
            ||
| 514 | |||
| 515 | $items = array();  | 
            ||
| 516 | $desciption=array();  | 
            ||
| 517 | |||
| 518 |         $discount = Configuration::get('PAYLATER_DISCOUNT'); | 
            ||
| 519 | $discount_boolean = $discount == 'true' ? 1:0;  | 
            ||
| 520 |         foreach ($cart_products as $p) { | 
            ||
| 521 | $items[] = array(  | 
            ||
| 522 | 'description' => $p['name'],  | 
            ||
| 523 | 'quantity' => $p['cart_quantity'],  | 
            ||
| 524 | 'amount' => number_format($p['total_wt'], 2, '.', '')  | 
            ||
| 525 | );  | 
            ||
| 526 |              $desciption[]=  $p['name']. " (".$p['cart_quantity'].")"; | 
            ||
| 527 | }  | 
            ||
| 528 | |||
| 529 | //Shipping address  | 
            ||
| 530 | $saddress = new Address($this->context->cart->id_address_delivery);  | 
            ||
| 531 | $sstreet=$saddress->address1.' '.$saddress->address2;  | 
            ||
| 532 | $scity=$saddress->city;  | 
            ||
| 533 | $suser_state = new State($saddress->id_state);  | 
            ||
| 534 | $sprovince=$suser_state->name;  | 
            ||
| 535 | $szipcode=$saddress->postcode;  | 
            ||
| 536 | $sphone = $saddress->phone;  | 
            ||
| 537 | $smobile_phone = $saddress->phone_mobile;  | 
            ||
| 538 | $shipping_last_updated = Tools::substr($saddress->date_upd, 0, 10);  | 
            ||
| 539 | $shipping_date_add = Tools::substr($saddress->date_add, 0, 10);  | 
            ||
| 540 | |||
| 541 | |||
| 542 | //billing address  | 
            ||
| 543 | $billing_address = new Address($this->context->cart->id_address_invoice);  | 
            ||
| 544 | $street=$billing_address->address1.' '.$billing_address->address2;  | 
            ||
| 545 | $city=$billing_address->city;  | 
            ||
| 546 | $user_state2 = new State($billing_address->id_state);  | 
            ||
| 547 | $province=$user_state2->name;  | 
            ||
| 548 | $zipcode=$billing_address->postcode;  | 
            ||
| 549 | $phone = $billing_address->phone;  | 
            ||
| 550 | $mobile_phone = $billing_address->phone_mobile;  | 
            ||
| 551 | $billing_last_updated = Tools::substr($billing_address->date_upd, 0, 10);  | 
            ||
| 552 | $billing_date_add = Tools::substr($billing_address->date_add, 0, 10);  | 
            ||
| 553 | $shipping_dni = '';  | 
            ||
| 554 | $billing_dni = '';  | 
            ||
| 555 | //dni  | 
            ||
| 556 | $customer_dob = ($this->context->customer->birthday ? $this->context->customer->birthday : $customer->birthday);  | 
            ||
| 557 | $dob = '';  | 
            ||
| 558 | $dni = '';  | 
            ||
| 559 |         if (property_exists($billing_address, 'dni')) { | 
            ||
| 560 | $dni=$billing_address->dni;  | 
            ||
| 561 | $billing_dni = $billing_address->dni;  | 
            ||
| 562 | }  | 
            ||
| 563 | |||
| 564 |         if (property_exists($saddress, 'dni')) { | 
            ||
| 565 | if (trim($dni) == '' && $saddress->firstname == $this->context->cookie->customer_firstname  | 
            ||
| 566 |               && $saddress->lastname == $this->context->cookie->customer_lastname) { | 
            ||
| 567 | $dni=$saddress->dni;  | 
            ||
| 568 | }  | 
            ||
| 569 | $shipping_dni = $saddress->dni;  | 
            ||
| 570 | }  | 
            ||
| 571 | if ($billing_address->firstname == $this->context->cookie->customer_firstname &&  | 
            ||
| 572 |             $billing_address->lastname == $this->context->cookie->customer_lastname) { | 
            ||
| 573 | $dob = ($this->context->customer->birthday ? $this->context->customer->birthday : $customer->birthday);  | 
            ||
| 574 | }  | 
            ||
| 575 | |||
| 576 | $customer_last_updated = Tools::substr($this->context->customer->date_upd, 0, 10);  | 
            ||
| 577 | |||
| 578 |         if (version_compare(_PS_VERSION_, "1.5", "<")) { | 
            ||
| 579 | $shippingCost = $this->context->cart->getOrderShippingCost();  | 
            ||
| 580 |         } else { | 
            ||
| 581 | $shippingCost = $this->context->cart->getTotalShippingCost(null, true, null);  | 
            ||
| 582 | }  | 
            ||
| 583 | |||
| 584 | $link = Context::getContext()->link;  | 
            ||
| 585 |         $callback_url =  $link->getModuleLink('paylater', 'validation', array()); | 
            ||
| 586 | $confirmationQuery = array(  | 
            ||
| 587 | 'id_cart' => $this->context->cart->id,  | 
            ||
| 588 | 'key' => $this->context->cart->secure_key,  | 
            ||
| 589 | );  | 
            ||
| 590 | |||
| 591 |         $url_OK =  $link->getModuleLink('paylater', 'confirmation', $confirmationQuery); | 
            ||
| 592 | |||
| 593 |         if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1) { | 
            ||
| 594 |             $url_NOK = $link->getPageLink('order-opc'); | 
            ||
| 595 |         } else { | 
            ||
| 596 |             $url_NOK = $link->getPageLink('order'); | 
            ||
| 597 | }  | 
            ||
| 598 | |||
| 599 |         $cancelled_url = $link->getPageLink('order'); | 
            ||
| 600 | |||
| 601 |         if ($shippingCost > 0) { | 
            ||
| 602 | $items[] = array(  | 
            ||
| 603 |                         'description' => $this->l('Shipping cost'), | 
            ||
| 604 | 'quantity' => 1,  | 
            ||
| 605 | 'amount' => number_format($shippingCost, 2, '.', '')  | 
            ||
| 606 | );  | 
            ||
| 607 |             $desciption[]= $this->l('Shipping cost'); | 
            ||
| 608 | }  | 
            ||
| 609 | View Code Duplication |         if (Configuration::get('PAYLATER_ENVIRONMENT') == 1) { | 
            |
| 610 | //mode live  | 
            ||
| 611 |             $account_id = Configuration::get('PAYLATER_ACCOUNT_ID_LIVE'); | 
            ||
| 612 |         } else { | 
            ||
| 613 | //mode test  | 
            ||
| 614 |             $account_id = Configuration::get('PAYLATER_ACCOUNT_ID_TEST'); | 
            ||
| 615 | }  | 
            ||
| 616 | //discount  | 
            ||
| 617 |         $discount = Configuration::get('PAYLATER_DISCOUNT'); | 
            ||
| 618 | //1.4 fix  | 
            ||
| 619 |         if ($discount == '0') { | 
            ||
| 620 | $discount = 'false';  | 
            ||
| 621 | }  | 
            ||
| 622 |         if ($discount == '1') { | 
            ||
| 623 | $discount = 'true';  | 
            ||
| 624 | }  | 
            ||
| 625 | |||
| 626 |         switch ($this->context->customer->id_gender) { | 
            ||
| 627 | case 2:  | 
            ||
| 628 | $gender = 'female';  | 
            ||
| 629 | break;  | 
            ||
| 630 | case 1:  | 
            ||
| 631 | $gender = 'male';  | 
            ||
| 632 | break;  | 
            ||
| 633 | default:  | 
            ||
| 634 | $gender = '';  | 
            ||
| 635 | break;  | 
            ||
| 636 | }  | 
            ||
| 637 | $discount_boolean = $discount == 'true' ? 1:0;  | 
            ||
| 638 | |||
| 639 |         $iframe = Configuration::get('PAYLATER_IFRAME'); | 
            ||
| 640 | |||
| 641 |         if ($iframe == 1) { | 
            ||
| 642 | $iframe = 'true';  | 
            ||
| 643 | }  | 
            ||
| 644 | |||
| 645 | $endpoint = $this->url;  | 
            ||
| 646 | |||
| 647 | $order_id = $this->context->cart->id;  | 
            ||
| 648 | |||
| 649 | //description  | 
            ||
| 650 |         $description = implode(',', $desciption); | 
            ||
| 651 | |||
| 652 | $convert_price = Tools::convertPrice(  | 
            ||
| 653 | $this->context->cart->getOrderTotal(true, 3),  | 
            ||
| 654 | $this->context->currency  | 
            ||
| 655 | );  | 
            ||
| 656 | |||
| 657 | $amount = number_format(  | 
            ||
| 658 | $convert_price,  | 
            ||
| 659 | 2,  | 
            ||
| 660 | '.',  | 
            ||
| 661 | ''  | 
            ||
| 662 | );  | 
            ||
| 663 |         $amount = str_replace('.', '', $amount); | 
            ||
| 664 | |||
| 665 | View Code Duplication |         if (Configuration::get('PAYLATER_ENVIRONMENT') == 1) { | 
            |
| 666 |             $key_to_use = Configuration::get('PAYLATER_ACCOUNT_KEY_LIVE'); | 
            ||
| 667 |         } else { | 
            ||
| 668 |             $key_to_use = Configuration::get('PAYLATER_ACCOUNT_KEY_TEST'); | 
            ||
| 669 | }  | 
            ||
| 670 | |||
| 671 |         $widget_type = Configuration::get('PAYLATER_WIDGET') == 'false' ?  '0' : '1'; | 
            ||
| 672 | |||
| 673 | $total_paid = 0;  | 
            ||
| 674 | $num_prev_orders = 0;  | 
            ||
| 675 | $sign_up_date = '';  | 
            ||
| 676 | $num_partial_refunds = 0;  | 
            ||
| 677 | $amount_refunded = 0;  | 
            ||
| 678 | $num_full_refunds = 0;  | 
            ||
| 679 | $super_checkout_enabled = false;  | 
            ||
| 680 | $onepagecheckoutps_enabled = false;  | 
            ||
| 681 | $onepagecheckout_enabled = false;  | 
            ||
| 682 | |||
| 683 |         if (version_compare(_PS_VERSION_, "1.5", "<")) { | 
            ||
| 684 |             if ($this->context->cookie->logged) { | 
            ||
| 685 | $orders = Order::getCustomerOrders($this->context->customer->id);  | 
            ||
| 686 |                 foreach ($orders as $o) { | 
            ||
| 687 | $total_paid += $o['total_paid'];  | 
            ||
| 688 | $num_prev_orders++;  | 
            ||
| 689 | }  | 
            ||
| 690 | $sign_up_date = Tools::substr($this->context->customer->date_add, 0, 10);  | 
            ||
| 691 | $order_slips = OrderSlip::getOrdersSlip((int)$this->context->cookie->id_customer);  | 
            ||
| 692 | |||
| 693 |                 foreach ($order_slips as $o) { | 
            ||
| 694 | $num_full_refunds++;  | 
            ||
| 695 | $amount_refunded += $o['amount'];  | 
            ||
| 696 | }  | 
            ||
| 697 | }  | 
            ||
| 698 |         } else { | 
            ||
| 699 | //query for paid statuses  | 
            ||
| 700 | $sql = new DbQuery();  | 
            ||
| 701 |             $sql->select('id_order_state'); | 
            ||
| 702 |             $sql->from('order_state', 'c'); | 
            ||
| 703 |             $sql->where('c.paid = 1'); | 
            ||
| 704 | $db_paid_statuses = Db::getInstance()->executeS($sql);  | 
            ||
| 705 | $paid_statuses = array();  | 
            ||
| 706 |             foreach ($db_paid_statuses as $p) { | 
            ||
| 707 | $paid_statuses[]=$p['id_order_state'];  | 
            ||
| 708 | }  | 
            ||
| 709 |             if ($this->context->cookie->logged) { | 
            ||
| 710 | $orders = Order::getCustomerOrders($this->context->customer->id);  | 
            ||
| 711 |                 foreach ($orders as $o) { | 
            ||
| 712 |                     if (array_key_exists('id_order_state', $o) && in_array($o['id_order_state'], $paid_statuses)) { | 
            ||
| 713 | $total_paid += $o['total_paid'];  | 
            ||
| 714 | $num_prev_orders++;  | 
            ||
| 715 | }  | 
            ||
| 716 | }  | 
            ||
| 717 | $sign_up_date = Tools::substr($this->context->customer->date_add, 0, 10);  | 
            ||
| 718 | $order_slips = OrderSlip::getOrdersSlip((int)$this->context->cookie->id_customer);  | 
            ||
| 719 |                 foreach ($order_slips as $o) { | 
            ||
| 720 | $sql = new DbQuery();  | 
            ||
| 721 |                     $sql->select('total_paid'); | 
            ||
| 722 |                     $sql->from('orders', 'c'); | 
            ||
| 723 |                     $sql->where('c.id_order = '.$o['id_order']); | 
            ||
| 724 | $db_total_paid = Db::getInstance()->executeS($sql);  | 
            ||
| 725 |                     if ($db_total_paid[0]['total_paid'] <= $o['amount']) { | 
            ||
| 726 | $num_full_refunds++;  | 
            ||
| 727 |                     } else { | 
            ||
| 728 | $num_partial_refunds++;  | 
            ||
| 729 | }  | 
            ||
| 730 | $amount_refunded += $o['amount'];  | 
            ||
| 731 | }  | 
            ||
| 732 | }  | 
            ||
| 733 |             $super_checkout_enabled = Module::isEnabled('supercheckout'); | 
            ||
| 734 |             $onepagecheckoutps_enabled = Module::isEnabled('onepagecheckoutps'); | 
            ||
| 735 |             $onepagecheckout_enabled = Module::isEnabled('onepagecheckout'); | 
            ||
| 736 | }  | 
            ||
| 737 | |||
| 738 |         $opc_enabled = Configuration::get('PS_ORDER_PROCESS_TYPE') == 1 ? 1 : 0; | 
            ||
| 739 | |||
| 740 | //d($key_to_use.$account_id.$order_id.$amount.$this->context->currency->iso_code.$url_OK.$url_NOK);  | 
            ||
| 741 | $text=$key_to_use.$account_id.$order_id.$amount.$this->context->currency->iso_code.  | 
            ||
| 742 | $url_OK.$url_NOK.$callback_url.$discount.$cancelled_url;  | 
            ||
| 743 |         $signature = hash('sha512', $text); | 
            ||
| 744 | $this->smarty->assign(array(  | 
            ||
| 745 | 'endpoint' => $endpoint,  | 
            ||
| 746 | 'account_id' => $account_id,  | 
            ||
| 747 | 'currency' => $this->context->currency->iso_code,  | 
            ||
| 748 | 'ok_url' => $url_OK,  | 
            ||
| 749 | 'nok_url' => $url_NOK,  | 
            ||
| 750 | 'cancelled_url' => $cancelled_url,  | 
            ||
| 751 | 'order_id' => $order_id,  | 
            ||
| 752 | 'amount' => $amount,  | 
            ||
| 753 | 'description' => $description,  | 
            ||
| 754 | 'items' => $items,  | 
            ||
| 755 | 'signature' => $signature,  | 
            ||
| 756 | 'customer_name' => ($this->context->cookie->logged ? $this->context->cookie->customer_firstname.  | 
            ||
| 757 | ' '.$this->context->cookie->customer_lastname : $customer->firstname." ".$customer->lastname),  | 
            ||
| 758 | 'customer_email' => ($this->context->cookie->logged ? $this->context->cookie->email : $customer->email),  | 
            ||
| 759 | 'locale' => $this->context->language->iso_code,  | 
            ||
| 760 | 'cart_products' => $cart_products,  | 
            ||
| 761 | 'billing_full_name' => $billing_address->firstname. " ".$billing_address->lastname ,  | 
            ||
| 762 | 'billing_dni' => $billing_dni,  | 
            ||
| 763 | 'billing_alias' => $billing_address->alias,  | 
            ||
| 764 | 'billing_company' => $billing_address->company,  | 
            ||
| 765 | 'billing_vat' => $billing_address->vat_number,  | 
            ||
| 766 | 'street' => $street,  | 
            ||
| 767 | 'city' => $city,  | 
            ||
| 768 | 'province' => $province,  | 
            ||
| 769 | 'zipcode' => $zipcode,  | 
            ||
| 770 | 'sstreet' => $sstreet,  | 
            ||
| 771 | 'scity' => $scity,  | 
            ||
| 772 | 'sprovince' => $sprovince,  | 
            ||
| 773 | 'szipcode' => $szipcode,  | 
            ||
| 774 | 'phone' => $phone,  | 
            ||
| 775 | 'mobile_phone' => $mobile_phone,  | 
            ||
| 776 | 'shipping_dni' => $shipping_dni,  | 
            ||
| 777 | 'shipping_alias' => $saddress->alias,  | 
            ||
| 778 | 'shipping_company' => $saddress->company,  | 
            ||
| 779 | 'shipping_vat' => $saddress->vat_number,  | 
            ||
| 780 | 'shipping_full_name' => $saddress->firstname. " ".$saddress->lastname ,  | 
            ||
| 781 | 'sphone' => $sphone,  | 
            ||
| 782 | 'smobile_phone' => $smobile_phone,  | 
            ||
| 783 | 'dni' => $dni,  | 
            ||
| 784 | 'callback_url' => $callback_url,  | 
            ||
| 785 | 'discount' => $discount,  | 
            ||
| 786 | 'discount_boolean' => $discount_boolean,  | 
            ||
| 787 | 'dob' => $dob,  | 
            ||
| 788 | 'customer_dob' => $customer_dob,  | 
            ||
| 789 | 'iframe' => $iframe,  | 
            ||
| 790 | 'total_paid' => $total_paid,  | 
            ||
| 791 | 'num_prev_orders' => $num_prev_orders,  | 
            ||
| 792 | 'sign_up_date' => $sign_up_date,  | 
            ||
| 793 | 'amount_refunded' => $amount_refunded,  | 
            ||
| 794 | 'num_full_refunds' => $num_full_refunds,  | 
            ||
| 795 | 'num_partial_refunds' => $num_partial_refunds,  | 
            ||
| 796 | 'module_version' => $this->version,  | 
            ||
| 797 | 'customer_last_updated' => $customer_last_updated,  | 
            ||
| 798 | 'billing_last_updated' => $billing_last_updated,  | 
            ||
| 799 | 'billing_date_add' => $billing_date_add,  | 
            ||
| 800 | 'shipping_last_updated' => $shipping_last_updated,  | 
            ||
| 801 | 'shipping_date_add' => $shipping_date_add,  | 
            ||
| 802 | 'customer_gender' => $gender,  | 
            ||
| 803 | 'opc_enabled' => $opc_enabled,  | 
            ||
| 804 | 'is_guest' => $this->context->customer->is_guest,  | 
            ||
| 805 | 'platform' => 'prestashop ' . _PS_VERSION_,  | 
            ||
| 806 | 'version4' => version_compare(_PS_VERSION_, "1.5", "<"),  | 
            ||
| 807 | 'version3' => version_compare(_PS_VERSION_, "1.4", "<"),  | 
            ||
| 808 |             'content' => "javascript:$('#paylater_form').submit();", | 
            ||
| 809 | ));  | 
            ||
| 810 | |||
| 811 |         if ($super_checkout_enabled) { | 
            ||
| 812 | return $this->display(__FILE__, 'views/templates/front/payment_supercheckout.tpl');  | 
            ||
| 813 |         } elseif ($onepagecheckoutps_enabled || $onepagecheckout_enabled) { | 
            ||
| 814 | return $this->display(__FILE__, 'views/templates/front/payment.tpl');  | 
            ||
| 815 |         } elseif ($widget_type) { | 
            ||
| 816 | return $this->display(__FILE__, 'views/templates/front/payment_widget.tpl');  | 
            ||
| 817 |         } else { | 
            ||
| 818 | return $this->display(__FILE__, 'views/templates/front/payment.tpl');  | 
            ||
| 819 | }  | 
            ||
| 820 | }  | 
            ||
| 821 | |||
| 1062 | 
When comparing two booleans, it is generally considered safer to use the strict comparison operator.