SendsPagarmeRequests.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Services\Pagarme\Concerns;
  3. use App\Data\Pagarme\PagarmeData;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. use Throwable;
  7. trait SendsPagarmeRequests
  8. {
  9. protected function pagarmeRequest(
  10. string $method,
  11. string $path,
  12. string $idempotencyKey,
  13. string $errorMessage, array|PagarmeData $payload,
  14. ): array {
  15. $payload = $payload instanceof PagarmeData ? $payload->toArray() : $payload;
  16. $endpoint = $this->pagarmeUrl($path);
  17. try {
  18. $response = $this->pagarmeHttp($idempotencyKey)
  19. ->send($method, $endpoint, ['json' => $payload])
  20. ->throw();
  21. $result = $response->json() ?? [];
  22. if (app()->environment('local', 'development')) {
  23. Log::channel('pagarme')->info('Pagar.me request succeeded', [
  24. 'method' => strtoupper($method),
  25. 'endpoint' => $endpoint,
  26. 'origin_ip' => $this->pagarmeOriginIp(),
  27. 'payload' => $payload,
  28. 'result' => $result,
  29. ]);
  30. }
  31. return $result;
  32. } catch (Throwable $e) {
  33. $responseBody = null;
  34. if (method_exists($e, 'getResponse')) {
  35. $responseBody = $e->getResponse()?->json();
  36. } elseif (isset($e->response)) {
  37. $responseBody = $e->response->json();
  38. }
  39. Log::channel('pagarme')->error('Pagar.me request failed', [
  40. 'method' => strtoupper($method),
  41. 'endpoint' => $endpoint,
  42. 'origin_ip' => $this->pagarmeOriginIp(),
  43. 'payload' => $payload,
  44. 'exception' => $e->getMessage(),
  45. 'result' => $responseBody,
  46. ]);
  47. $message = $errorMessage;
  48. $errors = $responseBody['errors'] ?? [];
  49. if ($errors) {
  50. $details = [];
  51. foreach ($errors as $field => $msgs) {
  52. foreach ((array) $msgs as $msg) {
  53. $details[] = $msg;
  54. }
  55. }
  56. if ($details) {
  57. $message .= ': '.implode(' ', $details);
  58. }
  59. }
  60. throw new \RuntimeException($message, previous: $e);
  61. }
  62. }
  63. protected function pagarmeHttp(string $idempotencyKey)
  64. {
  65. $secretKey = config('services.pagarme.secret_key');
  66. if (empty($secretKey)) {
  67. Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
  68. throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
  69. }
  70. return Http::withBasicAuth($secretKey, '')
  71. ->withHeaders([
  72. 'Idempotency-Key' => $idempotencyKey,
  73. 'Content-Type' => 'application/json',
  74. 'Accept' => 'application/json',
  75. ]);
  76. }
  77. protected function pagarmeUrl(string $path): string
  78. {
  79. return rtrim(config('services.pagarme.base_url'), '/').'/'.ltrim($path, '/');
  80. }
  81. protected function pagarmeOriginIp(): ?string
  82. {
  83. try {
  84. return trim(Http::timeout(3)->get('https://api.ipify.org')->body()) ?: null;
  85. } catch (Throwable) {
  86. return null;
  87. }
  88. }
  89. }