vendor/store.shopware.com/adyenpaymentshopware6/src/AdyenPaymentShopware6.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  *                       ######
  4.  *                       ######
  5.  * ############    ####( ######  #####. ######  ############   ############
  6.  * #############  #####( ######  #####. ######  #############  #############
  7.  *        ######  #####( ######  #####. ######  #####  ######  #####  ######
  8.  * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
  9.  * ###### ######  #####( ######  #####. ######  #####          #####  ######
  10.  * #############  #############  #############  #############  #####  ######
  11.  *  ############   ############  #############   ############  #####  ######
  12.  *                                      ######
  13.  *                               #############
  14.  *                               ############
  15.  *
  16.  * Adyen Payment Module
  17.  *
  18.  * Copyright (c) 2020 Adyen B.V.
  19.  * This file is open source and available under the MIT license.
  20.  * See the LICENSE file for more info.
  21.  *
  22.  * Author: Adyen <shopware@adyen.com>
  23.  */
  24. // phpcs:disable PSR1.Files.SideEffects
  25. namespace Adyen\Shopware;
  26. use Adyen\Shopware\Entity\Notification\NotificationEntityDefinition;
  27. use Adyen\Shopware\Entity\PaymentResponse\PaymentResponseEntityDefinition;
  28. use Adyen\Shopware\Entity\PaymentStateData\PaymentStateDataEntityDefinition;
  29. use Adyen\Shopware\Handlers\GenericGiftCardPaymentMethodHandler;
  30. use Adyen\Shopware\PaymentMethods;
  31. use Adyen\Shopware\Service\ConfigurationService;
  32. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  33. use Shopware\Core\Checkout\Payment\SalesChannel\SalesChannelPaymentMethodDefinition;
  34. use Shopware\Core\Framework\Plugin;
  35. use Shopware\Core\Framework\Context;
  36. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  37. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  38. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  39. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  40. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  41. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  42. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  43. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  44. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  45. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  46. use Doctrine\DBAL\Connection;
  47. use Shopware\Core\System\SalesChannel\SalesChannelCollection;
  48. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  49. class AdyenPaymentShopware6 extends Plugin
  50. {
  51.     public function install(InstallContext $installContext): void
  52.     {
  53.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  54.             $this->addPaymentMethod(new $paymentMethod(), $installContext->getContext());
  55.         }
  56.     }
  57.     public function activate(ActivateContext $activateContext): void
  58.     {
  59.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  60.             $this->setPaymentMethodIsActive(true$activateContext->getContext(), new $paymentMethod());
  61.         }
  62.         parent::activate($activateContext);
  63.     }
  64.     public function deactivate(DeactivateContext $deactivateContext): void
  65.     {
  66.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  67.             $this->setPaymentMethodIsActive(false$deactivateContext->getContext(), new $paymentMethod());
  68.         }
  69.         parent::deactivate($deactivateContext);
  70.     }
  71.     public function uninstall(UninstallContext $uninstallContext): void
  72.     {
  73.         parent::uninstall($uninstallContext);
  74.         //Deactivating payment methods
  75.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  76.             $this->setPaymentMethodIsActive(false$uninstallContext->getContext(), new $paymentMethod());
  77.         }
  78.         //Exit here if the user prefers to keep the plugin's data
  79.         if ($uninstallContext->keepUserData()) {
  80.             return;
  81.         }
  82.         $this->removePluginData();
  83.     }
  84.     public function update(UpdateContext $updateContext): void
  85.     {
  86.         $currentVersion $updateContext->getCurrentPluginVersion();
  87.         if (\version_compare($currentVersion'1.2.0''<')) {
  88.             $this->updateTo120($updateContext);
  89.         }
  90.         if (\version_compare($currentVersion'1.4.0''<')) {
  91.             $this->updateTo140($updateContext);
  92.         }
  93.         if (\version_compare($currentVersion'1.6.0''<')) {
  94.             $this->updateTo160($updateContext);
  95.         }
  96.         if (\version_compare($currentVersion'2.0.0''<')) {
  97.             $this->updateTo200($updateContext);
  98.         }
  99.         if (\version_compare($currentVersion'3.0.0''<')) {
  100.             $this->updateTo300($updateContext);
  101.         }
  102.         if (\version_compare($currentVersion'3.1.0''<')) {
  103.             $this->updateTo310($updateContext);
  104.         }
  105.         if (\version_compare($currentVersion'3.2.0''<')) {
  106.             $this->updateTo320($updateContext);
  107.         }
  108.         if (\version_compare($currentVersion'3.5.0''<')) {
  109.             $this->updateTo350($updateContext);
  110.         }
  111.     }
  112.     private function addPaymentMethod(PaymentMethods\PaymentMethodInterface $paymentMethodContext $context): void
  113.     {
  114.         $paymentMethodId $this->getPaymentMethodId($paymentMethod->getPaymentHandler());
  115.         /** @var PluginIdProvider $pluginIdProvider */
  116.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  117.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  118.         // Payment method exists already, set the pluginId
  119.         if ($paymentMethodId) {
  120.             $this->setPluginId($paymentMethodId$pluginId$context);
  121.             return;
  122.         }
  123.         $paymentData = [
  124.             'handlerIdentifier' => $paymentMethod->getPaymentHandler(),
  125.             'name' => $paymentMethod->getName(),
  126.             'description' => $paymentMethod->getDescription(),
  127.             'pluginId' => $pluginId,
  128.             'afterOrderEnabled' => true
  129.         ];
  130.         /** @var EntityRepositoryInterface $paymentRepository */
  131.         $paymentRepository $this->container->get('payment_method.repository');
  132.         $paymentRepository->create([$paymentData], $context);
  133.     }
  134.     private function setPluginId(string $paymentMethodIdstring $pluginIdContext $context): void
  135.     {
  136.         /** @var EntityRepositoryInterface $paymentRepository */
  137.         $paymentRepository $this->container->get('payment_method.repository');
  138.         $paymentMethodData = [
  139.             'id' => $paymentMethodId,
  140.             'pluginId' => $pluginId,
  141.         ];
  142.         $paymentRepository->update([$paymentMethodData], $context);
  143.     }
  144.     private function getPaymentMethodId(string $paymentMethodHandler): ?string
  145.     {
  146.         /** @var EntityRepositoryInterface $paymentRepository */
  147.         $paymentRepository $this->container->get('payment_method.repository');
  148.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter(
  149.             'handlerIdentifier',
  150.             $paymentMethodHandler
  151.         ));
  152.         $paymentIds $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext());
  153.         if ($paymentIds->getTotal() === 0) {
  154.             return null;
  155.         }
  156.         return $paymentIds->getIds()[0];
  157.     }
  158.     private function setPaymentMethodIsActive(
  159.         bool $active,
  160.         Context $context,
  161.         PaymentMethods\PaymentMethodInterface $paymentMethod
  162.     ): void {
  163.         /** @var EntityRepositoryInterface $paymentRepository */
  164.         $paymentRepository $this->container->get('payment_method.repository');
  165.         $paymentMethodId $this->getPaymentMethodId($paymentMethod->getPaymentHandler());
  166.         // Payment does not even exist, so nothing to (de-)activate here
  167.         if (!$paymentMethodId) {
  168.             return;
  169.         }
  170.         $paymentMethodData = [
  171.             'id' => $paymentMethodId,
  172.             'active' => $active,
  173.         ];
  174.         $paymentRepository->update([$paymentMethodData], $context);
  175.     }
  176.     private function removePluginData()
  177.     {
  178.         //Search for config keys that contain the bundle's name
  179.         /** @var EntityRepositoryInterface $systemConfigRepository */
  180.         $systemConfigRepository $this->container->get('system_config.repository');
  181.         $criteria = (new Criteria())
  182.             ->addFilter(
  183.                 new ContainsFilter('configurationKey'ConfigurationService::BUNDLE_NAME '.config')
  184.             );
  185.         $idSearchResult $systemConfigRepository->searchIds($criteriaContext::createDefaultContext());
  186.         //Formatting IDs array and deleting config keys
  187.         $ids \array_map(static function ($id) {
  188.             return ['id' => $id];
  189.         }, $idSearchResult->getIds());
  190.         $systemConfigRepository->delete($idsContext::createDefaultContext());
  191.         //Dropping database tables
  192.         $tables = [
  193.             NotificationEntityDefinition::ENTITY_NAME,
  194.             PaymentStateDataEntityDefinition::ENTITY_NAME,
  195.             PaymentResponseEntityDefinition::ENTITY_NAME
  196.         ];
  197.         $connection $this->container->get(Connection::class);
  198.         foreach ($tables as $table) {
  199.             $connection->executeUpdate(\sprintf('DROP TABLE IF EXISTS `%s`'$table));
  200.         }
  201.         $this->removeMigrations();
  202.     }
  203.     private function updateTo120(UpdateContext $updateContext): void
  204.     {
  205.         //Version 1.2.0 introduces storedPaymentMethod
  206.         $this->addPaymentMethod(
  207.             new PaymentMethods\OneClickPaymentMethod,
  208.             $updateContext->getContext()
  209.         );
  210.         $this->setPaymentMethodIsActive(
  211.             true,
  212.             $updateContext->getContext(),
  213.             new PaymentMethods\OneClickPaymentMethod
  214.         );
  215.     }
  216.     private function updateTo140(UpdateContext $updateContext): void
  217.     {
  218.         //Version 1.4.0 introduces giropay
  219.         $this->addPaymentMethod(
  220.             new PaymentMethods\GiroPayPaymentMethod,
  221.             $updateContext->getContext()
  222.         );
  223.         $this->setPaymentMethodIsActive(
  224.             true,
  225.             $updateContext->getContext(),
  226.             new PaymentMethods\GiroPayPaymentMethod
  227.         );
  228.     }
  229.     private function updateTo160(UpdateContext $updateContext): void
  230.     {
  231.         //Version 1.6.0 introduces applepay, paywithgoogle, dotpay and bancontact
  232.         foreach ([
  233.             new PaymentMethods\ApplePayPaymentMethod,
  234.             new PaymentMethods\GooglePayPaymentMethod,
  235.             new PaymentMethods\DotpayPaymentMethod,
  236.             new PaymentMethods\BancontactCardPaymentMethod
  237.         ] as $method) {
  238.             $this->addPaymentMethod(
  239.                 $method,
  240.                 $updateContext->getContext()
  241.             );
  242.             $this->setPaymentMethodIsActive(
  243.                 true,
  244.                 $updateContext->getContext(),
  245.                 $method
  246.             );
  247.         }
  248.     }
  249.     private function updateTo200(UpdateContext $updateContext): void
  250.     {
  251.         //Version 2.0.0 introduces amazonpay, blik
  252.         foreach ([
  253.             new PaymentMethods\AmazonPayPaymentMethod,
  254.             new PaymentMethods\BlikPaymentMethod,
  255.         ] as $method) {
  256.             $this->addPaymentMethod(
  257.                 $method,
  258.                 $updateContext->getContext()
  259.             );
  260.             $this->setPaymentMethodIsActive(
  261.                 true,
  262.                 $updateContext->getContext(),
  263.                 $method
  264.             );
  265.         }
  266.     }
  267.     private function updateTo300(UpdateContext $updateContext): void
  268.     {
  269.         //Version 3.0.0 introduces the following payment methods
  270.         foreach ([
  271.             new PaymentMethods\AfterpayDefaultPaymentMethod,
  272.             new PaymentMethods\AlipayPaymentMethod,
  273.             new PaymentMethods\AlipayHkPaymentMethod,
  274.             new PaymentMethods\ClearpayPaymentMethod,
  275.             new PaymentMethods\EpsPaymentMethod,
  276.             new PaymentMethods\Facilypay3xPaymentMethod,
  277.             new PaymentMethods\Facilypay4xPaymentMethod,
  278.             new PaymentMethods\Facilypay6xPaymentMethod,
  279.             new PaymentMethods\Facilypay10xPaymentMethod,
  280.             new PaymentMethods\Facilypay12xPaymentMethod,
  281.             new PaymentMethods\PaysafecardPaymentMethod,
  282.             new PaymentMethods\RatepayPaymentMethod,
  283.             new PaymentMethods\RatepayDirectdebitPaymentMethod,
  284.             new PaymentMethods\SwishPaymentMethod,
  285.             new PaymentMethods\TrustlyPaymentMethod,
  286.             new PaymentMethods\TwintPaymentMethod,
  287.         ] as $method) {
  288.             $this->addPaymentMethod(
  289.                 $method,
  290.                 $updateContext->getContext()
  291.             );
  292.             $this->setPaymentMethodIsActive(
  293.                 true,
  294.                 $updateContext->getContext(),
  295.                 $method
  296.             );
  297.         }
  298.     }
  299.     private function updateTo310(UpdateContext $updateContext): void
  300.     {
  301.         //Version 3.1.0 introduces gift cards
  302.         foreach ([
  303.                      new PaymentMethods\GivexGiftCardPaymentMethod,
  304.                      new PaymentMethods\WebshopGiftCardPaymentMethod,
  305.                      new PaymentMethods\KadowereldGiftCardPaymentMethod,
  306.                      new PaymentMethods\TCSTestGiftCardPaymentMethod,
  307.                      new PaymentMethods\AlbelliGiftCardPaymentMethod,
  308.                      new PaymentMethods\BijenkorfGiftCardPaymentMethod,
  309.                      new PaymentMethods\VVVGiftCardPaymentMethod,
  310.                      new PaymentMethods\GallGallGiftCardPaymentMethod,
  311.                      new PaymentMethods\HunkemollerLingerieGiftCardPaymentMethod,
  312.                      new PaymentMethods\BeautyGiftCardPaymentMethod,
  313.                      new PaymentMethods\SVSGiftCardPaymentMethod,
  314.                  ] as $method) {
  315.             $this->addPaymentMethod(
  316.                 $method,
  317.                 $updateContext->getContext()
  318.             );
  319.             $this->setPaymentMethodIsActive(
  320.                 true,
  321.                 $updateContext->getContext(),
  322.                 $method
  323.             );
  324.         }
  325.     }
  326.     /**
  327.      * This update will add FashionCheque and DeCadeaukaart
  328.      * It will also set Savvy to inactive, add it as GenericGiftCard.
  329.      * It will also remove all links of Savvy payment_method in sales_channel_payment_method and recreate these links
  330.      * for the new payment method
  331.      *
  332.      * @param UpdateContext $updateContext
  333.      */
  334.     private function updateTo320(UpdateContext $updateContext): void
  335.     {
  336.         foreach ([
  337.                      new PaymentMethods\FashionChequeGiftCardPaymentMethod(),
  338.                      new PaymentMethods\DeCadeaukaartGiftCardPaymentMethod(),
  339.                      new PaymentMethods\GenericGiftCardPaymentMethod()
  340.                  ] as $method) {
  341.             $this->addPaymentMethod(
  342.                 $method,
  343.                 $updateContext->getContext()
  344.             );
  345.             $this->setPaymentMethodIsActive(
  346.                 true,
  347.                 $updateContext->getContext(),
  348.                 $method
  349.             );
  350.         }
  351.         // Set the Savvy payment method to inactive
  352.         /** @var EntityRepositoryInterface $paymentRepository */
  353.         $paymentRepository $this->container->get('payment_method.repository');
  354.         $salesChannelPaymentRepository $this->container->get('sales_channel_payment_method.repository');
  355.         $savvyPaymentMethodId $this->getPaymentMethodId(
  356.             'Adyen\Shopware\Handlers\SavvyGiftCardPaymentMethodHandler'
  357.         );
  358.         // If savvy payment method is not found, return
  359.         if (!$savvyPaymentMethodId) {
  360.             return;
  361.         }
  362.         $paymentMethodData = [
  363.             'id' => $savvyPaymentMethodId,
  364.             'active' => false
  365.         ];
  366.         // Set the savvy payment method to inactive
  367.         $paymentRepository->update([$paymentMethodData], $updateContext->getContext());
  368.         $criteria = new Criteria();
  369.         $criteria->addFilter(new EqualsFilter('id'$savvyPaymentMethodId));
  370.         $criteria->addAssociation('salesChannels');
  371.         /** @var PaymentMethodEntity $savvyPaymentMethod */
  372.         $savvyPaymentMethod $paymentRepository->search($criteria$updateContext->getContext())->first();
  373.         $genericPaymentMethodId $this->getPaymentMethodId(
  374.             GenericGiftCardPaymentMethodHandler::class
  375.         );
  376.         $salesChannelIds = [];
  377.         $salesChannels $savvyPaymentMethod->getSalesChannels();
  378.         if (count($salesChannels) > 0) {
  379.             /** @var SalesChannelEntity $savvySalesChannel */
  380.             foreach ($savvyPaymentMethod->getSalesChannels() as $savvySalesChannel) {
  381.                 $salesChannelIds[] = ['id' => $savvySalesChannel->getId()];
  382.                 $salesChannelPaymentRepository->delete([
  383.                     [
  384.                         'salesChannelId' => $savvySalesChannel->getId(),
  385.                         'paymentMethodId' => $savvyPaymentMethodId
  386.                     ]
  387.                 ], $updateContext->getContext());
  388.             }
  389.             // Add new Generic giftcard links to the sales channel
  390.             $paymentRepository->update([
  391.                 [
  392.                     'id' => $genericPaymentMethodId,
  393.                     'salesChannels' => $salesChannelIds
  394.                 ]
  395.             ], $updateContext->getContext());
  396.         }
  397.     }
  398.     private function updateTo350(UpdateContext $updateContext): void
  399.     {
  400.         //Version 3.5.0 introduces Bancontact mobile
  401.         foreach ([
  402.                      new PaymentMethods\BancontactMobilePaymentMethod()
  403.                  ] as $method) {
  404.             $this->addPaymentMethod(
  405.                 $method,
  406.                 $updateContext->getContext()
  407.             );
  408.             $this->setPaymentMethodIsActive(
  409.                 true,
  410.                 $updateContext->getContext(),
  411.                 $method
  412.             );
  413.         }
  414.     }
  415. }
  416. if (file_exists(__DIR__ '/../vendor/autoload.php')) {
  417.     require_once __DIR__ '/../vendor/autoload.php';
  418. }