Files
ladill-mini/app/Http/Controllers/Public/PaymentController.php
T
isaacclad a50b5c6a44 Keep Mini QR Paystack checkout fully in-app.
Stop form POSTs from 307ing to ladl.link or redirecting away to
checkout.paystack.com, open Paystack Inline with access_code, and use
the single-chrome sheet so payment stays on the Mini page.
2026-07-21 21:32:36 +00:00

115 lines
4.0 KiB
PHP

<?php
namespace App\Http\Controllers\Public;
use App\Http\Controllers\Controller;
use App\Models\MiniPayment;
use App\Models\QrCode;
use App\Services\Mini\MiniPaymentService;
use App\Support\LadillLink;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use RuntimeException;
class PaymentController extends Controller
{
public function __construct(private MiniPaymentService $payments) {}
public function pay(Request $request, string $shortCode): JsonResponse|RedirectResponse
{
$qrCode = QrCode::query()
->with('user.qrSetting')
->where('short_code', $shortCode)
->where('type', QrCode::TYPE_PAYMENT)
->where('is_active', true)
->firstOrFail();
$validated = $request->validate([
'amount' => 'required|numeric|min:0.01',
'customer_phone' => 'nullable|string|max:32',
]);
try {
$result = $this->payments->initiate($qrCode, $validated);
} catch (RuntimeException $e) {
if ($request->expectsJson()) {
return response()->json(['error' => $e->getMessage()], 422);
}
return back()->withInput()->with('error', $e->getMessage());
}
if ($request->expectsJson() || $request->ajax()) {
return response()->json([
'checkout_url' => $result['checkout_url'],
'access_code' => $result['access_code'] ?? null,
'public_key' => $result['public_key'] ?? null,
'callback_url' => $result['callback_url'] ?? null,
'reference' => $result['reference'] ?? null,
'provider' => $result['provider'] ?? null,
]);
}
// Never send the browser to checkout.paystack.com — reopen the landing
// with checkout payload so Paystack Inline can run in-page.
$provider = (string) ($result['provider'] ?? '');
if ($provider === 'mtn_momo') {
$waiting = (string) ($result['checkout_url'] ?? '');
if ($waiting !== '' && str_starts_with($waiting, url('/'))) {
return redirect()->to($waiting);
}
}
return redirect()
->to(url('/q/'.$shortCode))
->with('checkout_url', $result['checkout_url'] ?? null)
->with('access_code', $result['access_code'] ?? null)
->with('public_key', $result['public_key'] ?? null)
->with('callback_url', $result['callback_url'] ?? null)
->with('provider', $provider !== '' ? $provider : 'paystack')
->with('amount', $validated['amount']);
}
public function callback(Request $request, string $shortCode): View
{
$reference = trim((string) $request->query('reference', ''));
if ($reference === '') {
return view('public.payment-return', [
'redirect' => LadillLink::url($shortCode),
]);
}
try {
$payment = $this->payments->complete($reference);
} catch (RuntimeException $e) {
return view('public.payment-return', [
'redirect' => LadillLink::url($shortCode),
]);
}
return view('public.payment-return', [
'redirect' => route('qr.public.payment.confirmed', [
'shortCode' => $shortCode,
'reference' => $payment->payment_reference,
], absolute: true),
]);
}
public function confirmed(string $shortCode, string $reference): View
{
$payment = MiniPayment::query()
->with('qrCode')
->where('payment_reference', $reference)
->where('status', MiniPayment::STATUS_PAID)
->whereHas('qrCode', fn ($q) => $q->where('short_code', $shortCode))
->firstOrFail();
return view('public.qr.payment-confirmed', [
'payment' => $payment,
'qrCode' => $payment->qrCode,
]);
}
}