From 739043755f813111a94d19892deae1ec49907a68 Mon Sep 17 00:00:00 2001 From: isaacclad Date: Tue, 23 Jun 2026 12:25:46 +0000 Subject: [PATCH] Add wallet balance widget to the profile menu (after Billing) Wallet balance peek rendered as a menu row directly under Billing, backed by a /wallet/balance endpoint (BillingClient) and guarded by Route::has. Also opens Afia via a direct window event for reliability. Co-Authored-By: Claude Opus 4.8 --- .../Controllers/WalletBalanceController.php | 40 +++++++++++++++++++ app/Support/UserProfileMenu.php | 5 +++ config/billing.php | 2 + resources/js/app.js | 20 ++++++++++ .../views/partials/afia-button.blade.php | 2 +- resources/views/partials/topbar.blade.php | 2 +- .../partials/user-profile-menu.blade.php | 2 + .../views/partials/wallet-widget.blade.php | 23 +++++++++++ routes/web.php | 2 + 9 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/WalletBalanceController.php create mode 100644 resources/views/partials/wallet-widget.blade.php diff --git a/app/Http/Controllers/WalletBalanceController.php b/app/Http/Controllers/WalletBalanceController.php new file mode 100644 index 0000000..29d4267 --- /dev/null +++ b/app/Http/Controllers/WalletBalanceController.php @@ -0,0 +1,40 @@ +user()->public_id; + + $minor = Cache::remember("wallet_balance:{$publicId}", now()->addSeconds(30), function () use ($billing, $publicId) { + try { + return $billing->balanceMinor($publicId); + } catch (\Throwable) { + return null; + } + }); + + if ($minor === null) { + return response()->json(['available' => false]); + } + + $currency = (string) config('billing.currency', 'GHS'); + + return response()->json([ + 'available' => true, + 'balance_minor' => $minor, + 'currency' => $currency, + 'formatted' => $currency.' '.number_format($minor / 100, 2), + ]); + } +} diff --git a/app/Support/UserProfileMenu.php b/app/Support/UserProfileMenu.php index 3228d49..2806cdb 100644 --- a/app/Support/UserProfileMenu.php +++ b/app/Support/UserProfileMenu.php @@ -43,6 +43,11 @@ class UserProfileMenu ]; } + + if (Route::has((string) config("billing.wallet_balance_route", "wallet.balance"))) { + $items[] = ["type" => "wallet"]; + } + if (self::userIsAdmin($user)) { $adminHref = self::platformUrl('account', 'admin'); diff --git a/config/billing.php b/config/billing.php index b60b1ed..83b9b7b 100644 --- a/config/billing.php +++ b/config/billing.php @@ -11,4 +11,6 @@ return [ 'api_url' => env('BILLING_API_URL', 'https://ladill.com/api/billing'), 'api_key' => env('BILLING_API_KEY_HOSTING'), 'service' => 'hosting', + 'wallet_balance_route' => 'wallet.balance', + 'currency' => 'GHS', ]; diff --git a/resources/js/app.js b/resources/js/app.js index 184f323..eefe37e 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -195,6 +195,26 @@ Alpine.data('topbarSearch', (config = {}) => ({ }, })); + +// Wallet balance peek for the avatar dropdown. +Alpine.data('walletWidget', (config = {}) => ({ + display: '…', + async load() { + if (! config.url) { + this.display = 'View wallet'; + + return; + } + try { + const res = await fetch(config.url, { headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' } }); + const data = await res.json(); + this.display = data.available ? data.formatted : 'View wallet'; + } catch (e) { + this.display = 'View wallet'; + } + }, +})); + window.Alpine = Alpine; registerLadillConfirmStore(Alpine); diff --git a/resources/views/partials/afia-button.blade.php b/resources/views/partials/afia-button.blade.php index f8eede3..0588ba6 100644 --- a/resources/views/partials/afia-button.blade.php +++ b/resources/views/partials/afia-button.blade.php @@ -8,7 +8,7 @@ @if ($handler === 'openAfia') @click="openAfia()" @else - @click="$dispatch('afia-open')" + @click="window.dispatchEvent(new CustomEvent('afia-open'))" @endif @class([ 'inline-flex shrink-0 items-center justify-center gap-2 rounded-xl bg-gradient-to-r from-indigo-700 via-blue-600 to-emerald-400 text-sm font-semibold text-white shadow-sm transition hover:opacity-95', diff --git a/resources/views/partials/topbar.blade.php b/resources/views/partials/topbar.blade.php index 10fd8b4..c54b526 100644 --- a/resources/views/partials/topbar.blade.php +++ b/resources/views/partials/topbar.blade.php @@ -79,7 +79,7 @@
+ class="absolute right-0 z-50 mt-2 w-64 rounded-xl border border-slate-200 bg-white shadow-lg"> @include('partials.user-profile-menu', [ 'items' => \App\Support\UserProfileMenu::items($u), ]) diff --git a/resources/views/partials/user-profile-menu.blade.php b/resources/views/partials/user-profile-menu.blade.php index ee7e421..c78cb3e 100644 --- a/resources/views/partials/user-profile-menu.blade.php +++ b/resources/views/partials/user-profile-menu.blade.php @@ -53,6 +53,8 @@ @if ($onNavigate) @click="{{ $onNavigate }}" @endif> {{ $item['label'] }} + @elseif (($item['type'] ?? '') === 'wallet') + @includeIf('partials.wallet-widget') @elseif (($item['type'] ?? '') === 'logout') @if ($variant !== 'sheet')
diff --git a/resources/views/partials/wallet-widget.blade.php b/resources/views/partials/wallet-widget.blade.php new file mode 100644 index 0000000..8661874 --- /dev/null +++ b/resources/views/partials/wallet-widget.blade.php @@ -0,0 +1,23 @@ +{{-- Wallet balance peek (links to the account wallet on account.ladill.com). --}} +@php + $balanceRoute = (string) config('billing.wallet_balance_route', 'wallet.balance'); + $balanceUrl = \Illuminate\Support\Facades\Route::has($balanceRoute) ? route($balanceRoute) : null; +@endphp +@if ($balanceUrl) + + + + + + + + + Wallet balance + + + + + +@endif diff --git a/routes/web.php b/routes/web.php index 2b05f30..6099137 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ auth()->check() ? redirect()->route('hosting.dashboard') : view('hosting.signed-out'))->name('hosting.signed-out'); Route::middleware(['auth', 'platform.session'])->group(function () use ($serversApp) { + Route::get('/wallet/balance', [WalletBalanceController::class, 'balance'])->name('wallet.balance'); Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index'); Route::get('/notifications/unread', [NotificationController::class, 'unread'])->name('notifications.unread'); Route::post('/notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.mark-read');