Conditions | 20 |
Paths | 7780 |
Total Lines | 229 |
Code Lines | 178 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
142 | public function pagantisReceiptPage($order_id) |
||
143 | { |
||
144 | try { |
||
145 | require_once(__ROOT__.'/vendor/autoload.php'); |
||
146 | global $woocommerce; |
||
147 | $order = new WC_Order($order_id); |
||
148 | $order->set_payment_method(ucfirst($this->id)); |
||
149 | $order->save(); |
||
150 | |||
151 | if (!isset($order)) { |
||
152 | throw new Exception(_("Order not found")); |
||
153 | } |
||
154 | |||
155 | $shippingAddress = $order->get_address('shipping'); |
||
156 | $billingAddress = $order->get_address('billing'); |
||
157 | if ($shippingAddress['address_1'] == '') { |
||
158 | $shippingAddress = $billingAddress; |
||
159 | } |
||
160 | |||
161 | $customer_id = $order->get_customer_id(); |
||
162 | $national_id = maybeGetIDNumber($order , $customer_id); |
||
163 | $tax_id = maybeGetTaxIDNumber($order , $customer_id); |
||
164 | $phoneNumber = maybeGetPhoneNumber($order , $customer_id); |
||
165 | |||
166 | |||
167 | $userAddress = new Address(); |
||
168 | $userAddress |
||
169 | ->setZipCode($shippingAddress['postcode']) |
||
170 | ->setFullName($shippingAddress['first_name']." ".$shippingAddress['last_name']) |
||
171 | ->setCountryCode($shippingAddress['country']!='' ? strtoupper($shippingAddress['country']) : strtoupper($this->language)) |
||
172 | ->setCity($shippingAddress['city']) |
||
173 | ->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2']) |
||
174 | ->setDni($national_id) |
||
175 | |||
176 | ; |
||
177 | $orderShippingAddress = new Address(); |
||
178 | $orderShippingAddress |
||
179 | ->setZipCode($shippingAddress['postcode']) |
||
180 | ->setFullName($shippingAddress['first_name']." ".$shippingAddress['last_name']) |
||
181 | ->setCountryCode($shippingAddress['country']!='' ? strtoupper($shippingAddress['country']) : strtoupper($this->language)) |
||
182 | ->setCity($shippingAddress['city']) |
||
183 | ->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2']) |
||
184 | ->setFixPhone($phoneNumber) |
||
185 | ->setMobilePhone($phoneNumber) |
||
186 | ->setNationalId($national_id) |
||
187 | ->setDni($national_id) |
||
188 | ->setTaxId($tax_id) |
||
189 | ; |
||
190 | $orderBillingAddress = new Address(); |
||
191 | $orderBillingAddress |
||
192 | ->setZipCode($billingAddress['postcode']) |
||
193 | ->setFullName($billingAddress['first_name']." ".$billingAddress['last_name']) |
||
194 | ->setCountryCode($billingAddress['country']!='' ? strtoupper($billingAddress['country']) : strtoupper($this->language)) |
||
195 | ->setCity($billingAddress['city']) |
||
196 | ->setAddress($billingAddress['address_1']." ".$billingAddress['address_2']) |
||
197 | ->setFixPhone($phoneNumber) |
||
198 | ->setMobilePhone($phoneNumber) |
||
199 | ->setNationalId($national_id) |
||
200 | ->setTaxId($tax_id) |
||
201 | ; |
||
202 | $orderUser = new User(); |
||
203 | $orderUser |
||
204 | ->setAddress($userAddress) |
||
205 | ->setFullName($billingAddress['first_name']." ".$billingAddress['last_name']) |
||
206 | ->setBillingAddress($orderBillingAddress) |
||
207 | ->setEmail($billingAddress['email']) |
||
208 | ->setFixPhone($phoneNumber) |
||
209 | ->setMobilePhone($phoneNumber) |
||
210 | ->setShippingAddress($orderShippingAddress) |
||
211 | ->setNationalId($national_id) |
||
212 | ->setTaxId($tax_id) |
||
213 | ; |
||
214 | |||
215 | if (!empty(maybeGetDateOfBirth($customer_id))) { |
||
216 | $orderUser->setDateOfBirth($birthday); |
||
217 | } |
||
218 | |||
219 | $previousOrders = getOrders($order->get_user(), $billingAddress['email']); |
||
220 | foreach ($previousOrders as $previousOrder) { |
||
221 | $orderHistory = new OrderHistory(); |
||
222 | $orderElement = wc_get_order($previousOrder); |
||
223 | $orderCreated = $orderElement->get_date_created(); |
||
224 | $orderHistory |
||
225 | ->setAmount(intval(100 * $orderElement->get_total())) |
||
226 | ->setDate(new \DateTime($orderCreated->date('Y-m-d H:i:s'))) |
||
227 | ; |
||
228 | $orderUser->addOrderHistory($orderHistory); |
||
229 | } |
||
230 | |||
231 | $metadataOrder = new Metadata(); |
||
232 | $metadata = array( |
||
233 | 'pg_module' => 'woocommerce', |
||
234 | 'pg_version' => getModuleVersion(), |
||
235 | 'ec_module' => 'woocommerce', |
||
236 | 'ec_version' => WC()->version |
||
237 | ); |
||
238 | |||
239 | foreach ($metadata as $key => $metadatum) { |
||
240 | $metadataOrder->addMetadata($key, $metadatum); |
||
241 | } |
||
242 | |||
243 | $details = new Details(); |
||
244 | $shippingCost = $order->shipping_total; |
||
245 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
246 | $items = $order->get_items(); |
||
247 | $promotedAmount = 0; |
||
248 | foreach ($items as $key => $item) { |
||
249 | $wcProduct = $item->get_product(); |
||
250 | $product = new Product(); |
||
251 | $productDescription = sprintf( |
||
252 | '%s %s %s', |
||
253 | $wcProduct->get_name(), |
||
254 | $wcProduct->get_description(), |
||
255 | $wcProduct->get_short_description() |
||
256 | ); |
||
257 | $productDescription = substr($productDescription, 0, 9999); |
||
258 | |||
259 | $product |
||
260 | ->setAmount(intval(100 * ($item->get_total() + $item->get_total_tax()))) |
||
261 | ->setQuantity($item->get_quantity()) |
||
262 | ->setDescription($productDescription) |
||
263 | ; |
||
264 | $details->addProduct($product); |
||
265 | |||
266 | $promotedProduct = isProductPromoted($item->get_product_id()); |
||
267 | if ($promotedProduct == 'true') { |
||
268 | $promotedAmount+=$product->getAmount(); |
||
269 | $promotedMessage = 'Promoted Item: ' . $wcProduct->get_name() . |
||
270 | ' - Price: ' . $item->get_total() . |
||
271 | ' - Qty: ' . $product->getQuantity() . |
||
272 | ' - Item ID: ' . $item['id_product']; |
||
273 | $promotedMessage = substr($promotedMessage, 0, 999); |
||
274 | $metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | $orderShoppingCart = new ShoppingCart(); |
||
279 | $orderShoppingCart |
||
280 | ->setDetails($details) |
||
281 | ->setOrderReference($order->get_id()) |
||
282 | ->setPromotedAmount($promotedAmount) |
||
283 | ->setTotalAmount(intval(strval(100 * $order->total))) |
||
284 | ; |
||
285 | $orderConfigurationUrls = new Urls(); |
||
286 | $cancelUrl = $this->getKoUrl($order); |
||
287 | $callback_arg = array('wc-api'=>'wcpagantisgateway', |
||
288 | 'key'=>$order->get_order_key(), |
||
289 | 'order-received'=>$order->get_id(), |
||
290 | 'origin' => '', |
||
291 | 'token' => $this->urlToken4x |
||
292 | |||
293 | ); |
||
294 | |||
295 | $callback_arg_user = $callback_arg; |
||
296 | $callback_arg_user['origin'] = 'redirect'; |
||
297 | $callback_arg_user['product'] = Ucfirst(WcPagantis4xGateway::METHOD_ID); |
||
298 | $callback_url_user = add_query_arg($callback_arg_user, home_url('/')); |
||
299 | |||
300 | $callback_arg_notif = $callback_arg; |
||
301 | $callback_arg_notif['origin'] = 'notification'; |
||
302 | $callback_arg_notif['product'] = Ucfirst(WcPagantis4xGateway::METHOD_ID); |
||
303 | $callback_url_notif = add_query_arg($callback_arg_notif, home_url('/')); |
||
304 | |||
305 | $orderConfigurationUrls |
||
306 | ->setCancel($cancelUrl) |
||
307 | ->setKo($callback_url_user) |
||
308 | ->setAuthorizedNotificationCallback($callback_url_notif) |
||
309 | ->setRejectedNotificationCallback(null) |
||
310 | ->setOk($callback_url_user) |
||
311 | ; |
||
312 | $orderChannel = new Channel(); |
||
313 | $orderChannel |
||
314 | ->setAssistedSale(false) |
||
315 | ->setType(Channel::ONLINE) |
||
316 | ; |
||
317 | |||
318 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
319 | $purchaseCountry = |
||
320 | in_array(strtolower($this->language), $allowedCountries) ? $this->language : |
||
321 | in_array(strtolower($shippingAddress['country']), $allowedCountries) ? $shippingAddress['country'] : |
||
322 | in_array(strtolower($billingAddress['country']), $allowedCountries) ? $billingAddress['country'] : null; |
||
323 | |||
324 | $orderConfiguration = new Configuration(); |
||
325 | $orderConfiguration |
||
326 | ->setChannel($orderChannel) |
||
327 | ->setUrls($orderConfigurationUrls) |
||
328 | ->setPurchaseCountry($purchaseCountry) |
||
329 | ; |
||
330 | |||
331 | $orderApiClient = new Order(); |
||
332 | $orderApiClient |
||
333 | ->setConfiguration($orderConfiguration) |
||
334 | ->setMetadata($metadataOrder) |
||
335 | ->setShoppingCart($orderShoppingCart) |
||
336 | ->setUser($orderUser) |
||
337 | ; |
||
338 | |||
339 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
340 | if ($cfg['pagantis_public_key_4x']=='' || $cfg['pagantis_private_key_4x']=='') { |
||
341 | throw new \Exception('Public and Secret Key not found'); |
||
342 | } |
||
343 | $orderClient = new Client($cfg['pagantis_public_key_4x'], $cfg['pagantis_private_key_4x']); |
||
344 | $pagantisOrder = $orderClient->createOrder($orderApiClient); |
||
345 | if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
346 | $url = $pagantisOrder->getActionUrls()->getForm(); |
||
347 | addOrderToProcessingQueue($pagantisOrder->getId(), $order->get_id(), $this->urlToken4x,self::METHOD_ID); |
||
348 | } else { |
||
349 | throw new OrderNotFoundException(); |
||
350 | } |
||
351 | |||
352 | if ($url=="") { |
||
353 | throw new Exception(_("No ha sido posible obtener una respuesta de Pagantis")); |
||
354 | } elseif ($this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE']=='0') { |
||
355 | wp_redirect($url); |
||
356 | exit; |
||
357 | } else { |
||
358 | // @todo TM refactor with wp_localize_script - maybe put in other function |
||
359 | $template_fields = array( |
||
360 | 'url' => $url, |
||
361 | 'checkoutUrl' => $cancelUrl |
||
362 | ); |
||
363 | wc_get_template('iframe.php', $template_fields, '', $this->template_path); |
||
364 | } |
||
365 | } catch (\Exception $exception) { |
||
366 | wc_add_notice(__('Payment error ', 'pagantis') . $exception->getMessage(), 'error'); |
||
367 | insertLogEntry($exception); |
||
368 | $checkout_url = get_permalink(wc_get_page_id('checkout')); |
||
369 | wp_redirect($checkout_url); |
||
370 | exit; |
||
371 | } |
||
689 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths