50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Licensing\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use RuntimeException;
|
|
|
|
/** Read-only Paddle Billing API access; no writes or webhook registration. */
|
|
final class PaddleReadOnlyClient
|
|
{
|
|
public function subscription(string $id): array
|
|
{
|
|
if (! preg_match('/^sub_[a-zA-Z0-9]+$/', $id)) {
|
|
throw new RuntimeException('Invalid Paddle subscription identifier.');
|
|
}
|
|
|
|
$token = trim((string) config('sonardesk-paddle.api_key'));
|
|
if ($token === '') {
|
|
throw new RuntimeException('SONARDESK_PADDLE_API_KEY is not configured.');
|
|
}
|
|
|
|
$environment = config('sonardesk-paddle.environment', 'sandbox');
|
|
if (! in_array($environment, ['sandbox', 'production'], true)) {
|
|
throw new RuntimeException('Invalid Paddle environment configuration.');
|
|
}
|
|
|
|
$url = $environment === 'production'
|
|
? 'https://api.paddle.com'
|
|
: 'https://sandbox-api.paddle.com';
|
|
|
|
$response = Http::baseUrl($url)
|
|
->withToken($token)
|
|
->acceptJson()
|
|
->timeout(8)
|
|
->retry(2, 200, throw: false)
|
|
->get('/subscriptions/'.$id);
|
|
|
|
if (! $response->successful()) {
|
|
throw new RuntimeException('Paddle lookup failed (HTTP '.$response->status().').');
|
|
}
|
|
|
|
$data = $response->json('data');
|
|
if (! is_array($data)) {
|
|
throw new RuntimeException('Unexpected Paddle response.');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|