|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PayPlug\SyliusPayPlugPlugin\Command; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use PayPlug\SyliusPayPlugPlugin\Repository\PaymentRepositoryInterface; |
| 9 | +use Psr\Log\LoggerInterface; |
| 10 | +use SM\Factory\Factory; |
| 11 | +use Sylius\Component\Payment\PaymentTransitions; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Input\InputOption; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class CaptureAuthorizedPaymentCommand extends Command |
| 18 | +{ |
| 19 | + private Factory $stateMachineFactory; |
| 20 | + private PaymentRepositoryInterface $paymentRepository; |
| 21 | + private EntityManagerInterface $entityManager; |
| 22 | + private LoggerInterface $logger; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + Factory $stateMachineFactory, |
| 26 | + PaymentRepositoryInterface $paymentRepository, |
| 27 | + EntityManagerInterface $entityManager, |
| 28 | + LoggerInterface $logger, |
| 29 | + ) { |
| 30 | + $this->stateMachineFactory = $stateMachineFactory; |
| 31 | + $this->paymentRepository = $paymentRepository; |
| 32 | + $this->entityManager = $entityManager; |
| 33 | + $this->logger = $logger; |
| 34 | + |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this->setName('payplug:capture-authorized-payments') |
| 41 | + ->setDescription('Capture payplug authorized payments older than X days (default 6)') |
| 42 | + ->addOption('days', 'd', InputOption::VALUE_OPTIONAL, 'Number of days to wait before capturing authorized payments', 6) |
| 43 | + ; |
| 44 | + } |
| 45 | + |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 47 | + { |
| 48 | + $days = \filter_var($input->getOption('days'), FILTER_VALIDATE_INT); |
| 49 | + if (false === $days) { |
| 50 | + throw new \InvalidArgumentException('Invalid number of days provided.'); |
| 51 | + } |
| 52 | + |
| 53 | + $payments = $this->paymentRepository->findAllAuthorizedOlderThanDays($days); |
| 54 | + |
| 55 | + if (\count($payments) === 0) { |
| 56 | + $this->logger->debug('[Payplug] No authorized payments found.'); |
| 57 | + } |
| 58 | + |
| 59 | + foreach ($payments as $i => $payment) { |
| 60 | + $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); |
| 61 | + $this->logger->info('[Payplug] Capturing payment {paymentId} (order #{orderNumber})', [ |
| 62 | + 'paymentId' => $payment->getId(), |
| 63 | + 'orderNumber' => $payment->getOrder()?->getNumber() ?? 'N/A', |
| 64 | + ]); |
| 65 | + $output->writeln(sprintf('Capturing payment %d (order #%s)', $payment->getId(), $payment->getOrder()?->getNumber() ?? 'N/A')); |
| 66 | + |
| 67 | + try { |
| 68 | + $stateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE); |
| 69 | + } catch (\Throwable $e) { |
| 70 | + $this->logger->critical('[Payplug] Error while capturing payment {paymentId}', [ |
| 71 | + 'paymentId' => $payment->getId(), |
| 72 | + 'exception' => $e->getMessage(), |
| 73 | + ]); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if ($i % 10 === 0) { |
| 78 | + $this->entityManager->flush(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $this->entityManager->flush(); |
| 83 | + |
| 84 | + return Command::SUCCESS; |
| 85 | + } |
| 86 | +} |
0 commit comments