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