Kaynağa Gözat

refactor: adiciona taxa mais baixa para caso de cart com mais de 3 schedules

Gustavo Mantovani 3 gün önce
ebeveyn
işleme
0c36289ab3

+ 4 - 2
.env.example

@@ -55,9 +55,11 @@ PAGARME_PLATFORM_RECIPIENT_ID=
 
 PAGARME_PIX_DISABLE_SPLIT=false
 
-PAGARME_PLATFORM_PIX_FEE_RATE=0.11
+PAGARME_PLATFORM_PIX_FEE_RATE=0.12
 
-PAGARME_PLATFORM_CREDIT_CARD_FEE_RATE=0.1457
+PAGARME_PLATFORM_CREDIT_CARD_FEE_RATE=0.16
+
+PAGARME_PLATFORM_CART_MIN_3_SCHEDULES_FEE_RATE=0.10
 
 PAGARME_TRANSFER_FEE_AMOUNT=3.67
 

+ 2 - 0
app/Http/Controllers/PaymentController.php

@@ -58,6 +58,7 @@ class PaymentController extends Controller
 
     //
 
+    /*
     public function payCart(Request $request): JsonResponse
     {
         $validated = $request->validate([
@@ -76,6 +77,7 @@ class PaymentController extends Controller
             code:    201,
         );
     }
+    */
 
     public function paySchedule(Request $request, Schedule $schedule): JsonResponse
     {

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

@@ -18,6 +18,7 @@ use App\Data\Pagarme\Order\Parts\Request\SplitOptionsData;
 use App\Enums\PaymentSplitStatusEnum;
 use App\Enums\PaymentStatusEnum;
 use App\Models\Address;
+use App\Models\Cart;
 use App\Models\Client;
 use App\Models\Payment;
 use App\Models\PaymentSplit;
@@ -32,7 +33,7 @@ class PagarmePaymentService
     use FormatsPagarmeData;
     use SendsPagarmeRequests;
 
-    public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod): array
+    public function calculatePaymentAmounts(float $serviceAmount, string $paymentMethod, ?Schedule $schedule = null): array
     {
         if ($serviceAmount <= 0) {
             throw new \InvalidArgumentException('Valor do servico precisa ser maior que zero.');
@@ -42,9 +43,7 @@ class PagarmePaymentService
             throw new \InvalidArgumentException('Forma de pagamento invalida.');
         }
 
-        $platformFeeRate = $paymentMethod === 'credit_card'
-            ? (float) config('services.pagarme.platform_credit_card_fee_rate')
-            : (float) config('services.pagarme.platform_pix_fee_rate');
+        $platformFeeRate = $this->platformFeeRate($paymentMethod, $schedule);
 
         $platformFee = round($serviceAmount * $platformFeeRate, 2);
         $grossAmount = round($serviceAmount + $platformFee, 2);
@@ -63,11 +62,31 @@ class PagarmePaymentService
     public function platformFeeRates(): array
     {
         return [
-            'pix'         => (float) config('services.pagarme.platform_pix_fee_rate'),
-            'credit_card' => (float) config('services.pagarme.platform_credit_card_fee_rate'),
+            'pix'                  => (float) config('services.pagarme.platform_pix_fee_rate'),
+            'credit_card'          => (float) config('services.pagarme.platform_credit_card_fee_rate'),
+            'cart_min_3_schedules' => (float) config('services.pagarme.platform_cart_min_3_schedules_fee_rate'),
         ];
     }
 
+    private function platformFeeRate(string $paymentMethod, ?Schedule $schedule = null): float
+    {
+        if ($schedule && $this->scheduleBelongsToCartWithAtLeastThreeItems($schedule)) {
+            return (float) config('services.pagarme.platform_cart_min_3_schedules_fee_rate');
+        }
+
+        return $paymentMethod === 'credit_card'
+            ? (float) config('services.pagarme.platform_credit_card_fee_rate')
+            : (float) config('services.pagarme.platform_pix_fee_rate');
+    }
+
+    private function scheduleBelongsToCartWithAtLeastThreeItems(Schedule $schedule): bool
+    {
+        return Cart::query()
+            ->whereHas('items', fn ($query) => $query->where('schedule_id', $schedule->id))
+            ->whereHas('items', null, '>=', 3)
+            ->exists();
+    }
+
     public function processPayment(
         Payment  $payment,
         Schedule $schedule,

+ 3 - 0
app/Services/PaymentService.php

@@ -172,6 +172,7 @@ class PaymentService
         $amounts = $this->pagarmePaymentService->calculatePaymentAmounts(
             serviceAmount: $serviceAmount,
             paymentMethod: $paymentMethod,
+            schedule:      $schedule,
         );
 
         $payment = Payment::create([
@@ -244,6 +245,7 @@ class PaymentService
         return $payment;
     }
 
+    /*
     public function payCart(array $data, int $userId): SupportCollection
     {
         $cart = Cart::query()
@@ -273,6 +275,7 @@ class PaymentService
 
         return $payments;
     }
+    */
 
     //
 

+ 6 - 6
config/services.php

@@ -47,11 +47,11 @@ return [
         'webhook_password'      => env('PAGARME_WEBHOOK_PASSWORD'),
         'platform_recipient_id' => env('PAGARME_PLATFORM_RECIPIENT_ID'),
 
-        'pix_disable_split'             => env('PAGARME_PIX_DISABLE_SPLIT', false),
-        'platform_pix_fee_rate'         => env('PAGARME_PLATFORM_PIX_FEE_RATE', 0.11),
-        'platform_credit_card_fee_rate' => env('PAGARME_PLATFORM_CREDIT_CARD_FEE_RATE', 0.1457),
-        'transfer_fee_amount'           => env('PAGARME_TRANSFER_FEE_AMOUNT', 3.67),
-        'withdrawal_release_days'       => env('PAGARME_WITHDRAWAL_RELEASE_DAYS', 2),
+        'pix_disable_split'                      => env('PAGARME_PIX_DISABLE_SPLIT', false),
+        'platform_pix_fee_rate'                  => env('PAGARME_PLATFORM_PIX_FEE_RATE', 0.12),
+        'platform_credit_card_fee_rate'          => env('PAGARME_PLATFORM_CREDIT_CARD_FEE_RATE', 0.16),
+        'platform_cart_min_3_schedules_fee_rate' => env('PAGARME_PLATFORM_CART_MIN_3_SCHEDULES_FEE_RATE', 0.10),
+        'transfer_fee_amount'                    => env('PAGARME_TRANSFER_FEE_AMOUNT', 3.67),
+        'withdrawal_release_days'                => env('PAGARME_WITHDRAWAL_RELEASE_DAYS', 2),
     ],
-
 ];

+ 2 - 1
routes/authRoutes/payment.php

@@ -10,7 +10,8 @@ Route::get('/payment/platform-fees', [PaymentController::class, 'platformFees'])
 
 Route::get('/payment/schedule/{schedule}/pix',  [PaymentController::class, 'getSchedulePix']);
 Route::post('/payment/schedule/{schedule}/pay', [PaymentController::class, 'paySchedule']);
-Route::post('/payment/cart/pay',                [PaymentController::class, 'payCart']);
+
+// Route::post('/payment/cart/pay', [PaymentController::class, 'payCart']);
 
 Route::get('/payment/{id}',    [PaymentController::class, 'show'])->middleware('permission:payment,view');
 Route::put('/payment/{id}',    [PaymentController::class, 'update'])->middleware('permission:payment,edit');