@fleetbase/storefront-engine 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.php-cs-fixer.php +29 -0
- package/LICENSE.md +16 -4
- package/README.md +106 -13
- package/composer.json +88 -0
- package/extension.json +10 -0
- package/package.json +6 -6
- package/phpstan.neon.dist +8 -0
- package/phpunit.xml.dist +16 -0
- package/server/.gitattributes +14 -0
- package/server/README.md +40 -0
- package/server/config/api.php +101 -0
- package/server/config/database.connections.php +57 -0
- package/server/config/storefront.php +19 -0
- package/server/config/twilio-notification-channel.php +36 -0
- package/server/migrations/2023_05_03_025307_create_carts_table.php +44 -0
- package/server/migrations/2023_05_03_025307_create_checkouts_table.php +51 -0
- package/server/migrations/2023_05_03_025307_create_gateways_table.php +48 -0
- package/server/migrations/2023_05_03_025307_create_network_stores_table.php +36 -0
- package/server/migrations/2023_05_03_025307_create_networks_table.php +56 -0
- package/server/migrations/2023_05_03_025307_create_notification_channels_table.php +43 -0
- package/server/migrations/2023_05_03_025307_create_payment_methods_table.php +44 -0
- package/server/migrations/2023_05_03_025307_create_product_addon_categories_table.php +38 -0
- package/server/migrations/2023_05_03_025307_create_product_addons_table.php +43 -0
- package/server/migrations/2023_05_03_025307_create_product_hours_table.php +37 -0
- package/server/migrations/2023_05_03_025307_create_product_store_locations_table.php +34 -0
- package/server/migrations/2023_05_03_025307_create_product_variant_options_table.php +40 -0
- package/server/migrations/2023_05_03_025307_create_product_variants_table.php +44 -0
- package/server/migrations/2023_05_03_025307_create_products_table.php +59 -0
- package/server/migrations/2023_05_03_025307_create_reviews_table.php +41 -0
- package/server/migrations/2023_05_03_025307_create_store_hours_table.php +37 -0
- package/server/migrations/2023_05_03_025307_create_store_locations_table.php +38 -0
- package/server/migrations/2023_05_03_025307_create_stores_table.php +57 -0
- package/server/migrations/2023_05_03_025307_create_votes_table.php +39 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_carts_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_checkouts_table.php +48 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_gateways_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_network_stores_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_networks_table.php +42 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_notification_channels_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_payment_methods_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_addon_categories_table.php +38 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_addons_table.php +38 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_hours_table.php +32 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_store_locations_table.php +34 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_variant_options_table.php +32 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_product_variants_table.php +32 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_products_table.php +44 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_reviews_table.php +38 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_store_hours_table.php +32 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_store_locations_table.php +40 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_stores_table.php +42 -0
- package/server/migrations/2023_05_03_025310_add_foreign_keys_to_votes_table.php +38 -0
- package/server/src/Auth/Schemas/Storefront.php +95 -0
- package/server/src/Console/Commands/NotifyStorefrontOrderNearby.php +86 -0
- package/server/src/Expansions/EntityExpansion.php +44 -0
- package/server/src/Http/Controllers/ActionController.php +119 -0
- package/server/src/Http/Controllers/AddonCategoryController.php +13 -0
- package/server/src/Http/Controllers/CustomerController.php +13 -0
- package/server/src/Http/Controllers/GatewayController.php +13 -0
- package/server/src/Http/Controllers/MetricsController.php +71 -0
- package/server/src/Http/Controllers/NetworkController.php +170 -0
- package/server/src/Http/Controllers/NotificationChannelController.php +13 -0
- package/server/src/Http/Controllers/OrderController.php +154 -0
- package/server/src/Http/Controllers/ProductAddonCategoryController.php +14 -0
- package/server/src/Http/Controllers/ProductAddonController.php +14 -0
- package/server/src/Http/Controllers/ProductController.php +123 -0
- package/server/src/Http/Controllers/ProductHourController.php +13 -0
- package/server/src/Http/Controllers/ProductVariantController.php +13 -0
- package/server/src/Http/Controllers/ProductVariantOptionController.php +13 -0
- package/server/src/Http/Controllers/ReviewController.php +13 -0
- package/server/src/Http/Controllers/StoreController.php +26 -0
- package/server/src/Http/Controllers/StoreHourController.php +13 -0
- package/server/src/Http/Controllers/StoreLocationController.php +13 -0
- package/server/src/Http/Controllers/StorefrontController.php +15 -0
- package/server/src/Http/Controllers/VoteController.php +13 -0
- package/server/src/Http/Controllers/v1/CartController.php +161 -0
- package/server/src/Http/Controllers/v1/CategoryController.php +138 -0
- package/server/src/Http/Controllers/v1/CheckoutController.php +957 -0
- package/server/src/Http/Controllers/v1/CustomerController.php +482 -0
- package/server/src/Http/Controllers/v1/GatewayControllerController.php +11 -0
- package/server/src/Http/Controllers/v1/NetworkController.php +281 -0
- package/server/src/Http/Controllers/v1/PaymentMethodController.php +11 -0
- package/server/src/Http/Controllers/v1/ProductController.php +94 -0
- package/server/src/Http/Controllers/v1/ReviewController.php +270 -0
- package/server/src/Http/Controllers/v1/ServiceQuoteController.php +402 -0
- package/server/src/Http/Controllers/v1/StoreController.php +176 -0
- package/server/src/Http/Filter/AddonCategoryFilter.php +19 -0
- package/server/src/Http/Filter/CustomerFilter.php +18 -0
- package/server/src/Http/Filter/GatewayFilter.php +13 -0
- package/server/src/Http/Filter/NetworkFilter.php +18 -0
- package/server/src/Http/Filter/NotificationChannelFilter.php +13 -0
- package/server/src/Http/Filter/OrderFilter.php +46 -0
- package/server/src/Http/Filter/ProductFilter.php +28 -0
- package/server/src/Http/Filter/StoreFilter.php +42 -0
- package/server/src/Http/Filter/StoreLocationFilter.php +23 -0
- package/server/src/Http/Middleware/SetStorefrontSession.php +130 -0
- package/server/src/Http/Requests/AddStoreToNetworkCategory.php +45 -0
- package/server/src/Http/Requests/CaptureOrderRequest.php +30 -0
- package/server/src/Http/Requests/CreateCustomerRequest.php +44 -0
- package/server/src/Http/Requests/CreateReviewRequest.php +34 -0
- package/server/src/Http/Requests/GetServiceQuoteFromCart.php +40 -0
- package/server/src/Http/Requests/InitializeCheckoutRequest.php +38 -0
- package/server/src/Http/Requests/NetworkActionRequest.php +43 -0
- package/server/src/Http/Requests/VerifyCreateCustomerRequest.php +31 -0
- package/server/src/Http/Resources/Cart.php +31 -0
- package/server/src/Http/Resources/Category.php +48 -0
- package/server/src/Http/Resources/Customer.php +36 -0
- package/server/src/Http/Resources/Gateway.php +32 -0
- package/server/src/Http/Resources/Media.php +29 -0
- package/server/src/Http/Resources/Network.php +48 -0
- package/server/src/Http/Resources/Product.php +209 -0
- package/server/src/Http/Resources/Review.php +45 -0
- package/server/src/Http/Resources/ReviewCustomer.php +60 -0
- package/server/src/Http/Resources/Store.php +76 -0
- package/server/src/Http/Resources/StoreHour.php +29 -0
- package/server/src/Http/Resources/StoreLocation.php +33 -0
- package/server/src/Imports/ProductsImport.php +20 -0
- package/server/src/Jobs/DownloadProductImageUrl.php +60 -0
- package/server/src/Listeners/HandleOrderCompleted.php +31 -0
- package/server/src/Listeners/HandleOrderDispatched.php +34 -0
- package/server/src/Listeners/HandleOrderDriverAssigned.php +37 -0
- package/server/src/Listeners/HandleOrderStarted.php +27 -0
- package/server/src/Mail/StorefrontNetworkInvite.php +48 -0
- package/server/src/Models/AddonCategory.php +30 -0
- package/server/src/Models/Cart.php +691 -0
- package/server/src/Models/Checkout.php +166 -0
- package/server/src/Models/Customer.php +88 -0
- package/server/src/Models/Gateway.php +165 -0
- package/server/src/Models/Network.php +300 -0
- package/server/src/Models/NetworkStore.php +86 -0
- package/server/src/Models/NotificationChannel.php +147 -0
- package/server/src/Models/PaymentMethod.php +99 -0
- package/server/src/Models/Product.php +315 -0
- package/server/src/Models/ProductAddon.php +128 -0
- package/server/src/Models/ProductAddonCategory.php +90 -0
- package/server/src/Models/ProductHour.php +59 -0
- package/server/src/Models/ProductStoreLocation.php +77 -0
- package/server/src/Models/ProductVariant.php +125 -0
- package/server/src/Models/ProductVariantOption.php +86 -0
- package/server/src/Models/Review.php +127 -0
- package/server/src/Models/Store.php +478 -0
- package/server/src/Models/StoreHour.php +59 -0
- package/server/src/Models/StoreLocation.php +126 -0
- package/server/src/Models/StorefrontModel.php +22 -0
- package/server/src/Models/Vote.php +84 -0
- package/server/src/Notifications/StorefrontOrderCanceled.php +196 -0
- package/server/src/Notifications/StorefrontOrderCompleted.php +201 -0
- package/server/src/Notifications/StorefrontOrderCreated.php +157 -0
- package/server/src/Notifications/StorefrontOrderDriverAssigned.php +200 -0
- package/server/src/Notifications/StorefrontOrderEnroute.php +199 -0
- package/server/src/Notifications/StorefrontOrderNearby.php +201 -0
- package/server/src/Notifications/StorefrontOrderPreparing.php +202 -0
- package/server/src/Notifications/StorefrontOrderReadyForPickup.php +202 -0
- package/server/src/Observers/NetworkObserver.php +40 -0
- package/server/src/Observers/ProductObserver.php +118 -0
- package/server/src/Providers/EventServiceProvider.php +23 -0
- package/server/src/Providers/StorefrontServiceProvider.php +103 -0
- package/server/src/Support/Metrics.php +193 -0
- package/server/src/Support/OrderConfig.php +13 -0
- package/server/src/Support/QPay.php +208 -0
- package/server/src/Support/Storefront.php +201 -0
- package/server/src/routes.php +180 -0
- package/server/tests/Feature.php +5 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Notifications;
|
|
4
|
+
|
|
5
|
+
use Exception;
|
|
6
|
+
use Fleetbase\FleetOps\Models\Order;
|
|
7
|
+
use Fleetbase\Storefront\Models\NotificationChannel;
|
|
8
|
+
use Fleetbase\Storefront\Support\Storefront;
|
|
9
|
+
use Fleetbase\FleetOps\Support\Utils;
|
|
10
|
+
// use Fleetbase\FleetOps\Support\Utils;
|
|
11
|
+
use Illuminate\Bus\Queueable;
|
|
12
|
+
// use Illuminate\Contracts\Queue\ShouldQueue;
|
|
13
|
+
use Illuminate\Notifications\Messages\MailMessage;
|
|
14
|
+
use Illuminate\Notifications\Notification;
|
|
15
|
+
use NotificationChannels\Fcm\FcmChannel;
|
|
16
|
+
use NotificationChannels\Fcm\FcmMessage;
|
|
17
|
+
use NotificationChannels\Apn\ApnChannel;
|
|
18
|
+
use NotificationChannels\Apn\ApnMessage;
|
|
19
|
+
use NotificationChannels\Fcm\Resources\AndroidConfig;
|
|
20
|
+
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
|
|
21
|
+
use NotificationChannels\Fcm\Resources\AndroidNotification;
|
|
22
|
+
use NotificationChannels\Fcm\Resources\ApnsConfig;
|
|
23
|
+
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;
|
|
24
|
+
use Pushok\Client as PushOkClient;
|
|
25
|
+
use Pushok\AuthProvider\Token as PuskOkToken;
|
|
26
|
+
|
|
27
|
+
class StorefrontOrderDriverAssigned extends Notification
|
|
28
|
+
{
|
|
29
|
+
use Queueable;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create a new notification instance.
|
|
33
|
+
*
|
|
34
|
+
* @return void
|
|
35
|
+
*/
|
|
36
|
+
public function __construct(Order $order)
|
|
37
|
+
{
|
|
38
|
+
$this->order = $order->setRelations([]);
|
|
39
|
+
$this->driver = $order->driverAssigned;
|
|
40
|
+
$this->storefront = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get the notification's delivery channels.
|
|
45
|
+
*
|
|
46
|
+
* @param mixed $notifiable
|
|
47
|
+
* @return array
|
|
48
|
+
*/
|
|
49
|
+
public function via($notifiable)
|
|
50
|
+
{
|
|
51
|
+
// $channels = ['mail'];
|
|
52
|
+
$channels = [];
|
|
53
|
+
|
|
54
|
+
if (!$this->storefront) {
|
|
55
|
+
return $channels;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
$hasApnNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'apn'])->count();
|
|
59
|
+
$hasFcmNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'android'])->count();
|
|
60
|
+
|
|
61
|
+
if ($hasApnNotificationChannels) {
|
|
62
|
+
$channels[] = ApnChannel::class;
|
|
63
|
+
}
|
|
64
|
+
if ($hasFcmNotificationChannels) {
|
|
65
|
+
$channels[] = FcmChannel::class;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return $channels;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get the mail representation of the notification.
|
|
73
|
+
*
|
|
74
|
+
* @param mixed $notifiable
|
|
75
|
+
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
76
|
+
*/
|
|
77
|
+
public function toMail($notifiable)
|
|
78
|
+
{
|
|
79
|
+
$message = (new MailMessage)
|
|
80
|
+
->subject('Your order from ' . $this->storefront->name . ' is being prepared')
|
|
81
|
+
->line('Your order is getting started.');
|
|
82
|
+
|
|
83
|
+
// $message->action('View Details', Utils::consoleUrl('', ['shift' => 'fleet-ops/orders/view/' . $this->order->public_id]));
|
|
84
|
+
|
|
85
|
+
return $message;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the firebase cloud message representation of the notification.
|
|
90
|
+
*
|
|
91
|
+
* @param mixed $notifiable
|
|
92
|
+
* @return array
|
|
93
|
+
*/
|
|
94
|
+
public function toFcm($notifiable)
|
|
95
|
+
{
|
|
96
|
+
$notification = \NotificationChannels\Fcm\Resources\Notification::create()
|
|
97
|
+
->setTitle('Your driver is ' . $this->driver->name)
|
|
98
|
+
->setBody('A driver has been assigned to your order, they are en-route for pickup');
|
|
99
|
+
|
|
100
|
+
$message = FcmMessage::create()
|
|
101
|
+
->setData(['order' => $this->order->uuid, 'id' => $this->order->public_id, 'type' => 'order_driver_assigned'])
|
|
102
|
+
->setNotification($notification)
|
|
103
|
+
->setAndroid(
|
|
104
|
+
AndroidConfig::create()
|
|
105
|
+
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
|
|
106
|
+
->setNotification(AndroidNotification::create()->setColor('#4391EA'))
|
|
107
|
+
)->setApns(
|
|
108
|
+
ApnsConfig::create()
|
|
109
|
+
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return $message;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public function fcmProject($notifiable, $message)
|
|
116
|
+
{
|
|
117
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
118
|
+
|
|
119
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
120
|
+
$notificationChannel = NotificationChannel::where([
|
|
121
|
+
'owner_uuid' => $about->uuid,
|
|
122
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
123
|
+
'scheme' => 'fcm'
|
|
124
|
+
])->first();
|
|
125
|
+
} else {
|
|
126
|
+
$notificationChannel = NotificationChannel::where([
|
|
127
|
+
'owner_uuid' => $about->uuid,
|
|
128
|
+
'scheme' => 'fcm'
|
|
129
|
+
])->first();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!$notificationChannel) {
|
|
133
|
+
return 'app';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
$this->configureFcm($notificationChannel);
|
|
137
|
+
|
|
138
|
+
return $notificationChannel->app_key;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public function configureFcm($notificationChannel)
|
|
142
|
+
{
|
|
143
|
+
$config = (array) $notificationChannel->config;
|
|
144
|
+
$fcmConfig = config('firebase.projects.app');
|
|
145
|
+
|
|
146
|
+
// set credentials
|
|
147
|
+
Utils::set($fcmConfig, 'credentials.file', $config['firebase_credentials_json']);
|
|
148
|
+
|
|
149
|
+
// set db url
|
|
150
|
+
Utils::set($fcmConfig, 'database.url', $config['firebase_database_url']);
|
|
151
|
+
|
|
152
|
+
config('firebase.projects.' . $notificationChannel->app_key, $fcmConfig);
|
|
153
|
+
|
|
154
|
+
return $fcmConfig;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get the apns message representation of the notification.
|
|
159
|
+
*
|
|
160
|
+
* @param mixed $notifiable
|
|
161
|
+
* @return array
|
|
162
|
+
*/
|
|
163
|
+
public function toApn($notifiable)
|
|
164
|
+
{
|
|
165
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
166
|
+
|
|
167
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
168
|
+
$notificationChannel = NotificationChannel::where([
|
|
169
|
+
'owner_uuid' => $about->uuid,
|
|
170
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
171
|
+
'scheme' => 'apn'
|
|
172
|
+
])->first();
|
|
173
|
+
} else {
|
|
174
|
+
$notificationChannel = NotificationChannel::where([
|
|
175
|
+
'owner_uuid' => $about->uuid,
|
|
176
|
+
'scheme' => 'apn'
|
|
177
|
+
])->first();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
$config = (array) $notificationChannel->config;
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
$channelClient = new PushOkClient(PuskOkToken::create($config));
|
|
184
|
+
} catch (Exception $e) {
|
|
185
|
+
// stop silently
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
$message = ApnMessage::create()
|
|
190
|
+
->badge(1)
|
|
191
|
+
->title('Your driver is ' . $this->driver->name)
|
|
192
|
+
->body('A driver has been assigned to your order, they are en-route for pickup')
|
|
193
|
+
->custom('type', 'order_driver_assigned')
|
|
194
|
+
->custom('order', $this->order->uuid)
|
|
195
|
+
->custom('id', $this->order->public_id)
|
|
196
|
+
->via($channelClient);
|
|
197
|
+
|
|
198
|
+
return $message;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Notifications;
|
|
4
|
+
|
|
5
|
+
use Exception;
|
|
6
|
+
use Fleetbase\FleetOps\Models\Order;
|
|
7
|
+
use Fleetbase\Storefront\Models\NotificationChannel;
|
|
8
|
+
use Fleetbase\Storefront\Support\Storefront;
|
|
9
|
+
use Fleetbase\FleetOps\Support\Utils;
|
|
10
|
+
// use Fleetbase\FleetOps\Support\Utils;
|
|
11
|
+
use Illuminate\Bus\Queueable;
|
|
12
|
+
// use Illuminate\Contracts\Queue\ShouldQueue;
|
|
13
|
+
use Illuminate\Notifications\Messages\MailMessage;
|
|
14
|
+
use Illuminate\Notifications\Notification;
|
|
15
|
+
use NotificationChannels\Fcm\FcmChannel;
|
|
16
|
+
use NotificationChannels\Fcm\FcmMessage;
|
|
17
|
+
use NotificationChannels\Apn\ApnChannel;
|
|
18
|
+
use NotificationChannels\Apn\ApnMessage;
|
|
19
|
+
use NotificationChannels\Fcm\Resources\AndroidConfig;
|
|
20
|
+
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
|
|
21
|
+
use NotificationChannels\Fcm\Resources\AndroidNotification;
|
|
22
|
+
use NotificationChannels\Fcm\Resources\ApnsConfig;
|
|
23
|
+
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;
|
|
24
|
+
use Pushok\Client as PushOkClient;
|
|
25
|
+
use Pushok\AuthProvider\Token as PuskOkToken;
|
|
26
|
+
|
|
27
|
+
class StorefrontOrderEnroute extends Notification
|
|
28
|
+
{
|
|
29
|
+
use Queueable;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create a new notification instance.
|
|
33
|
+
*
|
|
34
|
+
* @return void
|
|
35
|
+
*/
|
|
36
|
+
public function __construct(Order $order)
|
|
37
|
+
{
|
|
38
|
+
$this->order = $order->setRelations([]);
|
|
39
|
+
$this->storefront = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get the notification's delivery channels.
|
|
44
|
+
*
|
|
45
|
+
* @param mixed $notifiable
|
|
46
|
+
* @return array
|
|
47
|
+
*/
|
|
48
|
+
public function via($notifiable)
|
|
49
|
+
{
|
|
50
|
+
// $channels = ['mail'];
|
|
51
|
+
$channels = [];
|
|
52
|
+
|
|
53
|
+
if (!$this->storefront) {
|
|
54
|
+
return $channels;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
$hasApnNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'apn'])->count();
|
|
58
|
+
$hasFcmNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'android'])->count();
|
|
59
|
+
|
|
60
|
+
if ($hasApnNotificationChannels) {
|
|
61
|
+
$channels[] = ApnChannel::class;
|
|
62
|
+
}
|
|
63
|
+
if ($hasFcmNotificationChannels) {
|
|
64
|
+
$channels[] = FcmChannel::class;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return $channels;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get the mail representation of the notification.
|
|
72
|
+
*
|
|
73
|
+
* @param mixed $notifiable
|
|
74
|
+
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
75
|
+
*/
|
|
76
|
+
public function toMail($notifiable)
|
|
77
|
+
{
|
|
78
|
+
$message = (new MailMessage)
|
|
79
|
+
->subject('Your order from ' . $this->storefront->name . ' is on the way!')
|
|
80
|
+
->line('Your order from ' . $this->storefront->name . ' has been picked up, we will update you when your order is nearby');
|
|
81
|
+
|
|
82
|
+
// $message->action('View Details', Utils::consoleUrl('', ['shift' => 'fleet-ops/orders/view/' . $this->order->public_id]));
|
|
83
|
+
|
|
84
|
+
return $message;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the firebase cloud message representation of the notification.
|
|
89
|
+
*
|
|
90
|
+
* @param mixed $notifiable
|
|
91
|
+
* @return array
|
|
92
|
+
*/
|
|
93
|
+
public function toFcm($notifiable)
|
|
94
|
+
{
|
|
95
|
+
$notification = \NotificationChannels\Fcm\Resources\Notification::create()
|
|
96
|
+
->setTitle('Your driver is ' . $this->driver->name)
|
|
97
|
+
->setBody('A driver has been assigned to your order, they are en-route for pickup');
|
|
98
|
+
|
|
99
|
+
$message = FcmMessage::create()
|
|
100
|
+
->setData(['order' => $this->order->uuid, 'id' => $this->order->public_id, 'type' => 'order_enroute'])
|
|
101
|
+
->setNotification($notification)
|
|
102
|
+
->setAndroid(
|
|
103
|
+
AndroidConfig::create()
|
|
104
|
+
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
|
|
105
|
+
->setNotification(AndroidNotification::create()->setColor('#4391EA'))
|
|
106
|
+
)->setApns(
|
|
107
|
+
ApnsConfig::create()
|
|
108
|
+
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
return $message;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public function fcmProject($notifiable, $message)
|
|
115
|
+
{
|
|
116
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
117
|
+
|
|
118
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
119
|
+
$notificationChannel = NotificationChannel::where([
|
|
120
|
+
'owner_uuid' => $about->uuid,
|
|
121
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
122
|
+
'scheme' => 'fcm'
|
|
123
|
+
])->first();
|
|
124
|
+
} else {
|
|
125
|
+
$notificationChannel = NotificationChannel::where([
|
|
126
|
+
'owner_uuid' => $about->uuid,
|
|
127
|
+
'scheme' => 'fcm'
|
|
128
|
+
])->first();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!$notificationChannel) {
|
|
132
|
+
return 'app';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
$this->configureFcm($notificationChannel);
|
|
136
|
+
|
|
137
|
+
return $notificationChannel->app_key;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public function configureFcm($notificationChannel)
|
|
141
|
+
{
|
|
142
|
+
$config = (array) $notificationChannel->config;
|
|
143
|
+
$fcmConfig = config('firebase.projects.app');
|
|
144
|
+
|
|
145
|
+
// set credentials
|
|
146
|
+
Utils::set($fcmConfig, 'credentials.file', $config['firebase_credentials_json']);
|
|
147
|
+
|
|
148
|
+
// set db url
|
|
149
|
+
Utils::set($fcmConfig, 'database.url', $config['firebase_database_url']);
|
|
150
|
+
|
|
151
|
+
config('firebase.projects.' . $notificationChannel->app_key, $fcmConfig);
|
|
152
|
+
|
|
153
|
+
return $fcmConfig;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Get the apns message representation of the notification.
|
|
158
|
+
*
|
|
159
|
+
* @param mixed $notifiable
|
|
160
|
+
* @return array
|
|
161
|
+
*/
|
|
162
|
+
public function toApn($notifiable)
|
|
163
|
+
{
|
|
164
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
165
|
+
|
|
166
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
167
|
+
$notificationChannel = NotificationChannel::where([
|
|
168
|
+
'owner_uuid' => $about->uuid,
|
|
169
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
170
|
+
'scheme' => 'apn'
|
|
171
|
+
])->first();
|
|
172
|
+
} else {
|
|
173
|
+
$notificationChannel = NotificationChannel::where([
|
|
174
|
+
'owner_uuid' => $about->uuid,
|
|
175
|
+
'scheme' => 'apn'
|
|
176
|
+
])->first();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
$config = (array) $notificationChannel->config;
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
$channelClient = new PushOkClient(PuskOkToken::create($config));
|
|
183
|
+
} catch (Exception $e) {
|
|
184
|
+
// stop silently
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
$message = ApnMessage::create()
|
|
189
|
+
->badge(1)
|
|
190
|
+
->title('Your order from ' . $this->storefront->name . ' is on the way!')
|
|
191
|
+
->body('Your order from ' . $this->storefront->name . ' has been picked up, we will update you when your order is nearby')
|
|
192
|
+
->custom('type', 'order_enroute')
|
|
193
|
+
->custom('order', $this->order->uuid)
|
|
194
|
+
->custom('id', $this->order->public_id)
|
|
195
|
+
->via($channelClient);
|
|
196
|
+
|
|
197
|
+
return $message;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Notifications;
|
|
4
|
+
|
|
5
|
+
use Exception;
|
|
6
|
+
use Fleetbase\FleetOps\Models\Order;
|
|
7
|
+
use Fleetbase\Storefront\Models\NotificationChannel;
|
|
8
|
+
use Fleetbase\Storefront\Support\Storefront;
|
|
9
|
+
use Fleetbase\FleetOps\Support\Utils;
|
|
10
|
+
// use Fleetbase\FleetOps\Support\Utils;
|
|
11
|
+
use Illuminate\Bus\Queueable;
|
|
12
|
+
// use Illuminate\Contracts\Queue\ShouldQueue;
|
|
13
|
+
use Illuminate\Notifications\Messages\MailMessage;
|
|
14
|
+
use Illuminate\Notifications\Notification;
|
|
15
|
+
use NotificationChannels\Fcm\FcmChannel;
|
|
16
|
+
use NotificationChannels\Fcm\FcmMessage;
|
|
17
|
+
use NotificationChannels\Apn\ApnChannel;
|
|
18
|
+
use NotificationChannels\Apn\ApnMessage;
|
|
19
|
+
use NotificationChannels\Fcm\Resources\AndroidConfig;
|
|
20
|
+
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
|
|
21
|
+
use NotificationChannels\Fcm\Resources\AndroidNotification;
|
|
22
|
+
use NotificationChannels\Fcm\Resources\ApnsConfig;
|
|
23
|
+
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;
|
|
24
|
+
use Pushok\Client as PushOkClient;
|
|
25
|
+
use Pushok\AuthProvider\Token as PuskOkToken;
|
|
26
|
+
|
|
27
|
+
class StorefrontOrderNearby extends Notification
|
|
28
|
+
{
|
|
29
|
+
use Queueable;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create a new notification instance.
|
|
33
|
+
*
|
|
34
|
+
* @return void
|
|
35
|
+
*/
|
|
36
|
+
public function __construct(Order $order, $distance = 0, $time = 0)
|
|
37
|
+
{
|
|
38
|
+
$this->order = $order->setRelations([]);
|
|
39
|
+
$this->storefront = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
40
|
+
$this->distance = $distance;
|
|
41
|
+
$this->time = $time;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the notification's delivery channels.
|
|
46
|
+
*
|
|
47
|
+
* @param mixed $notifiable
|
|
48
|
+
* @return array
|
|
49
|
+
*/
|
|
50
|
+
public function via($notifiable)
|
|
51
|
+
{
|
|
52
|
+
// $channels = ['mail'];
|
|
53
|
+
$channels = [];
|
|
54
|
+
|
|
55
|
+
if (!$this->storefront) {
|
|
56
|
+
return $channels;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
$hasApnNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'apn'])->count();
|
|
60
|
+
$hasFcmNotificationChannels = NotificationChannel::where(['owner_uuid' => $this->storefront->uuid, 'scheme' => 'android'])->count();
|
|
61
|
+
|
|
62
|
+
if ($hasApnNotificationChannels) {
|
|
63
|
+
$channels[] = ApnChannel::class;
|
|
64
|
+
}
|
|
65
|
+
if ($hasFcmNotificationChannels) {
|
|
66
|
+
$channels[] = FcmChannel::class;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return $channels;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get the mail representation of the notification.
|
|
74
|
+
*
|
|
75
|
+
* @param mixed $notifiable
|
|
76
|
+
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
77
|
+
*/
|
|
78
|
+
public function toMail($notifiable)
|
|
79
|
+
{
|
|
80
|
+
$message = (new MailMessage)
|
|
81
|
+
->subject('Your order is nearby')
|
|
82
|
+
->line('Your order from ' . $this->storefront->name . ' is reaching in ' . Utils::formatSeconds($this->time));
|
|
83
|
+
|
|
84
|
+
// $message->action('View Details', Utils::consoleUrl('', ['shift' => 'fleet-ops/orders/view/' . $this->order->public_id]));
|
|
85
|
+
|
|
86
|
+
return $message;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get the firebase cloud message representation of the notification.
|
|
91
|
+
*
|
|
92
|
+
* @param mixed $notifiable
|
|
93
|
+
* @return array
|
|
94
|
+
*/
|
|
95
|
+
public function toFcm($notifiable)
|
|
96
|
+
{
|
|
97
|
+
$notification = \NotificationChannels\Fcm\Resources\Notification::create()
|
|
98
|
+
->setTitle('Your order is nearby')
|
|
99
|
+
->setBody('Your order from ' . $this->storefront->name . ' is reaching in ' . Utils::formatSeconds($this->time));
|
|
100
|
+
|
|
101
|
+
$message = FcmMessage::create()
|
|
102
|
+
->setData(['order' => $this->order->uuid, 'id' => $this->order->public_id, 'type' => 'order_nearby'])
|
|
103
|
+
->setNotification($notification)
|
|
104
|
+
->setAndroid(
|
|
105
|
+
AndroidConfig::create()
|
|
106
|
+
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
|
|
107
|
+
->setNotification(AndroidNotification::create()->setColor('#4391EA'))
|
|
108
|
+
)->setApns(
|
|
109
|
+
ApnsConfig::create()
|
|
110
|
+
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return $message;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public function fcmProject($notifiable, $message)
|
|
117
|
+
{
|
|
118
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
119
|
+
|
|
120
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
121
|
+
$notificationChannel = NotificationChannel::where([
|
|
122
|
+
'owner_uuid' => $about->uuid,
|
|
123
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
124
|
+
'scheme' => 'fcm'
|
|
125
|
+
])->first();
|
|
126
|
+
} else {
|
|
127
|
+
$notificationChannel = NotificationChannel::where([
|
|
128
|
+
'owner_uuid' => $about->uuid,
|
|
129
|
+
'scheme' => 'fcm'
|
|
130
|
+
])->first();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (!$notificationChannel) {
|
|
134
|
+
return 'app';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
$this->configureFcm($notificationChannel);
|
|
138
|
+
|
|
139
|
+
return $notificationChannel->app_key;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public function configureFcm($notificationChannel)
|
|
143
|
+
{
|
|
144
|
+
$config = (array) $notificationChannel->config;
|
|
145
|
+
$fcmConfig = config('firebase.projects.app');
|
|
146
|
+
|
|
147
|
+
// set credentials
|
|
148
|
+
Utils::set($fcmConfig, 'credentials.file', $config['firebase_credentials_json']);
|
|
149
|
+
|
|
150
|
+
// set db url
|
|
151
|
+
Utils::set($fcmConfig, 'database.url', $config['firebase_database_url']);
|
|
152
|
+
|
|
153
|
+
config('firebase.projects.' . $notificationChannel->app_key, $fcmConfig);
|
|
154
|
+
|
|
155
|
+
return $fcmConfig;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Get the apns message representation of the notification.
|
|
160
|
+
*
|
|
161
|
+
* @param mixed $notifiable
|
|
162
|
+
* @return array
|
|
163
|
+
*/
|
|
164
|
+
public function toApn($notifiable)
|
|
165
|
+
{
|
|
166
|
+
$about = Storefront::findAbout($this->order->getMeta('storefront_id'));
|
|
167
|
+
|
|
168
|
+
if ($this->order->hasMeta('storefront_notification_channel')) {
|
|
169
|
+
$notificationChannel = NotificationChannel::where([
|
|
170
|
+
'owner_uuid' => $about->uuid,
|
|
171
|
+
'app_key' => $this->order->getMeta('storefront_notification_channel'),
|
|
172
|
+
'scheme' => 'apn'
|
|
173
|
+
])->first();
|
|
174
|
+
} else {
|
|
175
|
+
$notificationChannel = NotificationChannel::where([
|
|
176
|
+
'owner_uuid' => $about->uuid,
|
|
177
|
+
'scheme' => 'apn'
|
|
178
|
+
])->first();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
$config = (array) $notificationChannel->config;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
$channelClient = new PushOkClient(PuskOkToken::create($config));
|
|
185
|
+
} catch (Exception $e) {
|
|
186
|
+
// stop silently
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
$message = ApnMessage::create()
|
|
191
|
+
->badge(1)
|
|
192
|
+
->title('Your order is nearby')
|
|
193
|
+
->body('Your order from ' . $this->storefront->name . ' is reaching in ' . Utils::formatSeconds($this->time))
|
|
194
|
+
->custom('type', 'order_nearby')
|
|
195
|
+
->custom('order', $this->order->uuid)
|
|
196
|
+
->custom('id', $this->order->public_id)
|
|
197
|
+
->via($channelClient);
|
|
198
|
+
|
|
199
|
+
return $message;
|
|
200
|
+
}
|
|
201
|
+
}
|