|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|