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, ]); } }