Parcourir la source

feat: ajusta com a taxa do gateway

Gustavo Mantovani il y a 1 semaine
Parent
commit
701def3532

+ 3 - 0
app/Data/Pagarme/Response/OrderResponseData/OrderChargeResponseData/OrderTransactionResponseData.php

@@ -10,6 +10,7 @@ final readonly class OrderTransactionResponseData extends PagarmeResponseData
         public ?string $id,
         public ?string $status,
         public ?int    $amount,
+        public ?int    $cost,
         public ?string $createdAt,
         public ?string $acquirerMessage,
         public array   $gatewayResponse,
@@ -24,6 +25,7 @@ final readonly class OrderTransactionResponseData extends PagarmeResponseData
             id:              static::arrString($payload, 'id'),
             status:          static::arrString($payload, 'status'),
             amount:          static::arrInt($payload, 'amount'),
+            cost:            static::arrInt($payload, 'cost'),
             createdAt:       static::arrString($payload, 'created_at'),
             acquirerMessage: static::arrString($payload, 'acquirer_message'),
             gatewayResponse: static::arrArray($payload, 'gateway_response'),
@@ -39,6 +41,7 @@ final readonly class OrderTransactionResponseData extends PagarmeResponseData
             'id'               => $this->id,
             'status'           => $this->status,
             'amount'           => $this->amount,
+            'cost'             => $this->cost,
             'created_at'       => $this->createdAt,
             'acquirer_message' => $this->acquirerMessage,
             'gateway_response' => $this->gatewayResponse,

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

@@ -184,6 +184,9 @@ class PagarmePaymentService
             $failureMessage = $order->failureMessage();
         }
 
+        $gatewayFeeCents = $order->lastTransaction()?->cost ?? 0;
+        $gatewayFee      = $gatewayFeeCents > 0 ? round($gatewayFeeCents / 100, 2) : 0;
+
         $payment->forceFill([
             'gateway_provider'            => 'pagarme',
             'gateway_entity_reference'    => $order->gatewayEntityReference(),
@@ -194,6 +197,7 @@ class PagarmePaymentService
             'paid_at'                     => $order->paidAt(),
             'authorized_at'               => $order->authorizedAt(),
             'gateway_payload'             => $orderResponse,
+            'gateway_fee_amount'          => $gatewayFee,
             'failure_code'                => $failureCode,
             'failure_message'             => $failureMessage,
         ])->save();

+ 5 - 1
app/Services/PaymentService.php

@@ -163,7 +163,11 @@ class PaymentService
         $serviceAmount = (float) $schedule->total_amount;
 
         $platformFee = round($serviceAmount * 0.11, 2);
-        $grossAmount = round($serviceAmount + $platformFee, 2);
+
+        $estimatedGatewayFeeRate = $paymentMethod === 'credit_card' ? 0.04 : 0.01;
+        $estimatedGatewayFee     = round($serviceAmount * $estimatedGatewayFeeRate, 2);
+
+        $grossAmount = round($serviceAmount + $platformFee + $estimatedGatewayFee, 2);
 
         $platformRecipientId = config('services.pagarme.platform_recipient_id');