|
|
@@ -2,11 +2,14 @@
|
|
|
|
|
|
namespace App\Services\Pagarme;
|
|
|
|
|
|
+use App\Data\Pagarme\Anticipation\AnticipationLimitData;
|
|
|
+use App\Data\Pagarme\Anticipation\BulkAnticipationLimitsResponseData;
|
|
|
use App\Data\Pagarme\Anticipation\BulkAnticipationRequestData;
|
|
|
use App\Data\Pagarme\Anticipation\BulkAnticipationResponseData;
|
|
|
use App\Data\Pagarme\Order\OrderRequestData;
|
|
|
use App\Models\Payment;
|
|
|
use App\Services\Pagarme\Concerns\SendsPagarmeRequests;
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Throwable;
|
|
|
|
|
|
@@ -42,13 +45,30 @@ class PagarmeAnticipationService
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ $paymentDate = $this->getAnticipationPaymentDate();
|
|
|
+
|
|
|
+ $limits = $this->fetchAnticipationLimits($recipientId, $paymentDate);
|
|
|
+
|
|
|
+ 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,
|
|
|
+ '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',
|
|
|
path: "/recipients/{$recipientId}/bulk_anticipations",
|
|
|
|
|
|
payload: new BulkAnticipationRequestData(
|
|
|
- paymentDate: $this->getAnticipationPaymentDate(),
|
|
|
+ paymentDate: $paymentDate,
|
|
|
timeframe: 'start',
|
|
|
requestedAmount: $requestedAmount,
|
|
|
automaticTransfer: false,
|
|
|
@@ -67,6 +87,8 @@ class PagarmeAnticipationService
|
|
|
'provider_id' => $payment->provider_id,
|
|
|
'recipient_id' => $recipientId,
|
|
|
'requested_amount' => $requestedAmount,
|
|
|
+ 'minimum_amount' => $limits?->minimum->amount,
|
|
|
+ 'maximum_amount' => $limits?->maximum->amount,
|
|
|
'error' => $e->getMessage(),
|
|
|
]);
|
|
|
|
|
|
@@ -74,6 +96,42 @@ class PagarmeAnticipationService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function fetchAnticipationLimits(string $recipientId, string $paymentDate): ?BulkAnticipationLimitsResponseData
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $secretKey = config('services.pagarme.secret_key');
|
|
|
+
|
|
|
+ if (empty($secretKey)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $endpoint = $this->pagarmeUrl("/recipients/{$recipientId}/bulk_anticipations/limits");
|
|
|
+
|
|
|
+ $response = Http::withBasicAuth($secretKey, '')
|
|
|
+ ->withHeaders([
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ 'Accept' => 'application/json',
|
|
|
+ ])
|
|
|
+ ->get($endpoint, [
|
|
|
+ 'payment_date' => $paymentDate,
|
|
|
+ 'timeframe' => 'start',
|
|
|
+ ])
|
|
|
+ ->throw();
|
|
|
+
|
|
|
+ $body = $response->json() ?? [];
|
|
|
+
|
|
|
+ return BulkAnticipationLimitsResponseData::fromArray($body);
|
|
|
+ } catch (Throwable $e) {
|
|
|
+ Log::channel('pagarme')->warning('Falha ao consultar limites de antecipacao.', [
|
|
|
+ 'recipient_id' => $recipientId,
|
|
|
+ 'payment_date' => $paymentDate,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private function getAnticipationPaymentDate(): string
|
|
|
{
|
|
|
$now = now()->timezone('America/Sao_Paulo');
|