PagarmeRecipientRequestData.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Data\Pagarme\Request\PagarmeRecipientRequestData;
  3. use App\Data\Pagarme\PagarmeData;
  4. use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientAddressData;
  5. use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientPhoneNumbersData\PagarmeRecipientPhoneData;
  6. use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientPhoneNumbersData\PagarmeRecipientPhoneNumbersData;
  7. use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientRegisterInformationData;
  8. use Carbon\Carbon;
  9. final readonly class PagarmeRecipientRequestData extends PagarmeData
  10. {
  11. public function __construct(
  12. public string $code,
  13. public PagarmeRecipientRegisterInformationData $registerInformation,
  14. public PagarmeRecipientBankAccountData $defaultBankAccount,
  15. public PagarmeRecipientTransferSettingsData $transferSettings,
  16. public PagarmeRecipientAutomaticAnticipationSettingsData $automaticAnticipationSettings,
  17. ) {
  18. self::requireFilled($this->code, 'code');
  19. }
  20. public static function fromPayload(string $code, array $data): self
  21. {
  22. $addressParts = self::extractAddressParts($data);
  23. return new self(
  24. code: $code,
  25. registerInformation: new PagarmeRecipientRegisterInformationData(
  26. name: $data['recipient_name'],
  27. email: $data['recipient_email'],
  28. document: self::digits($data['recipient_document'] ?? null),
  29. type: $data['recipient_type'] ?? 'individual',
  30. birthdate: self::formatBirthdate($data['birth_date'] ?? null),
  31. monthlyIncome: isset($data['monthly_income']) ? (int) $data['monthly_income'] : 1000,
  32. professionalOccupation: $data['professional_occupation'] ?? 'autonomo',
  33. phoneNumbers: new PagarmeRecipientPhoneNumbersData(
  34. self::phoneFromPayload($data['phone'] ?? null),
  35. ),
  36. address: new PagarmeRecipientAddressData(
  37. street: $data['address'],
  38. complementary: $addressParts['complementary'],
  39. streetNumber: $addressParts['street_number'],
  40. neighborhood: $addressParts['neighborhood'],
  41. city: $data['city'] ?? null,
  42. state: $data['state'] ?? null,
  43. zipCode: self::digits($data['zip_code'] ?? null),
  44. referencePoint: $addressParts['reference_point'],
  45. ),
  46. ),
  47. defaultBankAccount: PagarmeRecipientBankAccountData::fromArray(
  48. $data['recipient_default_bank_account'],
  49. ),
  50. transferSettings: new PagarmeRecipientTransferSettingsData(
  51. transferEnabled: false,
  52. transferInterval: 'Daily',
  53. transferDay: 0,
  54. ),
  55. automaticAnticipationSettings: new PagarmeRecipientAutomaticAnticipationSettingsData(
  56. enabled: false,
  57. ),
  58. );
  59. }
  60. public function toArray(): array
  61. {
  62. return $this->filterFilledRecursive([
  63. 'code' => $this->code,
  64. 'register_information' => $this->registerInformation,
  65. 'default_bank_account' => $this->defaultBankAccount,
  66. 'transfer_settings' => $this->transferSettings,
  67. 'automatic_anticipation_settings' => $this->automaticAnticipationSettings,
  68. ]);
  69. }
  70. //
  71. private static function extractAddressParts(array $data): array
  72. {
  73. $addressLine = trim((string) ($data['address'] ?? ''));
  74. $segments = array_map('trim', explode(',', $addressLine));
  75. $streetSegment = $segments[0] ?? '';
  76. if (($data['number'] ?? null) === null) {
  77. preg_match('/^(\d+)/', $streetSegment, $matches);
  78. }
  79. return [
  80. 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
  81. 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
  82. 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
  83. 'complementary' => (string) ($data['complement'] ?? 'N/A'),
  84. ];
  85. }
  86. private static function formatBirthdate(mixed $birthdate): ?string
  87. {
  88. if ($birthdate === null || $birthdate === '') {
  89. return null;
  90. }
  91. if ($birthdate instanceof \DateTimeInterface) {
  92. return Carbon::instance($birthdate)->format('d/m/Y');
  93. }
  94. $birthdate = trim((string) $birthdate);
  95. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  96. return $birthdate;
  97. }
  98. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  99. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  100. }
  101. return Carbon::parse($birthdate)->format('d/m/Y');
  102. }
  103. private static function phoneFromPayload(?string $phone): PagarmeRecipientPhoneData
  104. {
  105. $digits = self::digits($phone);
  106. if (strlen($digits) < 10) {
  107. return new PagarmeRecipientPhoneData(
  108. ddd: '11',
  109. number: '999999999',
  110. type: 'mobile',
  111. );
  112. }
  113. if (str_starts_with($digits, '55')) {
  114. $digits = substr($digits, 2);
  115. }
  116. return new PagarmeRecipientPhoneData(
  117. ddd: substr($digits, 0, 2),
  118. number: substr($digits, 2),
  119. type: 'mobile',
  120. );
  121. }
  122. }