From 8ddce667cc10e5aae01118029d4bb5ad3ec13951 Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Mon, 1 Jul 2024 18:33:59 +0200 Subject: [PATCH] Added billing information to inertia data --- app/Http/Middleware/HandleInertiaRequests.php | 15 ++++++++++++++- app/Providers/AppServiceProvider.php | 3 +++ app/Service/BillingContract.php | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/Service/BillingContract.php diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 84204c4b..c7bd5640 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Http\Middleware; +use App\Service\BillingContract; use Illuminate\Http\Request; use Inertia\Middleware; use Nwidart\Modules\Facades\Module; @@ -38,8 +39,20 @@ class HandleInertiaRequests extends Middleware */ public function share(Request $request): array { + $hasBilling = Module::has('Billing') && Module::isEnabled('Billing'); + $billing = null; + if ($hasBilling) { + /** @var BillingContract $billing */ + $billing = app(BillingContract::class); + } + + $currentOrganization = $request->user()?->currentTeam; + return array_merge(parent::share($request), [ - 'has_billing_extension' => Module::has('Billing'), + 'has_billing_extension' => $hasBilling, + 'billing' => $billing !== null ? [ + 'has_subscription' => $currentOrganization !== null ? $billing->hasSubscription($currentOrganization) : null, + ] : null, 'flash' => [ 'message' => fn () => $request->session()->get('message'), ], diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 5dbf207c..264ca38a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -13,6 +13,7 @@ use App\Models\Tag; use App\Models\Task; use App\Models\TimeEntry; use App\Models\User; +use App\Service\BillingContract; use App\Service\IpLookup\IpLookupServiceContract; use App\Service\IpLookup\NoIpLookupService; use App\Service\PermissionStore; @@ -87,7 +88,9 @@ class AppServiceProvider extends ServiceProvider return new PermissionStore(); }); + // Extensions $this->app->bind(IpLookupServiceContract::class, NoIpLookupService::class); + $this->app->bind(BillingContract::class); Route::model('member', Member::class); Route::model('invitation', OrganizationInvitation::class); diff --git a/app/Service/BillingContract.php b/app/Service/BillingContract.php new file mode 100644 index 00000000..ae9df7d5 --- /dev/null +++ b/app/Service/BillingContract.php @@ -0,0 +1,15 @@ +