custom/plugins/NetsCheckout/src/NetsCheckout.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nets\Checkout;
  3. use Nets\Checkout\Service\Checkout;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  14. class NetsCheckout extends Plugin
  15. {
  16.     public function install(InstallContext $context): void
  17.     {
  18.        $this->addPaymentMethod($context->getContext());
  19.     }
  20.     public function uninstall(UninstallContext $context): void
  21.     {
  22.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  23.         // cause data consistency issues, since the payment method might have been used in several orders
  24.         $this->setPaymentMethodIsActive(false$context->getContext());
  25.     }
  26.     public function activate(ActivateContext $context): void
  27.     {
  28.         $this->setPaymentMethodIsActive(true$context->getContext());
  29.         parent::activate($context);
  30.     }
  31.     public function deactivate(DeactivateContext $context): void
  32.     {
  33.         $this->setPaymentMethodIsActive(false$context->getContext());
  34.         parent::deactivate($context);
  35.     }
  36.     private function addPaymentMethod(Context $context): void
  37.     {
  38.         $paymentMethodExists $this->getPaymentMethodId();
  39.         // Payment method exists already, no need to continue here
  40.         if ($paymentMethodExists) {
  41.             return;
  42.         }
  43.         /** @var PluginIdProvider $pluginIdProvider */
  44.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  45.         $pluginId $pluginIdProvider->getPluginIdByBaseClass('Nets\Checkout\NetsCheckout'$context);
  46.         $netsCheckoutPaymentData = [
  47.             // payment handler will be selected by the identifier
  48.             'handlerIdentifier' => Checkout::class,
  49.             'name' => 'Nets Checkout Payment',
  50.             'description' => 'Nets Checkout Payment',
  51.             'pluginId' => $pluginId,
  52.         ];
  53.         /** @var EntityRepositoryInterface $paymentRepository */
  54.         $paymentRepository $this->container->get('payment_method.repository');
  55.         $paymentRepository->create([$netsCheckoutPaymentData], $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();
  62.         // Payment does not even exist, so nothing to (de-)activate here
  63.         if (!$paymentMethodId) {
  64.             return;
  65.         }
  66.         $paymentMethod = [
  67.             'id' => $paymentMethodId,
  68.             'active' => $active,
  69.         ];
  70.         $paymentRepository->update([$paymentMethod], $context);
  71.     }
  72.     private function getPaymentMethodId(): ?string
  73.     {
  74.         /** @var EntityRepositoryInterface $paymentRepository */
  75.         $paymentRepository $this->container->get('payment_method.repository');
  76.         // Fetch ID for update
  77.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'Checkout::class));
  78.         $paymentIds $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext());
  79.         if ($paymentIds->getTotal() === 0) {
  80.             return null;
  81.         }
  82.         return $paymentIds->getIds()[0];
  83.     }
  84. }