Browse Source

refactor: ad d logica para jogar antecipacao para proximo dia

Gustavo Mantovani 2 days ago
parent
commit
0620d1a510

+ 4 - 5
app/Services/Pagarme/Concerns/SendsPagarmeRequests.php

@@ -10,11 +10,10 @@ use Throwable;
 trait SendsPagarmeRequests
 {
     protected function pagarmeRequest(
-        string            $method,
-        string            $path,
-        string            $idempotencyKey,
-        string            $errorMessage,
-        array|PagarmeData $payload,
+        string $method,
+        string $path,
+        string $idempotencyKey,
+        string $errorMessage, array|PagarmeData $payload,
     ): array {
         $payload  = $payload instanceof PagarmeData ? $payload->toArray() : $payload;
         $endpoint = $this->pagarmeUrl($path);

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

@@ -48,7 +48,7 @@ class PagarmeAnticipationService
                 path:   "/recipients/{$recipientId}/bulk_anticipations",
 
                 payload: new BulkAnticipationRequestData(
-                    paymentDate:       now()->toISOString(),
+                    paymentDate:       $this->getAnticipationPaymentDate(),
                     timeframe:         'start',
                     requestedAmount:   $requestedAmount,
                     automaticTransfer: false,
@@ -73,4 +73,21 @@ class PagarmeAnticipationService
             return null;
         }
     }
+
+    private function getAnticipationPaymentDate(): string
+    {
+        $now = now()->timezone('America/Sao_Paulo');
+
+        if ($now->hour < 11) {
+            return $now->toISOString();
+        }
+
+        $nextBusinessDay = $now->copy()->addDay();
+
+        while ($nextBusinessDay->isWeekend()) {
+            $nextBusinessDay->addDay();
+        }
+
+        return $nextBusinessDay->startOfDay()->toISOString();
+    }
 }