PagarmeRecipientService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Services\Pagarme;
  3. use App\Data\Pagarme\Recipient\BankAccountUpdateRequestData;
  4. use App\Data\Pagarme\Recipient\Parts\Request\AddressData;
  5. use App\Data\Pagarme\Recipient\Parts\Request\AutomaticAnticipationSettingsData;
  6. use App\Data\Pagarme\Recipient\Parts\Request\BankAccountData;
  7. use App\Data\Pagarme\Recipient\Parts\Request\PhoneData;
  8. use App\Data\Pagarme\Recipient\Parts\Request\PhoneNumbersData;
  9. use App\Data\Pagarme\Recipient\Parts\Request\RegisterInformationData;
  10. use App\Data\Pagarme\Recipient\Parts\Request\TransferSettingsData;
  11. use App\Data\Pagarme\Recipient\RecipientRequestData;
  12. use App\Data\Pagarme\Recipient\RecipientResponseData;
  13. use App\Models\Provider;
  14. use App\Services\Pagarme\Concerns\FormatsPagarmeData;
  15. use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
  16. class PagarmeRecipientService
  17. {
  18. use FormatsPagarmeData;
  19. use SendsPagarmeRequests;
  20. public function createRecipientForProvider(Provider $provider, array $data): string
  21. {
  22. if (! empty($provider->recipient_id)) {
  23. return $provider->recipient_id;
  24. }
  25. $metadata = $data['recipient_metadata'] ?? [];
  26. $paymentMode = $data['recipient_payment_mode'];
  27. $recipientCode = $provider->ensureGatewayCode();
  28. $addressParts = $this->extractAddressParts($data);
  29. $registerInformation = new RegisterInformationData(
  30. name: $data['recipient_name'],
  31. email: $data['recipient_email'],
  32. document: $this->digits($data['recipient_document'] ?? null),
  33. type: $data['recipient_type'] ?? 'individual',
  34. birthdate: $this->formatBirthdate($data['birth_date'] ?? null),
  35. monthlyIncome: isset($data['monthly_income']) ? (int) $data['monthly_income'] : 1000,
  36. professionalOccupation: $data['professional_occupation'] ?? 'autonomo',
  37. phoneNumbers: new PhoneNumbersData(
  38. $this->buildRecipientPhone($data['phone'] ?? null),
  39. ),
  40. address: new AddressData(
  41. street: $addressParts['street'],
  42. complementary: $addressParts['complementary'],
  43. streetNumber: $addressParts['street_number'],
  44. neighborhood: $addressParts['neighborhood'],
  45. city: $data['city'] ?? null,
  46. state: $data['state'] ?? null,
  47. zipCode: $this->digits($data['zip_code'] ?? null),
  48. referencePoint: $addressParts['reference_point'],
  49. ),
  50. );
  51. $defaultBankAccount = $this->buildRecipientBankAccount(
  52. $data['recipient_default_bank_account'],
  53. );
  54. $payload = new RecipientRequestData(
  55. code: $recipientCode,
  56. registerInformation: $registerInformation,
  57. defaultBankAccount: $defaultBankAccount,
  58. transferSettings: new TransferSettingsData(
  59. transferEnabled: false,
  60. transferInterval: 'Daily',
  61. transferDay: 0,
  62. ),
  63. automaticAnticipationSettings: new AutomaticAnticipationSettingsData(
  64. enabled: true,
  65. type: '1025', // isso significa que vai ser feita de acordo com o delay (dias)
  66. delay: 1,
  67. ),
  68. );
  69. $bankAccountData = $payload->defaultBankAccount->toArray();
  70. $raw = $this->pagarmeRequest(
  71. method: 'POST',
  72. path: '/recipients',
  73. payload: $payload,
  74. idempotencyKey: $this->idempotencyKey($provider),
  75. errorMessage: 'Erro ao criar recebedor no Pagar.me.',
  76. );
  77. $recipientData = RecipientResponseData::fromArray($raw);
  78. $recipientId = $recipientData->requireId();
  79. $provider->forceFill([
  80. 'recipient_id' => $recipientId,
  81. 'recipient_name' => $data['recipient_name'],
  82. 'recipient_email' => $data['recipient_email'],
  83. 'recipient_description' => $data['recipient_description'] ?? '',
  84. 'recipient_document' => $data['recipient_document'],
  85. 'recipient_type' => $payload->registerInformation->type,
  86. 'recipient_code' => $recipientCode,
  87. 'recipient_payment_mode' => $paymentMode,
  88. 'recipient_default_bank_account' => $bankAccountData,
  89. 'recipient_transfer_settings' => [
  90. 'transfer_enabled' => false,
  91. 'transfer_interval' => 'daily',
  92. 'transfer_day' => 0,
  93. ],
  94. 'recipient_automatic_anticipation_settings' => [
  95. 'enabled' => true,
  96. 'type' => 'full',
  97. 'delay' => 1,
  98. ],
  99. 'recipient_metadata' => $metadata,
  100. ])->save();
  101. return $recipientId;
  102. }
  103. public function updateDefaultBankAccount(Provider $provider, array $bankAccountData): Provider
  104. {
  105. $payload = new BankAccountUpdateRequestData(
  106. holderName: $this->normalizeHolderName($bankAccountData['holder_name']),
  107. holderType: $bankAccountData['holder_type'],
  108. holderDocument: $this->digits($bankAccountData['holder_document']),
  109. bank: $bankAccountData['bank'],
  110. branchNumber: $bankAccountData['branch_number'],
  111. branchCheckDigit: $bankAccountData['branch_check_digit'] ?? null,
  112. accountNumber: $bankAccountData['account_number'],
  113. accountCheckDigit: $bankAccountData['account_check_digit'],
  114. type: $bankAccountData['type'],
  115. );
  116. $raw = $this->pagarmeRequest(
  117. method: 'PATCH',
  118. path: "/recipients/{$provider->recipient_id}/default-bank-account",
  119. payload: $payload,
  120. idempotencyKey: $this->idempotencyKey($provider, 'default-bank-account-'.sha1(json_encode($payload->toArray()))),
  121. errorMessage: 'Erro ao atualizar conta bancaria do recebedor no Pagar.me.',
  122. );
  123. $recipientData = RecipientResponseData::fromArray($raw);
  124. $provider->forceFill([
  125. 'recipient_default_bank_account' => $recipientData->defaultBankAccount?->toArray() ?: $payload->toArray()['bank_account'],
  126. ])->save();
  127. return $provider->fresh();
  128. }
  129. //
  130. private function buildRecipientBankAccount(array $data): BankAccountData
  131. {
  132. return new BankAccountData(
  133. holderName: $this->normalizeHolderName($data['holder_name']),
  134. holderType: $data['holder_type'],
  135. holderDocument: $this->digits($data['holder_document']),
  136. bank: $data['bank'],
  137. branchNumber: $data['branch_number'],
  138. branchCheckDigit: $data['branch_check_digit'] ?? null,
  139. accountNumber: $data['account_number'],
  140. accountCheckDigit: $data['account_check_digit'],
  141. type: $data['type'],
  142. );
  143. }
  144. private function buildRecipientPhone(?string $phone): PhoneData
  145. {
  146. $digits = $this->digits($phone);
  147. if (strlen($digits) < 10) {
  148. return new PhoneData(
  149. ddd: '11',
  150. number: '999999999',
  151. type: 'mobile',
  152. );
  153. }
  154. if (str_starts_with($digits, '55')) {
  155. $digits = substr($digits, 2);
  156. }
  157. return new PhoneData(
  158. ddd: substr($digits, 0, 2),
  159. number: substr($digits, 2),
  160. type: 'mobile',
  161. );
  162. }
  163. // evita criacao duplica de recipient
  164. private function idempotencyKey(Provider $provider, string $suffix = ''): string
  165. {
  166. $baseKey = $provider->idempotency_key;
  167. if (empty($baseKey)) {
  168. $baseKey = 'recipient-'.(string) \Illuminate\Support\Str::uuid();
  169. $provider->forceFill(['idempotency_key' => $baseKey])->save();
  170. }
  171. return $suffix ? "{$baseKey}-{$suffix}" : $baseKey;
  172. }
  173. }