PagarmeRecipientRequestData.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(self::phoneFromPayload($data['phone'] ?? null)),
  34. address: new PagarmeRecipientAddressData(
  35. street: $data['address'],
  36. complementary: $addressParts['complementary'],
  37. streetNumber: $addressParts['street_number'],
  38. neighborhood: $addressParts['neighborhood'],
  39. city: $data['city'] ?? null,
  40. state: $data['state'] ?? null,
  41. zipCode: self::digits($data['zip_code'] ?? null),
  42. referencePoint: $addressParts['reference_point'],
  43. ),
  44. ),
  45. defaultBankAccount: PagarmeRecipientBankAccountData::fromArray($data['recipient_default_bank_account']),
  46. transferSettings: new PagarmeRecipientTransferSettingsData(
  47. transferEnabled: false,
  48. transferInterval: 'Daily',
  49. transferDay: 0,
  50. ),
  51. automaticAnticipationSettings: new PagarmeRecipientAutomaticAnticipationSettingsData(
  52. enabled: false,
  53. ),
  54. );
  55. }
  56. public function toArray(): array
  57. {
  58. return $this->filterFilledRecursive([
  59. 'code' => $this->code,
  60. 'register_information' => $this->registerInformation,
  61. 'default_bank_account' => $this->defaultBankAccount,
  62. 'transfer_settings' => $this->transferSettings,
  63. 'automatic_anticipation_settings' => $this->automaticAnticipationSettings,
  64. ]);
  65. }
  66. //
  67. private static function extractAddressParts(array $data): array
  68. {
  69. $addressLine = trim((string) ($data['address'] ?? ''));
  70. $segments = array_map('trim', explode(',', $addressLine));
  71. $streetSegment = $segments[0] ?? '';
  72. if (($data['number'] ?? null) === null) {
  73. preg_match('/^(\d+)/', $streetSegment, $matches);
  74. }
  75. return [
  76. 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
  77. 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
  78. 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
  79. 'complementary' => (string) ($data['complement'] ?? 'N/A'),
  80. ];
  81. }
  82. private static function formatBirthdate(mixed $birthdate): ?string
  83. {
  84. if ($birthdate === null || $birthdate === '') {
  85. return null;
  86. }
  87. if ($birthdate instanceof \DateTimeInterface) {
  88. return Carbon::instance($birthdate)->format('d/m/Y');
  89. }
  90. $birthdate = trim((string) $birthdate);
  91. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  92. return $birthdate;
  93. }
  94. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  95. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  96. }
  97. return Carbon::parse($birthdate)->format('d/m/Y');
  98. }
  99. private static function phoneFromPayload(?string $phone): PagarmeRecipientPhoneData
  100. {
  101. $digits = self::digits($phone);
  102. if (strlen($digits) < 10) {
  103. return new PagarmeRecipientPhoneData(
  104. ddd: '11',
  105. number: '999999999',
  106. type: 'mobile',
  107. );
  108. }
  109. if (str_starts_with($digits, '55')) {
  110. $digits = substr($digits, 2);
  111. }
  112. return new PagarmeRecipientPhoneData(
  113. ddd: substr($digits, 0, 2),
  114. number: substr($digits, 2),
  115. type: 'mobile',
  116. );
  117. }
  118. }