FormatsPagarmeData.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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['address'] ?? ''));
  46. $segments = array_map('trim', explode(',', $addressLine));
  47. $streetSegment = $segments[0] ?? '';
  48. if (($data['number'] ?? null) === null) {
  49. preg_match('/^(\d+)/', $streetSegment, $matches);
  50. }
  51. return [
  52. 'street' => $streetSegment,
  53. 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
  54. 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
  55. 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
  56. 'complementary' => (string) ($data['complement'] ?? 'N/A'),
  57. ];
  58. }
  59. protected function formatBirthdate(mixed $birthdate): ?string
  60. {
  61. if ($birthdate === null || $birthdate === '') {
  62. return null;
  63. }
  64. if ($birthdate instanceof \DateTimeInterface) {
  65. return Carbon::instance($birthdate)->format('d/m/Y');
  66. }
  67. $birthdate = trim((string) $birthdate);
  68. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  69. return $birthdate;
  70. }
  71. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  72. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  73. }
  74. return Carbon::parse($birthdate)->format('d/m/Y');
  75. }
  76. protected function normalizeHolderName(string $holderName): string
  77. {
  78. $holderName = trim(preg_replace('/\s+/', ' ', $holderName) ?? '');
  79. if (Str::length($holderName) < 30) {
  80. return $holderName;
  81. }
  82. $parts = explode(' ', $holderName);
  83. if (count($parts) >= 3) {
  84. $firstName = array_shift($parts);
  85. $lastName = array_pop($parts);
  86. $initials = array_map(
  87. static fn (string $part): string => Str::upper(Str::substr($part, 0, 1)),
  88. $parts
  89. );
  90. $abbreviated = trim($firstName.' '.implode(' ', $initials).' '.$lastName);
  91. if (Str::length($abbreviated) < 30) {
  92. return $abbreviated;
  93. }
  94. $firstAndLast = trim($firstName.' '.$lastName);
  95. if (Str::length($firstAndLast) < 30) {
  96. return $firstAndLast;
  97. }
  98. }
  99. return Str::limit($holderName, 29, '');
  100. }
  101. }