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