diff --git a/src/Command/ProcessLatePaymongoTransactionsCommand.php b/src/Command/ProcessLatePaymongoTransactionsCommand.php new file mode 100644 index 00000000..6f65dde1 --- /dev/null +++ b/src/Command/ProcessLatePaymongoTransactionsCommand.php @@ -0,0 +1,101 @@ +em = $em; + $this->paymongo = $paymongo; + $this->insurance = $insurance; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('paymongo:checkpending') + ->setDescription('Check for any late PayMongo transactions and process if needed.') + ->setHelp('Check for any late PayMongo transactions and process if needed.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln('Fetching all late pending transactions...'); + + // set date threshold to 24 hours ago + $date_threshold = (new DateTime())->modify('-24 hours'); + + $transactions = $this->em->getRepository(GatewayTransaction::class) + ->createQueryBuilder('t') + ->select('t') + ->where('t.status = :status') + ->andWhere('t.date_create <= :date_threshold') + ->setParameter('status', TransactionStatus::PENDING) + ->setParameter('date_threshold', $date_threshold) + ->getQuery() + ->getResult(); + + $output->writeln('Found '. count($transactions) . ' rows matching criteria.'); + + $x = 0; + + foreach ($transactions as $trans) { + // check paymongo status + $checkout = $this->paymongo->getCheckout($trans->getExtTransactionId()); + + if ($checkout['success']) { + // check if we have any payments made + $payments = $checkout['response']['data']['attributes']['payments'] ?? []; + + if (!empty($payments)) { + $amount_paid = 0; + + // for good measure, we get all successful payments and add them up + foreach ($payments as $payment) { + if ($payment['attributes']['status'] === TransactionStatus::PAID) { + $amount_paid = bcadd($amount_paid, $payment['attributes']['amount']); + } + } + + // this transaction is fully paid, so we mark it as paid + if (bccomp($trans->getAmount(), $amount_paid) <= 0) { + $trans->setStatus(TransactionStatus::PAID); + $trans->setDatePay(new DateTime()); + $this->em->flush(); + + $output->writeln('Marked transaction '. $trans->getID() .'as paid.'); + $x++; + } + } else { + $output->writeln('No payments found for transaction: ' . $trans->getID() . ''); + } + } else { + $output->writeln('Checkout not found: ' . $checkout['error']['message'] . ''); + } + } + + $output->writeln('Done! Processed ' . $x . ' rows.'); + + return 0; + } +}