custom/plugins/CioBudget/src/CioBudget.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioBudget;
  3. use CioBudget\PaymentHandler\BudgetPayment;
  4. use CioBudget\PaymentHandler\ZeroPayment;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  16. class CioBudget extends Plugin
  17. {
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         $this->addPaymentMethod($installContext->getContext());
  21.         $this->addZeroPaymentMethod($installContext->getContext());
  22.     }
  23.     public function update(UpdateContext $updateContext): void
  24.     {
  25.         $this->addZeroPaymentMethod($updateContext->getContext());
  26.     }
  27.     public function uninstall(UninstallContext $uninstallContext): void
  28.     {
  29.         $this->setPaymentMethodIsActive(false$uninstallContext->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.     private function addZeroPaymentMethod(Context $context): void
  42.     {
  43.         $paymentMethodExists $this->getZeroPaymentMethodId();
  44.         // Payment method exists already, no need to continue here
  45.         if ($paymentMethodExists) {
  46.             return;
  47.         }
  48.         /** @var PluginIdProvider $pluginIdProvider */
  49.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  50.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  51.         $examplePaymentData = [
  52.             // payment handler will be selected by the identifier
  53.             'handlerIdentifier' => ZeroPayment::class,
  54.             'name' => 'Keine',
  55.             'description' => 'Keine Zahlung erforderlich',
  56.             'pluginId' => $pluginId,
  57.         ];
  58.         /** @var EntityRepositoryInterface $paymentRepository */
  59.         $paymentRepository $this->container->get('payment_method.repository');
  60.         $paymentRepository->create([$examplePaymentData], $context);
  61.     }
  62.     private function getZeroPaymentMethodId(): ?string
  63.     {
  64.         /** @var EntityRepositoryInterface $paymentRepository */
  65.         $paymentRepository $this->container->get('payment_method.repository');
  66.         // Fetch ID for update
  67.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'ZeroPayment::class));
  68.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  69.     }
  70.     private function addPaymentMethod(Context $context): void
  71.     {
  72.         $paymentMethodExists $this->getPaymentMethodId();
  73.         // Payment method exists already, no need to continue here
  74.         if ($paymentMethodExists) {
  75.             return;
  76.         }
  77.         /** @var PluginIdProvider $pluginIdProvider */
  78.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  79.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  80.         $examplePaymentData = [
  81.             // payment handler will be selected by the identifier
  82.             'handlerIdentifier' => BudgetPayment::class,
  83.             'name' => 'Budget',
  84.             'description' => 'Bezahlen mit Budget',
  85.             'pluginId' => $pluginId,
  86.         ];
  87.         /** @var EntityRepositoryInterface $paymentRepository */
  88.         $paymentRepository $this->container->get('payment_method.repository');
  89.         $paymentRepository->create([$examplePaymentData], $context);
  90.     }
  91.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  92.     {
  93.         /** @var EntityRepositoryInterface $paymentRepository */
  94.         $paymentRepository $this->container->get('payment_method.repository');
  95.         $paymentMethodId $this->getPaymentMethodId();
  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 getPaymentMethodId(): ?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'BudgetPayment::class));
  112.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  113.     }
  114. }