PagarmeRecipientService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Services\Pagarme;
  3. use App\Models\Provider;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. class PagarmeRecipientService
  7. {
  8. public function createRecipientForProvider(Provider $provider, array $data): string
  9. {
  10. if (!empty($provider->recipient_id)) {
  11. return $provider->recipient_id;
  12. }
  13. $bankAccountData = $data['recipient_default_bank_account'];
  14. $metadata = $data['recipient_metadata'] ?? [];
  15. $paymentMode = $data['recipient_payment_mode'];
  16. $response = $this->pagarmeRequest($provider->id)
  17. ->post($this->pagarmeUrl('/recipients'), [
  18. 'default_bank_account' => [
  19. 'holder_name' => $bankAccountData['holder_name'],
  20. 'holder_type' => $bankAccountData['holder_type'],
  21. 'holder_document' => $bankAccountData['holder_document'],
  22. 'bank' => $bankAccountData['bank'],
  23. 'branch_number' => $bankAccountData['branch_number'],
  24. 'branch_check_digit' => $bankAccountData['branch_check_digit'] ?? null,
  25. 'account_number' => $bankAccountData['account_number'],
  26. 'account_check_digit' => $bankAccountData['account_check_digit'],
  27. 'type' => $bankAccountData['type'],
  28. 'metadata' => $bankAccountData['metadata'] ?? [],
  29. 'pix_key' => $bankAccountData['pix_key'] ?? null,
  30. ],
  31. 'metadata' => $metadata,
  32. 'code' => $data['recipient_code'],
  33. 'payment_mode' => $paymentMode,
  34. 'name' => $data['recipient_name'],
  35. 'email' => $data['recipient_email'],
  36. 'description' => $data['recipient_description'],
  37. 'document' => $data['recipient_document'],
  38. 'type' => $data['recipient_type'],
  39. 'transfer_settings' => [
  40. 'transfer_enabled' => false,
  41. 'transfer_interval' => 'daily',
  42. 'transfer_day' => 0,
  43. ],
  44. ]);
  45. if ($response->failed()) {
  46. Log::channel('pagarme')->error('Pagar.me recipient creation failed', [
  47. 'status' => $response->status(),
  48. 'body' => $response->json() ?? $response->body(),
  49. ]);
  50. throw new \RuntimeException('Erro ao criar recebedor no Pagar.me.');
  51. }
  52. $recipientData = $response->json();
  53. $recipientId = $recipientData['id'] ?? null;
  54. if (!$recipientId) {
  55. Log::channel('pagarme')->error('Pagar.me recipient creation returned empty id', [
  56. 'response' => $recipientData,
  57. ]);
  58. throw new \RuntimeException('Pagar.me recipient creation returned an empty id.');
  59. }
  60. $provider->forceFill([
  61. 'recipient_id' => $recipientId,
  62. 'recipient_name' => $data['recipient_name'],
  63. 'recipient_email' => $data['recipient_email'],
  64. 'recipient_description' => $data['recipient_description'],
  65. 'recipient_document' => $data['recipient_document'],
  66. 'recipient_type' => $data['recipient_type'],
  67. 'recipient_code' => $data['recipient_code'],
  68. 'recipient_payment_mode' => $paymentMode,
  69. 'recipient_default_bank_account' => $bankAccountData,
  70. 'recipient_transfer_settings' => [
  71. 'transfer_enabled' => false,
  72. 'transfer_interval' => 'daily',
  73. 'transfer_day' => 0,
  74. ],
  75. 'recipient_automatic_anticipation_settings' => [
  76. 'enabled' => false,
  77. ],
  78. 'recipient_metadata' => $metadata,
  79. ])->save();
  80. $this->applyAutomaticAnticipationSettings($provider->id, $recipientId);
  81. return $recipientId;
  82. }
  83. //
  84. private function pagarmeUrl(string $path): string
  85. {
  86. return rtrim(config('services.pagarme.base_url'), '/') . '/' . ltrim($path, '/');
  87. }
  88. private function idempotencyKey(int $providerId, string $suffix = 'recipient'): string
  89. {
  90. return "provider-{$providerId}-{$suffix}";
  91. }
  92. //
  93. private function pagarmeRequest(int $providerId, string $suffix = 'recipient')
  94. {
  95. $secretKey = config('services.pagarme.secret_key');
  96. if (empty($secretKey)) {
  97. Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
  98. throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
  99. }
  100. return Http::withBasicAuth($secretKey, '')
  101. ->withHeaders([
  102. 'Idempotency-Key' => $this->idempotencyKey($providerId, $suffix),
  103. 'Content-Type' => 'application/json',
  104. 'Accept' => 'application/json',
  105. ]);
  106. }
  107. private function applyAutomaticAnticipationSettings(int $providerId, string $recipientId): void
  108. {
  109. $response = $this->pagarmeRequest($providerId, 'auto-anticipation')
  110. ->patch($this->pagarmeUrl("/recipients/{$recipientId}/automatic-anticipation-settings"), [
  111. 'enabled' => false,
  112. ]);
  113. if ($response->failed()) {
  114. Log::channel('pagarme')->error('Pagar.me automatic anticipation settings update failed', [
  115. 'status' => $response->status(),
  116. 'body' => $response->json() ?? $response->body(),
  117. 'recipient_id' => $recipientId,
  118. ]);
  119. throw new \RuntimeException('Erro ao atualizar antecipação automática do recebedor no Pagar.me.');
  120. }
  121. }
  122. }