1
|
|
|
<?php |
2
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\shop; |
3
|
|
|
|
4
|
|
|
use Stripe\Charge; |
5
|
|
|
use Stripe\Stripe; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Stripe\Exception\CardException; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Mongi\Mongicommerce\Models\Cart; |
10
|
|
|
use Mongi\Mongicommerce\Models\Order; |
11
|
|
|
use Illuminate\Support\Facades\Session; |
12
|
|
|
use Stripe\Exception\ApiErrorException; |
13
|
|
|
use Stripe\Exception\RateLimitException; |
14
|
|
|
use Mongi\Mongicommerce\Models\OrderStatus; |
15
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
16
|
|
|
use Mongi\Mongicommerce\Models\TypePayment; |
17
|
|
|
use Mongi\Mongicommerce\Models\AdminSetting; |
18
|
|
|
use Stripe\Exception\ApiConnectionException; |
19
|
|
|
use Stripe\Exception\AuthenticationException; |
20
|
|
|
use Stripe\Exception\InvalidRequestException; |
21
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
22
|
|
|
use Mongi\Mongicommerce\Models\ProductsOrder; |
23
|
|
|
|
24
|
|
|
class ShopPaymentController extends Controller |
25
|
|
|
{ |
26
|
|
|
public function page(){ |
27
|
|
|
$total = session('checkout.total'); |
28
|
|
|
$api_stripe_key = AdminSetting::getStripeApiKey(); |
29
|
|
|
$iban = AdminSetting::getIban(); |
30
|
|
|
return view('mongicommerce.pages.payment',compact('total','api_stripe_key','iban')); |
31
|
|
|
} |
32
|
|
|
|
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
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function normalPayment(Request $r){ |
139
|
|
|
$type = $r->get('type_payment'); |
140
|
|
|
switch ($type){ |
141
|
|
|
case 'iban': |
142
|
|
|
$typePayment = TypePayment::BONIFICO; |
143
|
|
|
$orderStatus = OrderStatus::ATTESA_PAGAMENTO; |
144
|
|
|
break; |
145
|
|
|
case 'negozio': |
146
|
|
|
$typePayment = TypePayment::IN_NEGOZIO; |
147
|
|
|
$orderStatus = OrderStatus::ATTESA_PAGAMENTO; |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$total = session('checkout.total'); |
152
|
|
|
$cost_shipping = session('checkout.shipping_price'); |
|
|
|
|
153
|
|
|
$order_weight = session('checkout.total_weight'); |
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
|
|
$note_delivery = session('checkout.note_delivery'); |
157
|
|
|
$get_in_shop_checkbox = session('checkout.get_in_shop_checkbox'); |
158
|
|
|
|
159
|
|
|
$order = new Order(); |
160
|
|
|
$order->user_id = Auth::user()->id; |
|
|
|
|
161
|
|
|
$order->total_price = $total; |
162
|
|
|
$order->shipping_price = 0; |
163
|
|
|
$order->order_weight = 0; |
164
|
|
|
$order->status_id = $orderStatus; |
|
|
|
|
165
|
|
|
$order->note_delivery = $note_delivery; |
166
|
|
|
$order->payment_type_id = $typePayment; |
|
|
|
|
167
|
|
|
$order->pick_up_in_shop = $get_in_shop_checkbox == 'true' ? true : false; |
168
|
|
|
$order->save(); |
169
|
|
|
|
170
|
|
|
//save into order_products |
171
|
|
|
|
172
|
|
|
$products = Cart::where('user_id',Auth::user()->id)->get(); |
173
|
|
|
foreach ($products as $product){ |
174
|
|
|
$order_products = new ProductsOrder(); |
175
|
|
|
$order_products->order_id = $order->id; |
176
|
|
|
$order_products->product_item_id = $product->product_item_id; |
177
|
|
|
$order_products->number_products = $product->quantity; |
178
|
|
|
$order_products->save(); |
179
|
|
|
|
180
|
|
|
//scalo quantità prodotti |
181
|
|
|
$productM = ProductItem::find($product->product_item_id); |
182
|
|
|
$productM->quantity = $productM->quantity - $product->quantity; |
183
|
|
|
$productM->save(); |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
//empty cart |
187
|
|
|
Cart::emptyCart(); |
188
|
|
|
Session::flash('success', 'Ordine inoltrato con successo'); |
189
|
|
|
return response()->json(['url'=>route('shop.user.orders')]); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|