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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioBudget;
  4. use CioBudget\PaymentHandler\BudgetPayment;
  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.     }
  22.     public function update(UpdateContext $updateContext): void
  23.     {
  24.         $this->addPaymentMethod($updateContext->getContext());
  25.     }
  26.     public function uninstall(UninstallContext $uninstallContext): void
  27.     {
  28.         $this->setPaymentMethodIsActive(false$uninstallContext->getContext());
  29.     }
  30.     public function activate(ActivateContext $context): void
  31.     {
  32.         $this->setPaymentMethodIsActive(true$context->getContext());
  33.         parent::activate($context);
  34.     }
  35.     public function deactivate(DeactivateContext $context): void
  36.     {
  37.         $this->setPaymentMethodIsActive(false$context->getContext());
  38.         parent::deactivate($context);
  39.     }
  40.     private function addPaymentMethod(Context $context): void
  41.     {
  42.         $paymentMethodExists $this->getPaymentMethodId();
  43.         // Payment method exists already, no need to continue here
  44.         if ($paymentMethodExists) {
  45.             return;
  46.         }
  47.         /** @var PluginIdProvider $pluginIdProvider */
  48.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  49.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  50.         $examplePaymentData = [
  51.             // payment handler will be selected by the identifier
  52.             'handlerIdentifier' => BudgetPayment::class,
  53.             'name' => 'Budget',
  54.             'description' => 'Bezahlen mit Budget',
  55.             'pluginId' => $pluginId,
  56.         ];
  57.         /** @var EntityRepositoryInterface $paymentRepository */
  58.         $paymentRepository $this->container->get('payment_method.repository');
  59.         $paymentRepository->create([$examplePaymentData], $context);
  60.     }
  61.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  62.     {
  63.         /** @var EntityRepositoryInterface $paymentRepository */
  64.         $paymentRepository $this->container->get('payment_method.repository');
  65.         $paymentMethodId $this->getPaymentMethodId();
  66.         // Payment does not even exist, so nothing to (de-)activate here
  67.         if (!$paymentMethodId) {
  68.             return;
  69.         }
  70.         $paymentMethod = [
  71.             'id' => $paymentMethodId,
  72.             'active' => $active,
  73.         ];
  74.         $paymentRepository->update([$paymentMethod], $context);
  75.     }
  76.     private function getPaymentMethodId(): ?string
  77.     {
  78.         /** @var EntityRepositoryInterface $paymentRepository */
  79.         $paymentRepository $this->container->get('payment_method.repository');
  80.         // Fetch ID for update
  81.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'BudgetPayment::class));
  82.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  83.     }
  84. }