custom/plugins/NetiNextOrderAmountHandler/src/NetiNextOrderAmountHandler.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextOrderAmountHandler;
  4. use NetInventors\NetiNextOrderAmountHandler\Service\CompilerPass;
  5. use NetInventors\NetiNextOrderAmountHandler\Service\PaymentHandler\FreePayment;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class NetiNextOrderAmountHandler extends Plugin
  18. {
  19.     public function install(InstallContext $context): void
  20.     {
  21.         $this->addPaymentMethod($context->getContext());
  22.     }
  23.     public function uninstall(UninstallContext $context): void
  24.     {
  25.         $this->setPaymentMethodIsActive(false$context->getContext());
  26.     }
  27.     public function activate(ActivateContext $context): void
  28.     {
  29.         $this->setPaymentMethodIsActive(true$context->getContext());
  30.         parent::activate($context);
  31.     }
  32.     public function deactivate(DeactivateContext $context): void
  33.     {
  34.         $this->setPaymentMethodIsActive(false$context->getContext());
  35.         parent::deactivate($context);
  36.     }
  37.     private function addPaymentMethod(Context $context): void
  38.     {
  39.         $paymentMethodExists $this->getPaymentMethodId($context);
  40.         if ($paymentMethodExists) {
  41.             return;
  42.         }
  43.         /** @var PluginIdProvider $pluginIdProvider */
  44.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  45.         $pluginId         $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  46.         $examplePaymentData = [
  47.             'handlerIdentifier' => FreePayment::class,
  48.             'name'              => 'Free',
  49.             'description'       => 'Can only be used when the invoice amount is zero.',
  50.             'pluginId'          => $pluginId,
  51.             'afterOrderEnabled' => false,
  52.         ];
  53.         /** @var EntityRepositoryInterface $paymentRepository */
  54.         $paymentRepository $this->container->get('payment_method.repository');
  55.         $paymentRepository->create([ $examplePaymentData ], $context);
  56.     }
  57.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  58.     {
  59.         /** @var EntityRepositoryInterface $paymentRepository */
  60.         $paymentRepository $this->container->get('payment_method.repository');
  61.         $paymentMethodId   $this->getPaymentMethodId($context);
  62.         if ($paymentMethodId) {
  63.             $paymentMethod = [
  64.                 'id'     => $paymentMethodId,
  65.                 'active' => $active,
  66.             ];
  67.             $paymentRepository->update([ $paymentMethod ], $context);
  68.         }
  69.     }
  70.     private function getPaymentMethodId(Context $context): ?string
  71.     {
  72.         /** @var EntityRepositoryInterface $paymentRepository */
  73.         $paymentRepository $this->container->get('payment_method.repository');
  74.         $criteria          = new Criteria();
  75.         $criteria->addFilter(new EqualsFilter('handlerIdentifier'FreePayment::class));
  76.         $paymentIds $paymentRepository->searchIds($criteria$context);
  77.         return $paymentIds->firstId();
  78.     }
  79.     public function build(ContainerBuilder $container): void
  80.     {
  81.         parent::build($container);
  82.         $container->addCompilerPass(new CompilerPass());
  83.     }
  84. }