| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Services\Pagarme\Concerns;
- use App\Data\Pagarme\PagarmeData;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- use Throwable;
- trait SendsPagarmeRequests
- {
- protected function pagarmeRequest(
- string $method,
- string $path,
- string $idempotencyKey,
- string $errorMessage,
- array|PagarmeData $payload,
- ): array {
- $payload = $payload instanceof PagarmeData ? $payload->toArray() : $payload;
- $endpoint = $this->pagarmeUrl($path);
- try {
- $response = $this->pagarmeHttp($idempotencyKey)
- ->send($method, $endpoint, ['json' => $payload])
- ->throw();
- return $response->json() ?? [];
- } catch (Throwable $e) {
- Log::channel('pagarme')->error('Pagar.me request failed', [
- 'method' => strtoupper($method),
- 'endpoint' => $endpoint,
- 'payload' => $payload,
- 'exception' => $e->getMessage(),
- ]);
- }
- }
- protected function pagarmeHttp(string $idempotencyKey)
- {
- $secretKey = config('services.pagarme.secret_key');
- if (empty($secretKey)) {
- Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
- throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
- }
- return Http::withBasicAuth($secretKey, '')
- ->withHeaders([
- 'Idempotency-Key' => $idempotencyKey,
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ]);
- }
- protected function pagarmeUrl(string $path): string
- {
- return rtrim(config('services.pagarme.base_url'), '/').'/'.ltrim($path, '/');
- }
- }
|