|
|
@@ -13,12 +13,12 @@ use App\Data\Pagarme\Request\RecipientRequestData\RecipientRequestData;
|
|
|
use App\Data\Pagarme\Request\RecipientRequestData\RecipientTransferSettingsData;
|
|
|
use App\Data\Pagarme\Response\RecipientResponseData\RecipientResponseData;
|
|
|
use App\Models\Provider;
|
|
|
+use App\Services\Pagarme\Concerns\FormatsPagarmeData;
|
|
|
use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
|
|
|
-use Carbon\Carbon;
|
|
|
-use Illuminate\Support\Str;
|
|
|
|
|
|
class PagarmeRecipientService
|
|
|
{
|
|
|
+ use FormatsPagarmeData;
|
|
|
use SendsPagarmeRequests;
|
|
|
|
|
|
public function createRecipientForProvider(Provider $provider, array $data): string
|
|
|
@@ -29,14 +29,14 @@ class PagarmeRecipientService
|
|
|
|
|
|
$metadata = $data['recipient_metadata'] ?? [];
|
|
|
$paymentMode = $data['recipient_payment_mode'];
|
|
|
- $recipientCode = $this->ensureRecipientCode($provider);
|
|
|
+ $recipientCode = $provider->ensureGatewayCode();
|
|
|
|
|
|
$addressParts = $this->extractAddressParts($data);
|
|
|
|
|
|
$registerInformation = new RecipientRegisterInformationData(
|
|
|
name: $data['recipient_name'],
|
|
|
email: $data['recipient_email'],
|
|
|
- document: $this->onlyDigits($data['recipient_document'] ?? null),
|
|
|
+ document: $this->digits($data['recipient_document'] ?? null),
|
|
|
type: $data['recipient_type'] ?? 'individual',
|
|
|
birthdate: $this->formatBirthdate($data['birth_date'] ?? null),
|
|
|
monthlyIncome: isset($data['monthly_income']) ? (int) $data['monthly_income'] : 1000,
|
|
|
@@ -53,7 +53,7 @@ class PagarmeRecipientService
|
|
|
neighborhood: $addressParts['neighborhood'],
|
|
|
city: $data['city'] ?? null,
|
|
|
state: $data['state'] ?? null,
|
|
|
- zipCode: $this->onlyDigits($data['zip_code'] ?? null),
|
|
|
+ zipCode: $this->digits($data['zip_code'] ?? null),
|
|
|
referencePoint: $addressParts['reference_point'],
|
|
|
),
|
|
|
);
|
|
|
@@ -125,7 +125,7 @@ class PagarmeRecipientService
|
|
|
$payload = new BankAccountUpdateRequestData(
|
|
|
holderName: $this->normalizeHolderName($bankAccountData['holder_name']),
|
|
|
holderType: $bankAccountData['holder_type'],
|
|
|
- holderDocument: $this->onlyDigits($bankAccountData['holder_document']),
|
|
|
+ holderDocument: $this->digits($bankAccountData['holder_document']),
|
|
|
bank: $bankAccountData['bank'],
|
|
|
branchNumber: $bankAccountData['branch_number'],
|
|
|
branchCheckDigit: $bankAccountData['branch_check_digit'] ?? null,
|
|
|
@@ -158,7 +158,7 @@ class PagarmeRecipientService
|
|
|
return new RecipientBankAccountData(
|
|
|
holderName: $this->normalizeHolderName($data['holder_name']),
|
|
|
holderType: $data['holder_type'],
|
|
|
- holderDocument: $this->onlyDigits($data['holder_document']),
|
|
|
+ holderDocument: $this->digits($data['holder_document']),
|
|
|
bank: $data['bank'],
|
|
|
branchNumber: $data['branch_number'],
|
|
|
branchCheckDigit: $data['branch_check_digit'] ?? null,
|
|
|
@@ -170,7 +170,7 @@ class PagarmeRecipientService
|
|
|
|
|
|
private function buildRecipientPhone(?string $phone): RecipientPhoneData
|
|
|
{
|
|
|
- $digits = $this->onlyDigits($phone);
|
|
|
+ $digits = $this->digits($phone);
|
|
|
|
|
|
if (strlen($digits) < 10) {
|
|
|
return new RecipientPhoneData(
|
|
|
@@ -191,104 +191,10 @@ class PagarmeRecipientService
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- private function extractAddressParts(array $data): array
|
|
|
- {
|
|
|
- $addressLine = trim((string) ($data['address'] ?? ''));
|
|
|
- $segments = array_map('trim', explode(',', $addressLine));
|
|
|
- $streetSegment = $segments[0] ?? '';
|
|
|
-
|
|
|
- if (($data['number'] ?? null) === null) {
|
|
|
- preg_match('/^(\d+)/', $streetSegment, $matches);
|
|
|
- }
|
|
|
-
|
|
|
- return [
|
|
|
- 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
|
|
|
- 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
|
|
|
- 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
|
|
|
- 'complementary' => (string) ($data['complement'] ?? 'N/A'),
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- private function formatBirthdate(mixed $birthdate): ?string
|
|
|
- {
|
|
|
- if ($birthdate === null || $birthdate === '') {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- if ($birthdate instanceof \DateTimeInterface) {
|
|
|
- return Carbon::instance($birthdate)->format('d/m/Y');
|
|
|
- }
|
|
|
-
|
|
|
- $birthdate = trim((string) $birthdate);
|
|
|
-
|
|
|
- if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
|
|
|
- return $birthdate;
|
|
|
- }
|
|
|
-
|
|
|
- if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
|
|
|
- return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
|
|
|
- }
|
|
|
-
|
|
|
- return Carbon::parse($birthdate)->format('d/m/Y');
|
|
|
- }
|
|
|
-
|
|
|
- private function normalizeHolderName(string $holderName): string
|
|
|
- {
|
|
|
- $holderName = trim(preg_replace('/\s+/', ' ', $holderName) ?? '');
|
|
|
-
|
|
|
- if (Str::length($holderName) < 30) {
|
|
|
- return $holderName;
|
|
|
- }
|
|
|
-
|
|
|
- $parts = explode(' ', $holderName);
|
|
|
-
|
|
|
- if (count($parts) >= 3) {
|
|
|
- $firstName = array_shift($parts);
|
|
|
- $lastName = array_pop($parts);
|
|
|
-
|
|
|
- $initials = array_map(
|
|
|
- static fn (string $part): string => Str::upper(Str::substr($part, 0, 1)),
|
|
|
- $parts
|
|
|
- );
|
|
|
-
|
|
|
- $abbreviated = trim($firstName.' '.implode(' ', $initials).' '.$lastName);
|
|
|
-
|
|
|
- if (Str::length($abbreviated) < 30) {
|
|
|
- return $abbreviated;
|
|
|
- }
|
|
|
-
|
|
|
- $firstAndLast = trim($firstName.' '.$lastName);
|
|
|
-
|
|
|
- if (Str::length($firstAndLast) < 30) {
|
|
|
- return $firstAndLast;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return Str::limit($holderName, 29, '');
|
|
|
- }
|
|
|
-
|
|
|
- private function onlyDigits(?string $value): string
|
|
|
- {
|
|
|
- return preg_replace('/\D+/', '', (string) $value) ?? '';
|
|
|
- }
|
|
|
-
|
|
|
// evita criacao duplica de recipient
|
|
|
|
|
|
private function idempotencyKey(int $providerId, string $suffix = 'recipient'): string
|
|
|
{
|
|
|
return "provider-{$providerId}-{$suffix}";
|
|
|
}
|
|
|
-
|
|
|
- private function ensureRecipientCode(Provider $provider): string
|
|
|
- {
|
|
|
- if (! empty($provider->recipient_code)) {
|
|
|
- return $provider->recipient_code;
|
|
|
- }
|
|
|
-
|
|
|
- $recipientCode = 'provider-'.(string) Str::uuid();
|
|
|
-
|
|
|
- $provider->forceFill(['recipient_code' => $recipientCode])->save();
|
|
|
-
|
|
|
- return $recipientCode;
|
|
|
- }
|
|
|
}
|