Răsfoiți Sursa

refactor: pix consulta

Gustavo Mantovani 1 săptămână în urmă
părinte
comite
8cad82dcef

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

@@ -72,6 +72,17 @@ class PaymentController extends Controller
         );
     }
 
+    public function getSchedulePix(Schedule $schedule): JsonResponse
+    {
+        if ($schedule->client?->user_id !== Auth::id()) {
+            abort(403);
+        }
+
+        $item = $this->service->getOrCreatePixPayment($schedule);
+
+        return $this->successResponse(payload: new PaymentResource($item));
+    }
+
     public function show(int $id): JsonResponse
     {
         $item = $this->service->findById($id);

+ 72 - 0
app/Services/PaymentService.php

@@ -111,6 +111,16 @@ class PaymentService
                     'failed_at'       => now(),
                     'failure_message' => 'Pagamento pendente sem retorno do gateway.',
                 ])->save();
+            } elseif ($this->isExpiredPixPayment($existingPayment)) {
+                $existingPayment->forceFill([
+                    'status'          => PaymentStatusEnum::FAILED,
+                    'failed_at'       => now(),
+                    'failure_message' => 'Pagamento Pix expirado.',
+                ])->save();
+
+                PaymentSplit::query()
+                    ->where('payment_id', $existingPayment->id)
+                    ->update(['status' => PaymentSplitStatusEnum::FAILED]);
             } else {
                 if ($existingPayment->payment_method !== $paymentMethod && $existingPayment->status !== PaymentStatusEnum::PAID) {
                     throw new \InvalidArgumentException('Ja existe um pagamento em andamento para este agendamento.');
@@ -229,8 +239,70 @@ class PaymentService
         return $payment;
     }
 
+    public function getOrCreatePixPayment(Schedule $schedule): Payment
+    {
+        $existingPayment = Payment::query()
+            ->where('schedule_id', $schedule->id)
+            ->where('payment_method', 'pix')
+            ->whereIn('status', [
+                PaymentStatusEnum::PENDING->value,
+                PaymentStatusEnum::PROCESSING->value,
+                PaymentStatusEnum::AUTHORIZED->value,
+                PaymentStatusEnum::PAID->value,
+            ])
+            ->latest('id')
+            ->first();
+
+        if ($existingPayment && $this->isExpiredPixPayment($existingPayment)) {
+            $existingPayment->forceFill([
+                'status'          => PaymentStatusEnum::FAILED,
+                'failed_at'       => Carbon::now(),
+                'failure_message' => 'Pagamento Pix expirado.',
+            ])->save();
+
+            PaymentSplit::query()
+                ->where('payment_id', $existingPayment->id)
+                ->update(['status' => PaymentSplitStatusEnum::FAILED]);
+
+            $existingPayment = null;
+        }
+
+        if ($existingPayment) {
+            if ($this->isIncompleteGatewayPayment($existingPayment)) {
+                $existingPayment->forceFill([
+                    'status'          => PaymentStatusEnum::FAILED,
+                    'failed_at'       => Carbon::now(),
+                    'failure_message' => 'Pagamento pendente sem retorno do gateway.',
+                ])->save();
+
+                PaymentSplit::query()
+                    ->where('payment_id', $existingPayment->id)
+                    ->update(['status' => PaymentSplitStatusEnum::FAILED]);
+            } else {
+                $this->syncScheduleStatusAfterPayment($schedule, $existingPayment);
+
+                return $existingPayment;
+            }
+        }
+
+        return $this->payAcceptedSchedule(
+            schedule: $schedule,
+            paymentMethod: 'pix',
+        );
+    }
+
     //
 
+    private function isExpiredPixPayment(Payment $payment): bool
+    {
+        if ($payment->payment_method !== 'pix') {
+            return false;
+        }
+
+        return $payment->expires_at !== null
+            && $payment->expires_at->isPast();
+    }
+
     private function isIncompleteGatewayPayment(Payment $payment): bool
     {
         return $payment->status === PaymentStatusEnum::PENDING

+ 5 - 2
routes/authRoutes/payment.php

@@ -3,9 +3,12 @@
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\PaymentController;
 
-Route::get('/payment',         [PaymentController::class, 'index'])->middleware('permission:payment,view');
-Route::post('/payment',        [PaymentController::class, 'store'])->middleware('permission:payment,add');
+Route::get('/payment',  [PaymentController::class, 'index'])->middleware('permission:payment,view');
+Route::post('/payment', [PaymentController::class, 'store'])->middleware('permission:payment,add');
+
+Route::get('/payment/schedule/{schedule}/pix',  [PaymentController::class, 'getSchedulePix'])->middleware('permission:config.schedule,view');
 Route::post('/payment/schedule/{schedule}/pay', [PaymentController::class, 'paySchedule'])->middleware('permission:config.schedule,edit');
+
 Route::get('/payment/{id}',    [PaymentController::class, 'show'])->middleware('permission:payment,view');
 Route::put('/payment/{id}',    [PaymentController::class, 'update'])->middleware('permission:payment,edit');
 Route::delete('/payment/{id}', [PaymentController::class, 'destroy'])->middleware('permission:payment,delete');