custom/static-plugins/WabsAuth/src/WabsAuth.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WabsAuth;
  3. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  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\Context\UpdateContext;
  14. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  15. use WabsAuth\Service\CostCenterPayment;
  16. use WabsAuth\Service\WabsInvoicePayment;
  17. class WabsAuth extends Plugin
  18. {
  19.     public function install(InstallContext $context): void
  20.     {
  21.         $this->addCostCenterPaymentMethod($context->getContext());
  22.         $this->updateInvoicePaymentMethod($context->getContext());
  23.     }
  24.     public function uninstall(UninstallContext $context): void
  25.     {
  26.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  27.         // cause data consistency issues, since the payment method might have been used in several orders
  28.         $this->setCostCenterPaymentMethodIsActive(false$context->getContext());
  29.         $this->setInvoicePaymentMethodIsActive(false$context->getContext());
  30.     }
  31.     public function activate(ActivateContext $activateContext): void
  32.     {
  33.         $this->setCostCenterPaymentMethodIsActive(true$activateContext->getContext());
  34.         $this->setInvoicePaymentMethodIsActive(true$activateContext->getContext());
  35.         parent::activate($activateContext);
  36.         $activateContext->setAutoMigrate(true);
  37.     }
  38.     public function update(UpdateContext $updateContext): void
  39.     {
  40.         $this->addCostCenterPaymentMethod($updateContext->getContext());
  41.         $this->updateInvoicePaymentMethod($updateContext->getContext());
  42.         parent::update($updateContext);
  43.         $updateContext->setAutoMigrate(true);
  44.     }
  45.     public function deactivate(DeactivateContext $context): void
  46.     {
  47.         $this->setCostCenterPaymentMethodIsActive(false$context->getContext());
  48.         $this->setInvoicePaymentMethodIsActive(false$context->getContext());
  49.         parent::deactivate($context);
  50.     }
  51.     private function addCostCenterPaymentMethod(Context $context): void
  52.     {
  53.         $paymentMethodExists $this->getCostCenterPaymentMethodId();
  54.         // Payment method exists already, no need to continue here
  55.         if ($paymentMethodExists) {
  56.             return;
  57.         }
  58.         /** @var PluginIdProvider $pluginIdProvider */
  59.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  60.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  61.         $costCenterPayment = [
  62.             // payment handler will be selected by the identifier
  63.             'handlerIdentifier' => CostCenterPayment::class,
  64.             'name' => 'Kostenstelle',
  65.             'description' => 'Bezahlmethode Kostenstelle für Hauptamtliche Kunden',
  66.             'pluginId' => $pluginId,
  67.             'customFields' => [
  68.                 'wabsauth_template' => '@Storefront/storefront/wabsauth/cost-center-payment.html.twig',
  69.                 'cost_center_payment' => true,
  70.             ],
  71.         ];
  72.         /** @var EntityRepositoryInterface $paymentRepository */
  73.         $paymentRepository $this->container->get('payment_method.repository');
  74.         $paymentRepository->create([$costCenterPayment], $context);
  75.     }
  76.     private function setCostCenterPaymentMethodIsActive(bool $activeContext $context): void
  77.     {
  78.         /** @var EntityRepositoryInterface $paymentRepository */
  79.         $paymentRepository $this->container->get('payment_method.repository');
  80.         $paymentMethodId $this->getCostCenterPaymentMethodId();
  81.         // Payment does not even exist, so nothing to (de-)activate here
  82.         if (!$paymentMethodId) {
  83.             return;
  84.         }
  85.         $paymentMethod = [
  86.             'id' => $paymentMethodId,
  87.             'active' => $active,
  88.         ];
  89.         $paymentRepository->update([$paymentMethod], $context);
  90.     }
  91.     private function setInvoicePaymentMethodIsActive(bool $activeContext $context): void
  92.     {
  93.         /** @var EntityRepositoryInterface $paymentRepository */
  94.         $paymentRepository $this->container->get('payment_method.repository');
  95.         $paymentMethodId $this->getInvoicePaymentMethodId();
  96.         // Payment does not even exist, so nothing to (de-)activate here
  97.         if (!$paymentMethodId) {
  98.             return;
  99.         }
  100.         $paymentMethod = [
  101.             'id' => $paymentMethodId,
  102.             'active' => $active,
  103.         ];
  104.         $paymentRepository->update([$paymentMethod], $context);
  105.     }
  106.     private function getCostCenterPaymentMethodId(): ?string
  107.     {
  108.         /** @var EntityRepositoryInterface $paymentRepository */
  109.         $paymentRepository $this->container->get('payment_method.repository');
  110.         // Fetch ID for update
  111.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'CostCenterPayment::class));
  112.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  113.     }
  114.     private function getInvoicePaymentMethodId(): ?string
  115.     {
  116.         /** @var EntityRepositoryInterface $paymentRepository */
  117.         $paymentRepository $this->container->get('payment_method.repository');
  118.         // Fetch ID for update
  119.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier',
  120.             WabsInvoicePayment::class));
  121.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  122.     }
  123.     private function updateInvoicePaymentMethod(Context $context)
  124.     {
  125.         /** @var EntityRepositoryInterface $paymentRepository */
  126.         $paymentRepository $this->container->get('payment_method.repository');
  127.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('name''Rechnung'));
  128.         $invoicePaymentId $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  129.         if (!$invoicePaymentId) {
  130.             return;
  131.         }
  132.         /** @var PluginIdProvider $pluginIdProvider */
  133.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  134.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  135.         $paymentRepository->update([
  136.             [
  137.                 'id' => $invoicePaymentId,
  138.                 'handlerIdentifier' => WabsInvoicePayment::class,
  139.                 'pluginId' => $pluginId,
  140.             ]
  141.         ], $context);
  142.     }
  143. }