Deploy Ladill Hosting / deploy (push) Successful in 24s
Display hosted_sites on account cards, scope Afia and Settings to hosting, add hosting_settings for notifications, and remove mailbox UI from the hosting layout and account pages. Co-authored-by: Cursor <cursoragent@cursor.com>
107 lines
3.1 KiB
PHP
107 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Hosting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\HostingSetting;
|
|
use App\Services\Billing\BillingClient;
|
|
use App\Services\Payment\WalletPaymentService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
/**
|
|
* Account — Wallet, Billing, Settings for Ladill Hosting.
|
|
* Money is read from the one platform UserWallet via the Billing API.
|
|
*/
|
|
class AccountController extends Controller
|
|
{
|
|
public function __construct(
|
|
private WalletPaymentService $wallet,
|
|
private BillingClient $billing,
|
|
) {}
|
|
|
|
private function topupUrl(): string
|
|
{
|
|
return 'https://'.config('app.account_domain').'/wallet';
|
|
}
|
|
|
|
public function wallet(): View
|
|
{
|
|
$account = ladill_account();
|
|
[$balanceMinor, $ledger] = $this->billingSnapshot($account?->public_id);
|
|
|
|
return view('hosting.account.wallet', [
|
|
'balanceMinor' => $balanceMinor,
|
|
'spentMinor' => (int) ($ledger['spent_minor'] ?? 0),
|
|
'creditedMinor' => (int) ($ledger['credited_minor'] ?? 0),
|
|
'topupUrl' => $this->topupUrl(),
|
|
]);
|
|
}
|
|
|
|
public function billing(): View
|
|
{
|
|
$account = ladill_account();
|
|
[$balanceMinor, $ledger] = $this->billingSnapshot($account?->public_id);
|
|
|
|
return view('hosting.account.billing', [
|
|
'balanceMinor' => $balanceMinor,
|
|
'spentMinor' => (int) ($ledger['spent_minor'] ?? 0),
|
|
'creditedMinor' => (int) ($ledger['credited_minor'] ?? 0),
|
|
'topupUrl' => $this->topupUrl(),
|
|
]);
|
|
}
|
|
|
|
public function settings(): View
|
|
{
|
|
$account = ladill_account();
|
|
$settings = HostingSetting::firstOrNew(['user_id' => $account?->id]);
|
|
|
|
return view('hosting.account.settings', [
|
|
'account' => $account,
|
|
'settings' => $settings,
|
|
]);
|
|
}
|
|
|
|
public function updateSettings(Request $request): RedirectResponse
|
|
{
|
|
$account = ladill_account();
|
|
|
|
$data = $request->validate([
|
|
'notify_email' => ['nullable', 'email', 'max:255'],
|
|
'product_updates' => ['nullable', 'boolean'],
|
|
]);
|
|
|
|
HostingSetting::updateOrCreate(
|
|
['user_id' => $account->id],
|
|
[
|
|
'notify_email' => $data['notify_email'] ?? null,
|
|
'product_updates' => $request->boolean('product_updates'),
|
|
],
|
|
);
|
|
|
|
return redirect()->route('account.settings')->with('success', 'Settings saved.');
|
|
}
|
|
|
|
/**
|
|
* @return array{0:int,1:array<string,mixed>} [balanceMinor, ledger]
|
|
*/
|
|
private function billingSnapshot(?string $publicId): array
|
|
{
|
|
if (! $publicId) {
|
|
return [0, []];
|
|
}
|
|
|
|
try {
|
|
return [
|
|
$this->wallet->balanceMinor(ladill_account()),
|
|
$this->billing->serviceLedger($publicId, (string) config('billing.service', 'hosting')),
|
|
];
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
|
|
return [0, []];
|
|
}
|
|
}
|
|
}
|