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