switched to paddle inline checkout, removed paypal and most of stripe. added product sync between app and paddle.
This commit is contained in:
82
app/Services/Paddle/PaddleClient.php
Normal file
82
app/Services/Paddle/PaddleClient.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Paddle;
|
||||
|
||||
use App\Services\Paddle\Exceptions\PaddleException;
|
||||
use Illuminate\Http\Client\Factory as HttpFactory;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PaddleClient
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HttpFactory $http,
|
||||
) {}
|
||||
|
||||
public function get(string $endpoint, array $query = []): array
|
||||
{
|
||||
return $this->send('GET', $endpoint, ['query' => $query]);
|
||||
}
|
||||
|
||||
public function post(string $endpoint, array $payload = []): array
|
||||
{
|
||||
return $this->send('POST', $endpoint, ['json' => $payload]);
|
||||
}
|
||||
|
||||
public function patch(string $endpoint, array $payload = []): array
|
||||
{
|
||||
return $this->send('PATCH', $endpoint, ['json' => $payload]);
|
||||
}
|
||||
|
||||
public function delete(string $endpoint, array $payload = []): array
|
||||
{
|
||||
return $this->send('DELETE', $endpoint, ['json' => $payload]);
|
||||
}
|
||||
|
||||
protected function send(string $method, string $endpoint, array $options = []): array
|
||||
{
|
||||
$request = $this->preparedRequest();
|
||||
|
||||
try {
|
||||
$response = $request->send(strtoupper($method), ltrim($endpoint, '/'), $options);
|
||||
} catch (RequestException $exception) {
|
||||
throw new PaddleException($exception->getMessage(), $exception->response?->status(), $exception->response?->json() ?? []);
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
$body = $response->json() ?? [];
|
||||
$message = Arr::get($body, 'error.message')
|
||||
?? Arr::get($body, 'message')
|
||||
?? sprintf('Paddle request failed with status %s', $response->status());
|
||||
|
||||
throw new PaddleException($message, $response->status(), $body);
|
||||
}
|
||||
|
||||
return $response->json() ?? [];
|
||||
}
|
||||
|
||||
protected function preparedRequest(): PendingRequest
|
||||
{
|
||||
$apiKey = config('paddle.api_key');
|
||||
if (! $apiKey) {
|
||||
throw new PaddleException('Paddle API key is not configured.');
|
||||
}
|
||||
|
||||
$baseUrl = rtrim((string) config('paddle.base_url'), '/');
|
||||
$environment = (string) config('paddle.environment', 'production');
|
||||
|
||||
$headers = [
|
||||
'User-Agent' => sprintf('FotospielApp/%s PaddleClient', app()->version()),
|
||||
'Paddle-Environment' => Str::lower($environment) === 'sandbox' ? 'sandbox' : 'production',
|
||||
];
|
||||
|
||||
return $this->http
|
||||
->baseUrl($baseUrl)
|
||||
->withHeaders($headers)
|
||||
->withToken($apiKey)
|
||||
->acceptJson()
|
||||
->asJson();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user