| Conditions | 16 |
| Paths | > 20000 |
| Total Lines | 219 |
| Code Lines | 160 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 13 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 48 | public function postProcess() |
||
| 49 | { |
||
| 50 | /** @var Cart $cart */ |
||
| 51 | $cart = $this->context->cart; |
||
| 52 | |||
| 53 | if (!$cart->id) { |
||
| 54 | Tools::redirect('index.php?controller=order'); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** @var Customer $customer */ |
||
| 58 | $customer = $this->context->customer; |
||
| 59 | $query = array( |
||
| 60 | 'id_cart' => $cart->id, |
||
| 61 | 'key' => $cart->secure_key, |
||
| 62 | ); |
||
| 63 | |||
| 64 | $koUrl = $this->context->link->getPageLink( |
||
| 65 | 'order', |
||
| 66 | null, |
||
| 67 | null, |
||
| 68 | array('step'=>3) |
||
| 69 | ); |
||
| 70 | $iframe = getenv('PMT_FORM_DISPLAY_TYPE'); |
||
| 71 | $cancelUrl = (getenv('PMT_URL_KO') !== '') ? getenv('PMT_URL_KO') : $koUrl; |
||
| 72 | $paylaterPublicKey = Configuration::get('pmt_public_key'); |
||
| 73 | $paylaterPrivateKey = Configuration::get('pmt_private_key'); |
||
| 74 | $okUrl = _PS_BASE_URL_.__PS_BASE_URI__ |
||
| 75 | .'index.php?canonical=true&fc=module&module=paylater&controller=notify&' |
||
| 76 | .http_build_query($query) |
||
| 77 | ; |
||
| 78 | |||
| 79 | $shippingAddress = new Address($cart->id_address_delivery); |
||
| 80 | $billingAddress = new Address($cart->id_address_invoice); |
||
| 81 | $curlInfo = curl_version(); |
||
| 82 | $curlVersion = $curlInfo['version']; |
||
| 83 | $metadata = array( |
||
| 84 | 'ps' => _PS_VERSION_, |
||
| 85 | 'pmt' => $this->module->version, |
||
| 86 | 'php' => phpversion(), |
||
| 87 | 'curl' => $curlVersion, |
||
| 88 | ); |
||
| 89 | |||
| 90 | try { |
||
| 91 | $userAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
||
| 92 | $userAddress |
||
| 93 | ->setZipCode($shippingAddress->postcode) |
||
| 94 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
||
| 95 | ->setCountryCode('ES') |
||
| 96 | ->setCity($shippingAddress->city) |
||
| 97 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
||
| 98 | ; |
||
| 99 | |||
| 100 | $orderShippingAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
||
| 101 | $orderShippingAddress |
||
| 102 | ->setZipCode($shippingAddress->postcode) |
||
| 103 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
||
| 104 | ->setCountryCode('ES') |
||
| 105 | ->setCity($shippingAddress->city) |
||
| 106 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
||
| 107 | ->setDni($shippingAddress->dni) |
||
| 108 | ->setFixPhone($shippingAddress->phone) |
||
| 109 | ->setMobilePhone($shippingAddress->phone_mobile) |
||
| 110 | ; |
||
| 111 | |||
| 112 | $orderBillingAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
||
| 113 | $orderBillingAddress |
||
| 114 | ->setZipCode($billingAddress->postcode) |
||
| 115 | ->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname) |
||
| 116 | ->setCountryCode('ES') |
||
| 117 | ->setCity($billingAddress->city) |
||
| 118 | ->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2) |
||
| 119 | ->setDni($billingAddress->dni) |
||
| 120 | ->setFixPhone($billingAddress->phone) |
||
| 121 | ->setMobilePhone($billingAddress->phone_mobile) |
||
| 122 | ; |
||
| 123 | |||
| 124 | $orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User(); |
||
| 125 | $orderUser |
||
| 126 | ->setAddress($userAddress) |
||
| 127 | ->setFullName($orderShippingAddress->getFullName()) |
||
| 128 | ->setBillingAddress($orderBillingAddress) |
||
| 129 | ->setEmail($this->context->cookie->logged ? $this->context->cookie->email : $customer->email) |
||
| 130 | ->setFixPhone($shippingAddress->phone) |
||
| 131 | ->setMobilePhone($shippingAddress->phone_mobile) |
||
| 132 | ->setShippingAddress($orderShippingAddress) |
||
| 133 | ->setDni($shippingAddress->dni) |
||
| 134 | ; |
||
| 135 | |||
| 136 | if ($customer->birthday!='0000-00-00') { |
||
| 137 | $orderUser->setDateOfBirth($customer->birthday); |
||
| 138 | } |
||
| 139 | |||
| 140 | $orders = Order::getCustomerOrders($customer->id); |
||
| 141 | /** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */ |
||
| 142 | foreach ($orders as $order) { |
||
| 143 | if ($order['valid']) { |
||
| 144 | $orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
| 145 | $orderHistory |
||
| 146 | ->setAmount(intval(100 * $order['total_paid'])) |
||
| 147 | ->setDate(new \DateTime($order['date_add'])) |
||
| 148 | ; |
||
| 149 | $orderUser->addOrderHistory($orderHistory); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
| 154 | $details->setShippingCost(intval(strval(100 * $cart->getTotalShippingCost()))); |
||
| 155 | $items = $cart->getProducts(); |
||
| 156 | foreach ($items as $key => $item) { |
||
| 157 | $product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
| 158 | $product |
||
| 159 | ->setAmount(intval(100 * $item['price_wt'])) |
||
| 160 | ->setQuantity($item['quantity']) |
||
| 161 | ->setDescription($item['name']); |
||
| 162 | $details->addProduct($product); |
||
| 163 | } |
||
| 164 | |||
| 165 | $orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart(); |
||
| 166 | $orderShoppingCart |
||
| 167 | ->setDetails($details) |
||
| 168 | ->setOrderReference($cart->id) |
||
| 169 | ->setPromotedAmount(0) |
||
| 170 | ->setTotalAmount(intval(strval(100 * $cart->getOrderTotal(true)))) |
||
| 171 | ; |
||
| 172 | |||
| 173 | $orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
| 174 | $orderConfigurationUrls |
||
| 175 | ->setCancel($cancelUrl) |
||
| 176 | ->setKo($cancelUrl) |
||
| 177 | ->setAuthorizedNotificationCallback($okUrl) |
||
| 178 | ->setRejectedNotificationCallback($okUrl) |
||
| 179 | ->setOk($okUrl) |
||
| 180 | ; |
||
| 181 | |||
| 182 | $orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
| 183 | $orderChannel |
||
| 184 | ->setAssistedSale(false) |
||
| 185 | ->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
||
| 186 | ; |
||
| 187 | |||
| 188 | $orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration(); |
||
| 189 | $orderConfiguration |
||
| 190 | ->setChannel($orderChannel) |
||
| 191 | ->setUrls($orderConfigurationUrls) |
||
| 192 | ; |
||
| 193 | |||
| 194 | $metadataOrder = new \PagaMasTarde\OrdersApiClient\Model\Order\Metadata(); |
||
| 195 | foreach ($metadata as $key => $metadatum) { |
||
| 196 | $metadataOrder |
||
| 197 | ->addMetadata($key, $metadatum); |
||
| 198 | } |
||
| 199 | |||
| 200 | $order = new \PagaMasTarde\OrdersApiClient\Model\Order(); |
||
| 201 | $order |
||
| 202 | ->setConfiguration($orderConfiguration) |
||
| 203 | ->setMetadata($metadataOrder) |
||
| 204 | ->setShoppingCart($orderShoppingCart) |
||
| 205 | ->setUser($orderUser) |
||
| 206 | ; |
||
| 207 | } catch (\Exception $exception) { |
||
| 208 | $this->saveLog( |
||
| 209 | array( |
||
| 210 | 'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
||
| 211 | ) |
||
| 212 | ); |
||
| 213 | Tools::redirect($cancelUrl); |
||
| 214 | } |
||
| 215 | |||
| 216 | $url =''; |
||
| 217 | try { |
||
| 218 | $orderClient = new \PagaMasTarde\OrdersApiClient\Client( |
||
| 219 | $paylaterPublicKey, |
||
| 220 | $paylaterPrivateKey |
||
| 221 | ); |
||
| 222 | $order = $orderClient->createOrder($order); |
||
| 223 | if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
||
| 224 | $url = $order->getActionUrls()->getForm(); |
||
| 225 | $orderId = $order->getId(); |
||
| 226 | $result = Db::getInstance()->execute( |
||
| 227 | "INSERT INTO `" . _DB_PREFIX_ . "pmt_order` (`id`, `order_id`) |
||
| 228 | VALUES ('$cart->id','$orderId') |
||
| 229 | ON DUPLICATE KEY UPDATE `order_id` = '$orderId'" |
||
| 230 | ); |
||
| 231 | if (!$result) { |
||
| 232 | throw new \Exception('Unable to save pmt-order-id'); |
||
| 233 | } |
||
| 234 | } else { |
||
| 235 | throw new \Exception('Order not created'); |
||
| 236 | } |
||
| 237 | } catch (\Exception $exception) { |
||
| 238 | $this->saveLog( |
||
| 239 | array( |
||
| 240 | 'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
||
| 241 | ) |
||
| 242 | ); |
||
| 243 | Tools::redirect($cancelUrl); |
||
| 244 | } |
||
| 245 | |||
| 246 | if (!$iframe) { |
||
| 247 | Tools::redirect($url); |
||
| 248 | } else { |
||
| 249 | $this->context->smarty->assign(array( |
||
| 250 | 'url' => $url, |
||
| 251 | 'checkoutUrl' => $cancelUrl, |
||
| 252 | )); |
||
| 253 | |||
| 254 | try { |
||
| 255 | if (_PS_VERSION_ < 1.7) { |
||
| 256 | $this->setTemplate('payment-15.tpl'); |
||
| 257 | } else { |
||
| 258 | $this->setTemplate('module:paylater/views/templates/front/payment-17.tpl'); |
||
| 259 | } |
||
| 260 | } catch (\Exception $exception) { |
||
| 261 | $this->saveLog( |
||
| 262 | array( |
||
| 263 | 'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
||
| 264 | ) |
||
| 265 | ); |
||
| 266 | Tools::redirect($url); |
||
| 267 | } |
||
| 271 |