provider()->first(); if (! $provider) { return null; } $recipientId = $provider->recipient_id; if (empty($recipientId)) { return null; } $providerSplit = $payment->splits() ->where('provider_id', $payment->provider_id) ->first(); if (! $providerSplit) { return null; } $requestedAmount = OrderRequestData::amountInCents((float) $providerSplit->gross_amount); if ($requestedAmount <= 0) { return null; } try { $response = BulkAnticipationResponseData::fromArray($this->pagarmeRequest( method: 'POST', path: "/recipients/{$recipientId}/bulk_anticipations", payload: new BulkAnticipationRequestData( paymentDate: $this->getAnticipationPaymentDate(), timeframe: 'start', requestedAmount: $requestedAmount, automaticTransfer: false, ), idempotencyKey: "bulk-ant-{$payment->id}-{$payment->provider_id}", errorMessage: 'Erro ao criar antecipacao no Pagar.me.', )); $response->requireId(); return $response; } catch (Throwable $e) { Log::channel('pagarme')->warning('Falha ao criar antecipacao no Pagar.me.', [ 'payment_id' => $payment->id, 'provider_id' => $payment->provider_id, 'recipient_id' => $recipientId, 'requested_amount' => $requestedAmount, 'error' => $e->getMessage(), ]); 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(); } }