Donation checkout via Ladill Pay (9% fee), church/giving QR pages, SSO, and platform scan forwarding. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\GiveDonation;
|
|
use App\Models\QrCode;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class SearchController extends Controller
|
|
{
|
|
public function __invoke(Request $request): JsonResponse|View
|
|
{
|
|
$q = trim((string) $request->query('q'));
|
|
$results = mb_strlen($q) >= 2 ? $this->results($q) : [];
|
|
|
|
if ($request->expectsJson() || $request->ajax() || $request->wantsJson()) {
|
|
return response()->json(['results' => $results]);
|
|
}
|
|
|
|
return view('search.index', [
|
|
'query' => $q,
|
|
'results' => $results,
|
|
]);
|
|
}
|
|
|
|
/** @return list<array{type: string, title: string, subtitle: string, url: string}> */
|
|
private function results(string $q): array
|
|
{
|
|
$account = ladill_account();
|
|
$like = '%'.$q.'%';
|
|
|
|
$qrIds = $account->qrCodes()
|
|
->where('type', QrCode::TYPE_CHURCH)
|
|
->pluck('id');
|
|
|
|
if ($qrIds->isEmpty()) {
|
|
return [];
|
|
}
|
|
|
|
return GiveDonation::query()
|
|
->whereIn('qr_code_id', $qrIds)
|
|
->with('qrCode')
|
|
->where(function ($query) use ($like) {
|
|
$query->where('payer_name', 'like', $like)
|
|
->orWhere('payer_email', 'like', $like)
|
|
->orWhere('collection_type', 'like', $like)
|
|
->orWhere('reference', 'like', $like)
|
|
->orWhere('payment_reference', 'like', $like)
|
|
->orWhereHas('qrCode', fn ($qr) => $qr->where('label', 'like', $like));
|
|
})
|
|
->latest('created_at')
|
|
->limit(15)
|
|
->get()
|
|
->map(function (GiveDonation $donation): array {
|
|
$amount = number_format(
|
|
($donation->status === GiveDonation::STATUS_PAID ? $donation->merchant_amount_minor : $donation->amount_minor) / 100,
|
|
2,
|
|
);
|
|
|
|
return [
|
|
'type' => 'donation',
|
|
'title' => $donation->payer_name ?: 'Anonymous donor',
|
|
'subtitle' => 'GHS '.$amount.' · '.($donation->collection_type ?: 'Donation').' · '.ucfirst($donation->status),
|
|
'url' => route('give.donations.index', ['q' => $donation->reference]),
|
|
];
|
|
})
|
|
->all();
|
|
}
|
|
}
|