| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Services\Pagarme;
- use App\Models\Provider;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class PagarmeRecipientService
- {
- public function createRecipientForProvider(Provider $provider, array $data): string
- {
- if (!empty($provider->recipient_id)) {
- return $provider->recipient_id;
- }
- $bankAccountData = $data['recipient_default_bank_account'];
- $metadata = $data['recipient_metadata'] ?? [];
- $paymentMode = $data['recipient_payment_mode'];
- $response = $this->pagarmeRequest($provider->id)
- ->post($this->pagarmeUrl('/recipients'), [
- 'default_bank_account' => [
- 'holder_name' => $bankAccountData['holder_name'],
- 'holder_type' => $bankAccountData['holder_type'],
- 'holder_document' => $bankAccountData['holder_document'],
- 'bank' => $bankAccountData['bank'],
- 'branch_number' => $bankAccountData['branch_number'],
- 'branch_check_digit' => $bankAccountData['branch_check_digit'] ?? null,
- 'account_number' => $bankAccountData['account_number'],
- 'account_check_digit' => $bankAccountData['account_check_digit'],
- 'type' => $bankAccountData['type'],
- 'metadata' => $bankAccountData['metadata'] ?? [],
- 'pix_key' => $bankAccountData['pix_key'] ?? null,
- ],
- 'metadata' => $metadata,
- 'code' => $data['recipient_code'],
- 'payment_mode' => $paymentMode,
- 'name' => $data['recipient_name'],
- 'email' => $data['recipient_email'],
- 'description' => $data['recipient_description'],
- 'document' => $data['recipient_document'],
- 'type' => $data['recipient_type'],
- 'transfer_settings' => [
- 'transfer_enabled' => false,
- 'transfer_interval' => 'daily',
- 'transfer_day' => 0,
- ],
- ]);
- if ($response->failed()) {
- Log::channel('pagarme')->error('Pagar.me recipient creation failed', [
- 'status' => $response->status(),
- 'body' => $response->json() ?? $response->body(),
- ]);
- throw new \RuntimeException('Erro ao criar recebedor no Pagar.me.');
- }
- $recipientData = $response->json();
- $recipientId = $recipientData['id'] ?? null;
- if (!$recipientId) {
- Log::channel('pagarme')->error('Pagar.me recipient creation returned empty id', [
- 'response' => $recipientData,
- ]);
- throw new \RuntimeException('Pagar.me recipient creation returned an empty id.');
- }
- $provider->forceFill([
- 'recipient_id' => $recipientId,
- 'recipient_name' => $data['recipient_name'],
- 'recipient_email' => $data['recipient_email'],
- 'recipient_description' => $data['recipient_description'],
- 'recipient_document' => $data['recipient_document'],
- 'recipient_type' => $data['recipient_type'],
- 'recipient_code' => $data['recipient_code'],
- 'recipient_payment_mode' => $paymentMode,
- 'recipient_default_bank_account' => $bankAccountData,
- 'recipient_transfer_settings' => [
- 'transfer_enabled' => false,
- 'transfer_interval' => 'daily',
- 'transfer_day' => 0,
- ],
- 'recipient_automatic_anticipation_settings' => [
- 'enabled' => false,
- ],
- 'recipient_metadata' => $metadata,
- ])->save();
- $this->applyAutomaticAnticipationSettings($provider->id, $recipientId);
- return $recipientId;
- }
- //
- private function pagarmeUrl(string $path): string
- {
- return rtrim(config('services.pagarme.base_url'), '/') . '/' . ltrim($path, '/');
- }
- private function idempotencyKey(int $providerId, string $suffix = 'recipient'): string
- {
- return "provider-{$providerId}-{$suffix}";
- }
- //
- private function pagarmeRequest(int $providerId, string $suffix = 'recipient')
- {
- $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' => $this->idempotencyKey($providerId, $suffix),
- 'Content-Type' => 'application/json',
- 'Accept' => 'application/json',
- ]);
- }
- private function applyAutomaticAnticipationSettings(int $providerId, string $recipientId): void
- {
- $response = $this->pagarmeRequest($providerId, 'auto-anticipation')
- ->patch($this->pagarmeUrl("/recipients/{$recipientId}/automatic-anticipation-settings"), [
- 'enabled' => false,
- ]);
- if ($response->failed()) {
- Log::channel('pagarme')->error('Pagar.me automatic anticipation settings update failed', [
- 'status' => $response->status(),
- 'body' => $response->json() ?? $response->body(),
- 'recipient_id' => $recipientId,
- ]);
- throw new \RuntimeException('Erro ao atualizar antecipação automática do recebedor no Pagar.me.');
- }
- }
- }
|