|
|
@@ -3,8 +3,10 @@
|
|
|
namespace App\Services\Pagarme;
|
|
|
|
|
|
use App\Models\Provider;
|
|
|
+use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class PagarmeRecipientService
|
|
|
{
|
|
|
@@ -14,10 +16,11 @@ class PagarmeRecipientService
|
|
|
return $provider->recipient_id;
|
|
|
}
|
|
|
|
|
|
- $bankAccountData = $data['recipient_default_bank_account'];
|
|
|
- $metadata = $data['recipient_metadata'] ?? [];
|
|
|
- $paymentMode = $data['recipient_payment_mode'];
|
|
|
- $recipientType = $data['recipient_type'] ?? 'individual';
|
|
|
+ $bankAccountData = $data['recipient_default_bank_account'];
|
|
|
+ $bankAccountData['holder_name'] = $this->normalizeBankAccountHolderName($bankAccountData['holder_name']);
|
|
|
+ $metadata = $data['recipient_metadata'] ?? [];
|
|
|
+ $paymentMode = $data['recipient_payment_mode'];
|
|
|
+ $recipientType = $data['recipient_type'] ?? 'individual';
|
|
|
|
|
|
$addressParts = $this->extractAddressParts($data);
|
|
|
$monthlyIncome = isset($data['monthly_income']) ? (int) $data['monthly_income'] : 1000;
|
|
|
@@ -31,7 +34,7 @@ class PagarmeRecipientService
|
|
|
'email' => $data['recipient_email'],
|
|
|
'document' => preg_replace('/\D+/', '', $data['recipient_document']),
|
|
|
'type' => $recipientType,
|
|
|
- 'birthdate' => $data['birth_date'] ?? null,
|
|
|
+ 'birthdate' => $this->formatBirthdate($data['birth_date'] ?? null),
|
|
|
'monthly_income' => $monthlyIncome,
|
|
|
'professional_occupation' => $occupation,
|
|
|
'phone_numbers' => $this->buildPhoneNumbers($data['phone'] ?? null),
|
|
|
@@ -120,8 +123,6 @@ class PagarmeRecipientService
|
|
|
'recipient_metadata' => $metadata,
|
|
|
])->save();
|
|
|
|
|
|
- $this->applyAutomaticAnticipationSettings($provider->id, $recipientId);
|
|
|
-
|
|
|
return $recipientId;
|
|
|
}
|
|
|
|
|
|
@@ -139,42 +140,31 @@ class PagarmeRecipientService
|
|
|
|
|
|
//
|
|
|
|
|
|
- private function pagarmeRequest(int $providerId, string $suffix = 'recipient')
|
|
|
+ private function buildPhoneNumbers(?string $phone): array
|
|
|
{
|
|
|
- $secretKey = config('services.pagarme.secret_key');
|
|
|
-
|
|
|
- if (empty($secretKey)) {
|
|
|
- Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
|
|
|
+ $digits = preg_replace('/\D+/', '', (string) $phone) ?? '';
|
|
|
|
|
|
- throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
|
|
|
+ if (strlen($digits) < 10) {
|
|
|
+ return [[
|
|
|
+ 'ddd' => '11',
|
|
|
+ 'number' => '999999999',
|
|
|
+ 'type' => 'mobile',
|
|
|
+ ]];
|
|
|
}
|
|
|
|
|
|
- return Http::withBasicAuth($secretKey, '')
|
|
|
- ->withHeaders([
|
|
|
- 'Idempotency-Key' => $this->idempotencyKey($providerId, $suffix),
|
|
|
- 'Content-Type' => 'application/json',
|
|
|
- 'Accept' => 'application/json',
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
- private function applyAutomaticAnticipationSettings(int $providerId, string $recipientId): void
|
|
|
- {
|
|
|
- $payload = [
|
|
|
- 'enabled' => false,
|
|
|
- ];
|
|
|
+ if (str_starts_with($digits, '55')) {
|
|
|
+ $digits = substr($digits, 2);
|
|
|
+ }
|
|
|
|
|
|
- $response = $this->pagarmeRequest($providerId, 'auto-anticipation')
|
|
|
- ->patch($this->pagarmeUrl("/recipients/{$recipientId}/automatic-anticipation-settings"), $payload);
|
|
|
+ $areaCode = substr($digits, 0, 2);
|
|
|
|
|
|
- if ($response->failed()) {
|
|
|
- Log::channel('pagarme')->error('Pagar.me automatic anticipation settings update failed', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->json() ?? $response->body(),
|
|
|
- 'payload' => $payload,
|
|
|
- ]);
|
|
|
+ $number = substr($digits, 2);
|
|
|
|
|
|
- throw new \RuntimeException('Erro ao atualizar antecipação automática do recebedor no Pagar.me.');
|
|
|
- }
|
|
|
+ return [[
|
|
|
+ 'ddd' => $areaCode,
|
|
|
+ 'number' => $number,
|
|
|
+ 'type' => 'mobile',
|
|
|
+ ]];
|
|
|
}
|
|
|
|
|
|
private function extractAddressParts(array $data): array
|
|
|
@@ -215,51 +205,100 @@ class PagarmeRecipientService
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- private function buildPhoneNumbers(?string $phone): array
|
|
|
+ //
|
|
|
+
|
|
|
+ private function filterFilledRecursive(array $data): array
|
|
|
{
|
|
|
- $digits = preg_replace('/\D+/', '', (string) $phone) ?? '';
|
|
|
+ $filtered = [];
|
|
|
|
|
|
- if (strlen($digits) < 10) {
|
|
|
- return [[
|
|
|
- 'ddd' => '11',
|
|
|
- 'number' => '999999999',
|
|
|
- 'type' => 'mobile',
|
|
|
- ]];
|
|
|
+ foreach ($data as $key => $value) {
|
|
|
+ if (is_array($value)) {
|
|
|
+ $value = $this->filterFilledRecursive($value);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($value !== null && $value !== '' && $value !== []) {
|
|
|
+ $filtered[$key] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (str_starts_with($digits, '55')) {
|
|
|
- $digits = substr($digits, 2);
|
|
|
+ return $filtered;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function formatBirthdate(mixed $birthdate): ?string
|
|
|
+ {
|
|
|
+ if ($birthdate === null || $birthdate === '') {
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- $areaCode = substr($digits, 0, 2);
|
|
|
+ if ($birthdate instanceof \DateTimeInterface) {
|
|
|
+ return Carbon::instance($birthdate)->format('d/m/Y');
|
|
|
+ }
|
|
|
|
|
|
- $number = substr($digits, 2);
|
|
|
+ $birthdate = trim((string) $birthdate);
|
|
|
|
|
|
- return [[
|
|
|
- 'ddd' => $areaCode,
|
|
|
- 'number' => $number,
|
|
|
- 'type' => 'mobile',
|
|
|
- ]];
|
|
|
+ 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');
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param array<string, mixed> $data
|
|
|
- * @return array<string, mixed>
|
|
|
- */
|
|
|
- private function filterFilledRecursive(array $data): array
|
|
|
+ private function normalizeBankAccountHolderName(string $holderName): string
|
|
|
{
|
|
|
- $filtered = [];
|
|
|
+ $holderName = trim(preg_replace('/\s+/', ' ', $holderName) ?? '');
|
|
|
|
|
|
- foreach ($data as $key => $value) {
|
|
|
- if (is_array($value)) {
|
|
|
- $value = $this->filterFilledRecursive($value);
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
- if ($value !== null && $value !== '' && $value !== []) {
|
|
|
- $filtered[$key] = $value;
|
|
|
+ $firstAndLast = trim($firstName.' '.$lastName);
|
|
|
+
|
|
|
+ if (Str::length($firstAndLast) < 30) {
|
|
|
+ return $firstAndLast;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return $filtered;
|
|
|
+ return Str::limit($holderName, 29, '');
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ private function pagarmeRequest(int $providerId, string $suffix = 'recipient')
|
|
|
+ {
|
|
|
+ $secretKey = config('services.pagarme.secret_key');
|
|
|
+
|
|
|
+ if (empty($secretKey)) {
|
|
|
+ Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.');
|
|
|
+
|
|
|
+ throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.');
|
|
|
+ }
|
|
|
+
|
|
|
+ return Http::withBasicAuth($secretKey, '')
|
|
|
+ ->withHeaders([
|
|
|
+ 'Idempotency-Key' => $this->idempotencyKey($providerId, $suffix),
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ 'Accept' => 'application/json',
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|