| Conditions | 19 |
| Paths | > 20000 |
| Total Lines | 247 |
| Code Lines | 186 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 12 | ||
| Bugs | 2 | 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 |
||
| 61 | public function postProcess() |
||
| 62 | { |
||
| 63 | /** @var Cart $cart */ |
||
| 64 | $cart = $this->context->cart; |
||
| 65 | |||
| 66 | if (!$cart->id) { |
||
| 67 | Tools::redirect('index.php?controller=order'); |
||
| 68 | } |
||
| 69 | |||
| 70 | $urlToken = strtoupper(md5(uniqid(rand(), true))); |
||
| 71 | |||
| 72 | /** @var Customer $customer */ |
||
| 73 | $customer = $this->context->customer; |
||
| 74 | $query = array( |
||
| 75 | 'id_cart' => $cart->id, |
||
| 76 | 'key' => $cart->secure_key, |
||
| 77 | ); |
||
| 78 | |||
| 79 | $koUrl = $this->context->link->getPageLink( |
||
| 80 | 'order', |
||
| 81 | null, |
||
| 82 | null, |
||
| 83 | array('step'=>3) |
||
| 84 | ); |
||
| 85 | $cancelUrl = (Pagantis::getExtraConfig('URL_KO') !== '') ? Pagantis::getExtraConfig('URL_KO', null) : $koUrl; |
||
| 86 | |||
| 87 | $product = Tools::getValue('product'); |
||
| 88 | $configs = json_decode(Pagantis::getExtraConfig($product, null), true); |
||
| 89 | $iframe = Pagantis::getExtraConfig('FORM_DISPLAY_TYPE', $product); |
||
| 90 | |||
| 91 | $pagantisPublicKey = Configuration::get(Tools::strtolower($configs['CODE']) . '_public_key'); |
||
| 92 | $pagantisPrivateKey = Configuration::get(Tools::strtolower($configs['CODE']) . '_private_key'); |
||
| 93 | |||
| 94 | $okUrl = _PS_BASE_URL_SSL_.__PS_BASE_URI__ |
||
| 95 | .'index.php?canonical=true&fc=module&module=pagantis&controller=notify&token='.$urlToken.'&origin=redirect&product=' . Tools::strtolower($configs['CODE']) . '&' |
||
| 96 | .http_build_query($query) |
||
| 97 | ; |
||
| 98 | $notificationOkUrl = _PS_BASE_URL_SSL_.__PS_BASE_URI__ |
||
| 99 | .'index.php?canonical=true&fc=module&module=pagantis&controller=notify&token='.$urlToken.'&origin=notification&product=' . Tools::strtolower($configs['CODE']) . '&' |
||
| 100 | .http_build_query($query) |
||
| 101 | ; |
||
| 102 | |||
| 103 | $shippingAddress = new Address($cart->id_address_delivery); |
||
| 104 | $billingAddress = new Address($cart->id_address_invoice); |
||
| 105 | $metadata = array( |
||
| 106 | 'pg_module' => 'prestashop', |
||
| 107 | 'pg_version' => $this->module->version, |
||
| 108 | 'ec_module' => 'prestashop', |
||
| 109 | 'ec_version' => _PS_VERSION_ |
||
| 110 | ); |
||
| 111 | |||
| 112 | try { |
||
| 113 | $shippingCountry = Country::getIsoById($shippingAddress->id_country); |
||
| 114 | $userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
||
| 115 | $userAddress |
||
| 116 | ->setZipCode($shippingAddress->postcode) |
||
| 117 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
||
| 118 | ->setCountryCode($shippingCountry) |
||
| 119 | ->setCity($shippingAddress->city) |
||
| 120 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
||
| 121 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
||
| 122 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 123 | ->setDni($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 124 | ; |
||
| 125 | |||
| 126 | $orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
||
| 127 | $shippingPhone = (empty($shippingAddress->phone_mobile)) ? |
||
| 128 | $shippingAddress->phone : $shippingAddress->phone_mobile; |
||
| 129 | $orderShippingAddress |
||
| 130 | ->setZipCode($shippingAddress->postcode) |
||
| 131 | ->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
||
| 132 | ->setCountryCode($shippingCountry) |
||
| 133 | ->setCity($shippingAddress->city) |
||
| 134 | ->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
||
| 135 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
||
| 136 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 137 | ->setDni($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 138 | ->setFixPhone($shippingAddress->phone) |
||
| 139 | ->setMobilePhone($shippingPhone) |
||
| 140 | ; |
||
| 141 | |||
| 142 | $billingCountry = Country::getIsoById($billingAddress->id_country); |
||
| 143 | $orderBillingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
||
| 144 | $billingPhone = (empty($billingAddress->phone_mobile)) ? |
||
| 145 | $billingAddress->phone : $billingAddress->phone_mobile; |
||
| 146 | $orderBillingAddress |
||
| 147 | ->setZipCode($billingAddress->postcode) |
||
| 148 | ->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname) |
||
| 149 | ->setCountryCode($billingCountry) |
||
| 150 | ->setCity($billingAddress->city) |
||
| 151 | ->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2) |
||
| 152 | ->setTaxId($this->getTaxId($customer, $billingAddress, $shippingAddress)) |
||
| 153 | ->setNationalId($this->getNationalId($customer, $billingAddress, $shippingAddress)) |
||
| 154 | ->setDni($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 155 | ->setFixPhone($billingAddress->phone) |
||
| 156 | ->setMobilePhone($billingPhone) |
||
| 157 | ; |
||
| 158 | |||
| 159 | $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
||
| 160 | $email = $this->context->cookie->logged ? $this->context->cookie->email : $customer->email; |
||
| 161 | $orderUser |
||
| 162 | ->setAddress($userAddress) |
||
| 163 | ->setFullName($orderShippingAddress->getFullName()) |
||
| 164 | ->setBillingAddress($orderBillingAddress) |
||
| 165 | ->setEmail($email) |
||
| 166 | ->setFixPhone($shippingAddress->phone) |
||
| 167 | ->setMobilePhone($shippingPhone) |
||
| 168 | ->setShippingAddress($orderShippingAddress) |
||
| 169 | ->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
||
| 170 | ->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 171 | ->setDni($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
||
| 172 | ; |
||
| 173 | |||
| 174 | if ($customer->birthday!='0000-00-00') { |
||
| 175 | $orderUser->setDateOfBirth($customer->birthday); |
||
| 176 | } |
||
| 177 | |||
| 178 | $orders = Order::getCustomerOrders($customer->id); |
||
| 179 | /** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */ |
||
| 180 | foreach ($orders as $order) { |
||
| 181 | if ($order['valid']) { |
||
| 182 | $orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
| 183 | $orderHistory |
||
| 184 | ->setAmount((string) floor(100 * $order['total_paid'])) |
||
| 185 | ->setDate(new \DateTime($order['date_add'])) |
||
| 186 | ; |
||
| 187 | $orderUser->addOrderHistory($orderHistory); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | $metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata(); |
||
| 192 | foreach ($metadata as $key => $metadatum) { |
||
| 193 | $metadataOrder->addMetadata($key, $metadatum); |
||
| 194 | } |
||
| 195 | |||
| 196 | $details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
| 197 | $details->setShippingCost((string) floor(100 * $cart->getTotalShippingCost())); |
||
| 198 | $items = $cart->getProducts(); |
||
| 199 | $promotedAmount = 0; |
||
| 200 | foreach ($items as $key => $item) { |
||
| 201 | $promotedProduct = $this->isPromoted($item['id_product']); |
||
| 202 | $product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
| 203 | $product |
||
| 204 | ->setAmount((string) floor(100 * $item['price_wt'])) |
||
| 205 | ->setQuantity($item['quantity']) |
||
| 206 | ->setDescription($item['name']); |
||
| 207 | if ($promotedProduct) { |
||
| 208 | $promotedAmount+=$product->getAmount(); |
||
| 209 | $productId = $item['id_product']; |
||
| 210 | $finalPrice = Product::getPriceStatic($productId); |
||
| 211 | $promotedMessage = 'Promoted Item: ' . $product->getDescription() . |
||
| 212 | ' Price: ' . $finalPrice . |
||
| 213 | ' Qty: ' . $product->getQuantity() . |
||
| 214 | ' Item ID: ' . $item['id_product']; |
||
| 215 | $metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
||
| 216 | } |
||
| 217 | $details->addProduct($product); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | $orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
||
| 222 | $totalAmount = (string) floor(100 * $cart->getOrderTotal(true)); |
||
| 223 | $orderShoppingCart |
||
| 224 | ->setDetails($details) |
||
| 225 | ->setOrderReference($cart->id) |
||
| 226 | ->setTotalAmount($totalAmount) |
||
| 227 | ->setPromotedAmount($promotedAmount) |
||
| 228 | ; |
||
| 229 | |||
| 230 | $orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
| 231 | $orderConfigurationUrls |
||
| 232 | ->setCancel($cancelUrl) |
||
| 233 | ->setKo($cancelUrl) |
||
| 234 | ->setAuthorizedNotificationCallback($notificationOkUrl) |
||
| 235 | ->setRejectedNotificationCallback(null) |
||
| 236 | ->setOk($okUrl) |
||
| 237 | ; |
||
| 238 | |||
| 239 | $orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
| 240 | $orderChannel |
||
| 241 | ->setAssistedSale(false) |
||
| 242 | ->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
||
| 243 | ; |
||
| 244 | |||
| 245 | $purchaseCountry = $this->getUserLanguage($shippingAddress, $billingAddress); |
||
| 246 | $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
||
| 247 | $orderConfiguration |
||
| 248 | ->setChannel($orderChannel) |
||
| 249 | ->setUrls($orderConfigurationUrls) |
||
| 250 | ->setPurchaseCountry($purchaseCountry) |
||
| 251 | ; |
||
| 252 | |||
| 253 | $order = new \Pagantis\OrdersApiClient\Model\Order(); |
||
| 254 | $order |
||
| 255 | ->setConfiguration($orderConfiguration) |
||
| 256 | ->setMetadata($metadataOrder) |
||
| 257 | ->setShoppingCart($orderShoppingCart) |
||
| 258 | ->setUser($orderUser) |
||
| 259 | ; |
||
| 260 | } catch (\Exception $exception) { |
||
| 261 | $this->saveLog(array(), $exception); |
||
| 262 | Tools::redirect($cancelUrl); |
||
| 263 | } |
||
| 264 | |||
| 265 | $url =''; |
||
| 266 | try { |
||
| 267 | $orderClient = new \Pagantis\OrdersApiClient\Client( |
||
| 268 | trim($pagantisPublicKey), |
||
| 269 | trim($pagantisPrivateKey) |
||
| 270 | ); |
||
| 271 | $order = $orderClient->createOrder($order); |
||
| 272 | |||
| 273 | if ($order instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
| 274 | $url = $order->getActionUrls()->getForm(); |
||
| 275 | /** @var string $orderId MD5 value */ |
||
| 276 | $orderId = $order->getId(); |
||
| 277 | $sql = "INSERT INTO `" . _DB_PREFIX_ . "pagantis_order` (`id`, `order_id`, `token`) |
||
| 278 | VALUES ('$cart->id','$orderId', '$urlToken')"; |
||
| 279 | $result = Db::getInstance()->execute($sql); |
||
| 280 | if (!$result) { |
||
| 281 | throw new UnknownException('Unable to save pagantis-order-id in database: '. $sql); |
||
| 282 | } |
||
| 283 | } else { |
||
| 284 | throw new OrderNotFoundException(); |
||
| 285 | } |
||
| 286 | } catch (\Exception $exception) { |
||
| 287 | $this->saveLog(array(), $exception); |
||
| 288 | Tools::redirect($cancelUrl); |
||
| 289 | } |
||
| 290 | |||
| 291 | if (!$iframe) { |
||
| 292 | Tools::redirect($url); |
||
| 293 | } else { |
||
| 294 | $this->context->smarty->assign(array( |
||
| 295 | 'url' => $url, |
||
| 296 | 'checkoutUrl' => $cancelUrl, |
||
| 297 | )); |
||
| 298 | |||
| 299 | try { |
||
| 300 | if (_PS_VERSION_ < 1.7) { |
||
| 301 | $this->setTemplate('payment-15.tpl'); |
||
| 302 | } else { |
||
| 303 | $this->setTemplate('module:pagantis/views/templates/front/payment-17.tpl'); |
||
| 304 | } |
||
| 305 | } catch (\Exception $exception) { |
||
| 306 | $this->saveLog(array(), $exception); |
||
| 307 | Tools::redirect($url); |
||
| 308 | } |
||
| 439 |