Files
ladill-mini/app/Services/Pay/PayClient.php
T
isaaccladandCursor 53a83b84c5
Deploy Ladill Mini / deploy (push) Successful in 42s
Collect MoMo MSISDN and open waiting page on Pay.
Payment QR was still Paystack-only: no customer_phone and mobile iframe sheet. Require MoMo number, pass it to Ladill Pay, and full-page redirect for mtn_momo waiting URLs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-13 22:52:52 +00:00

72 lines
2.1 KiB
PHP

<?php
namespace App\Services\Pay;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use RuntimeException;
/**
* Client for the platform Ladill Pay HTTP API — checkout and settlement for
* merchant↔buyer money. Provider is configured on the platform
* (PAY_DEFAULT_PROVIDER); settlement lands via wallet or direct MoMo.
*/
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);
return $this->jsonOrFail($res, 'Could not start checkout. Please try again.');
}
public function verify(string $reference): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/checkouts/verify', [
'reference' => $reference,
]);
return $this->jsonOrFail($res, 'Could not verify payment. Please try again.');
}
public function show(string $reference): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(10)->get($this->base().'/orders/'.$reference);
return $this->jsonOrFail($res, 'Could not load payment order.');
}
/** @return array<string,mixed> */
private function jsonOrFail(Response $res, string $fallback): array
{
if ($res->failed()) {
throw new RuntimeException($this->errorMessage($res, $fallback));
}
return (array) $res->json();
}
private function errorMessage(Response $res, string $fallback): string
{
$message = $res->json('error') ?? $res->json('message');
if (is_array($message)) {
$message = collect($message)->flatten()->first();
}
$message = trim((string) ($message ?? ''));
return $message !== '' ? $message : $fallback;
}
}