Route event registrations through Ladill Pay API.
Deploy Ladill Events / deploy (push) Successful in 29s

Ticket and contribution checkouts use platform Pay for fees and wallet settlement, with legacy QREP-* fallback via Billing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-06-08 05:20:30 +00:00
co-authored by Cursor
parent 55e2ec71b3
commit c92ad91a06
7 changed files with 187 additions and 57 deletions
+1
View File
@@ -13,6 +13,7 @@ class QrEventRegistration extends Model
public const STATUS_CANCELLED = 'cancelled';
protected $fillable = [
'pay_order_id',
'qr_code_id',
'user_id',
'reference',
+104 -57
View File
@@ -4,24 +4,19 @@ namespace App\Services\Events;
use App\Models\QrCode;
use App\Models\QrEventRegistration;
use App\Models\UserWalletTransaction;
use App\Services\Billing\BillingClient;
use App\Services\Billing\PaystackService;
use App\Services\Billing\SmsService;
use App\Services\Billing\WalletService;
use App\Services\Pay\PayClient;
use Illuminate\Support\Str;
use RuntimeException;
class EventRegistrationService
{
/** Platform fee on paid ticket sales. */
public const PLATFORM_FEE_RATE_TICKETING = 0.15;
/** Reduced platform fee on contribution / donation funds. */
public const PLATFORM_FEE_RATE_CONTRIBUTIONS = 0.09;
public function __construct(
private PayClient $pay,
private PaystackService $paystack,
private WalletService $wallet,
private BillingClient $billing,
private SmsService $sms,
) {}
@@ -66,7 +61,6 @@ class EventRegistrationService
throw new RuntimeException('Enter a valid email address.');
}
// Capacity guard (0 = unlimited) — ticketing only.
if ($mode === 'ticketing') {
$capacity = (int) ($tier['capacity'] ?? 0);
if ($capacity > 0) {
@@ -76,7 +70,7 @@ class EventRegistrationService
->where('status', QrEventRegistration::STATUS_CONFIRMED)
->count();
if ($taken >= $capacity) {
throw new RuntimeException('Sorry, "' . $tierName . '" is sold out.');
throw new RuntimeException('Sorry, "'.$tierName.'" is sold out.');
}
}
}
@@ -100,54 +94,108 @@ class EventRegistrationService
$amountMinor = (int) round($priceGhs * 100);
$registration = QrEventRegistration::create([
'qr_code_id' => $qrCode->id,
'user_id' => $qrCode->user_id,
'reference' => 'QRE-' . strtoupper(Str::random(16)),
'badge_code' => $this->uniqueBadgeCode(),
'tier_name' => $tierName,
'amount_minor' => $amountMinor,
'currency' => $content['currency'] ?? 'GHS',
'attendee_name' => mb_substr($name, 0, 120),
'qr_code_id' => $qrCode->id,
'user_id' => $qrCode->user_id,
'reference' => 'QRE-'.strtoupper(Str::random(16)),
'badge_code' => $this->uniqueBadgeCode(),
'tier_name' => $tierName,
'amount_minor' => $amountMinor,
'currency' => $content['currency'] ?? 'GHS',
'attendee_name' => mb_substr($name, 0, 120),
'attendee_email' => $email,
'attendee_phone' => trim((string) ($data['attendee_phone'] ?? '')) ?: null,
'badge_fields' => $badgeFields ?: null,
'status' => $amountMinor > 0 ? QrEventRegistration::STATUS_PENDING : QrEventRegistration::STATUS_CONFIRMED,
'badge_fields' => $badgeFields ?: null,
'status' => $amountMinor > 0 ? QrEventRegistration::STATUS_PENDING : QrEventRegistration::STATUS_CONFIRMED,
]);
// Free ticket — done.
if ($amountMinor === 0) {
$this->notifyConfirmed($registration->fresh('qrCode'));
return ['registration' => $registration, 'paid' => false, 'checkout_url' => null];
}
// Paid ticket — start Paystack.
$reference = 'QREP-' . strtoupper(Str::random(16));
$registration->update(['payment_reference' => $reference]);
$qrCode->loadMissing('user');
$feeTier = $mode === 'contributions' ? 'donations' : 'sales';
$lineName = $mode === 'contributions'
? sprintf('%s — %s', $content['name'] ?? $qrCode->label, $tierName)
: sprintf('%s ticket — %s', $content['name'] ?? $qrCode->label, $tierName);
$paystackData = $this->paystack->initializeTransaction([
'email' => $email,
'amount' => $amountMinor,
'currency' => $registration->currency,
'reference' => $reference,
$payOrder = $this->pay->createCheckout([
'merchant' => $qrCode->user->public_id,
'fee_tier' => $feeTier,
'source_service' => 'events',
'source_ref' => (string) $qrCode->id,
'callback_url' => route('qr.public.event.callback', ['shortCode' => $qrCode->short_code]),
'metadata' => [
'customer_name' => $registration->attendee_name,
'customer_email' => $email,
'customer_phone' => $registration->attendee_phone,
'line_items' => [
[
'name' => $lineName,
'unit_price_minor' => $amountMinor,
'quantity' => 1,
],
],
'metadata' => [
'registration_id' => $registration->id,
'qr_code_id' => $qrCode->id,
'merchant_id' => $qrCode->user_id,
'attendee_name' => $registration->attendee_name,
'attendee_email' => $email,
'registration_reference' => $registration->reference,
'qr_code_id' => $qrCode->id,
'mode' => $mode,
],
]);
$registration->update([
'pay_order_id' => $payOrder['id'] ?? null,
'payment_reference' => $payOrder['reference'],
]);
$checkoutUrl = (string) ($payOrder['checkout_url'] ?? '');
if ($checkoutUrl === '') {
throw new RuntimeException('Could not start checkout. Please try again.');
}
return [
'registration' => $registration,
'paid' => true,
'checkout_url' => 'https://checkout.paystack.com/' . $paystackData['access_code'],
'registration' => $registration->fresh(),
'paid' => true,
'checkout_url' => $checkoutUrl,
];
}
public function complete(string $reference): QrEventRegistration
{
if (str_starts_with($reference, 'LP-')) {
return $this->completeLadillPay($reference);
}
return $this->completeLegacy($reference);
}
private function completeLadillPay(string $reference): QrEventRegistration
{
$registration = QrEventRegistration::where('payment_reference', $reference)
->where('status', QrEventRegistration::STATUS_PENDING)
->firstOrFail();
$payOrder = $this->pay->verify($reference);
$platformFeeGhs = ((int) ($payOrder['platform_fee_minor'] ?? 0)) / 100;
$registration->update([
'status' => QrEventRegistration::STATUS_CONFIRMED,
'paid_at' => now(),
'pay_order_id' => $payOrder['id'] ?? $registration->pay_order_id,
'metadata' => array_merge((array) $registration->metadata, [
'ladill_pay' => $payOrder,
'platform_fee_ghs' => $platformFeeGhs,
]),
]);
$this->notifyConfirmed($registration->fresh('qrCode'));
return $registration;
}
/** Legacy QREP-* references before Ladill Pay migration. */
private function completeLegacy(string $reference): QrEventRegistration
{
$registration = QrEventRegistration::where('payment_reference', $reference)
->where('status', QrEventRegistration::STATUS_PENDING)
@@ -160,37 +208,36 @@ class EventRegistrationService
throw new RuntimeException('Payment was not successful.');
}
$feeRate = ($registration->qrCode?->content()['mode'] ?? 'ticketing') === 'contributions'
? self::PLATFORM_FEE_RATE_CONTRIBUTIONS
: self::PLATFORM_FEE_RATE_TICKETING;
$feeRate = ($registration->qrCode?->content()['mode'] ?? 'ticketing') === 'contributions' ? 0.09 : 0.15;
$paidAmount = round(($data['amount'] ?? 0) / 100, 2);
$platformFee = round($paidAmount * $feeRate, 2);
$organizerAmount = round($paidAmount - $platformFee, 2);
$organizerAmountMinor = (int) round(($paidAmount - $platformFee) * 100);
$registration->update([
'status' => QrEventRegistration::STATUS_CONFIRMED,
'paid_at' => now(),
'metadata' => array_merge((array) $registration->metadata, ['paystack' => $data, 'platform_fee_ghs' => $platformFee]),
'status' => QrEventRegistration::STATUS_CONFIRMED,
'paid_at' => now(),
'metadata' => array_merge((array) $registration->metadata, [
'paystack' => $data,
'platform_fee_ghs' => $platformFee,
'legacy' => true,
]),
]);
$this->wallet->credit(
$registration->organizer,
$organizerAmount,
UserWalletTransaction::SOURCE_SERVICE_TRANSFER,
$mode = ($registration->qrCode->content()['mode'] ?? 'ticketing') === 'contributions' ? 'contributions' : 'ticketing';
$this->billing->credit(
$registration->organizer->public_id,
$organizerAmountMinor,
'events',
'pay',
$reference,
$registration->id,
sprintf(
'%s — %s (%s)',
($registration->qrCode->content()['mode'] ?? 'ticketing') === 'contributions' ? 'Event contribution' : 'Event ticket',
$mode === 'contributions' ? 'Event contribution' : 'Event ticket',
$registration->qrCode->label,
$registration->tier_name
),
[
'source' => 'qr_event',
'registration_id' => $registration->id,
'qr_code_id' => $registration->qr_code_id,
'platform_fee_ghs' => $platformFee,
]
);
$this->notifyConfirmed($registration->fresh('qrCode'));
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace App\Services\Pay;
use Illuminate\Support\Facades\Http;
/**
* Client for the platform Ladill Pay HTTP API checkout and settlement for
* merchant↔buyer money. Paystack is the processor today; settlement lands in
* the one UserWallet via Platform Billing.
*/
class PayClient
{
private function base(): string
{
return rtrim((string) config('pay.api_url'), '/');
}
private function token(): string
{
return (string) (config('pay.api_key') ?? '');
}
/** @param array<string,mixed> $payload */
public function createCheckout(array $payload): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/checkouts', $payload);
$res->throw();
return (array) $res->json();
}
public function verify(string $reference): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/checkouts/verify', [
'reference' => $reference,
]);
$res->throw();
return (array) $res->json();
}
public function show(string $reference): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(10)->get($this->base().'/orders/'.$reference);
$res->throw();
return (array) $res->json();
}
}