SendsPagarmeRequests.php 3.4 KB

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