Deploy Ladill QR Plus / deploy (push) Successful in 1m15s
Store per-account preferences in qr_settings and pre-fill the create flow with saved type and brand styling. Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
4.7 KiB
PHP
122 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Qr;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\QrSetting;
|
|
use App\Services\Billing\BillingClient;
|
|
use App\Support\Qr\QrCornerStyleCatalog;
|
|
use App\Support\Qr\QrFrameStyleCatalog;
|
|
use App\Support\Qr\QrModuleStyleCatalog;
|
|
use App\Support\Qr\QrTypeCatalog;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class AccountController extends Controller
|
|
{
|
|
public function __construct(private BillingClient $billing) {}
|
|
|
|
private function topupUrl(): string
|
|
{
|
|
return 'https://'.config('app.account_domain').'/wallet';
|
|
}
|
|
|
|
/** @return array{0: int, 1: array<string, mixed>} */
|
|
private function billingSnapshot(?string $publicId): array
|
|
{
|
|
if (! $publicId) {
|
|
return [0, []];
|
|
}
|
|
|
|
$balanceMinor = $this->billing->balanceMinor($publicId);
|
|
$ledger = $this->billing->serviceLedger($publicId, 'qr');
|
|
|
|
return [$balanceMinor, $ledger];
|
|
}
|
|
|
|
public function wallet(): View
|
|
{
|
|
$user = ladill_account();
|
|
[$balanceMinor, $ledger] = $this->billingSnapshot($user?->public_id);
|
|
|
|
return view('qr.account.wallet', [
|
|
'balanceMinor' => $balanceMinor,
|
|
'spentMinor' => (int) ($ledger['spent_minor'] ?? 0),
|
|
'creditedMinor' => (int) ($ledger['credited_minor'] ?? 0),
|
|
'topupUrl' => $this->topupUrl(),
|
|
]);
|
|
}
|
|
|
|
public function billing(): View
|
|
{
|
|
$user = ladill_account();
|
|
[$balanceMinor, $ledger] = $this->billingSnapshot($user?->public_id);
|
|
|
|
return view('qr.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 = $account->getOrCreateQrSetting();
|
|
$style = $settings->resolvedDefaultStyle();
|
|
|
|
return view('qr.account.settings', [
|
|
'account' => $account,
|
|
'settings' => $settings,
|
|
'style' => $style,
|
|
'types' => QrTypeCatalog::all(),
|
|
'moduleStyles' => QrModuleStyleCatalog::visible(),
|
|
'cornerOuterStyles' => QrCornerStyleCatalog::outerStyles(),
|
|
'cornerInnerStyles' => QrCornerStyleCatalog::innerStyles(),
|
|
'frameStyles' => QrFrameStyleCatalog::visible(),
|
|
]);
|
|
}
|
|
|
|
public function updateSettings(Request $request): RedirectResponse
|
|
{
|
|
$account = ladill_account();
|
|
|
|
$data = $request->validate([
|
|
'notify_email' => ['nullable', 'email', 'max:255'],
|
|
'product_updates' => ['nullable', 'boolean'],
|
|
'low_balance_alerts' => ['nullable', 'boolean'],
|
|
'default_type' => ['nullable', 'in:'.implode(',', QrTypeCatalog::plusTypes())],
|
|
'default_style' => ['nullable', 'array'],
|
|
'default_style.foreground' => ['nullable', 'regex:/^#[0-9a-fA-F]{6}$/'],
|
|
'default_style.background' => ['nullable', 'regex:/^#[0-9a-fA-F]{6}$/'],
|
|
'default_style.error_correction' => ['nullable', 'in:L,M,Q,H'],
|
|
'default_style.margin' => ['nullable', 'integer', 'min:0', 'max:10'],
|
|
'default_style.module_style' => ['nullable', 'in:'.implode(',', QrModuleStyleCatalog::keys())],
|
|
'default_style.finder_outer' => ['nullable', 'string', 'max:32'],
|
|
'default_style.finder_inner' => ['nullable', 'string', 'max:32'],
|
|
'default_style.frame_style' => ['nullable', 'string', 'max:32'],
|
|
'default_style.frame_text' => ['nullable', 'string', 'max:100'],
|
|
'default_style.frame_color' => ['nullable', 'regex:/^#[0-9a-fA-F]{6}$/'],
|
|
'default_style.gradient_type' => ['nullable', 'in:none,linear,radial'],
|
|
'default_style.gradient_color1' => ['nullable', 'regex:/^#[0-9a-fA-F]{6}$/'],
|
|
'default_style.gradient_color2' => ['nullable', 'regex:/^#[0-9a-fA-F]{6}$/'],
|
|
'default_style.gradient_rotation' => ['nullable', 'integer', 'min:0', 'max:360'],
|
|
]);
|
|
|
|
QrSetting::updateOrCreate(
|
|
['user_id' => $account->id],
|
|
[
|
|
'notify_email' => $data['notify_email'] ?? null,
|
|
'product_updates' => $request->boolean('product_updates'),
|
|
'low_balance_alerts' => $request->boolean('low_balance_alerts'),
|
|
'default_type' => $data['default_type'] ?? null,
|
|
'default_style' => QrSetting::sanitizeDefaultStyle($data['default_style'] ?? null),
|
|
],
|
|
);
|
|
|
|
return redirect()->route('account.settings')->with('success', 'Settings saved.');
|
|
}
|
|
}
|