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 @@