Bladeren bron

refactor: ajustando cadastro do recipient para garantir limites de antecipacao 100%

Gustavo Mantovani 5 uur geleden
bovenliggende
commit
aa348fe3c4

+ 23 - 3
app/Data/Pagarme/Recipient/Parts/Request/AutomaticAnticipationSettingsData.php

@@ -7,13 +7,33 @@ use App\Data\Pagarme\PagarmeData;
 final readonly class AutomaticAnticipationSettingsData extends PagarmeData
 {
     public function __construct(
-        public bool $enabled,
-    ) {}
+        public bool    $enabled,
+        public ?string $type             = null,
+        public ?int    $volumePercentage = null,
+        public ?int    $delay            = null,
+        public ?array  $days             = null,
+    ) {
+        if ($this->type !== null) {
+            self::requireIn($this->type, ['full', '1025'], 'automatic_anticipation_settings.type');
+        }
+
+        if ($this->volumePercentage !== null && ($this->volumePercentage < 0 || $this->volumePercentage > 100)) {
+            throw new \InvalidArgumentException('automatic_anticipation_settings.volume_percentage deve estar entre 0 e 100.');
+        }
+
+        if ($this->delay !== null && $this->delay < 0) {
+            throw new \InvalidArgumentException('automatic_anticipation_settings.delay deve ser maior ou igual a zero.');
+        }
+    }
 
     public function toArray(): array
     {
         return $this->filterFilledRecursive([
-            'enabled' => $this->enabled,
+            'enabled'           => $this->enabled,
+            'type'              => $this->type,
+            'volume_percentage' => $this->volumePercentage,
+            'delay'             => $this->delay,
+            'days'              => $this->days,
         ]);
     }
 }

+ 35 - 1
app/Services/Pagarme/PagarmeAnticipationService.php

@@ -52,6 +52,19 @@ class PagarmeAnticipationService
 
         $limits ??= $this->fetchAnticipationLimits($recipientId, $paymentDate);
 
+        if ($limits && $limits->maximum->amount <= 0) {
+            Log::channel('pagarme')->warning('Antecipacao ignorada: recebedor sem valor disponivel para antecipar.', [
+                'payment_id'       => $payment->id,
+                'provider_id'      => $payment->provider_id,
+                'recipient_id'     => $recipientId,
+                'requested_amount' => $requestedAmount,
+                'minimum_amount'   => $limits->minimum->amount,
+                'maximum_amount'   => $limits->maximum->amount,
+            ]);
+
+            return null;
+        }
+
         if ($limits && $limits->minimum->amount > 0 && $requestedAmount < $limits->minimum->amount) {
             Log::channel('pagarme')->warning('Antecipacao ignorada: valor abaixo do minimo do recebedor.', [
                 'payment_id'       => $payment->id,
@@ -65,6 +78,19 @@ class PagarmeAnticipationService
             return null;
         }
 
+        if ($limits && $limits->maximum->amount > 0 && $requestedAmount > $limits->maximum->amount) {
+            Log::channel('pagarme')->warning('Antecipacao ignorada: valor acima do maximo disponivel do recebedor.', [
+                'payment_id'       => $payment->id,
+                'provider_id'      => $payment->provider_id,
+                'recipient_id'     => $recipientId,
+                'requested_amount' => $requestedAmount,
+                'minimum_amount'   => $limits->minimum->amount,
+                'maximum_amount'   => $limits->maximum->amount,
+            ]);
+
+            return null;
+        }
+
         try {
             $response = BulkAnticipationResponseData::fromArray($this->pagarmeRequest(
                 method: 'POST',
@@ -136,8 +162,16 @@ class PagarmeAnticipationService
                 ->throw();
 
             $body = $response->json() ?? [];
+            $limits = BulkAnticipationLimitsResponseData::fromArray($body);
+
+            Log::channel('pagarme')->info('Limites de antecipacao consultados no Pagar.me.', [
+                'recipient_id'   => $recipientId,
+                'payment_date'   => $paymentDate,
+                'minimum_amount' => $limits->minimum->amount,
+                'maximum_amount' => $limits->maximum->amount,
+            ]);
 
-            return BulkAnticipationLimitsResponseData::fromArray($body);
+            return $limits;
         } catch (Throwable $e) {
             Log::channel('pagarme')->warning('Falha ao consultar limites de antecipacao.', [
                 'recipient_id' => $recipientId,

+ 6 - 0
app/Services/Pagarme/PagarmePaymentService.php

@@ -106,6 +106,12 @@ class PagarmePaymentService
                 options:    $orderOptions,
             );
 
+            $orderStatus = OrderResponseData::fromArray($result)->paymentStatus();
+
+            if (! in_array($orderStatus, [PaymentStatusEnum::PAID, PaymentStatusEnum::AUTHORIZED], true)) {
+                return $result;
+            }
+
             $anticipationLimits = $this->anticipationService->fetchAnticipationLimitsForPayment($payment);
 
             $this->anticipationService->createBulkAnticipation($payment, $anticipationLimits);

+ 8 - 2
app/Services/Pagarme/PagarmeRecipientService.php

@@ -75,7 +75,10 @@ class PagarmeRecipientService
             ),
 
             automaticAnticipationSettings: new AutomaticAnticipationSettingsData(
-                enabled: false,
+                enabled:          true,
+                type:             'full',
+                volumePercentage: 100,
+                delay:            0,
             ),
         );
 
@@ -111,7 +114,10 @@ class PagarmeRecipientService
             ],
 
             'recipient_automatic_anticipation_settings' => [
-                'enabled' => false,
+                'enabled'           => true,
+                'type'              => 'full',
+                'volume_percentage' => 100,
+                'delay'             => 0,
             ],
 
             'recipient_metadata' => $metadata,