vendor/store.shopware.com/wexoquickpay/src/WexoQuickpay.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\Quickpay;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  8. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  9. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Wexo\Quickpay\Service\ApplepayPayment;
  14. use Wexo\Quickpay\Service\GooglepayPayment;
  15. use Wexo\Quickpay\Service\PaypalPayment;
  16. use Wexo\Quickpay\Service\QuickpayPayment;
  17. use Wexo\Quickpay\Service\MobilepayPayment;
  18. use Wexo\Quickpay\Service\KlarnaPayment;
  19. use Wexo\Quickpay\Service\SwishPayment;
  20. use Wexo\Quickpay\Service\ViabillPayment;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  23. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  24. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  25. /**
  26.  * Class WexoQuickpay
  27.  * @package Wexo\Quickpay
  28.  */
  29. class WexoQuickpay extends Plugin
  30. {
  31.     public const DEFAULT_PAYMENT_METHODS = [
  32.         'MobilePay' => [
  33.             'handler' => MobilepayPayment::class,
  34.             'description' => 'MobilePay from QuickPay'
  35.         ],
  36.         'Credit Card' => [
  37.             'handler' => QuickpayPayment::class,
  38.             'description' => 'Credit cards from QuickPay'
  39.         ],
  40.         'Klarna' => [
  41.             'handler' => KlarnaPayment::class,
  42.             'description' => 'Klarna from QuickPay'
  43.         ],
  44.         'Viabill' => [
  45.             'handler' => ViabillPayment::class,
  46.             'description' => 'Viabill from QuickPay'
  47.         ],
  48.         'Swish' => [
  49.             'handler' => SwishPayment::class,
  50.             'description' => 'Swish from QuickPay'
  51.         ],
  52.         'Paypal' => [
  53.             'handler' => PaypalPayment::class,
  54.             'description' => 'Paypal from Quickpay'
  55.         ],
  56.         'GooglePay' => [
  57.             'handler' => GooglepayPayment::class,
  58.             'description' => 'GooglePay from Quickpay'
  59.         ],
  60.         'ApplePay' => [
  61.             'handler' => ApplepayPayment::class,
  62.             'description' => 'ApplePay from Quickpay'
  63.         ]
  64.     ];
  65.     public const QUICKPAY_FIELD_SET 'quickpay';
  66.     public const QUICKPAY_RESPONSE_FIELD 'quickpay_response';
  67.     public const QUICKPAY_SUBSCRIPTION_ID 'quickpay_subscription_id';
  68.     public const LOG_CHANNEL 'quickpay';
  69.     public const ORDER_CREATE_SUCCESS 'quickpay.order.create.success';
  70.     public const ORDER_CREATE_ERROR 'quickpay.order.create.error';
  71.     public const ORDER_COMPLETE_SUCCESS 'quickpay.order.finalize.success';
  72.     public const ORDER_COMPLETE_ERROR 'quickpay.order.finalize.error';
  73.     public const ORDER_CANCEL_ERROR 'quickpay.order.cancel.error';
  74.     /**
  75.      * @param InstallContext $installContext
  76.      */
  77.     public function install(InstallContext $installContext): void
  78.     {
  79.         parent::install($installContext);
  80.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  81.         $criteria = new Criteria();
  82.         $criteria->addFilter(new EqualsFilter('name'self::QUICKPAY_FIELD_SET));
  83.         /** @var CustomFieldSetEntity $customFieldSet */
  84.         $customFieldSet $customFieldSetRepository->search(
  85.             $criteria,
  86.             $installContext->getContext()
  87.         )->first();
  88.         if (! $customFieldSet) {
  89.             $customFieldSetRepository->upsert([[
  90.                 'name' => self::QUICKPAY_FIELD_SET,
  91.                 'customFields' => [
  92.                     [
  93.                         'name' => self::QUICKPAY_RESPONSE_FIELD,
  94.                         'type' => CustomFieldTypes::JSON,
  95.                         'config' => [
  96.                             'label' => [
  97.                                 'da-DK' => 'QuickPay svar',
  98.                                 'en-GB' => 'QuickPay response',
  99.                                 'de-DE' => 'QuickPay-Antwort',
  100.                             ]
  101.                         ]
  102.                     ]
  103.                 ],
  104.                 'config' => [
  105.                     'label' => [
  106.                         'da-DK' => 'QuickPay',
  107.                         'en-GB' => 'QuickPay',
  108.                         'de-DE' => 'QuickPay',
  109.                     ]
  110.                 ],
  111.                 'relations' => [
  112.                     [
  113.                         'entityName' => 'order',
  114.                     ],
  115.                 ],
  116.             ]], $installContext->getContext());
  117.         }
  118.         $this->addPaymentMethods($installContext->getContext());
  119.     }
  120.     /**
  121.      * Only set the payment method to inactive when uninstalling.  Removing the payment method would
  122.      * cause data consistency issues, since the payment method might have been used in several orders
  123.      *
  124.      * @param UninstallContext $context
  125.      */
  126.     public function uninstall(UninstallContext $context): void
  127.     {
  128.         parent::uninstall($context);
  129.         foreach (self::DEFAULT_PAYMENT_METHODS as $props) {
  130.             $paymentMethodId $this->getPaymentMethodId($props['handler']);
  131.             $this->setPaymentMethodIsActive(false$context->getContext(), $paymentMethodId);
  132.         }
  133.     }
  134.     public function update(UpdateContext $context): void
  135.     {
  136.         if (version_compare($context->getCurrentPluginVersion(), '3.0.3''<')) {
  137.             $oldMobilePayId $this->getPaymentMethodIdByName(QuickpayPayment::class, 'MobilePay');
  138.             if ($oldMobilePayId) {
  139.                 $paymentRepository $this->container->get('payment_method.repository');
  140.                 $paymentMethod = [
  141.                     'id' => $oldMobilePayId,
  142.                     'handlerIdentifier' => MobilepayPayment::class,
  143.                 ];
  144.                 $paymentRepository->update([$paymentMethod], Context::createDefaultContext());
  145.             }
  146.         }
  147.         if (version_compare($context->getCurrentPluginVersion(), '6.0.0''>')) {
  148.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  149.             $criteria = new Criteria();
  150.             $criteria->addFilter(new EqualsFilter('name'self::QUICKPAY_FIELD_SET));
  151.             /** @var CustomFieldSetEntity $customFieldSet */
  152.             $customFieldSet $customFieldSetRepository->search(
  153.                 $criteria,
  154.                 $context->getContext()
  155.             )->first();
  156.             if ($customFieldSet) {
  157.                 $customFieldSetRepository->upsert([
  158.                     [
  159.                         'id'           => $customFieldSet->getId(),
  160.                         'customFields' => [
  161.                             [
  162.                                 'name'   => self::QUICKPAY_SUBSCRIPTION_ID,
  163.                                 'type'   => CustomFieldTypes::TEXT,
  164.                                 'config' => [
  165.                                     'label'               => 'Subscription ID',
  166.                                     'componentName'       => 'sw-field',
  167.                                     'customFieldType'     => CustomFieldTypes::TEXT,
  168.                                     'customFieldPosition' => 2,
  169.                                 ],
  170.                             ]
  171.                         ]
  172.                     ]
  173.                 ], $context->getContext());
  174.             }
  175.         }
  176.         $this->addPaymentMethods(Context::createDefaultContext());
  177.     }
  178.     /**
  179.      * @param ActivateContext $context
  180.      */
  181.     public function activate(ActivateContext $context): void
  182.     {
  183.         foreach (self::DEFAULT_PAYMENT_METHODS as $props) {
  184.             $paymentMethodId $this->getPaymentMethodId($props['handler']);
  185.             $this->setPaymentMethodIsActive(true$context->getContext(), $paymentMethodId);
  186.         }
  187.         parent::activate($context);
  188.     }
  189.     /**
  190.      * @param DeactivateContext $context
  191.      */
  192.     public function deactivate(DeactivateContext $context): void
  193.     {
  194.         foreach (self::DEFAULT_PAYMENT_METHODS as $props) {
  195.             $paymentMethodId $this->getPaymentMethodId($props['handler']);
  196.             $this->setPaymentMethodIsActive(false$context->getContext(), $paymentMethodId);
  197.         }
  198.         parent::deactivate($context);
  199.     }
  200.     /**
  201.      * @param Context $context
  202.      */
  203.     private function addPaymentMethods(Context $context): void
  204.     {
  205.         $paymentRepository $this->container->get('payment_method.repository');
  206.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  207.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(WexoQuickpay::class, $context);
  208.         foreach (self::DEFAULT_PAYMENT_METHODS as $name => $props) {
  209.             $paymentMethodExists $this->getPaymentMethodId($props['handler']);
  210.             // Payment method exists already, no need to continue here
  211.             if ($paymentMethodExists) {
  212.                 continue;
  213.             }
  214.             $paymentMethodData = [
  215.                 'handlerIdentifier' => $props['handler'],
  216.                 'name' => $name,
  217.                 'description' => $props['description'],
  218.                 'pluginId' => $pluginId,
  219.             ];
  220.             $paymentRepository->upsert([$paymentMethodData], $context);
  221.         }
  222.     }
  223.     /**
  224.      * @param bool $active
  225.      * @param Context $context
  226.      * @param $paymentMethodId
  227.      */
  228.     private function setPaymentMethodIsActive(bool $activeContext $context$paymentMethodId): void
  229.     {
  230.         /** @var EntityRepositoryInterface $paymentRepository */
  231.         $paymentRepository $this->container->get('payment_method.repository');
  232.         // Payment does not even exist, so nothing to (de-)activate here
  233.         if (!$paymentMethodId) {
  234.             return;
  235.         }
  236.         $paymentMethod = [
  237.             'id' => $paymentMethodId,
  238.             'active' => $active,
  239.         ];
  240.         $paymentRepository->update([$paymentMethod], $context);
  241.     }
  242.     /**
  243.      * @param $identifier
  244.      * @return string|null
  245.      */
  246.     private function getPaymentMethodId($identifier): ?string
  247.     {
  248.         /** @var EntityRepositoryInterface $paymentRepository */
  249.         $paymentRepository $this->container->get('payment_method.repository');
  250.         // Fetch ID for update
  251.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'$identifier));
  252.         $paymentId $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  253.         if (empty($paymentId)) {
  254.             return null;
  255.         }
  256.         return $paymentId;
  257.     }
  258.     private function getPaymentMethodIdByName($identifier$name): ?string
  259.     {
  260.         /** @var EntityRepositoryInterface $paymentRepository */
  261.         $paymentRepository $this->container->get('payment_method.repository');
  262.         // Fetch ID for update
  263.         $paymentCriteria = (new Criteria())
  264.             ->addFilter(new EqualsFilter('handlerIdentifier'$identifier))
  265.             ->addFilter(new ContainsFilter('name'$name));
  266.         $paymentId $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  267.         if (empty($paymentId)) {
  268.             return null;
  269.         }
  270.         return $paymentId;
  271.     }
  272. }