Files
ladill-give/app/Http/Controllers/Qr/AccountController.php
T
isaaccladandCursor e5d95167dd
Deploy Ladill Give / deploy (push) Successful in 1m0s
Add optional owner gateway routing to Give.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 19:47:13 +00:00

128 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers\Qr;
use App\Http\Controllers\Controller;
use App\Models\PaymentGatewaySetting;
use App\Models\QrSetting;
use App\Services\Billing\BillingClient;
use App\Services\Billing\PlanEntitlementService;
use App\Services\Payments\MerchantGatewayService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
class AccountController extends Controller
{
public function __construct(
private BillingClient $billing,
private PlanEntitlementService $entitlements,
private MerchantGatewayService $gateway,
) {}
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, config('billing.service', 'mini'));
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();
return view('give.settings', [
'account' => $account,
'settings' => $settings,
'gateway' => $this->gateway->settingFor($account),
'canUsePaymentGateway' => $this->entitlements->canUseCustomPaymentGateway($account),
]);
}
public function updateSettings(Request $request): RedirectResponse
{
$account = ladill_account();
$data = $request->validate([
'notify_email' => ['nullable', 'email', 'max:255'],
'product_updates' => ['nullable', 'boolean'],
'notify_registrations' => ['nullable', 'boolean'],
'notify_payouts' => ['nullable', 'boolean'],
'gateway_provider' => ['nullable', Rule::in(['', PaymentGatewaySetting::PROVIDER_PAYSTACK])],
'gateway_public_key' => ['nullable', 'string', 'max:2000'],
'gateway_secret_key' => ['nullable', 'string', 'max:2000'],
'gateway_is_active' => ['nullable', 'boolean'],
]);
QrSetting::updateOrCreate(
['user_id' => $account->id],
[
'notify_email' => $data['notify_email'] ?? null,
'product_updates' => $request->boolean('product_updates'),
'notify_registrations' => $request->boolean('notify_registrations'),
'notify_payouts' => $request->boolean('notify_payouts'),
],
);
if ($this->entitlements->canUseCustomPaymentGateway($account)) {
$provider = trim((string) ($data['gateway_provider'] ?? ''));
$setting = PaymentGatewaySetting::query()->where('owner_ref', $account->public_id)->first();
if ($provider !== '' || $setting) {
$setting ??= new PaymentGatewaySetting(['owner_ref' => $account->public_id]);
if ($provider !== '') {
$setting->provider = $provider;
}
if (filled($data['gateway_public_key'] ?? null)) {
$setting->public_key = $data['gateway_public_key'];
}
if (filled($data['gateway_secret_key'] ?? null)) {
$setting->secret_key = $data['gateway_secret_key'];
}
$setting->is_active = $provider !== '' && $request->boolean('gateway_is_active');
$setting->save();
}
}
return redirect()->route('account.settings')->with('success', 'Settings saved.');
}
}