1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Order; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\License\LicensePermissionsController; |
6
|
|
|
use App\Model\Common\StatusSetting; |
7
|
|
|
use App\Model\Order\Order; |
8
|
|
|
use App\Model\Product\Product; |
9
|
|
|
use App\Plugins\Stripe\Controllers\SettingsController; |
10
|
|
|
use App\Traits\Order\UpdateDates; |
11
|
|
|
use App\User; |
12
|
|
|
use Bugsnag; |
13
|
|
|
use Crypt; |
14
|
|
|
|
15
|
|
|
class BaseOrderController extends ExtendedOrderController |
16
|
|
|
{ |
17
|
|
|
protected $sendMail; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
$this->middleware('admin'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
use UpdateDates; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public function getUrl($model, $status, $subscriptionId) |
28
|
|
|
{ |
29
|
|
|
$url = ''; |
30
|
|
|
if ($status == 'success') { |
31
|
|
|
if ($subscriptionId) { |
32
|
|
|
$url = '<a href='.url('renew/'.$subscriptionId)." |
33
|
|
|
class='btn btn-sm btn-secondary btn-xs'".tooltip('Renew')."<i class='fas fa-credit-card' |
34
|
|
|
style='color:white;'> </i></a>"; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return '<p><a href='.url('orders/'.$model->id)." |
39
|
|
|
class='btn btn-sm btn-secondary btn-xs'".tooltip('View')."<i class='fas fa-eye' |
40
|
|
|
style='color:white;'> </i></a> $url</p>"; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* inserting the values to orders table. |
45
|
|
|
* |
46
|
|
|
* @param type $invoiceid |
47
|
|
|
* @param type $order_status |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function executeOrder($invoiceid, $order_status = 'executed') |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$invoice_items = $this->invoice_items->where('invoice_id', $invoiceid)->get(); |
57
|
|
|
$user_id = $this->invoice->find($invoiceid)->user_id; |
58
|
|
|
if (count($invoice_items) > 0) { |
59
|
|
|
foreach ($invoice_items as $item) { |
60
|
|
|
if ($item) { |
61
|
|
|
$items = $this->getIfItemPresent($item, $invoiceid, $user_id, $order_status); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return 'success'; |
67
|
|
|
} catch (\Exception $ex) { |
68
|
|
|
app('log')->error($ex->getMessage()); |
69
|
|
|
Bugsnag::notifyException($ex); |
70
|
|
|
|
71
|
|
|
throw new \Exception($ex->getMessage()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getIfItemPresent($item, $invoiceid, $user_id, $order_status) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$product = $this->product->where('name', $item->product_name)->first()->id; |
79
|
|
|
$version = $this->product->where('name', $item->product_name)->first()->version; |
80
|
|
|
if ($version == null) { |
81
|
|
|
//Get Version from Product Upload Table |
82
|
|
|
$version = $this->product_upload->where('product_id', $product)->pluck('version')->first(); |
83
|
|
|
} |
84
|
|
|
$serial_key = $this->generateSerialKey($product, $item->agents); //Send Product Id and Agents to generate Serial Key |
85
|
|
|
$domain = $item->domain; |
86
|
|
|
$plan_id = $this->plan($item->id); |
87
|
|
|
$order = $this->order->create([ |
88
|
|
|
|
89
|
|
|
'invoice_id' => $invoiceid, |
90
|
|
|
'invoice_item_id' => $item->id, |
91
|
|
|
'client' => $user_id, |
92
|
|
|
'order_status' => $order_status, |
93
|
|
|
'serial_key' => Crypt::encrypt($serial_key), |
94
|
|
|
'product' => $product, |
95
|
|
|
'price_override' => $item->subtotal, |
96
|
|
|
'qty' => $item->quantity, |
97
|
|
|
'domain' => $domain, |
98
|
|
|
'number' => $this->generateNumber(), |
99
|
|
|
]); |
100
|
|
|
$this->addOrderInvoiceRelation($invoiceid, $order->id); |
101
|
|
|
|
102
|
|
|
if ($plan_id != 0) { |
103
|
|
|
$this->addSubscription($order->id, $plan_id, $version, $product, $serial_key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (emailSendingStatus()) { |
107
|
|
|
$this->sendOrderMail($user_id, $order->id, $item->id); |
108
|
|
|
} |
109
|
|
|
//Update Subscriber To Mailchimp |
110
|
|
|
$mailchimpStatus = StatusSetting::pluck('mailchimp_status')->first(); |
111
|
|
|
if ($mailchimpStatus) { |
112
|
|
|
$this->addtoMailchimp($product, $user_id, $item); |
113
|
|
|
} |
114
|
|
|
} catch (\Exception $ex) { |
115
|
|
|
Bugsnag::notifyException($ex); |
116
|
|
|
app('log')->error($ex->getMessage()); |
117
|
|
|
|
118
|
|
|
throw new \Exception($ex->getMessage()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function addToMailchimp($product, $user_id, $item) |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
$mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
126
|
|
|
$email = User::where('id', $user_id)->pluck('email')->first(); |
127
|
|
|
if ($item->subtotal > 0) { |
128
|
|
|
$r = $mailchimp->updateSubscriberForPaidProduct($email, $product); |
|
|
|
|
129
|
|
|
} else { |
130
|
|
|
$r = $mailchimp->updateSubscriberForFreeProduct($email, $product); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} catch (\Exception $ex) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* inserting the values to subscription table. |
139
|
|
|
* |
140
|
|
|
* @param int $orderid |
141
|
|
|
* @param int $planid |
142
|
|
|
* @param string $version |
143
|
|
|
* @param int $product |
144
|
|
|
* @param string $serial_key |
145
|
|
|
* |
146
|
|
|
* @throws \Exception |
147
|
|
|
* |
148
|
|
|
* @author Ashutosh Pathak <[email protected]> |
149
|
|
|
*/ |
150
|
|
|
public function addSubscription($orderid, $planid, $version, $product, $serial_key) |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
|
|
$permissions = LicensePermissionsController::getPermissionsForProduct($product); |
154
|
|
|
if ($version == null) { |
155
|
|
|
$version = ''; |
156
|
|
|
} |
157
|
|
|
$days = $this->plan->where('id', $planid)->first()->days; |
158
|
|
|
$licenseExpiry = $this->getLicenseExpiryDate($permissions['generateLicenseExpiryDate'], $days); |
159
|
|
|
$updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $days); |
160
|
|
|
$supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $days); |
161
|
|
|
$user_id = $this->order->find($orderid)->client; |
162
|
|
|
$this->subscription->create(['user_id' => $user_id, |
163
|
|
|
'plan_id' => $planid, 'order_id' => $orderid, 'update_ends_at' =>$updatesExpiry, 'ends_at' => $licenseExpiry, 'support_ends_at'=>$supportExpiry, 'version'=> $version, 'product_id' =>$product, ]); |
164
|
|
|
|
165
|
|
|
$licenseStatus = StatusSetting::pluck('license_status')->first(); |
166
|
|
|
if ($licenseStatus == 1) { |
167
|
|
|
$cont = new \App\Http\Controllers\License\LicenseController(); |
168
|
|
|
$createNewLicense = $cont->createNewLicene($orderid, $product, $user_id, $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} catch (\Exception $ex) { |
171
|
|
|
Bugsnag::notifyException($ex); |
172
|
|
|
app('log')->error($ex->getMessage()); |
173
|
|
|
|
174
|
|
|
throw new \Exception('Can not Generate Subscription'); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the Expiry Date for License. |
180
|
|
|
* |
181
|
|
|
* @param bool $permissions [Whether Permissons for generating License Expiry Date are there or not] |
182
|
|
|
* @param int $days [No of days that would get addeed to the current date ] |
183
|
|
|
* |
184
|
|
|
* @return string [The final License Expiry date that is generated] |
185
|
|
|
*/ |
186
|
|
|
protected function getLicenseExpiryDate(bool $permissions, $days) |
187
|
|
|
{ |
188
|
|
|
$ends_at = ''; |
189
|
|
|
if ($days > 0 && $permissions == 1) { |
190
|
|
|
$dt = \Carbon\Carbon::now(); |
191
|
|
|
$ends_at = $dt->addDays($days); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $ends_at; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the Expiry Date for Updates. |
199
|
|
|
* |
200
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
201
|
|
|
* @param int $days [No of days that would get added to the current date ] |
202
|
|
|
* |
203
|
|
|
* @return string [The final Updates Expiry date that is generated] |
204
|
|
|
*/ |
205
|
|
|
protected function getUpdatesExpiryDate(bool $permissions, $days) |
206
|
|
|
{ |
207
|
|
|
$update_ends_at = ''; |
208
|
|
|
if ($days > 0 && $permissions == 1) { |
209
|
|
|
$dt = \Carbon\Carbon::now(); |
210
|
|
|
$update_ends_at = $dt->addDays($days); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $update_ends_at; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the Expiry Date for Support. |
218
|
|
|
* |
219
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
220
|
|
|
* @param int $days [No of days that would get added to the current date ] |
221
|
|
|
* |
222
|
|
|
* @return string [The final Suport Expiry date that is generated] |
223
|
|
|
*/ |
224
|
|
|
protected function getSupportExpiryDate(bool $permissions, $days) |
225
|
|
|
{ |
226
|
|
|
$support_ends_at = ''; |
227
|
|
|
if ($days > 0 && $permissions == 1) { |
228
|
|
|
$dt = \Carbon\Carbon::now(); |
229
|
|
|
$support_ends_at = $dt->addDays($days); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $support_ends_at; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function addOrderInvoiceRelation($invoiceid, $orderid) |
236
|
|
|
{ |
237
|
|
|
try { |
238
|
|
|
$relation = new \App\Model\Order\OrderInvoiceRelation(); |
239
|
|
|
$relation->create(['order_id' => $orderid, 'invoice_id' => $invoiceid]); |
240
|
|
|
} catch (\Exception $ex) { |
241
|
|
|
Bugsnag::notifyException($ex); |
242
|
|
|
|
243
|
|
|
throw new \Exception($ex->getMessage()); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function sendOrderMail($userid, $orderid, $itemid) |
248
|
|
|
{ |
249
|
|
|
//order |
250
|
|
|
$order = $this->order->find($orderid); |
251
|
|
|
//product |
252
|
|
|
$product = $this->product($itemid); |
253
|
|
|
//user |
254
|
|
|
$productId = Product::where('name', $product)->pluck('id')->first(); |
255
|
|
|
$users = new User(); |
256
|
|
|
$user = $users->find($userid); |
257
|
|
|
//check in the settings |
258
|
|
|
$settings = new \App\Model\Common\Setting(); |
259
|
|
|
$setting = $settings->where('id', 1)->first(); |
260
|
|
|
$orders = new Order(); |
261
|
|
|
$order = $orders->where('id', $orderid)->first(); |
262
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
263
|
|
|
$number = $invoice->number; |
264
|
|
|
$downloadurl = ''; |
265
|
|
|
if ($user && $order->order_status == 'Executed') { |
266
|
|
|
$downloadurl = url('product/'.'download'.'/'.$productId.'/'.$number); |
267
|
|
|
} |
268
|
|
|
// $downloadurl = $this->downloadUrl($userid, $orderid,$productId); |
269
|
|
|
$myaccounturl = url('my-order/'.$orderid); |
270
|
|
|
$invoiceurl = $this->invoiceUrl($orderid); |
271
|
|
|
//template |
272
|
|
|
$mail = $this->getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl) |
276
|
|
|
{ |
277
|
|
|
try { |
278
|
|
|
$templates = new \App\Model\Common\Template(); |
279
|
|
|
$temp_id = $setting->order_mail; |
280
|
|
|
$template = $templates->where('id', $temp_id)->first(); |
281
|
|
|
$knowledgeBaseUrl = $setting->company_url; |
282
|
|
|
$from = $setting->email; |
283
|
|
|
$to = $user->email; |
284
|
|
|
$adminEmail = $setting->company_email; |
285
|
|
|
$subject = $template->name; |
286
|
|
|
$data = $template->data; |
287
|
|
|
$replace = [ |
288
|
|
|
'name' => $user->first_name.' '.$user->last_name, |
289
|
|
|
'serialkeyurl' => $myaccounturl, |
290
|
|
|
'downloadurl' => $downloadurl, |
291
|
|
|
'invoiceurl' => $invoiceurl, |
292
|
|
|
'product' => $product, |
293
|
|
|
'number' => $order->number, |
294
|
|
|
'expiry' => $this->expiry($orderid), |
295
|
|
|
'url' => $this->renew($orderid), |
296
|
|
|
'knowledge_base'=> $knowledgeBaseUrl, |
297
|
|
|
|
298
|
|
|
]; |
299
|
|
|
$type = ''; |
300
|
|
|
if ($template) { |
301
|
|
|
$type_id = $template->type; |
302
|
|
|
$temp_type = new \App\Model\Common\TemplateType(); |
303
|
|
|
$type = $temp_type->where('id', $type_id)->first()->name; |
304
|
|
|
} |
305
|
|
|
$mail = new \App\Http\Controllers\Common\PhpMailController(); |
306
|
|
|
$mail->sendEmail($from, $to, $data, $subject, $replace, $type); |
307
|
|
|
|
308
|
|
|
if ($order->invoice->grand_total) { |
309
|
|
|
SettingsController::sendPaymentSuccessMailtoAdmin($order->invoice->currency, $order->invoice->grand_total, $user, $product); |
310
|
|
|
} |
311
|
|
|
} catch (\Exception $ex) { |
312
|
|
|
throw new \Exception($ex->getMessage()); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function invoiceUrl($orderid) |
317
|
|
|
{ |
318
|
|
|
$orders = new Order(); |
319
|
|
|
$order = $orders->where('id', $orderid)->first(); |
320
|
|
|
$invoiceid = $order->invoice_id; |
321
|
|
|
$url = url('my-invoice/'.$invoiceid); |
322
|
|
|
|
323
|
|
|
return $url; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* get the price of a product by id. |
328
|
|
|
* |
329
|
|
|
* @param type $product_id |
330
|
|
|
* |
331
|
|
|
* @throws \Exception |
332
|
|
|
* |
333
|
|
|
* @return type collection |
334
|
|
|
*/ |
335
|
|
|
public function getPrice($product_id) |
336
|
|
|
{ |
337
|
|
|
try { |
338
|
|
|
return $this->price->where('product_id', $product_id)->first(); |
339
|
|
|
} catch (\Exception $ex) { |
340
|
|
|
Bugsnag::notifyException($ex); |
341
|
|
|
|
342
|
|
|
throw new \Exception($ex->getMessage()); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function downloadUrl($userid, $orderid) |
347
|
|
|
{ |
348
|
|
|
$orders = new Order(); |
349
|
|
|
$order = $orders->where('id', $orderid)->first(); |
350
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
351
|
|
|
$number = $invoice->number; |
352
|
|
|
$url = url('download/'.$userid.'/'.$number); |
353
|
|
|
|
354
|
|
|
return $url; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|