Files
ladill-pos/app/Http/Controllers/Api/Mini/WalletController.php
T
isaaccladandCursor e5d2b84388
Deploy Ladill Mini / deploy (push) Successful in 23s
Add Ladill POS v1 — register, Pay checkout, and commerce links.
Staff-facing counter register at pos.ladill.com with catalog cart, cash and
MoMo/card checkout via Ladill Pay, CRM timeline/import, invoice prefill, and
Merchant catalog import.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 22:52:24 +00:00

162 lines
5.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Mini;
use App\Http\Controllers\Api\Concerns\CallsIdentityApi;
use App\Http\Controllers\Controller;
use App\Services\Billing\BillingClient;
use App\Services\Mini\AutoWithdrawService;
use App\Services\Notifications\MiniNotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Throwable;
class WalletController extends Controller
{
use CallsIdentityApi;
public function __construct(
private BillingClient $billing,
private MiniNotificationService $notifications,
private AutoWithdrawService $autoWithdraw,
) {}
public function show(): JsonResponse
{
$account = ladill_account();
$balanceMinor = 0;
$ledger = [];
try {
$balanceMinor = $this->billing->balanceMinor($account->public_id);
$ledger = $this->billing->serviceLedger($account->public_id, config('billing.service', 'mini'));
} catch (Throwable $e) {
Log::warning('Mini API wallet load failed', ['user' => $account->public_id, 'error' => $e->getMessage()]);
}
$settings = $account->getOrCreateQrSetting();
return response()->json([
'data' => [
'currency' => 'GHS',
'balance_minor' => $balanceMinor,
'spent_minor' => (int) ($ledger['spent_minor'] ?? 0),
'credited_minor' => (int) ($ledger['credited_minor'] ?? 0),
'auto_withdraw_amount_minor' => $settings->auto_withdraw_amount_minor,
],
]);
}
public function updateAutoWithdraw(Request $request): JsonResponse
{
$account = ladill_account();
$data = $request->validate([
'amount' => ['nullable', 'numeric', 'min:1', 'max:50000'],
]);
$amountMinor = isset($data['amount']) ? (int) round((float) $data['amount'] * 100) : null;
$settings = $account->getOrCreateQrSetting();
$settings->update(['auto_withdraw_amount_minor' => $amountMinor]);
if ($amountMinor !== null) {
$this->autoWithdraw->attemptForUser($account);
}
return response()->json([
'data' => [
'auto_withdraw_amount_minor' => $settings->fresh()->auto_withdraw_amount_minor,
],
]);
}
public function topup(Request $request): JsonResponse
{
$account = ladill_account();
$data = $request->validate([
'amount' => ['required', 'numeric', 'min:1', 'max:10000'],
]);
$response = $this->identitySend('POST', '/api/identity/wallet-topup-account', [
'user' => $account->public_id,
'amount' => (float) $data['amount'],
// Paystack returns the customer here after payment; the app catches the deep link.
'return_url' => 'https://'.config('app.platform_domain').'/mini/wallet/topup-complete',
]);
$this->rethrowValidation($response, 'amount');
$checkoutUrl = (string) $response->json('data.checkout_url', '');
if ($checkoutUrl === '') {
return response()->json(['message' => 'Could not start top-up. Please try again.'], 422);
}
return response()->json(['data' => ['checkout_url' => $checkoutUrl]]);
}
public function banks(Request $request): JsonResponse
{
$type = $request->query('type') === 'mobile_money' ? 'mobile_money' : 'bank';
$response = $this->identitySend('GET', '/api/identity/banks?type='.urlencode($type), []);
return response()->json(['data' => $response->json('data', [])]);
}
public function payoutAccount(): JsonResponse
{
$response = $this->identitySend('GET', '/api/identity/payout-account?user='.urlencode((string) ladill_account()->public_id), []);
return response()->json(['data' => ['payout_account' => $response->json('data.payout_account')]]);
}
public function updatePayoutAccount(Request $request): JsonResponse
{
$data = $request->validate([
'account_type' => ['required', 'in:mobile_money,bank_account'],
'account_name' => ['required', 'string', 'max:200'],
'account_number' => ['required', 'string', 'max:30'],
'bank_code' => ['required', 'string', 'max:30'],
'bank_name' => ['required', 'string', 'max:200'],
'currency' => ['sometimes', 'string', 'size:3'],
]);
$data['currency'] = $data['currency'] ?? 'GHS';
$response = $this->identitySend('PUT', '/api/identity/payout-account', array_merge(
['user' => ladill_account()->public_id],
$data,
));
$this->rethrowValidation($response, 'account_number');
return response()->json(['data' => ['payout_account' => $response->json('data.payout_account')]]);
}
public function withdrawals(): JsonResponse
{
$response = $this->identitySend('GET', '/api/identity/wallet/withdrawals?user='.urlencode((string) ladill_account()->public_id), []);
return response()->json(['data' => $response->json('data', [])]);
}
public function withdraw(Request $request): JsonResponse
{
$data = $request->validate([
'amount' => ['required', 'numeric', 'min:1', 'max:50000'],
]);
$response = $this->identitySend('POST', '/api/identity/wallet/withdraw', [
'user' => ladill_account()->public_id,
'amount' => (float) $data['amount'],
]);
$this->rethrowValidation($response, 'amount');
$this->notifications->withdrawalSubmitted(
ladill_account(),
(float) $data['amount'],
);
return response()->json(['data' => $response->json('data', [])]);
}
}