NotificationUtility.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Utility;
  3. use App\Mail\InvoiceEmailManager;
  4. use App\Models\User;
  5. use App\Models\SmsTemplate;
  6. use App\Http\Controllers\OTPVerificationController;
  7. use Mail;
  8. use Illuminate\Support\Facades\Notification;
  9. use App\Notifications\OrderNotification;
  10. use App\Models\FirebaseNotification;
  11. class NotificationUtility
  12. {
  13. public static function sendOrderPlacedNotification($order, $request = null)
  14. {
  15. //sends email to customer with the invoice pdf attached
  16. $array['view'] = 'emails.invoice';
  17. $array['subject'] = translate('A new order has been placed') . ' - ' . $order->code;
  18. $array['from'] = env('MAIL_FROM_ADDRESS');
  19. $array['order'] = $order;
  20. try {
  21. Mail::to($order->user->email)->queue(new InvoiceEmailManager($array));
  22. Mail::to($order->orderDetails->first()->product->user->email)->queue(new InvoiceEmailManager($array));
  23. } catch (\Exception $e) {
  24. }
  25. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'order_placement')->first()->status == 1) {
  26. try {
  27. $otpController = new OTPVerificationController;
  28. $otpController->send_order_code($order);
  29. } catch (\Exception $e) {
  30. }
  31. }
  32. //sends Notifications to user
  33. self::sendNotification($order, 'placed');
  34. if ($request !=null && get_setting('google_firebase') == 1 && $order->user->device_token != null) {
  35. $request->device_token = $order->user->device_token;
  36. $request->title = "Order placed !";
  37. $request->text = "An order {$order->code} has been placed";
  38. $request->type = "order";
  39. $request->id = $order->id;
  40. $request->user_id = $order->user->id;
  41. self::sendFirebaseNotification($request);
  42. }
  43. }
  44. public static function sendNotification($order, $order_status)
  45. {
  46. if ($order->seller_id == \App\Models\User::where('user_type', 'admin')->first()->id) {
  47. $users = User::findMany([$order->user->id, $order->seller_id]);
  48. } else {
  49. $users = User::findMany([$order->user->id, $order->seller_id, \App\Models\User::where('user_type', 'admin')->first()->id]);
  50. }
  51. $order_notification = array();
  52. $order_notification['order_id'] = $order->id;
  53. $order_notification['order_code'] = $order->code;
  54. $order_notification['user_id'] = $order->user_id;
  55. $order_notification['seller_id'] = $order->seller_id;
  56. $order_notification['status'] = $order_status;
  57. Notification::send($users, new OrderNotification($order_notification));
  58. }
  59. public static function sendFirebaseNotification($req)
  60. {
  61. $url = 'https://fcm.googleapis.com/fcm/send';
  62. $fields = array
  63. (
  64. 'to' => $req->device_token,
  65. 'notification' => [
  66. 'body' => $req->text,
  67. 'title' => $req->title,
  68. 'sound' => 'default' /*Default sound*/
  69. ],
  70. 'data' => [
  71. 'item_type' => $req->type,
  72. 'item_type_id' => $req->id,
  73. 'click_action' => 'FLUTTER_NOTIFICATION_CLICK'
  74. ]
  75. );
  76. //$fields = json_encode($arrayToSend);
  77. $headers = array(
  78. 'Authorization: key=' . env('FCM_SERVER_KEY'),
  79. 'Content-Type: application/json'
  80. );
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, $url);
  83. curl_setopt($ch, CURLOPT_POST, true);
  84. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  88. $result = curl_exec($ch);
  89. curl_close($ch);
  90. $firebase_notification = new FirebaseNotification;
  91. $firebase_notification->title = $req->title;
  92. $firebase_notification->text = $req->text;
  93. $firebase_notification->item_type = $req->type;
  94. $firebase_notification->item_type_id = $req->id;
  95. $firebase_notification->receiver_id = $req->user_id;
  96. $firebase_notification->save();
  97. }
  98. }