FormatsPagarmeData.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Services\Pagarme\Concerns;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Str;
  5. trait FormatsPagarmeData
  6. {
  7. protected function digits(?string $value): string
  8. {
  9. return preg_replace('/\D+/', '', (string) $value) ?? '';
  10. }
  11. protected function pagarmeIdempotencyKey(string $prefix, array $parts): string
  12. {
  13. $segments = [Str::slug($prefix)];
  14. foreach ($parts as $part) {
  15. $slug = Str::slug(trim((string) $part));
  16. if ($slug !== '') {
  17. $segments[] = $slug;
  18. }
  19. }
  20. return implode('-', array_filter($segments));
  21. }
  22. //
  23. protected function customerDocument(?string $value): string
  24. {
  25. $document = trim((string) $value);
  26. if (preg_match('/[A-Za-z]/', $document) === 1) {
  27. return $document;
  28. }
  29. return $this->digits($document);
  30. }
  31. protected function customerDocumentType(string $document): string
  32. {
  33. if (preg_match('/[A-Za-z]/', $document) === 1) {
  34. return 'PASSPORT';
  35. }
  36. $length = strlen($this->digits($document));
  37. return match ($length) {
  38. 11 => 'CPF',
  39. 14 => 'CNPJ',
  40. default => 'PASSPORT',
  41. };
  42. }
  43. protected function extractAddressParts(array $data): array
  44. {
  45. $addressLine = trim((string) data_get($data, 'address', ''));
  46. $segments = array_map('trim', explode(',', $addressLine));
  47. $streetSegment = data_get($segments, 0, '');
  48. $streetNumber = data_get($data, 'number');
  49. if ($streetNumber === null) {
  50. $matches = [];
  51. preg_match('/^(\d+)/', $streetSegment, $matches);
  52. $streetNumber = data_get($matches, 1, 'S/N');
  53. }
  54. return [
  55. 'street' => $streetSegment,
  56. 'street_number' => (string) $streetNumber,
  57. 'neighborhood' => (string) data_get($data, 'district', data_get($segments, 1, 'N/A')),
  58. 'reference_point' => (string) data_get($data, 'reference_point', 'N/A'),
  59. 'complementary' => (string) data_get($data, 'complement', 'N/A'),
  60. ];
  61. }
  62. protected function formatBirthdate(mixed $birthdate): ?string
  63. {
  64. if ($birthdate === null || $birthdate === '') {
  65. return null;
  66. }
  67. if ($birthdate instanceof \DateTimeInterface) {
  68. return Carbon::instance($birthdate)->format('d/m/Y');
  69. }
  70. $birthdate = trim((string) $birthdate);
  71. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  72. return $birthdate;
  73. }
  74. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  75. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  76. }
  77. return Carbon::parse($birthdate)->format('d/m/Y');
  78. }
  79. protected function normalizeHolderName(string $holderName): string
  80. {
  81. $holderName = trim(preg_replace('/\s+/', ' ', $holderName) ?? '');
  82. if (Str::length($holderName) < 30) {
  83. return $holderName;
  84. }
  85. $parts = explode(' ', $holderName);
  86. if (count($parts) >= 3) {
  87. $firstName = array_shift($parts);
  88. $lastName = array_pop($parts);
  89. $initials = array_map(
  90. static fn (string $part): string => Str::upper(Str::substr($part, 0, 1)),
  91. $parts
  92. );
  93. $abbreviated = trim($firstName.' '.implode(' ', $initials).' '.$lastName);
  94. if (Str::length($abbreviated) < 30) {
  95. return $abbreviated;
  96. }
  97. $firstAndLast = trim($firstName.' '.$lastName);
  98. if (Str::length($firstAndLast) < 30) {
  99. return $firstAndLast;
  100. }
  101. }
  102. return Str::limit($holderName, 29, '');
  103. }
  104. }