| Conditions | 15 |
| Paths | 2363 |
| Total Lines | 182 |
| Code Lines | 138 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 93 | public function execute() |
||
| 94 | { |
||
| 95 | try { |
||
| 96 | $cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
||
| 97 | $quote = $this->session->getQuote(); |
||
| 98 | /** @var Order $order */ |
||
| 99 | $lastOrder = $this->session->getLastRealOrder(); |
||
|
|
|||
| 100 | $params = $this->getRequest()->getParams(); |
||
| 101 | $customer = $quote->getCustomer(); |
||
| 102 | $shippingAddress = $quote->getShippingAddress(); |
||
| 103 | |||
| 104 | if (isset($params['email']) && $params['email']!='') { |
||
| 105 | $this->session->setEmail($params['email']); //Get guest email after refresh page |
||
| 106 | $customer->setEmail($params['email']); |
||
| 107 | $quote->setCheckoutMethod('guest'); |
||
| 108 | $quote->getBillingAddress()->setEmail($params['email']); |
||
| 109 | } elseif ($customer->getEmail()=='') { |
||
| 110 | $customer->setEmail($this->session->getEmail()); |
||
| 111 | $quote->setCheckoutMethod('guest'); |
||
| 112 | $quote->getBillingAddress()->setEmail($this->session->getEmail()); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** @var Quote $currentQuote */ |
||
| 116 | $currentQuote = $this->quoteRepository->get($quote->getId()); |
||
| 117 | $currentQuote->setCustomerEmail($customer->getEmail()); |
||
| 118 | $this->quoteRepository->save($currentQuote); |
||
| 119 | |||
| 120 | $userAddress = new Address(); |
||
| 121 | $userAddress |
||
| 122 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 123 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 124 | ->setCountryCode('ES') |
||
| 125 | ->setCity($shippingAddress->getCity()) |
||
| 126 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 127 | ; |
||
| 128 | |||
| 129 | $orderShippingAddress = new Address(); |
||
| 130 | $orderShippingAddress |
||
| 131 | ->setZipCode($shippingAddress->getPostcode()) |
||
| 132 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 133 | ->setCountryCode('ES') |
||
| 134 | ->setCity($shippingAddress->getCity()) |
||
| 135 | ->setAddress($shippingAddress->getStreetFull()) |
||
| 136 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 137 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 138 | ; |
||
| 139 | |||
| 140 | $orderBillingAddress = new Address(); |
||
| 141 | $billingAddress = $quote->getBillingAddress(); |
||
| 142 | $orderBillingAddress |
||
| 143 | ->setZipCode($billingAddress->getPostcode()) |
||
| 144 | ->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 145 | ->setCountryCode('ES') |
||
| 146 | ->setCity($billingAddress->getCity()) |
||
| 147 | ->setAddress($billingAddress->getStreetFull()) |
||
| 148 | ->setFixPhone($billingAddress->getTelephone()) |
||
| 149 | ->setMobilePhone($billingAddress->getTelephone()) |
||
| 150 | ; |
||
| 151 | |||
| 152 | $orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User(); |
||
| 153 | $billingAddress->setEmail($customer->getEmail()); |
||
| 154 | $orderUser |
||
| 155 | ->setAddress($userAddress) |
||
| 156 | ->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
||
| 157 | ->setBillingAddress($orderBillingAddress) |
||
| 158 | ->setEmail($customer->getEmail()) |
||
| 159 | ->setFixPhone($shippingAddress->getTelephone()) |
||
| 160 | ->setMobilePhone($shippingAddress->getTelephone()) |
||
| 161 | ->setShippingAddress($orderShippingAddress) |
||
| 162 | ; |
||
| 163 | |||
| 164 | if ($customer->getDob()) { |
||
| 165 | $orderUser->setDateOfBirth($customer->getDob()); |
||
| 166 | } |
||
| 167 | if ($customer->getTaxvat()!='') { |
||
| 168 | $orderUser->setDni($customer->getTaxvat()); |
||
| 169 | $orderBillingAddress->setDni($customer->getTaxvat()); |
||
| 170 | $orderShippingAddress->setDni($customer->getTaxvat()); |
||
| 171 | } |
||
| 172 | |||
| 173 | $previousOrders = $this->getOrders($customer->getId()); |
||
| 174 | foreach ($previousOrders as $orderElement) { |
||
| 175 | $orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
| 176 | $orderHistory |
||
| 177 | ->setAmount(intval(100 * $orderElement['grand_total'])) |
||
| 178 | ->setDate(new \DateTime($orderElement['created_at'])) |
||
| 179 | ; |
||
| 180 | $orderUser->addOrderHistory($orderHistory); |
||
| 181 | } |
||
| 182 | |||
| 183 | $details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
| 184 | $shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value'); |
||
| 185 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
| 186 | $items = $quote->getAllVisibleItems(); |
||
| 187 | foreach ($items as $key => $item) { |
||
| 188 | $product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
| 189 | $product |
||
| 190 | ->setAmount(intval(100 * $item->getPrice())) |
||
| 191 | ->setQuantity($item->getQty()) |
||
| 192 | ->setDescription($item->getName()); |
||
| 193 | $details->addProduct($product); |
||
| 194 | } |
||
| 195 | |||
| 196 | $orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart(); |
||
| 197 | $orderShoppingCart |
||
| 198 | ->setDetails($details) |
||
| 199 | ->setOrderReference($quote->getId()) |
||
| 200 | ->setPromotedAmount(0) |
||
| 201 | ->setTotalAmount(intval(strval(100 * $quote->getGrandTotal()))) |
||
| 202 | ; |
||
| 203 | |||
| 204 | $orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
| 205 | $quoteId = $quote->getId(); |
||
| 206 | $okUrl = $this->_url->getUrl('paylater/notify', ['_query' => ['quoteId'=>$quoteId]]); |
||
| 207 | $orderConfigurationUrls |
||
| 208 | ->setCancel($cancelUrl) |
||
| 209 | ->setKo($okUrl) |
||
| 210 | ->setNotificationCallback($okUrl) |
||
| 211 | ->setOk($okUrl) |
||
| 212 | ; |
||
| 213 | |||
| 214 | $orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
| 215 | $orderChannel |
||
| 216 | ->setAssistedSale(false) |
||
| 217 | ->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
||
| 218 | ; |
||
| 219 | $orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration(); |
||
| 220 | $orderConfiguration |
||
| 221 | ->setChannel($orderChannel) |
||
| 222 | ->setUrls($orderConfigurationUrls) |
||
| 223 | ; |
||
| 224 | |||
| 225 | $metadataOrder = new \PagaMasTarde\OrdersApiClient\Model\Order\Metadata(); |
||
| 226 | $metadata = $this->getMetadata(); |
||
| 227 | foreach ($metadata as $key => $metadatum) { |
||
| 228 | $metadataOrder->addMetadata($key, $metadatum); |
||
| 229 | } |
||
| 230 | |||
| 231 | $order = new \PagaMasTarde\OrdersApiClient\Model\Order(); |
||
| 232 | $order |
||
| 233 | ->setConfiguration($orderConfiguration) |
||
| 234 | ->setMetadata($metadataOrder) |
||
| 235 | ->setShoppingCart($orderShoppingCart) |
||
| 236 | ->setUser($orderUser) |
||
| 237 | ; |
||
| 238 | |||
| 239 | if ($this->config['public_key']=='' || $this->config['secret_key']=='') { |
||
| 240 | throw new \Exception('Public and Secret Key not found'); |
||
| 241 | } |
||
| 242 | |||
| 243 | $orderClient = new \PagaMasTarde\OrdersApiClient\Client( |
||
| 244 | $this->config['public_key'], |
||
| 245 | $this->config['secret_key'] |
||
| 246 | ); |
||
| 247 | |||
| 248 | $order = $orderClient->createOrder($order); |
||
| 249 | if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
||
| 250 | $url = $order->getActionUrls()->getForm(); |
||
| 251 | $result = $this->insertRow($quote->getId(), $order->getId()); |
||
| 252 | if (!$result) { |
||
| 253 | throw new \Exception('Unable to save pmt-order-id'); |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | throw new \Exception('Order not created'); |
||
| 257 | } |
||
| 258 | } catch (\Exception $exception) { |
||
| 259 | $this->insertLog($exception); |
||
| 260 | echo $cancelUrl; |
||
| 261 | exit; |
||
| 262 | } |
||
| 263 | |||
| 264 | $displayMode = $this->config['display_mode']; |
||
| 265 | if (!$displayMode) { |
||
| 266 | echo $url; |
||
| 267 | exit; |
||
| 268 | } else { |
||
| 269 | $iframeUrl = $this->_url->getUrl( |
||
| 270 | "paylater/Payment/iframe", |
||
| 271 | ['_query' => ["orderId"=>$order->getId()]] |
||
| 272 | ); |
||
| 273 | echo $iframeUrl; |
||
| 274 | exit; |
||
| 275 | } |
||
| 400 |