Donation checkout via Ladill Pay (9% fee), church/giving QR pages, SSO, and platform scan forwarding. Co-authored-by: Cursor <cursoragent@cursor.com>
77 lines
2.7 KiB
PHP
77 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Public;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\QrCode;
|
|
use App\Services\Give\GiveDonationService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use RuntimeException;
|
|
|
|
class DonationController extends Controller
|
|
{
|
|
public function __construct(private GiveDonationService $donations) {}
|
|
|
|
public function store(Request $request, string $shortCode): JsonResponse
|
|
{
|
|
$qrCode = QrCode::query()
|
|
->where('short_code', $shortCode)
|
|
->where('type', QrCode::TYPE_CHURCH)
|
|
->where('is_active', true)
|
|
->firstOrFail();
|
|
|
|
if (! $qrCode->acceptsOrders()) {
|
|
return response()->json(['error' => 'This giving page does not accept donations.'], 422);
|
|
}
|
|
|
|
if (empty($qrCode->content()['accepts_payment'])) {
|
|
return response()->json(['error' => 'Online giving is not enabled for this page.'], 422);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'customer_name' => ['required', 'string', 'max:120'],
|
|
'customer_email' => ['required', 'email', 'max:200'],
|
|
'customer_phone' => ['required', 'string', 'max:30'],
|
|
'items' => ['required', 'array', 'min:1'],
|
|
'items.*.name' => ['required', 'string', 'max:200'],
|
|
'items.*.price' => ['required', 'numeric', 'min:0.01'],
|
|
'items.*.qty' => ['required', 'integer', 'min:1', 'max:1'],
|
|
]);
|
|
|
|
try {
|
|
$result = $this->donations->initiateOrder($qrCode, $validated);
|
|
} catch (RuntimeException $e) {
|
|
return response()->json(['error' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json([
|
|
'checkout_url' => $result['checkout_url'],
|
|
'callback_url' => $result['callback_url'],
|
|
]);
|
|
}
|
|
|
|
public function callback(Request $request, string $shortCode): RedirectResponse|View
|
|
{
|
|
$reference = trim((string) $request->query('reference', ''));
|
|
if ($reference === '') {
|
|
return redirect('/q/'.$shortCode)->with('error', 'Missing payment reference.');
|
|
}
|
|
|
|
try {
|
|
$donation = $this->donations->complete($reference);
|
|
} catch (RuntimeException $e) {
|
|
return redirect('/q/'.$shortCode)->with('order_error', $e->getMessage());
|
|
} catch (\Throwable) {
|
|
return redirect('/q/'.$shortCode)->with('order_error', 'Payment could not be verified. Contact the organisation with reference: '.$reference);
|
|
}
|
|
|
|
return view('public.qr.donation-confirmed', [
|
|
'donation' => $donation,
|
|
'qrCode' => $donation->qrCode,
|
|
]);
|
|
}
|
|
}
|