Browse Source

comando para confirmar ultimo pagamento solicitado de agendamento

Gustavo Zanatta 4 days ago
parent
commit
1a0d83d224
2 changed files with 82 additions and 0 deletions
  1. 75 0
      app/Commands/ConfirmarPagamento.php
  2. 7 0
      routes/console.php

+ 75 - 0
app/Commands/ConfirmarPagamento.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Commands;
+
+use App\Enums\PaymentStatusEnum;
+use App\Models\Payment;
+use App\Services\Pagarme\PagarmePaymentService;
+use App\Services\PaymentService;
+use Illuminate\Console\Command;
+
+class ConfirmarPagamento extends Command
+{
+    protected $signature = 'confirma_pagamento {payment_id? : ID do payment a confirmar (padrao: ultimo pendente)}';
+
+    protected $description = 'Simula a confirmacao de pagamento (Pagar.me) do ultimo agendamento solicitado, para facilitar testes locais';
+
+    public function __construct(
+        protected PagarmePaymentService $pagarmePaymentService,
+        protected PaymentService $paymentService,
+    ) {
+        parent::__construct();
+    }
+
+    public function handle(): int
+    {
+        if (app()->environment('production')) {
+            $this->error('Este comando nao pode ser executado em producao.');
+
+            return Command::FAILURE;
+        }
+
+        $paymentId = $this->argument('payment_id');
+
+        $payment = $paymentId
+            ? Payment::query()->find($paymentId)
+            : Payment::query()
+                ->whereIn('status', [PaymentStatusEnum::PENDING, PaymentStatusEnum::PROCESSING, PaymentStatusEnum::AUTHORIZED])
+                ->latest('id')
+                ->first();
+
+        if (! $payment) {
+            $this->error($paymentId ? "Payment #{$paymentId} nao encontrado." : 'Nenhum payment pendente encontrado.');
+
+            return Command::FAILURE;
+        }
+
+        if ($payment->status === PaymentStatusEnum::PAID) {
+            $this->warn("Payment #{$payment->id} ja esta pago.");
+
+            return Command::SUCCESS;
+        }
+
+        $this->pagarmePaymentService->applyGatewayResponseToPayment($payment, [
+            'id'      => 'or_local_test',
+            'charges' => [[
+                'id'               => 'ch_local_test',
+                'status'           => 'paid',
+                'paid_at'          => now()->toISOString(),
+                'last_transaction' => [
+                    'id'     => 'tran_local_test',
+                    'status' => 'captured',
+                    'cost'   => 0,
+                ],
+            ]],
+        ]);
+
+        $this->paymentService->syncPaymentTargets($payment->fresh());
+
+        $status = $payment->fresh()->status->value;
+
+        $this->info("Payment #{$payment->id} (schedule #{$payment->schedule_id}) atualizado para status: {$status}");
+
+        return Command::SUCCESS;
+    }
+}

+ 7 - 0
routes/console.php

@@ -1,5 +1,6 @@
 <?php
 
+use App\Commands\ConfirmarPagamento;
 use App\Commands\CreateCrud;
 use App\Commands\RefreshPagarmeEntities;
 use App\Commands\RefreshPermissions;
@@ -46,6 +47,12 @@ Artisan::command('websocket:test {room} {--event=test-event} {--data=}', functio
     ]);
 })->purpose('Test websocket broadcasting by emitting an event');
 
+Artisan::command('confirma_pagamento {payment_id?}', function () {
+    $this->call(ConfirmarPagamento::class, [
+        'payment_id' => $this->argument('payment_id'),
+    ]);
+})->purpose('[LOCAL] Simula a confirmacao de pagamento do ultimo agendamento solicitado');
+
 //
 
 Artisan::command('pagarme:recipient-balance {recipient_id} {--skip-local}', function (