custom/plugins/payplacePaymentPlugin/src/payplacePaymentPlugin.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace payplace\Payment;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  12. use payplace\Payment\Service\payplacePaymentHandlerCC;
  13. use payplace\Payment\Service\payplacePaymentHandlerDD;
  14. use payplace\Payment\Service\payplacePaymentHandlerGP;
  15. use payplace\Payment\Service\payplacePaymentHandlerP2;
  16. use payplace\Payment\Service\payplacePaymentHandlerPD;
  17. use payplace\Payment\Service\payplacePaymentHandlerPP;
  18. use payplace\Payment\Service\payplacePaymentHandlerSU;
  19. class payplacePaymentPlugin extends Plugin
  20. {
  21.     public function install(InstallContext $context): void
  22.     {
  23.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerCC::class, -106);
  24.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerDD::class, -105);
  25.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerGP::class, -104);
  26.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerP2::class, -103);
  27.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerPD::class, -102);
  28.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerPP::class, -101);
  29.         $this->addPaymentMethod($context->getContext(), payplacePaymentHandlerSU::class, -100);
  30.     }
  31.     public function uninstall(UninstallContext $context): void
  32.     {
  33.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  34.         // cause data consistency issues, since the payment method might have been used in several orders
  35.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerCC::class);
  36.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerDD::class);
  37.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerGP::class);
  38.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerP2::class);
  39.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerPD::class);
  40.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerPP::class);
  41.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerSU::class);
  42.     }
  43.     public function activate(ActivateContext $context): void
  44.     {
  45.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerCC::class);
  46.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerDD::class);
  47.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerGP::class);
  48.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerP2::class);
  49.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerPD::class);
  50.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerPP::class);
  51.         $this->setPaymentActive(true$context->getContext(), payplacePaymentHandlerSU::class);
  52.         parent::activate($context);
  53.     }
  54.     public function deactivate(DeactivateContext $context): void
  55.     {
  56.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerCC::class);
  57.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerDD::class);
  58.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerGP::class);
  59.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerP2::class);
  60.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerPD::class);
  61.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerPP::class);
  62.         $this->setPaymentActive(false$context->getContext(), payplacePaymentHandlerSU::class);
  63.         parent::deactivate($context);
  64.     }
  65.     private function addPaymentMethod(Context $contextstring $paymentHandlerClassint $position): void
  66.     {
  67.         if ($this->getPaymentMethodId($context$paymentHandlerClass) ) {
  68.             return;
  69.         }
  70.         $pluginId $this->container->get(PluginIdProvider::class)->getPluginIdByBaseClass(get_class($this), $context);
  71.         $paymentData = [
  72.             'handlerIdentifier'     => $paymentHandlerClass,
  73.             'position'              => $position,
  74.             'translations'          => $paymentHandlerClass::getPaymentTitle(),
  75.             'pluginId'              => $pluginId,
  76.             'afterOrderEnabled'     => TRUE,
  77.         ];
  78.         $this->container->get('payment_method.repository')->create([$paymentData], $context);
  79.     }
  80.     private function setPaymentActive(bool $activeContext $contextstring $paymentHandlerClass): void
  81.     {
  82.         $paymentMethodId $this->getPaymentMethodId($context$paymentHandlerClass);
  83.         if (!$paymentMethodId) {
  84.             return;
  85.         }
  86.         $paymentMethod = [
  87.             'id'     => $paymentMethodId,
  88.             'active' => $active,
  89.         ];
  90.         $this->container->get('payment_method.repository')->update([$paymentMethod], $context);
  91.     }
  92.     private function getPaymentMethodId(Context $contextstring $paymentHandlerClass): ?string
  93.     {
  94.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'$paymentHandlerClass));
  95.         return $this->container->get('payment_method.repository')->searchIds($paymentCriteria$context)->firstId();
  96.     }
  97. }
  98. ?>