| Conditions | 11 |
| Paths | 463 |
| Total Lines | 101 |
| Code Lines | 73 |
| 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 |
||
| 33 | public function pay(Request $request){ |
||
| 34 | |||
| 35 | try { |
||
| 36 | $total = session('checkout.total'); |
||
| 37 | $cost_shipping = session('checkout.shipping_price'); |
||
|
|
|||
| 38 | $order_weight = session('checkout.total_weight'); |
||
| 39 | |||
| 40 | $order_id = 0; |
||
| 41 | $check_order = Order::orderBy('created_at','desc')->first(); |
||
| 42 | if(is_null($check_order)){ |
||
| 43 | $order_id = 1; |
||
| 44 | }else{ |
||
| 45 | $order_id = $check_order->id + 1; |
||
| 46 | } |
||
| 47 | Stripe::setApiKey(AdminSetting::getStripeApiSecretKey()); |
||
| 48 | // Use Stripe's library to make requests... |
||
| 49 | Charge::create ([ |
||
| 50 | "amount" => number_format(($total*100) , 0, '', ''), |
||
| 51 | "currency" => "eur", |
||
| 52 | "source" => $request->stripeToken, |
||
| 53 | "description" => "Pagamento ordine N.".$order_id |
||
| 54 | ]); |
||
| 55 | |||
| 56 | $note_delivery = session('checkout.note_delivery'); |
||
| 57 | $get_in_shop_checkbox = session('checkout.get_in_shop_checkbox'); |
||
| 58 | |||
| 59 | $order = new Order(); |
||
| 60 | $order->user_id = Auth::user()->id; |
||
| 61 | $order->total_price = $total; |
||
| 62 | $order->shipping_price = 0; |
||
| 63 | $order->order_weight = 0; |
||
| 64 | $order->status_id = OrderStatus::IN_PREPARAZIONE; |
||
| 65 | $order->note_delivery = $note_delivery; |
||
| 66 | $order->payment_type_id = TypePayment::STRIPE; |
||
| 67 | $order->pick_up_in_shop = $get_in_shop_checkbox == 'true' ? true : false; |
||
| 68 | $order->save(); |
||
| 69 | //save into order_products |
||
| 70 | |||
| 71 | $products = Cart::where('user_id',Auth::user()->id)->get(); |
||
| 72 | foreach ($products as $product){ |
||
| 73 | $order_products = new ProductsOrder(); |
||
| 74 | $order_products->order_id = $order->id; |
||
| 75 | $order_products->product_item_id = $product->product_item_id; |
||
| 76 | $order_products->number_products = $product->quantity; |
||
| 77 | $order_products->save(); |
||
| 78 | |||
| 79 | //scalo quantità prodotti |
||
| 80 | $productM = ProductItem::find($product->product_item_id); |
||
| 81 | $productM->quantity = $productM->quantity - $product->quantity; |
||
| 82 | $productM->save(); |
||
| 83 | |||
| 84 | } |
||
| 85 | //empty cart |
||
| 86 | Cart::emptyCart(); |
||
| 87 | Session::flash('success', 'Pagamento avvenuto con successo'); |
||
| 88 | return redirect(route('shop.user.orders')); |
||
| 89 | |||
| 90 | } catch(CardException $e) { |
||
| 91 | |||
| 92 | // Since it's a decline, \Stripe\Exception\CardException will be caught |
||
| 93 | $error = '<h4>'.$e->getError()->message.'</h4><br>'; |
||
| 94 | $error .= 'Status: ' . $e->getHttpStatus() . '<br>'; |
||
| 95 | $error .= 'Type is:' . $e->getError()->type . '<br>'; |
||
| 96 | $error .= 'Code is:' . $e->getError()->code . '<br>'; |
||
| 97 | // param is '' in this case |
||
| 98 | $error .= 'Param is:' . $e->getError()->param . '<br>'; |
||
| 99 | |||
| 100 | Session::flash('error', $error); |
||
| 101 | return back(); |
||
| 102 | } catch (RateLimitException $e) { |
||
| 103 | // Too many requests made to the API too quickly |
||
| 104 | $error = $e->getError()->message; |
||
| 105 | Session::flash('error', $error); |
||
| 106 | return back(); |
||
| 107 | } catch (InvalidRequestException $e) { |
||
| 108 | // Invalid parameters were supplied to Stripe's API |
||
| 109 | $error = $e->getError()->message; |
||
| 110 | Session::flash('error', $error); |
||
| 111 | return back(); |
||
| 112 | } catch (AuthenticationException $e) { |
||
| 113 | // Authentication with Stripe's API failed |
||
| 114 | // (maybe you changed API keys recently) |
||
| 115 | $error = $e->getError()->message; |
||
| 116 | Session::flash('error', $error); |
||
| 117 | return back(); |
||
| 118 | } catch (ApiConnectionException $e) { |
||
| 119 | // Network communication with Stripe failed |
||
| 120 | $error = $e->getError()->message; |
||
| 121 | Session::flash('error', $error); |
||
| 122 | return back(); |
||
| 123 | } catch (ApiErrorException $e) { |
||
| 124 | // Display a very generic error to the user, and maybe send |
||
| 125 | // yourself an email |
||
| 126 | $error = $e->getError()->message; |
||
| 127 | Session::flash('error', $error); |
||
| 128 | return back(); |
||
| 129 | } catch (Exception $e) { |
||
| 130 | // Something else happened, completely unrelated to Stripe |
||
| 131 | $error = $e->getError()->message; |
||
| 132 | Session::flash('error', $error); |
||
| 133 | return back(); |
||
| 134 | } |
||
| 193 |