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