custom/plugins/VRPaymentVRPay/src/VRPay.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace VRPayment\VRPay;
  3. use \Generator;
  4. use Shopware\Core\Checkout\Payment\DataAbstractionLayer\PaymentMethodRepositoryDecorator;
  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\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  14. /**
  15.  * Class VRPay
  16.  * Main plugin class, takes care of (un)installing routines
  17.  *
  18.  * @package VRPayment\VRPay
  19.  */
  20. class VRPay extends Plugin
  21. {
  22.     private static $paymentHandlers  = [
  23.         'GiroPay',
  24.         'Paypal',
  25.         'Paydirekt',
  26.         'CreditCard',
  27.         'DirectDebit',
  28.         'KlarnaSofort',
  29.         'EasyCredit',
  30.     ];
  31.     /*
  32.      * plugin lifecycle methods
  33.      */
  34.     public function install(InstallContext $installContext): void
  35.     {
  36.         parent::install($installContext);
  37.         $this->createPaymentMethods($installContext->getContext());
  38.     }
  39.     public function uninstall(UninstallContext $uninstallContext): void
  40.     {
  41.         $this->updatePaymentMethodsActiveState(false$uninstallContext->getContext());
  42.         parent::uninstall($uninstallContext);
  43.     }
  44.     public function activate(ActivateContext $activateContext): void
  45.     {
  46.         parent::activate($activateContext);
  47.         $this->updatePaymentMethodsActiveState(true$activateContext->getContext());
  48.     }
  49.     public function deactivate(DeactivateContext $deactivateContext): void
  50.     {
  51.         $this->updatePaymentMethodsActiveState(false$deactivateContext->getContext());
  52.         parent::deactivate($deactivateContext);
  53.     }
  54.     /*
  55.      * helpers to create and update payment methods
  56.      */
  57.     /**
  58.      * Creates a payment method for every payment method this plugin provides
  59.      *
  60.      * @param Context $context
  61.      */
  62.     private function createPaymentMethods(Context $context)
  63.     {
  64.         /** @var Plugin\Util\PluginIdProvider $pluginIdProvider */
  65.         $pluginIdProvider $this->container->get(Plugin\Util\PluginIdProvider::class);
  66.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  67.         $methodsToCreate = [];
  68.         foreach($this->enumeratePaymentHandlers() as $paymentHandler)
  69.         {
  70.             if($this->getPaymentMethodId($paymentHandler))
  71.             {
  72.                 continue;
  73.             }
  74.             $methodsToCreate[] = [
  75.                 'handlerIdentifier' => $paymentHandler,
  76.                 'name'              => $paymentHandler::getLabel(),
  77.                 'description'       => $paymentHandler::getDescription(),
  78.                 'pluginId'          => $pluginId,
  79.             ];
  80.         }
  81.         if(!empty($methodsToCreate))
  82.         {
  83.             /** @var EntityRepositoryInterface $paymentRepository */
  84.             /** @var PaymentMethodRepositoryDecorator $paymentRepository */
  85.             $paymentRepository $this->container->get('payment_method.repository');
  86.             $paymentRepository->create($methodsToCreate$context);
  87.         }
  88.     }
  89.     /**
  90.      * Lists all class names of payment handlers this plugin provides
  91.      *
  92.      * @return string[]|Generator
  93.      */
  94.     public function enumeratePaymentHandlers(): Generator
  95.     {
  96.         foreach(self::$paymentHandlers as $handler)
  97.         {
  98.             yield sprintf('VRPayment\VRPay\Service\Payment\%s'$handler);
  99.         }
  100.     }
  101.     /**
  102.      * Finds the id of a payment method based on the current plugin id and the given payment handler class
  103.      *
  104.      * @param string $paymentHandler the class name of the payment handler to find the payment method for
  105.      *
  106.      * @return string|null the id of the according payment method or null if none can be found
  107.      *
  108.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  109.      */
  110.     private function getPaymentMethodId(string $paymentHandler): ?string
  111.     {
  112.         /** @var EntityRepositoryInterface $paymentRepository */
  113.         $paymentRepository $this->container->get('payment_method.repository');
  114.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'$paymentHandler));
  115.         $paymentIds $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext());
  116.         if($paymentIds->getTotal() === 0)
  117.         {
  118.             return null;
  119.         }
  120.         return $paymentIds->getIds()[0];
  121.     }
  122.     /**
  123.      * Updates the active state of all payment methods this plugin provides
  124.      *
  125.      * @param bool $activeState the active state to set
  126.      *
  127.      * @param Context $context
  128.      *
  129.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  130.      */
  131.     private function updatePaymentMethodsActiveState(bool $activeStateContext $context)
  132.     {
  133.         $updates = [];
  134.         foreach($this->enumeratePaymentHandlers() as $paymentHandler)
  135.         {
  136.             $paymentMethodId $this->getPaymentMethodId($paymentHandler);
  137.             if(!$paymentMethodId)
  138.             {
  139.                 continue;
  140.             }
  141.             $updates[] = [
  142.                 'id'     => $paymentMethodId,
  143.                 'active' => $activeState,
  144.             ];
  145.         }
  146.         if(!empty($updates))
  147.         {
  148.             /** @var EntityRepositoryInterface $paymentRepository */
  149.             $paymentRepository $this->container->get('payment_method.repository');
  150.             $paymentRepository->update($updates$context);
  151.         }
  152.     }
  153. }