Deploy Ladill Frontdesk / deploy (push) Successful in 44s
Host alerts use email/phone on the host record without requiring a Ladill account link; SMS and over-quota emails debit the org wallet at platform rates. Co-authored-by: Cursor <cursoragent@cursor.com>
83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Frontdesk;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Frontdesk\Concerns\ScopesToAccount;
|
|
use App\Services\Billing\BillingClient;
|
|
use App\Services\Frontdesk\PlanService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class WalletController extends Controller
|
|
{
|
|
use ScopesToAccount;
|
|
|
|
public function balance(Request $request, BillingClient $billing): JsonResponse
|
|
{
|
|
$publicId = (string) $request->user()->public_id;
|
|
|
|
$minor = Cache::remember("frontdesk_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),
|
|
]);
|
|
}
|
|
|
|
public function upgrade(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'settings.manage');
|
|
$organization = $this->organization($request);
|
|
|
|
if ($plans->isPro($organization)) {
|
|
return back()->with('success', 'Your organization is already on Frontdesk Pro.');
|
|
}
|
|
|
|
$priceMinor = $plans->proPriceMinor();
|
|
$ownerRef = $organization->owner_ref;
|
|
|
|
try {
|
|
if (! $billing->canAfford($ownerRef, $priceMinor)) {
|
|
return back()->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade to Pro.');
|
|
}
|
|
|
|
$charged = $billing->debit(
|
|
$ownerRef,
|
|
$priceMinor,
|
|
'frontdesk_pro',
|
|
'frontdesk-pro-'.$organization->id.'-'.now()->format('Y-m'),
|
|
'Ladill Frontdesk Pro — monthly subscription',
|
|
);
|
|
} catch (\Throwable) {
|
|
return back()->with('error', 'Could not process upgrade. Please try again.');
|
|
}
|
|
|
|
if (! $charged) {
|
|
return back()->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade to Pro.');
|
|
}
|
|
|
|
$settings = $organization->settings ?? [];
|
|
$settings['plan'] = 'pro';
|
|
$settings['plan_expires_at'] = now()->addMonth()->toIso8601String();
|
|
$organization->update(['settings' => $settings]);
|
|
|
|
return back()->with('success', 'Upgraded to Frontdesk Pro for one month.');
|
|
}
|
|
}
|