|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Front; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Common\TemplateController; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Http\Requests\Front\CheckoutRequest; |
|
8
|
|
|
use App\Model\Common\Setting; |
|
9
|
|
|
use App\Model\Common\Template; |
|
10
|
|
|
use App\Model\Order\Invoice; |
|
11
|
|
|
use App\Model\Order\InvoiceItem; |
|
12
|
|
|
use App\Model\Order\Order; |
|
13
|
|
|
use App\Model\Payment\Plan; |
|
14
|
|
|
use App\Model\Product\Addon; |
|
15
|
|
|
use App\Model\Product\Price; |
|
16
|
|
|
use App\Model\Product\Product; |
|
17
|
|
|
use App\Model\Product\Subscription; |
|
18
|
|
|
use App\User; |
|
19
|
|
|
use Cart; |
|
20
|
|
|
use Illuminate\Http\Request; |
|
21
|
|
|
|
|
22
|
|
|
class CheckoutController extends Controller { |
|
23
|
|
|
|
|
24
|
|
|
public $subscription; |
|
25
|
|
|
public $plan; |
|
26
|
|
|
public $templateController; |
|
27
|
|
|
public $product; |
|
28
|
|
|
public $price; |
|
29
|
|
|
public $user; |
|
30
|
|
|
public $setting; |
|
31
|
|
|
public $template; |
|
32
|
|
|
public $order; |
|
33
|
|
|
public $addon; |
|
34
|
|
|
public $invoice; |
|
35
|
|
|
public $invoiceItem; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() { |
|
38
|
|
|
$subscription = new Subscription(); |
|
39
|
|
|
$this->subscription = $subscription; |
|
40
|
|
|
|
|
41
|
|
|
$plan = new Plan(); |
|
42
|
|
|
$this->plan = $plan; |
|
43
|
|
|
|
|
44
|
|
|
$templateController = new TemplateController(); |
|
45
|
|
|
$this->templateController = $templateController; |
|
46
|
|
|
|
|
47
|
|
|
$product = new Product(); |
|
48
|
|
|
$this->product = $product; |
|
49
|
|
|
|
|
50
|
|
|
$price = new Price(); |
|
51
|
|
|
$this->price = $price; |
|
52
|
|
|
|
|
53
|
|
|
$user = new User(); |
|
54
|
|
|
$this->user = $user; |
|
55
|
|
|
|
|
56
|
|
|
$setting = new Setting(); |
|
57
|
|
|
$this->setting = $setting; |
|
58
|
|
|
|
|
59
|
|
|
$template = new Template(); |
|
60
|
|
|
$this->template = $template; |
|
61
|
|
|
|
|
62
|
|
|
$order = new Order(); |
|
63
|
|
|
$this->order = $order; |
|
64
|
|
|
|
|
65
|
|
|
$invoice = new Invoice(); |
|
66
|
|
|
$this->invoice = $invoice; |
|
67
|
|
|
|
|
68
|
|
|
$invoiceItem = new InvoiceItem(); |
|
69
|
|
|
$this->invoiceItem = $invoiceItem; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function CheckoutForm() { |
|
73
|
|
|
try { |
|
74
|
|
|
$content = Cart::getContent(); |
|
75
|
|
|
foreach ($content as $item) { |
|
76
|
|
|
$attributes[] = $item->attributes; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return view('themes.default1.front.checkout', compact('content', 'attributes')); |
|
80
|
|
|
} catch (\Exception $ex) { |
|
81
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function postCheckout(CheckoutRequest $request) { |
|
86
|
|
|
try { |
|
87
|
|
|
if (\Cart::getSubTotal() > 0) { |
|
88
|
|
|
$v = $this->validate($request, [ |
|
|
|
|
|
|
89
|
|
|
'payment_gateway' => 'required', |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
if (!$this->setting->where('id', 1)->first()) { |
|
93
|
|
|
return redirect()->back()->with('fails', 'Complete your settings'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$httpMethod = $request->method(); |
|
97
|
|
|
//dd($httpMethod); |
|
98
|
|
|
if ($httpMethod == 'PATCH') { |
|
99
|
|
|
//do update the auth user |
|
100
|
|
|
|
|
101
|
|
|
\Auth::user()->fill($request->input())->save(); |
|
102
|
|
|
} elseif ($httpMethod == 'POST') { |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
//do saving user |
|
106
|
|
|
|
|
107
|
|
|
$str = str_random(8); |
|
108
|
|
|
$password = \Hash::make($str); |
|
109
|
|
|
$this->user->password = $password; |
|
|
|
|
|
|
110
|
|
|
$this->user->fill($request->input())->save(); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$token = str_random(40); |
|
113
|
|
|
$activate = new \App\Model\User\AccountActivate(); |
|
114
|
|
|
$activate->create(['email' => $this->user->email, 'token' => $token]); |
|
|
|
|
|
|
115
|
|
|
$url = url("activate/$token"); |
|
116
|
|
|
|
|
117
|
|
|
//send welcome note |
|
118
|
|
|
$settings = $this->setting->where('id', 1)->first(); |
|
119
|
|
|
$from = $settings->email; |
|
120
|
|
|
$to = $this->user->email; |
|
|
|
|
|
|
121
|
|
|
$data = $this->template->where('id', $settings->where('id', 1)->first()->welcome_mail)->first()->data; |
|
122
|
|
|
$replace = ['name' => $this->user->first_name . ' ' . $this->user->last_name, 'username' => $this->user->email, 'password' => $str,'url'=>$url]; |
|
|
|
|
|
|
123
|
|
|
$this->templateController->Mailing($from, $to, $data, 'Welcome Email', $replace); |
|
124
|
|
|
|
|
125
|
|
|
\Auth::login($this->user); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/* |
|
129
|
|
|
* Do order, invoicing etc |
|
130
|
|
|
*/ |
|
131
|
|
|
$invoice_controller = new \App\Http\Controllers\Order\InvoiceController(); |
|
132
|
|
|
$invoice = $invoice_controller->GenerateInvoice(); |
|
133
|
|
|
$payment_method = $request->input('payment_gateway'); |
|
134
|
|
|
if (!$payment_method) { |
|
135
|
|
|
$payment_method = 'free'; |
|
136
|
|
|
} |
|
137
|
|
|
$invoiceid = $invoice->id; |
|
|
|
|
|
|
138
|
|
|
$amount = $invoice->grand_total; |
|
|
|
|
|
|
139
|
|
|
$payment = $invoice_controller->doPayment($payment_method, $invoiceid, $amount); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
//trasfer the control to event if cart price is not equal 0 |
|
142
|
|
|
if (Cart::getSubTotal() != 0) { |
|
143
|
|
|
\Event::fire(new \App\Events\PaymentGateway(['request' => $request, 'cart' => Cart::getContent(), 'order' => []])); |
|
144
|
|
|
} else { |
|
145
|
|
|
$this->checkoutAction($invoice); |
|
146
|
|
|
} |
|
147
|
|
|
return redirect()->back()->with('success', \Lang::get('message.check-your-mail-for-further-datails')); |
|
148
|
|
|
} catch (\Exception $ex) { |
|
149
|
|
|
dd($ex); |
|
150
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function checkoutAction($invoice) { |
|
155
|
|
|
try { |
|
156
|
|
|
//dd($invoice); |
|
157
|
|
|
//get elements from invoice |
|
158
|
|
|
$invoice_number = $invoice->number; |
|
159
|
|
|
$invoice_id = $invoice->id; |
|
160
|
|
|
$invoice_items = $this->invoiceItem->findOrFail($invoice_id); |
|
161
|
|
|
$product = $invoice_items->product_name; |
|
162
|
|
|
|
|
163
|
|
|
$user_id = \Auth::user()->id; |
|
164
|
|
|
$url = url("download/$user_id/$invoice_number"); |
|
165
|
|
|
|
|
166
|
|
|
//get system values |
|
167
|
|
|
$settings = new Setting(); |
|
168
|
|
|
$settings = $settings->findOrFail(1); |
|
169
|
|
|
$name = \Auth::user()->first_name.' '.\Auth::user()->last_name; |
|
170
|
|
|
$from = $settings->email; |
|
171
|
|
|
$to = \Auth::user()->email; |
|
172
|
|
|
$data = $this->template->where('name','download link')->first()->data; |
|
173
|
|
|
$subject = "download"; |
|
174
|
|
|
$replace = ['url'=>$url,'name'=>$name,'product'=>$product]; |
|
175
|
|
|
|
|
176
|
|
|
//send mail |
|
177
|
|
|
$template_controller = new TemplateController(); |
|
178
|
|
|
$template_controller->Mailing($from, $to, $data, $subject,$replace); |
|
179
|
|
|
} catch (\Exception $ex) { |
|
180
|
|
|
dd($ex); |
|
181
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// public function GenerateOrder() { |
|
|
|
|
|
|
186
|
|
|
// try { |
|
187
|
|
|
// |
|
188
|
|
|
// $products = []; |
|
189
|
|
|
// $items = \Cart::getContent(); |
|
190
|
|
|
// foreach ($items as $item) { |
|
191
|
|
|
// |
|
192
|
|
|
// //this is product |
|
193
|
|
|
// $id = $item->id; |
|
194
|
|
|
// $this->AddProductToOrder($id); |
|
195
|
|
|
// } |
|
196
|
|
|
// } catch (\Exception $ex) { |
|
197
|
|
|
// dd($ex); |
|
198
|
|
|
// throw new \Exception('Can not Generate Order'); |
|
199
|
|
|
// } |
|
200
|
|
|
// } |
|
201
|
|
|
// |
|
202
|
|
|
// public function AddProductToOrder($id) { |
|
203
|
|
|
// try { |
|
204
|
|
|
// $cart = \Cart::get($id); |
|
205
|
|
|
// $client = \Auth::user()->id; |
|
206
|
|
|
// $payment_method = \Input::get('payment_gateway'); |
|
207
|
|
|
// $promotion_code = ''; |
|
208
|
|
|
// $order_status = 'pending'; |
|
209
|
|
|
// $serial_key = ''; |
|
210
|
|
|
// $product = $id; |
|
211
|
|
|
// $addon = ''; |
|
212
|
|
|
// $domain = ''; |
|
213
|
|
|
// $price_override = $cart->getPriceSumWithConditions(); |
|
214
|
|
|
// $qty = $cart->quantity; |
|
215
|
|
|
// |
|
216
|
|
|
// $planid = $this->price->where('product_id', $id)->first()->subscription; |
|
217
|
|
|
// |
|
218
|
|
|
// $or = $this->order->create(['client' => $client, 'payment_method' => $payment_method, 'promotion_code' => $promotion_code, 'order_status' => $order_status, 'serial_key' => $serial_key, 'product' => $product, 'addon' => $addon, 'domain' => $domain, 'price_override' => $price_override, 'qty' => $qty]); |
|
219
|
|
|
// |
|
220
|
|
|
// $this->AddSubscription($or->id, $planid); |
|
221
|
|
|
// } catch (\Exception $ex) { |
|
222
|
|
|
// dd($ex); |
|
223
|
|
|
// throw new \Exception('Can not Generate Order for Product'); |
|
224
|
|
|
// } |
|
225
|
|
|
// } |
|
226
|
|
|
// |
|
227
|
|
|
// public function AddSubscription($orderid, $planid) { |
|
228
|
|
|
// try { |
|
229
|
|
|
// $days = $this->plan->where('id', $planid)->first()->days; |
|
230
|
|
|
// //dd($days); |
|
231
|
|
|
// if ($days > 0) { |
|
232
|
|
|
// $dt = \Carbon\Carbon::now(); |
|
233
|
|
|
// //dd($dt); |
|
234
|
|
|
// $user_id = \Auth::user()->id; |
|
235
|
|
|
// $ends_at = $dt->addDays($days); |
|
236
|
|
|
// } else { |
|
237
|
|
|
// $ends_at = ''; |
|
238
|
|
|
// } |
|
239
|
|
|
// $this->subscription->create(['user_id' => \Auth::user()->id, 'plan_id' => $planid, 'order_id' => $orderid, 'ends_at' => $ends_at]); |
|
240
|
|
|
// } catch (\Exception $ex) { |
|
241
|
|
|
// dd($ex); |
|
242
|
|
|
// throw new \Exception('Can not Generate Subscription'); |
|
243
|
|
|
// } |
|
244
|
|
|
// } |
|
245
|
|
|
// |
|
246
|
|
|
// public function GenerateInvoice() { |
|
247
|
|
|
// try { |
|
248
|
|
|
// |
|
249
|
|
|
// $user_id = \Auth::user()->id; |
|
250
|
|
|
// $number = rand(11111111, 99999999); |
|
251
|
|
|
// $date = \Carbon\Carbon::now(); |
|
252
|
|
|
// $grand_total = \Cart::getSubTotal(); |
|
253
|
|
|
// |
|
254
|
|
|
// $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total]); |
|
255
|
|
|
// foreach (\Cart::getContent() as $cart) { |
|
256
|
|
|
// $this->CreateInvoiceItems($invoice->id, $cart); |
|
257
|
|
|
// } |
|
258
|
|
|
// } catch (\Exception $ex) { |
|
259
|
|
|
// dd($ex); |
|
260
|
|
|
// throw new \Exception('Can not Generate Invoice'); |
|
261
|
|
|
// } |
|
262
|
|
|
// } |
|
263
|
|
|
// |
|
264
|
|
|
// public function CreateInvoiceItems($invoiceid, $cart) { |
|
265
|
|
|
// try { |
|
266
|
|
|
// |
|
267
|
|
|
// $product_name = $cart->name; |
|
268
|
|
|
// $regular_price = $cart->price; |
|
269
|
|
|
// $quantity = $cart->quantity; |
|
270
|
|
|
// $subtotal = $cart->getPriceSumWithConditions(); |
|
271
|
|
|
// |
|
272
|
|
|
// $tax_name = ''; |
|
273
|
|
|
// $tax_percentage = ''; |
|
274
|
|
|
// |
|
275
|
|
|
// foreach ($cart->attributes['tax'] as $tax) { |
|
276
|
|
|
// //dd($tax['name']); |
|
277
|
|
|
// $tax_name .= $tax['name'] . ','; |
|
278
|
|
|
// $tax_percentage .= $tax['rate'] . ','; |
|
279
|
|
|
// } |
|
280
|
|
|
// |
|
281
|
|
|
// //dd($tax_name); |
|
282
|
|
|
// |
|
283
|
|
|
// $invoiceItem = $this->invoiceItem->create(['invoice_id' => $invoiceid, 'product_name' => $product_name, 'regular_price' => $regular_price, 'quantity' => $quantity, 'tax_name' => $tax_name, 'tax_percentage' => $tax_percentage, 'subtotal' => $subtotal]); |
|
284
|
|
|
// } catch (\Exception $ex) { |
|
285
|
|
|
// dd($ex); |
|
286
|
|
|
// throw new \Exception('Can not create Invoice Items'); |
|
287
|
|
|
// } |
|
288
|
|
|
// } |
|
289
|
|
|
} |
|
290
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.