Files
ladill-give/app/Http/Controllers/Public/DonationController.php
T
isaaccladandCursor bd16fb307b
Deploy Ladill Give / deploy (push) Successful in 48s
Make guest giving checkouts email-optional like Ladill Mini.
Donors without Ladill accounts no longer need to enter email at checkout; phone remains required for contact.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-27 14:18:45 +00:00

73 lines
2.5 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);
}
$validated = $request->validate([
'customer_name' => ['required', 'string', 'max:120'],
'customer_email' => ['nullable', '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,
]);
}
}