@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,166 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Models;
|
|
4
|
+
|
|
5
|
+
use Fleetbase\Casts\Json;
|
|
6
|
+
use Fleetbase\Models\Company;
|
|
7
|
+
use Fleetbase\Models\ServiceQuote;
|
|
8
|
+
use Fleetbase\FleetOps\Support\Utils;
|
|
9
|
+
use Fleetbase\Traits\HasOptionsAttributes;
|
|
10
|
+
use Fleetbase\Traits\HasUuid;
|
|
11
|
+
use Fleetbase\Traits\HasPublicid;
|
|
12
|
+
use Illuminate\Support\Str;
|
|
13
|
+
|
|
14
|
+
class Checkout extends StorefrontModel
|
|
15
|
+
{
|
|
16
|
+
use HasUuid, HasPublicid, HasOptionsAttributes;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The type of public Id to generate
|
|
20
|
+
*
|
|
21
|
+
* @var string
|
|
22
|
+
*/
|
|
23
|
+
protected $publicIdType = 'chkt';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The database table used by the model.
|
|
27
|
+
*
|
|
28
|
+
* @var string
|
|
29
|
+
*/
|
|
30
|
+
protected $table = 'checkouts';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* These attributes that can be queried
|
|
34
|
+
*
|
|
35
|
+
* @var array
|
|
36
|
+
*/
|
|
37
|
+
protected $searchableColumns = [];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The attributes that are mass assignable.
|
|
41
|
+
*
|
|
42
|
+
* @var array
|
|
43
|
+
*/
|
|
44
|
+
protected $fillable = ['company_uuid', 'order_uuid', 'network_uuid', 'store_uuid', 'cart_uuid', 'gateway_uuid', 'service_quote_uuid', 'owner_uuid', 'owner_type', 'amount', 'currency', 'is_cod', 'is_pickup', 'options', 'token', 'cart_state', 'captured'];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The attributes that should be cast to native types.
|
|
48
|
+
*
|
|
49
|
+
* @var array
|
|
50
|
+
*/
|
|
51
|
+
protected $casts = [
|
|
52
|
+
'cart_state' => Json::class,
|
|
53
|
+
'options' => Json::class,
|
|
54
|
+
'captured' => 'boolean',
|
|
55
|
+
'is_cod' => 'boolean',
|
|
56
|
+
'is_pickup' => 'boolean'
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Dynamic attributes that are appended to object
|
|
61
|
+
*
|
|
62
|
+
* @var array
|
|
63
|
+
*/
|
|
64
|
+
protected $appends = [];
|
|
65
|
+
|
|
66
|
+
/** on boot generate token */
|
|
67
|
+
public static function boot()
|
|
68
|
+
{
|
|
69
|
+
parent::boot();
|
|
70
|
+
static::creating(function ($model) {
|
|
71
|
+
$model->token = 'checkout_' . md5(Str::random(14) . time());
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
77
|
+
*/
|
|
78
|
+
public function company()
|
|
79
|
+
{
|
|
80
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(Company::class);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
85
|
+
*/
|
|
86
|
+
public function order()
|
|
87
|
+
{
|
|
88
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(Order::class);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
93
|
+
*/
|
|
94
|
+
public function owner()
|
|
95
|
+
{
|
|
96
|
+
return $this->setConnection(config('fleetbase.connection.db'))->morphTo(__FUNCTION__, 'owner_type', 'owner_uuid')->withoutGlobalScopes();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
101
|
+
*/
|
|
102
|
+
public function serviceQuote()
|
|
103
|
+
{
|
|
104
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(ServiceQuote::class);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
109
|
+
*/
|
|
110
|
+
public function store()
|
|
111
|
+
{
|
|
112
|
+
return $this->belongsTo(Store::class);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
117
|
+
*/
|
|
118
|
+
public function network()
|
|
119
|
+
{
|
|
120
|
+
return $this->belongsTo(Network::class);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
125
|
+
*/
|
|
126
|
+
public function gateway()
|
|
127
|
+
{
|
|
128
|
+
return $this->belongsTo(Gateway::class);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
133
|
+
*/
|
|
134
|
+
public function cart()
|
|
135
|
+
{
|
|
136
|
+
return $this->belongsTo(Cart::class);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Sets the owner type
|
|
141
|
+
*
|
|
142
|
+
* @return void
|
|
143
|
+
*/
|
|
144
|
+
public function setOwnerTypeAttribute($type)
|
|
145
|
+
{
|
|
146
|
+
$this->attributes['owner_type'] = Utils::getMutationType($type);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Update the cart as checkout.
|
|
151
|
+
*
|
|
152
|
+
* @return void
|
|
153
|
+
*/
|
|
154
|
+
public function checkedout()
|
|
155
|
+
{
|
|
156
|
+
if (!isset($this->cart)) {
|
|
157
|
+
$this->load(['cart']);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
$cart = $this->cart;
|
|
161
|
+
|
|
162
|
+
if ($cart) {
|
|
163
|
+
$this->cart->update(['checkout_uuid' => $this->uuid]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Models;
|
|
4
|
+
|
|
5
|
+
use Fleetbase\FleetOps\Models\Contact;
|
|
6
|
+
use Illuminate\Support\Str;
|
|
7
|
+
|
|
8
|
+
class Customer extends Contact
|
|
9
|
+
{
|
|
10
|
+
/**
|
|
11
|
+
* The key to use in the payload responses
|
|
12
|
+
*
|
|
13
|
+
* @var string
|
|
14
|
+
*/
|
|
15
|
+
protected string $payloadKey = 'customer';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
19
|
+
*/
|
|
20
|
+
public function reviews()
|
|
21
|
+
{
|
|
22
|
+
return $this->hasMany(Review::class, 'customer_uuid');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
27
|
+
*/
|
|
28
|
+
public function productReviews()
|
|
29
|
+
{
|
|
30
|
+
return $this->hasMany(Review::class, 'customer_uuid')->where('subject_type', 'Fleetbase\Storefront\Models\Product');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
35
|
+
*/
|
|
36
|
+
public function storeReviews()
|
|
37
|
+
{
|
|
38
|
+
return $this->hasMany(Review::class, 'customer_uuid')->where('subject_type', 'Fleetbase\Storefront\Models\Store');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
43
|
+
*/
|
|
44
|
+
public function reviewUploads()
|
|
45
|
+
{
|
|
46
|
+
return $this->hasMany(\Fleetbase\Models\File::class, 'uploader_uuid')->where('type', 'storefront_review_upload');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @return integer
|
|
51
|
+
*/
|
|
52
|
+
public function getReviewsCountAttribute()
|
|
53
|
+
{
|
|
54
|
+
return $this->reviews()->count();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Count the number of orders for the storefront with the given ID
|
|
59
|
+
*
|
|
60
|
+
* @param int $id The ID of the storefront to count orders for
|
|
61
|
+
* @return int The number of storefront orders for the customer with this UUID
|
|
62
|
+
*/
|
|
63
|
+
public function countStorefrontOrdersFrom($id)
|
|
64
|
+
{
|
|
65
|
+
return \Fleetbase\FleetOps\Models\Order::where(
|
|
66
|
+
[
|
|
67
|
+
'customer_uuid' => $this->uuid,
|
|
68
|
+
'type' => 'storefront',
|
|
69
|
+
'meta->storefront_id' => $id
|
|
70
|
+
]
|
|
71
|
+
)->count();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Find a customer with the given public ID
|
|
76
|
+
*
|
|
77
|
+
* @param string $publicId The public ID of the customer to find
|
|
78
|
+
* @return static|null The customer with the given public ID, or null if none was found
|
|
79
|
+
*/
|
|
80
|
+
public static function findFromCustomerId($publicId)
|
|
81
|
+
{
|
|
82
|
+
if (Str::startsWith($publicId, 'customer')) {
|
|
83
|
+
$publicId = Str::replaceFirst('customer', 'contact', $publicId);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return static::where('public_id', $publicId)->first();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\Storefront\Models;
|
|
4
|
+
|
|
5
|
+
use Fleetbase\Casts\Json;
|
|
6
|
+
use Fleetbase\Models\User;
|
|
7
|
+
use Fleetbase\Models\Company;
|
|
8
|
+
use Fleetbase\Models\File;
|
|
9
|
+
use Fleetbase\FleetOps\Support\Utils;
|
|
10
|
+
use Fleetbase\Traits\HasUuid;
|
|
11
|
+
use Fleetbase\Traits\HasApiModelBehavior;
|
|
12
|
+
use Fleetbase\Traits\HasPublicid;
|
|
13
|
+
|
|
14
|
+
class Gateway extends StorefrontModel
|
|
15
|
+
{
|
|
16
|
+
use HasUuid, HasPublicid, HasApiModelBehavior;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The type of public Id to generate
|
|
20
|
+
*
|
|
21
|
+
* @var string
|
|
22
|
+
*/
|
|
23
|
+
protected $publicIdType = 'gateway';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The database table used by the model.
|
|
27
|
+
*
|
|
28
|
+
* @var string
|
|
29
|
+
*/
|
|
30
|
+
protected $table = 'gateways';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* These attributes that can be queried
|
|
34
|
+
*
|
|
35
|
+
* @var array
|
|
36
|
+
*/
|
|
37
|
+
protected $searchableColumns = ['name'];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The attributes that are mass assignable.
|
|
41
|
+
*
|
|
42
|
+
* @var array
|
|
43
|
+
*/
|
|
44
|
+
protected $fillable = ['public_id', 'company_uuid', 'created_by_uuid', 'logo_file_uuid', 'owner_uuid', 'owner_type', 'name', 'description', 'code', 'type', 'sandbox', 'meta', 'config', 'return_url', 'callback_url'];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The attributes that should be cast to native types.
|
|
48
|
+
*
|
|
49
|
+
* @var array
|
|
50
|
+
*/
|
|
51
|
+
protected $casts = [
|
|
52
|
+
'sandbox' => 'boolean',
|
|
53
|
+
'meta' => Json::class,
|
|
54
|
+
'config' => Json::class
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Dynamic attributes that are appended to object
|
|
59
|
+
*
|
|
60
|
+
* @var array
|
|
61
|
+
*/
|
|
62
|
+
protected $appends = ['is_stripe_gateway', 'logo_url'];
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The attributes excluded from the model's JSON form.
|
|
66
|
+
*
|
|
67
|
+
* @var array
|
|
68
|
+
*/
|
|
69
|
+
protected $hidden = ['logoFile'];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
73
|
+
*/
|
|
74
|
+
public function createdBy()
|
|
75
|
+
{
|
|
76
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(User::class);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
81
|
+
*/
|
|
82
|
+
public function company()
|
|
83
|
+
{
|
|
84
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(Company::class);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
89
|
+
*/
|
|
90
|
+
public function owner()
|
|
91
|
+
{
|
|
92
|
+
return $this->morphTo(__FUNCTION__, 'owner_type', 'owner_uuid');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
97
|
+
*/
|
|
98
|
+
public function logoFile()
|
|
99
|
+
{
|
|
100
|
+
return $this->setConnection(config('fleetbase.connection.db'))->belongsTo(File::class);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @return string
|
|
105
|
+
*/
|
|
106
|
+
public function getLogoUrlAttribute()
|
|
107
|
+
{
|
|
108
|
+
$default = $this->logoFile->url ?? null;
|
|
109
|
+
$backup = 'https://flb-assets.s3.ap-southeast-1.amazonaws.com/static/image-file-icon.png';
|
|
110
|
+
|
|
111
|
+
return $default ?? $backup;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Sets the owner type
|
|
116
|
+
*/
|
|
117
|
+
public function setOwnerTypeAttribute($type)
|
|
118
|
+
{
|
|
119
|
+
$this->attributes['owner_type'] = Utils::getMutationType($type);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public function getConfigAttribute($config)
|
|
123
|
+
{
|
|
124
|
+
$config = Json::decode($config);
|
|
125
|
+
$sortedKeys = collect($config)->keys()->sort(function ($key) use ($config) {
|
|
126
|
+
return Utils::isBooleanValue($config[$key]) ? 1 : 0;
|
|
127
|
+
});
|
|
128
|
+
$sortedConfig = [];
|
|
129
|
+
|
|
130
|
+
foreach ($sortedKeys as $key) {
|
|
131
|
+
$sortedConfig[$key] = $config[$key];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return (object) $sortedConfig;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public function getIsStripeGatewayAttribute()
|
|
138
|
+
{
|
|
139
|
+
return $this->type === 'stripe';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public function getIsQpayGatewayAttribute()
|
|
143
|
+
{
|
|
144
|
+
return $this->type === 'qpay';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Generates a new cash/cash on delivery gateway
|
|
149
|
+
*
|
|
150
|
+
* @return Gateway
|
|
151
|
+
*/
|
|
152
|
+
public static function cash($attributes = ['sandbox' => 0]): Gateway
|
|
153
|
+
{
|
|
154
|
+
return new static([
|
|
155
|
+
'public_id' => 'gateway_cash',
|
|
156
|
+
'name' => 'Cash',
|
|
157
|
+
'code' => 'cash',
|
|
158
|
+
'type' => 'cash',
|
|
159
|
+
'sandbox' => $attributes['sandbox'],
|
|
160
|
+
'return_url' => null,
|
|
161
|
+
'callback_url' => null,
|
|
162
|
+
...$attributes
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
}
|