Пример #1
0
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /**
         * @var \Sparkling\AdyenBundle\Entity\SubscriptionRepositoryInterface $repository
         */
        $repository = $this->em->getRepository($this->container->getParameter('adyen.subscription_entity'));

        if ($input->getArgument('subscription') !== null) {
            if ($subscription = $repository->find($input->getArgument('subscription'))) {
                $expired = array($subscription);
            } else return $output->writeln('<error>Subscription not found</error>');
        } else {
            $expired = $repository->getSubscriptionsThatNeedToBeCharged();
        }

        if ($expired) {
            $output->writeln("Charging " . (count($expired) == 1 ? "1 subscription" : count($expired) . " subscriptions"));
            $output->writeLn('');

            foreach ($expired AS $subscription) {
                if ($subscription->getRecurringReference() === null) {
                    if ($this->adyen->loadContract($subscription) !== true) {
                        $output->writeln(sprintf('Subscription %d <error>%s</error>', $subscription->getId(), 'Recurring contract not found'));
                        continue;
                    }
                }

                $transaction = $this->adyen->charge($subscription);

                if($transaction === FALSE)
                    $output->writeln(sprintf('Subscription %d <error>%s</error>', $subscription->getId(), $this->adyen->getError()));
                else {
                    $output->writeln(sprintf(
                        'Subscription %d -> Transaction %d [<info>OK</info>]',
                        $subscription->getId(),
                        $transaction->getId()
                    ));
                }
            }

            $this->em->flush();
        } else $output->writeln('There are subscriptions that need to be charged.');
    }