AsaasCustomerService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Services\Integrations\Asaas;
  3. use App\Models\Unit;
  4. use Exception;
  5. class AsaasCustomerService
  6. {
  7. protected AsaasClient $client;
  8. public function __construct(AsaasClient $client)
  9. {
  10. $this->client = $client;
  11. }
  12. /**
  13. * Garante que a Franquia exista como Customer (Cliente) na conta-mãe.
  14. * Retorna o ID do cliente no Asaas (cus_xxxxxx).
  15. */
  16. public function ensureFranchiseeCustomer(Unit $unit): string
  17. {
  18. $cpfCnpj = preg_replace('/[^0-9]/', '', $unit->cnpj);
  19. if (empty($cpfCnpj)) {
  20. throw new Exception("Unidade {$unit->fantasy_name} não possui CNPJ para ser cobrada.");
  21. }
  22. // Tenta buscar no Asaas se já existe o customer com esse CNPJ
  23. $existing = $this->client->get('/customers', ['cpfCnpj' => $cpfCnpj]);
  24. if (isset($existing['data']) && count($existing['data']) > 0) {
  25. return $existing['data'][0]['id'];
  26. }
  27. // Se não existe, cria um novo
  28. $payload = [
  29. 'name' => $unit->social_reason ?? $unit->fantasy_name ?? 'Franquia',
  30. 'cpfCnpj' => $cpfCnpj,
  31. 'email' => $unit->email,
  32. 'postalCode' => preg_replace('/[^0-9]/', '', $unit->postal_code),
  33. 'address' => $unit->street,
  34. 'addressNumber' => $unit->address_number ?? 'S/N',
  35. 'province' => $unit->neighborhood,
  36. ];
  37. $mobilePhone = preg_replace('/[^0-9]/', '', $unit->cell_number ?? $unit->phone_number ?? '');
  38. if (strlen($mobilePhone) >= 10 && strlen($mobilePhone) <= 11) {
  39. $payload['mobilePhone'] = $mobilePhone;
  40. }
  41. $response = $this->client->post('/customers', $payload);
  42. return $response['id'];
  43. }
  44. /**
  45. * Garante que o Aluno exista como Customer na Subconta da Franquia.
  46. * Retorna o ID do cliente no Asaas (cus_xxxxxx).
  47. */
  48. public function ensureStudentCustomer(\App\Models\Student $student, string $apiKey): string
  49. {
  50. $client = new AsaasClient($apiKey);
  51. $cpf = preg_replace('/[^0-9]/', '', $student->document_number);
  52. if (empty($cpf)) {
  53. throw new Exception("Aluno {$student->name} não possui CPF cadastrado para cobrança.");
  54. }
  55. // Tenta buscar no Asaas se já existe
  56. $existing = $client->get('/customers', ['cpfCnpj' => $cpf]);
  57. if (isset($existing['data']) && count($existing['data']) > 0) {
  58. return $existing['data'][0]['id'];
  59. }
  60. // Se não existe, cria um novo
  61. $payload = [
  62. 'name' => $student->name,
  63. 'cpfCnpj' => $cpf,
  64. 'email' => $student->email,
  65. 'postalCode' => preg_replace('/[^0-9]/', '', $student->postal_code ?? ''),
  66. 'address' => $student->street,
  67. 'addressNumber' => $student->address_number ?? 'S/N',
  68. 'province' => $student->neighborhood,
  69. ];
  70. $mobilePhone = preg_replace('/[^0-9]/', '', $student->phone ?? '');
  71. if (strlen($mobilePhone) >= 10 && strlen($mobilePhone) <= 11) {
  72. $payload['mobilePhone'] = $mobilePhone;
  73. }
  74. $response = $client->post('/customers', $payload);
  75. return $response['id'];
  76. }
  77. }