Files
ladill-events/app/Services/Identity/IdentityClient.php
T
isaaccladandCursor d25682bc6c
Deploy Ladill Events / deploy (push) Successful in 38s
Complete Ladill Pay payouts UI for Events.
Show per-event payment history with fees and net amounts, and let organizers manage payout accounts and withdrawals without leaving the app.

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

133 lines
3.8 KiB
PHP

<?php
namespace App\Services\Identity;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
class IdentityClient
{
/** @return array<string, mixed> */
public function mailboxLinkStatus(string $publicId): array
{
$response = $this->request()->get($this->url('/identity/mailbox-link'), [
'user' => $publicId,
]);
$response->throw();
return (array) $response->json('data', []);
}
/** @return array<string, mixed> */
public function linkMailbox(string $publicId, string $mailboxAddress): array
{
$response = $this->request()->put($this->url('/identity/mailbox-link'), [
'user' => $publicId,
'mailbox_address' => $mailboxAddress,
]);
$response->throw();
return (array) $response->json('data', []);
}
/** @return array<string, mixed> */
public function unlinkMailbox(string $publicId): array
{
$response = $this->request()->delete($this->url('/identity/mailbox-link'), [
'user' => $publicId,
]);
$response->throw();
return (array) $response->json('data', []);
}
/** @return array<int, array<string, mixed>> */
public function banks(string $type = 'mobile_money'): array
{
$queryType = $type === 'mobile_money' ? 'mobile_money' : 'bank';
$response = $this->request()->get($this->url('/identity/banks'), [
'type' => $queryType,
]);
$response->throw();
return (array) $response->json('data', []);
}
/** @return array<string, mixed>|null */
public function payoutAccount(string $publicId): ?array
{
$response = $this->request()->get($this->url('/identity/payout-account'), [
'user' => $publicId,
]);
$response->throw();
$account = $response->json('data.payout_account');
return is_array($account) ? $account : null;
}
/** @param array<string, mixed> $data
* @return array<string, mixed>
*/
public function updatePayoutAccount(string $publicId, array $data): array
{
$response = $this->request()->put($this->url('/identity/payout-account'), array_merge(
['user' => $publicId],
$data,
));
$this->throwValidation($response, 'account_number');
$account = $response->json('data.payout_account');
return is_array($account) ? $account : [];
}
/** @return array<int, array<string, mixed>> */
public function withdrawals(string $publicId): array
{
$response = $this->request()->get($this->url('/identity/wallet/withdrawals'), [
'user' => $publicId,
]);
$response->throw();
return (array) $response->json('data', []);
}
/** @return array<string, mixed> */
public function withdraw(string $publicId, float $amountGhs): array
{
$response = $this->request()->post($this->url('/identity/wallet/withdraw'), [
'user' => $publicId,
'amount' => $amountGhs,
]);
$this->throwValidation($response, 'amount');
return (array) $response->json('data', []);
}
private function throwValidation(Response $response, string $fallbackField = 'base'): void
{
if ($response->status() === 422) {
throw ValidationException::withMessages(
$response->json('errors') ?: [$fallbackField => [$response->json('message') ?: 'Request failed.']],
);
}
}
private function request()
{
return Http::withToken((string) config('identity.api_key'))
->acceptJson()
->timeout(15);
}
private function url(string $path): string
{
return rtrim((string) config('identity.api_url'), '/').$path;
}
}