52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\Billing\BillingClient;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* In-app wallet top-up. Posts to the central identity API to start a Paystack
|
|
* checkout against the one Ladill wallet, then hands the user to checkout. This
|
|
* backs the two-step "Add funds" modal so users never leave for the wallet page
|
|
* when they hit an insufficient-balance action.
|
|
*/
|
|
class WalletTopupController extends Controller
|
|
{
|
|
public function store(Request $request, BillingClient $billing): JsonResponse|RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'amount' => ['required', 'numeric', 'min:5', 'max:5000'],
|
|
'return_url' => ['nullable', 'url', 'max:2048'],
|
|
]);
|
|
|
|
$returnUrl = $validated['return_url'] ?? (string) (url()->previous() ?: url('/'));
|
|
|
|
try {
|
|
$checkoutUrl = $billing->topup(
|
|
(string) $request->user()->public_id,
|
|
(float) $validated['amount'],
|
|
$returnUrl,
|
|
);
|
|
} catch (\Throwable) {
|
|
$checkoutUrl = '';
|
|
}
|
|
|
|
if ($checkoutUrl === '') {
|
|
if ($request->expectsJson()) {
|
|
return response()->json(['error' => 'Unable to start payment. Please try again.'], 422);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to start payment. Please try again.');
|
|
}
|
|
|
|
if ($request->expectsJson()) {
|
|
return response()->json(['checkout_url' => $checkoutUrl]);
|
|
}
|
|
|
|
return redirect()->away($checkoutUrl);
|
|
}
|
|
}
|