PagarmeRecipientRequestData.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. private static function phoneFromPayload(?string $phone): PagarmeRecipientPhoneData
  67. {
  68. $digits = self::digits($phone);
  69. if (strlen($digits) < 10) {
  70. return new PagarmeRecipientPhoneData(
  71. ddd: '11',
  72. number: '999999999',
  73. type: 'mobile',
  74. );
  75. }
  76. if (str_starts_with($digits, '55')) {
  77. $digits = substr($digits, 2);
  78. }
  79. return new PagarmeRecipientPhoneData(
  80. ddd: substr($digits, 0, 2),
  81. number: substr($digits, 2),
  82. type: 'mobile',
  83. );
  84. }
  85. private static function extractAddressParts(array $data): array
  86. {
  87. $addressLine = trim((string) ($data['address'] ?? ''));
  88. $segments = array_map('trim', explode(',', $addressLine));
  89. $streetSegment = $segments[0] ?? '';
  90. if (($data['number'] ?? null) === null) {
  91. preg_match('/^(\d+)/', $streetSegment, $matches);
  92. }
  93. return [
  94. 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
  95. 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
  96. 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
  97. 'complementary' => (string) ($data['complement'] ?? 'N/A'),
  98. ];
  99. }
  100. private static function formatBirthdate(mixed $birthdate): ?string
  101. {
  102. if ($birthdate === null || $birthdate === '') {
  103. return null;
  104. }
  105. if ($birthdate instanceof \DateTimeInterface) {
  106. return Carbon::instance($birthdate)->format('d/m/Y');
  107. }
  108. $birthdate = trim((string) $birthdate);
  109. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  110. return $birthdate;
  111. }
  112. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  113. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  114. }
  115. return Carbon::parse($birthdate)->format('d/m/Y');
  116. }
  117. }