ConfirmarPagamento.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Commands;
  3. use App\Enums\PaymentStatusEnum;
  4. use App\Models\Payment;
  5. use App\Services\Pagarme\PagarmePaymentService;
  6. use App\Services\PaymentService;
  7. use Illuminate\Console\Command;
  8. class ConfirmarPagamento extends Command
  9. {
  10. protected $signature = 'confirma_pagamento {payment_id? : ID do payment a confirmar (padrao: ultimo pendente)}';
  11. protected $description = 'Simula a confirmacao de pagamento (Pagar.me) do ultimo agendamento solicitado, para facilitar testes locais';
  12. public function __construct(
  13. protected PagarmePaymentService $pagarmePaymentService,
  14. protected PaymentService $paymentService,
  15. ) {
  16. parent::__construct();
  17. }
  18. public function handle(): int
  19. {
  20. if (app()->environment('production')) {
  21. $this->error('Este comando nao pode ser executado em producao.');
  22. return Command::FAILURE;
  23. }
  24. $paymentId = $this->argument('payment_id');
  25. $payment = $paymentId
  26. ? Payment::query()->find($paymentId)
  27. : Payment::query()
  28. ->whereIn('status', [PaymentStatusEnum::PENDING, PaymentStatusEnum::PROCESSING, PaymentStatusEnum::AUTHORIZED])
  29. ->latest('id')
  30. ->first();
  31. if (! $payment) {
  32. $this->error($paymentId ? "Payment #{$paymentId} nao encontrado." : 'Nenhum payment pendente encontrado.');
  33. return Command::FAILURE;
  34. }
  35. if ($payment->status === PaymentStatusEnum::PAID) {
  36. $this->warn("Payment #{$payment->id} ja esta pago.");
  37. return Command::SUCCESS;
  38. }
  39. $this->pagarmePaymentService->applyGatewayResponseToPayment($payment, [
  40. 'id' => 'or_local_test',
  41. 'charges' => [[
  42. 'id' => 'ch_local_test',
  43. 'status' => 'paid',
  44. 'paid_at' => now()->toISOString(),
  45. 'last_transaction' => [
  46. 'id' => 'tran_local_test',
  47. 'status' => 'captured',
  48. 'cost' => 0,
  49. ],
  50. ]],
  51. ]);
  52. $this->paymentService->syncPaymentTargets($payment->fresh());
  53. $status = $payment->fresh()->status->value;
  54. $this->info("Payment #{$payment->id} (schedule #{$payment->schedule_id}) atualizado para status: {$status}");
  55. return Command::SUCCESS;
  56. }
  57. }