AsaasCustomerService.php 3.1 KB

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