Checkout and settlement now go via platform Pay instead of direct Paystack + Billing credit, with legacy MIN-* support retained. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -34,6 +34,9 @@ LADILL_SSO_CLIENT_SECRET=
|
|||||||
BILLING_API_URL=https://ladill.com/api/billing
|
BILLING_API_URL=https://ladill.com/api/billing
|
||||||
BILLING_API_KEY_MINI=
|
BILLING_API_KEY_MINI=
|
||||||
|
|
||||||
|
PAY_API_URL=https://ladill.com/api/pay
|
||||||
|
PAY_API_KEY_MINI=
|
||||||
|
|
||||||
IDENTITY_API_URL=https://ladill.com/api
|
IDENTITY_API_URL=https://ladill.com/api
|
||||||
IDENTITY_API_KEY_MINI=
|
IDENTITY_API_KEY_MINI=
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ main app DB user.
|
|||||||
|---|---|
|
|---|---|
|
||||||
| `LADILL_SSO_CLIENT_ID` / `LADILL_SSO_CLIENT_SECRET` | `passport:client` on platform |
|
| `LADILL_SSO_CLIENT_ID` / `LADILL_SSO_CLIENT_SECRET` | `passport:client` on platform |
|
||||||
| `BILLING_API_KEY_MINI` | platform `.env` + this app |
|
| `BILLING_API_KEY_MINI` | platform `.env` + this app |
|
||||||
|
| `PAY_API_KEY_MINI` | platform `.env` (`PAY_API_KEY_MINI`) + this app — Ladill Pay checkout |
|
||||||
| `IDENTITY_API_KEY_MINI` | platform `.env` + this app |
|
| `IDENTITY_API_KEY_MINI` | platform `.env` + this app |
|
||||||
| `PLATFORM_DB_*` | read `ladilldb.platform_settings` (Paystack keys from admin → Billing) |
|
| `PLATFORM_DB_*` | read `ladilldb.platform_settings` (Paystack keys from admin → Billing) |
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class MiniPayment extends Model
|
|||||||
public const PLATFORM_FEE_RATE = 0.05;
|
public const PLATFORM_FEE_RATE = 0.05;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
|
'pay_order_id',
|
||||||
'qr_code_id',
|
'qr_code_id',
|
||||||
'user_id',
|
'user_id',
|
||||||
'reference',
|
'reference',
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ use App\Models\QrCode;
|
|||||||
use App\Services\Billing\BillingClient;
|
use App\Services\Billing\BillingClient;
|
||||||
use App\Services\Billing\PaystackService;
|
use App\Services\Billing\PaystackService;
|
||||||
use App\Services\Billing\SmsService;
|
use App\Services\Billing\SmsService;
|
||||||
|
use App\Services\Pay\PayClient;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class MiniPaymentService
|
class MiniPaymentService
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private PayClient $pay,
|
||||||
private PaystackService $paystack,
|
private PaystackService $paystack,
|
||||||
private BillingClient $billing,
|
private BillingClient $billing,
|
||||||
private SmsService $sms,
|
private SmsService $sms,
|
||||||
@@ -36,7 +38,7 @@ class MiniPaymentService
|
|||||||
$qrCode->loadMissing('user');
|
$qrCode->loadMissing('user');
|
||||||
$amountMinor = (int) round($amountGhs * 100);
|
$amountMinor = (int) round($amountGhs * 100);
|
||||||
$reference = 'MINP-'.strtoupper(Str::random(16));
|
$reference = 'MINP-'.strtoupper(Str::random(16));
|
||||||
$payRef = 'MIN-'.strtoupper(Str::random(16));
|
$businessName = $qrCode->content()['business_name'] ?? $qrCode->label ?? 'Payment';
|
||||||
|
|
||||||
$payment = MiniPayment::create([
|
$payment = MiniPayment::create([
|
||||||
'qr_code_id' => $qrCode->id,
|
'qr_code_id' => $qrCode->id,
|
||||||
@@ -49,38 +51,81 @@ class MiniPaymentService
|
|||||||
'payer_phone' => null,
|
'payer_phone' => null,
|
||||||
'payer_note' => null,
|
'payer_note' => null,
|
||||||
'status' => MiniPayment::STATUS_PENDING,
|
'status' => MiniPayment::STATUS_PENDING,
|
||||||
'payment_reference' => $payRef,
|
'payment_reference' => null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$paystackData = $this->paystack->initializeTransaction([
|
$payOrder = $this->pay->createCheckout([
|
||||||
'email' => (string) config('services.paystack.checkout_email', 'pay@ladill.com'),
|
'merchant' => $qrCode->user->public_id,
|
||||||
'amount' => $amountMinor,
|
'fee_tier' => 'payments',
|
||||||
'currency' => $payment->currency,
|
'source_service' => 'mini',
|
||||||
'reference' => $payRef,
|
'source_ref' => (string) $qrCode->id,
|
||||||
'callback_url' => route('qr.public.payment.callback', ['shortCode' => $qrCode->short_code]),
|
'callback_url' => route('qr.public.payment.callback', ['shortCode' => $qrCode->short_code]),
|
||||||
|
'line_items' => [
|
||||||
|
[
|
||||||
|
'name' => $businessName,
|
||||||
|
'unit_price_minor' => $amountMinor,
|
||||||
|
'quantity' => 1,
|
||||||
|
],
|
||||||
|
],
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
'mini_payment_id' => $payment->id,
|
'mini_payment_id' => $payment->id,
|
||||||
|
'mini_reference' => $reference,
|
||||||
'qr_code_id' => $qrCode->id,
|
'qr_code_id' => $qrCode->id,
|
||||||
'merchant_id' => $qrCode->user_id,
|
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$checkoutUrl = $paystackData['authorization_url']
|
$payment->update([
|
||||||
?? (isset($paystackData['access_code']) && $paystackData['access_code'] !== ''
|
'pay_order_id' => $payOrder['id'] ?? null,
|
||||||
? 'https://checkout.paystack.com/'.$paystackData['access_code']
|
'payment_reference' => $payOrder['reference'],
|
||||||
: '');
|
'platform_fee_minor' => $payOrder['platform_fee_minor'] ?? null,
|
||||||
|
'merchant_amount_minor' => $payOrder['merchant_amount_minor'] ?? null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$checkoutUrl = (string) ($payOrder['checkout_url'] ?? '');
|
||||||
if ($checkoutUrl === '') {
|
if ($checkoutUrl === '') {
|
||||||
throw new RuntimeException('Could not start Paystack checkout. Please try again.');
|
throw new RuntimeException('Could not start checkout. Please try again.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'payment' => $payment,
|
'payment' => $payment->fresh(),
|
||||||
'checkout_url' => $checkoutUrl,
|
'checkout_url' => $checkoutUrl,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function complete(string $paymentReference): MiniPayment
|
public function complete(string $paymentReference): MiniPayment
|
||||||
|
{
|
||||||
|
if (str_starts_with($paymentReference, 'LP-')) {
|
||||||
|
return $this->completeLadillPay($paymentReference);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->completeLegacy($paymentReference);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function completeLadillPay(string $reference): MiniPayment
|
||||||
|
{
|
||||||
|
$payment = MiniPayment::where('payment_reference', $reference)
|
||||||
|
->where('status', MiniPayment::STATUS_PENDING)
|
||||||
|
->firstOrFail();
|
||||||
|
|
||||||
|
$payOrder = $this->pay->verify($reference);
|
||||||
|
|
||||||
|
$payment->update([
|
||||||
|
'status' => MiniPayment::STATUS_PAID,
|
||||||
|
'amount_minor' => (int) ($payOrder['amount_minor'] ?? $payment->amount_minor),
|
||||||
|
'platform_fee_minor' => (int) ($payOrder['platform_fee_minor'] ?? 0),
|
||||||
|
'merchant_amount_minor' => (int) ($payOrder['merchant_amount_minor'] ?? 0),
|
||||||
|
'pay_order_id' => $payOrder['id'] ?? $payment->pay_order_id,
|
||||||
|
'paid_at' => now(),
|
||||||
|
'metadata' => array_merge((array) $payment->metadata, ['ladill_pay' => $payOrder]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->notifyPayer($payment->fresh(['qrCode', 'merchant']));
|
||||||
|
|
||||||
|
return $payment;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Legacy MIN-* references before Ladill Pay migration. */
|
||||||
|
private function completeLegacy(string $paymentReference): MiniPayment
|
||||||
{
|
{
|
||||||
$payment = MiniPayment::where('payment_reference', $paymentReference)
|
$payment = MiniPayment::where('payment_reference', $paymentReference)
|
||||||
->where('status', MiniPayment::STATUS_PENDING)
|
->where('status', MiniPayment::STATUS_PENDING)
|
||||||
@@ -103,14 +148,12 @@ class MiniPaymentService
|
|||||||
'platform_fee_minor' => $platformFeeMinor,
|
'platform_fee_minor' => $platformFeeMinor,
|
||||||
'merchant_amount_minor' => $merchantMinor,
|
'merchant_amount_minor' => $merchantMinor,
|
||||||
'paid_at' => now(),
|
'paid_at' => now(),
|
||||||
'metadata' => array_merge((array) $payment->metadata, ['paystack' => $data]),
|
'metadata' => array_merge((array) $payment->metadata, ['paystack' => $data, 'legacy' => true]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$merchant = $payment->merchant;
|
|
||||||
$businessName = $payment->qrCode?->content()['business_name'] ?? $payment->qrCode?->label ?? 'Payment QR';
|
$businessName = $payment->qrCode?->content()['business_name'] ?? $payment->qrCode?->label ?? 'Payment QR';
|
||||||
|
|
||||||
$this->billing->credit(
|
$this->billing->credit(
|
||||||
$merchant->public_id,
|
$payment->merchant->public_id,
|
||||||
$merchantMinor,
|
$merchantMinor,
|
||||||
'mini',
|
'mini',
|
||||||
'pay',
|
'pay',
|
||||||
@@ -119,19 +162,28 @@ class MiniPaymentService
|
|||||||
sprintf('Payment via %s', $businessName),
|
sprintf('Payment via %s', $businessName),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($payment->payer_phone) {
|
$this->notifyPayer($payment->fresh(['qrCode', 'merchant']));
|
||||||
$this->sms->send(
|
|
||||||
$payment->payer_phone,
|
return $payment;
|
||||||
sprintf(
|
}
|
||||||
'Payment of %s %s to %s confirmed. Ref: %s',
|
|
||||||
$payment->currency,
|
private function notifyPayer(MiniPayment $payment): void
|
||||||
number_format($paidMinor / 100, 2),
|
{
|
||||||
$businessName,
|
if (! $payment->payer_phone) {
|
||||||
$payment->reference
|
return;
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $payment->fresh(['qrCode', 'merchant']);
|
$businessName = $payment->qrCode?->content()['business_name'] ?? $payment->qrCode?->label ?? 'Payment QR';
|
||||||
|
|
||||||
|
$this->sms->send(
|
||||||
|
$payment->payer_phone,
|
||||||
|
sprintf(
|
||||||
|
'Payment of %s %s to %s confirmed. Ref: %s',
|
||||||
|
$payment->currency,
|
||||||
|
number_format($payment->amount_minor / 100, 2),
|
||||||
|
$businessName,
|
||||||
|
$payment->reference
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'api_url' => env('PAY_API_URL', 'https://ladill.com/api/pay'),
|
||||||
|
'api_key' => env('PAY_API_KEY_MINI'),
|
||||||
|
];
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('mini_payments', function (Blueprint $table) {
|
||||||
|
$table->unsignedBigInteger('pay_order_id')->nullable()->after('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('mini_payments', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('pay_order_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user