Files
ladill-mini/app/Services/Mini/MiniPaymentService.php
T
isaaccladandCursor db66d99895 Initial Ladill Mini app — trader payment QRs without styling.
Lean control center at mini.ladill.com: payment QR CRUD, Paystack checkout with 5% fee settlement via Billing API, payments feed, and payouts. QR codes use a fixed black-and-white preset only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-07 18:43:39 +00:00

133 lines
4.7 KiB
PHP

<?php
namespace App\Services\Mini;
use App\Models\MiniPayment;
use App\Models\QrCode;
use App\Services\Billing\BillingClient;
use App\Services\Billing\PaystackService;
use App\Services\Billing\SmsService;
use Illuminate\Support\Str;
use RuntimeException;
class MiniPaymentService
{
public function __construct(
private PaystackService $paystack,
private BillingClient $billing,
private SmsService $sms,
) {}
/**
* @param array{payer_name?: string, payer_email: string, payer_phone?: string, payer_note?: string, amount: float} $data
* @return array{payment: MiniPayment, checkout_url: string}
*/
public function initiate(QrCode $qrCode, array $data): array
{
if ($qrCode->type !== QrCode::TYPE_PAYMENT) {
throw new RuntimeException('This QR is not a payment code.');
}
$amountGhs = round((float) ($data['amount'] ?? 0), 2);
if ($amountGhs <= 0) {
throw new RuntimeException('Enter an amount greater than zero.');
}
$email = trim((string) ($data['payer_email'] ?? ''));
if ($email === '' || ! str_contains($email, '@')) {
throw new RuntimeException('Enter a valid email address.');
}
$amountMinor = (int) round($amountGhs * 100);
$reference = 'MINP-'.strtoupper(Str::random(16));
$payRef = 'MIN-'.strtoupper(Str::random(16));
$payment = MiniPayment::create([
'qr_code_id' => $qrCode->id,
'user_id' => $qrCode->user_id,
'reference' => $reference,
'amount_minor' => $amountMinor,
'currency' => $qrCode->content()['currency'] ?? 'GHS',
'payer_name' => trim((string) ($data['payer_name'] ?? '')) ?: null,
'payer_email' => $email,
'payer_phone' => trim((string) ($data['payer_phone'] ?? '')) ?: null,
'payer_note' => trim((string) ($data['payer_note'] ?? '')) ?: null,
'status' => MiniPayment::STATUS_PENDING,
'payment_reference' => $payRef,
]);
$paystackData = $this->paystack->initializeTransaction([
'email' => $email,
'amount' => $amountMinor,
'currency' => $payment->currency,
'reference' => $payRef,
'callback_url' => route('qr.public.payment.callback', ['shortCode' => $qrCode->short_code]),
'metadata' => [
'mini_payment_id' => $payment->id,
'qr_code_id' => $qrCode->id,
'merchant_id' => $qrCode->user_id,
],
]);
return [
'payment' => $payment,
'checkout_url' => 'https://checkout.paystack.com/'.($paystackData['access_code'] ?? ''),
];
}
public function complete(string $paymentReference): MiniPayment
{
$payment = MiniPayment::where('payment_reference', $paymentReference)
->where('status', MiniPayment::STATUS_PENDING)
->firstOrFail();
$data = $this->paystack->verifyTransaction($paymentReference);
if (($data['status'] ?? '') !== 'success') {
$payment->update(['status' => MiniPayment::STATUS_FAILED]);
throw new RuntimeException('Payment was not successful.');
}
$paidMinor = (int) ($data['amount'] ?? $payment->amount_minor);
$platformFeeMinor = (int) round($paidMinor * MiniPayment::PLATFORM_FEE_RATE);
$merchantMinor = $paidMinor - $platformFeeMinor;
$payment->update([
'status' => MiniPayment::STATUS_PAID,
'amount_minor' => $paidMinor,
'platform_fee_minor' => $platformFeeMinor,
'merchant_amount_minor' => $merchantMinor,
'paid_at' => now(),
'metadata' => array_merge((array) $payment->metadata, ['paystack' => $data]),
]);
$merchant = $payment->merchant;
$businessName = $payment->qrCode?->content()['business_name'] ?? $payment->qrCode?->label ?? 'Payment QR';
$this->billing->credit(
$merchant->public_id,
$merchantMinor,
'mini',
'pay',
$paymentReference,
$payment->id,
sprintf('Payment via %s — %s', $businessName, $payment->payer_name ?: 'Customer'),
);
if ($payment->payer_phone) {
$this->sms->send(
$payment->payer_phone,
sprintf(
'Payment of %s %s to %s confirmed. Ref: %s',
$payment->currency,
number_format($paidMinor / 100, 2),
$businessName,
$payment->reference
)
);
}
return $payment->fresh(['qrCode', 'merchant']);
}
}