custom/plugins/WizmoGmbhIvyPayment/src/WizmoGmbhIvyPayment.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) WIZMO GmbH <plugins@shopentwickler.berlin>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace WizmoGmbh\IvyPayment;
  9. use Doctrine\DBAL\Connection;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Plugin;
  15. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  16. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  17. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  18. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  19. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  20. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  21. use Symfony\Component\DependencyInjection\ContainerBuilder;
  22. use WizmoGmbh\IvyPayment\PaymentHandler\IvyPaymentHandler;
  23. use WizmoGmbh\IvyPayment\Setup\DataHolder\Tables;
  24. use WizmoGmbh\IvyPayment\Setup\Uninstaller;
  25. class WizmoGmbhIvyPayment extends Plugin
  26. {
  27.     public function install(InstallContext $context): void
  28.     {
  29.         $this->addPaymentMethod($context->getContext());
  30.     }
  31.     public function activate(ActivateContext $context): void
  32.     {
  33.         $this->setPaymentMethodIsActive(true$context->getContext());
  34.         parent::activate($context);
  35.     }
  36.     public function deactivate(DeactivateContext $context): void
  37.     {
  38.         $this->setPaymentMethodIsActive(false$context->getContext());
  39.         parent::deactivate($context);
  40.     }
  41.     public function uninstall(UninstallContext $context): void
  42.     {
  43.         $tables Tables::$tables;
  44.         $this->getUninstaller()->uninstall($context$tables);
  45.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  46.         // cause data consistency issues, since the payment method might have been used in several orders
  47.         $this->setPaymentMethodIsActive(false$context->getContext());
  48.     }
  49.     private function getUninstaller(): Uninstaller
  50.     {
  51.         return new Uninstaller($this->getConnection());
  52.     }
  53.     private function getConnection(): Connection
  54.     {
  55.         $connection $this->container->get(Connection::class);
  56.         if (!$connection instanceof Connection) {
  57.             throw new \Exception('DBAL connection service not found!');
  58.         }
  59.         return $connection;
  60.     }
  61.     private function addPaymentMethod(Context $context): void
  62.     {
  63.         /** @var EntityRepositoryInterface $paymentRepository */
  64.         $paymentRepository $this->container->get('payment_method.repository');
  65.         /** @var PluginIdProvider $pluginIdProvider */
  66.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  67.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(static::class, $context);
  68.         $paymentMethodExists $this->getPaymentMethodId();
  69.         if ($paymentMethodExists) {
  70.             $ivyPaymentData = [
  71.                 'id' => $paymentMethodExists,
  72.                 'pluginId' => $pluginId,
  73.             ];
  74.             $paymentRepository->update([$ivyPaymentData], $context);
  75.             return;
  76.         }
  77.         $ivyPaymentData = [
  78.             // payment handler will be selected by the identifier
  79.             'handlerIdentifier' => IvyPaymentHandler::class,
  80.             'name' => 'IvyPayment',
  81.             'description' => 'Ivy - Payments with Impact',
  82.             'pluginId' => $pluginId,
  83.         ];
  84.         $paymentRepository->create([$ivyPaymentData], $context);
  85.     }
  86.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  87.     {
  88.         /** @var EntityRepositoryInterface $paymentRepository */
  89.         $paymentRepository $this->container->get('payment_method.repository');
  90.         $paymentMethodId $this->getPaymentMethodId();
  91.         // Payment does not even exist, so nothing to (de-)activate here
  92.         if (!$paymentMethodId) {
  93.             return;
  94.         }
  95.         $paymentMethod = [
  96.             'id' => $paymentMethodId,
  97.             'active' => $active,
  98.         ];
  99.         $paymentRepository->update([$paymentMethod], $context);
  100.     }
  101.     private function getPaymentMethodId(): ?string
  102.     {
  103.         /** @var EntityRepositoryInterface $paymentRepository */
  104.         $paymentRepository $this->container->get('payment_method.repository');
  105.         // Fetch ID for update
  106.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'IvyPaymentHandler::class));
  107.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  108.     }
  109. }