custom/plugins/AdyenPaymentShopware6/src/AdyenPaymentShopware6.php line 51

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\Framework\Plugin;
  34. use Shopware\Core\Framework\Context;
  35. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  36. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  37. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  38. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  39. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  40. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  41. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  42. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  43. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  44. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  45. use Doctrine\DBAL\Connection;
  46. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  47. use Symfony\Component\DependencyInjection\ContainerBuilder;
  48. class AdyenPaymentShopware6 extends Plugin
  49. {
  50.     public function installJsAssets($shopwareVersion)
  51.     {
  52.         $storefrontAssetPath = __DIR__ . '/Resources/app/storefront/dist/storefront/js/adyen-payment-shopware6.js';
  53.         $adminAssetPath = __DIR__ . '/Resources/public/administration/js/adyen-payment-shopware6.js';
  54.         if (\version_compare($shopwareVersion, '6.5.0.0', '<')) {
  55.             $resultStorefront = $this->safeCopyAsset(
  56.                 __DIR__ . '/Resources/app/storefront/dist/storefront/js/adyen-payment-shopware64.js.dist',
  57.                 $storefrontAssetPath
  58.             );
  59.             $resultAdmin = $this->safeCopyAsset(
  60.                 __DIR__ . '/Resources/public/administration/js/adyen-payment-shopware64.js.dist',
  61.                 $adminAssetPath
  62.             );
  63.         } else {
  64.             $resultStorefront = $this->safeCopyAsset(
  65.                 __DIR__ . '/Resources/app/storefront/dist/storefront/js/adyen-payment-shopware65.js.dist',
  66.                 $storefrontAssetPath
  67.             );
  68.             $resultAdmin = $this->safeCopyAsset(
  69.                 __DIR__ . '/Resources/public/administration/js/adyen-payment-shopware64.js.dist',
  70.                 $adminAssetPath
  71.             );
  72.         }
  73.         if (!$resultStorefront) {
  74.             // @todo: add notice:
  75.             // Unable to install your storefront javascript assets, please run the command `bin/build-storefront.sh`
  76.             // from your Shopware web directory
  77.         }
  78.         if (!$resultAdmin) {
  79.             // @todo: add notice:
  80.             // Unable to install your admin javascript assets, please run the command `bin/build-administration.sh`
  81.             // from your Shopware web directory
  82.         }
  83.     }
  84.     public function install(InstallContext $installContext): void
  85.     {
  86.         $this->installJsAssets($installContext->getCurrentShopwareVersion());
  87.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  88.             $this->addPaymentMethod(new $paymentMethod(), $installContext->getContext());
  89.         }
  90.     }
  91.     public function activate(ActivateContext $activateContext): void
  92.     {
  93.         $this->installJsAssets($activateContext->getCurrentShopwareVersion());
  94.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  95.             $this->setPaymentMethodIsActive(true, $activateContext->getContext(), new $paymentMethod());
  96.         }
  97.         parent::activate($activateContext);
  98.     }
  99.     public function deactivate(DeactivateContext $deactivateContext): void
  100.     {
  101.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  102.             $this->setPaymentMethodIsActive(false, $deactivateContext->getContext(), new $paymentMethod());
  103.         }
  104.         parent::deactivate($deactivateContext);
  105.     }
  106.     public function uninstall(UninstallContext $uninstallContext): void
  107.     {
  108.         parent::uninstall($uninstallContext);
  109.         //Deactivating payment methods
  110.         foreach (PaymentMethods\PaymentMethods::PAYMENT_METHODS as $paymentMethod) {
  111.             $this->setPaymentMethodIsActive(false, $uninstallContext->getContext(), new $paymentMethod());
  112.         }
  113.         //Exit here if the user prefers to keep the plugin's data
  114.         if ($uninstallContext->keepUserData()) {
  115.             return;
  116.         }
  117.         $this->removePluginData();
  118.     }
  119.     public function update(UpdateContext $updateContext): void
  120.     {
  121.         $this->installJsAssets($updateContext->getCurrentShopwareVersion());
  122.         $currentVersion = $updateContext->getCurrentPluginVersion();
  123.         if (\version_compare($currentVersion, '1.2.0', '<')) {
  124.             $this->updateTo120($updateContext);
  125.         }
  126.         if (\version_compare($currentVersion, '1.4.0', '<')) {
  127.             $this->updateTo140($updateContext);
  128.         }
  129.         if (\version_compare($currentVersion, '1.6.0', '<')) {
  130.             $this->updateTo160($updateContext);
  131.         }
  132.         if (\version_compare($currentVersion, '2.0.0', '<')) {
  133.             $this->updateTo200($updateContext);
  134.         }
  135.         if (\version_compare($currentVersion, '3.0.0', '<')) {
  136.             $this->updateTo300($updateContext);
  137.         }
  138.         if (\version_compare($currentVersion, '3.1.0', '<')) {
  139.             $this->updateTo310($updateContext);
  140.         }
  141.         if (\version_compare($currentVersion, '3.2.0', '<')) {
  142.             $this->updateTo320($updateContext);
  143.         }
  144.         if (\version_compare($currentVersion, '3.5.0', '<')) {
  145.             $this->updateTo350($updateContext);
  146.         }
  147.         if (\version_compare($currentVersion, '3.7.0', '<')) {
  148.             $this->updateTo370($updateContext);
  149.         }
  150.         if (\version_compare($currentVersion, '3.10.0', '<')) {
  151.             $this->updateTo3100($updateContext);
  152.         }
  153.     }
  154.     private function addPaymentMethod(PaymentMethods\PaymentMethodInterface $paymentMethod, Context $context): void
  155.     {
  156.         $paymentMethodId = $this->getPaymentMethodId($paymentMethod->getPaymentHandler());
  157.         /** @var PluginIdProvider $pluginIdProvider */
  158.         $pluginIdProvider = $this->container->get(PluginIdProvider::class);
  159.         $pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  160.         // Payment method exists already, set the pluginId
  161.         if ($paymentMethodId) {
  162.             $this->setPluginId($paymentMethodId, $pluginId, $context);
  163.             return;
  164.         }
  165.         $paymentData = [
  166.             'handlerIdentifier' => $paymentMethod->getPaymentHandler(),
  167.             'name' => $paymentMethod->getName(),
  168.             'description' => $paymentMethod->getDescription(),
  169.             'pluginId' => $pluginId,
  170.             'afterOrderEnabled' => true
  171.         ];
  172.         /** @var EntityRepository $paymentRepository */
  173.         $paymentRepository = $this->container->get('payment_method.repository');
  174.         $paymentRepository->create([$paymentData], $context);
  175.     }
  176.     private function setPluginId(string $paymentMethodId, string $pluginId, Context $context): void
  177.     {
  178.         /** @var EntityRepository $paymentRepository */
  179.         $paymentRepository = $this->container->get('payment_method.repository');
  180.         $paymentMethodData = [
  181.             'id' => $paymentMethodId,
  182.             'pluginId' => $pluginId,
  183.         ];
  184.         $paymentRepository->update([$paymentMethodData], $context);
  185.     }
  186.     private function getPaymentMethodId(string $paymentMethodHandler): ?string
  187.     {
  188.         /** @var EntityRepository $paymentRepository */
  189.         $paymentRepository = $this->container->get('payment_method.repository');
  190.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter(
  191.             'handlerIdentifier',
  192.             $paymentMethodHandler
  193.         ));
  194.         $paymentIds = $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext());
  195.         if ($paymentIds->getTotal() === 0) {
  196.             return null;
  197.         }
  198.         return $paymentIds->getIds()[0];
  199.     }
  200.     private function setPaymentMethodIsActive(
  201.         bool $active,
  202.         Context $context,
  203.         PaymentMethods\PaymentMethodInterface $paymentMethod
  204.     ): void {
  205.         /** @var EntityRepository $paymentRepository */
  206.         $paymentRepository = $this->container->get('payment_method.repository');
  207.         $paymentMethodId = $this->getPaymentMethodId($paymentMethod->getPaymentHandler());
  208.         // Payment does not even exist, so nothing to (de-)activate here
  209.         if (!$paymentMethodId) {
  210.             return;
  211.         }
  212.         $paymentMethodData = [
  213.             'id' => $paymentMethodId,
  214.             'active' => $active,
  215.         ];
  216.         $paymentRepository->update([$paymentMethodData], $context);
  217.     }
  218.     private function removePluginData()
  219.     {
  220.         //Search for config keys that contain the bundle's name
  221.         /** @var EntityRepository $systemConfigRepository */
  222.         $systemConfigRepository = $this->container->get('system_config.repository');
  223.         $criteria = (new Criteria())
  224.             ->addFilter(
  225.                 new ContainsFilter('configurationKey', ConfigurationService::BUNDLE_NAME . '.config')
  226.             );
  227.         $idSearchResult = $systemConfigRepository->searchIds($criteria, Context::createDefaultContext());
  228.         //Formatting IDs array and deleting config keys
  229.         $ids = \array_map(static function ($id) {
  230.             return ['id' => $id];
  231.         }, $idSearchResult->getIds());
  232.         $systemConfigRepository->delete($ids, Context::createDefaultContext());
  233.         //Dropping database tables
  234.         $tables = [
  235.             NotificationEntityDefinition::ENTITY_NAME,
  236.             PaymentStateDataEntityDefinition::ENTITY_NAME,
  237.             PaymentResponseEntityDefinition::ENTITY_NAME
  238.         ];
  239.         $connection = $this->container->get(Connection::class);
  240.         foreach ($tables as $table) {
  241.             $connection->executeStatement(\sprintf('DROP TABLE IF EXISTS `%s`', $table));
  242.         }
  243.         $this->removeMigrations();
  244.     }
  245.     private function updateTo120(UpdateContext $updateContext): void
  246.     {
  247.         //Version 1.2.0 introduces storedPaymentMethod
  248.         $this->addPaymentMethod(
  249.             new PaymentMethods\OneClickPaymentMethod,
  250.             $updateContext->getContext()
  251.         );
  252.         $this->setPaymentMethodIsActive(
  253.             true,
  254.             $updateContext->getContext(),
  255.             new PaymentMethods\OneClickPaymentMethod
  256.         );
  257.     }
  258.     private function updateTo140(UpdateContext $updateContext): void
  259.     {
  260.         //Version 1.4.0 introduces giropay
  261.         $this->addPaymentMethod(
  262.             new PaymentMethods\GiroPayPaymentMethod,
  263.             $updateContext->getContext()
  264.         );
  265.         $this->setPaymentMethodIsActive(
  266.             true,
  267.             $updateContext->getContext(),
  268.             new PaymentMethods\GiroPayPaymentMethod
  269.         );
  270.     }
  271.     private function updateTo160(UpdateContext $updateContext): void
  272.     {
  273.         //Version 1.6.0 introduces applepay, paywithgoogle, dotpay and bancontact
  274.         foreach ([
  275.             new PaymentMethods\ApplePayPaymentMethod,
  276.             new PaymentMethods\GooglePayPaymentMethod,
  277.             new PaymentMethods\DotpayPaymentMethod,
  278.             new PaymentMethods\BancontactCardPaymentMethod
  279.         ] as $method) {
  280.             $this->addPaymentMethod(
  281.                 $method,
  282.                 $updateContext->getContext()
  283.             );
  284.             $this->setPaymentMethodIsActive(
  285.                 true,
  286.                 $updateContext->getContext(),
  287.                 $method
  288.             );
  289.         }
  290.     }
  291.     private function updateTo200(UpdateContext $updateContext): void
  292.     {
  293.         //Version 2.0.0 introduces amazonpay, blik
  294.         foreach ([
  295.             new PaymentMethods\AmazonPayPaymentMethod,
  296.             new PaymentMethods\BlikPaymentMethod,
  297.         ] as $method) {
  298.             $this->addPaymentMethod(
  299.                 $method,
  300.                 $updateContext->getContext()
  301.             );
  302.             $this->setPaymentMethodIsActive(
  303.                 true,
  304.                 $updateContext->getContext(),
  305.                 $method
  306.             );
  307.         }
  308.     }
  309.     private function updateTo300(UpdateContext $updateContext): void
  310.     {
  311.         //Version 3.0.0 introduces the following payment methods
  312.         foreach ([
  313.             new PaymentMethods\AfterpayDefaultPaymentMethod,
  314.             new PaymentMethods\AlipayPaymentMethod,
  315.             new PaymentMethods\AlipayHkPaymentMethod,
  316.             new PaymentMethods\ClearpayPaymentMethod,
  317.             new PaymentMethods\EpsPaymentMethod,
  318.             new PaymentMethods\Facilypay3xPaymentMethod,
  319.             new PaymentMethods\Facilypay4xPaymentMethod,
  320.             new PaymentMethods\Facilypay6xPaymentMethod,
  321.             new PaymentMethods\Facilypay10xPaymentMethod,
  322.             new PaymentMethods\Facilypay12xPaymentMethod,
  323.             new PaymentMethods\PaysafecardPaymentMethod,
  324.             new PaymentMethods\RatepayPaymentMethod,
  325.             new PaymentMethods\RatepayDirectdebitPaymentMethod,
  326.             new PaymentMethods\SwishPaymentMethod,
  327.             new PaymentMethods\TrustlyPaymentMethod,
  328.             new PaymentMethods\TwintPaymentMethod,
  329.         ] as $method) {
  330.             $this->addPaymentMethod(
  331.                 $method,
  332.                 $updateContext->getContext()
  333.             );
  334.             $this->setPaymentMethodIsActive(
  335.                 true,
  336.                 $updateContext->getContext(),
  337.                 $method
  338.             );
  339.         }
  340.     }
  341.     private function updateTo310(UpdateContext $updateContext): void
  342.     {
  343.         //Version 3.1.0 introduces gift cards
  344.         foreach ([
  345.                      new PaymentMethods\GivexGiftCardPaymentMethod,
  346.                      new PaymentMethods\WebshopGiftCardPaymentMethod,
  347.                      new PaymentMethods\KadowereldGiftCardPaymentMethod,
  348.                      new PaymentMethods\TCSTestGiftCardPaymentMethod,
  349.                      new PaymentMethods\AlbelliGiftCardPaymentMethod,
  350.                      new PaymentMethods\BijenkorfGiftCardPaymentMethod,
  351.                      new PaymentMethods\VVVGiftCardPaymentMethod,
  352.                      new PaymentMethods\GallGallGiftCardPaymentMethod,
  353.                      new PaymentMethods\HunkemollerLingerieGiftCardPaymentMethod,
  354.                      new PaymentMethods\BeautyGiftCardPaymentMethod,
  355.                      new PaymentMethods\SVSGiftCardPaymentMethod,
  356.                  ] as $method) {
  357.             $this->addPaymentMethod(
  358.                 $method,
  359.                 $updateContext->getContext()
  360.             );
  361.             $this->setPaymentMethodIsActive(
  362.                 true,
  363.                 $updateContext->getContext(),
  364.                 $method
  365.             );
  366.         }
  367.     }
  368.     /**
  369.      * This update will add FashionCheque and DeCadeaukaart
  370.      * It will also set Savvy to inactive, add it as GenericGiftCard.
  371.      * It will also remove all links of Savvy payment_method in sales_channel_payment_method and recreate these links
  372.      * for the new payment method
  373.      *
  374.      * @param UpdateContext $updateContext
  375.      */
  376.     private function updateTo320(UpdateContext $updateContext): void
  377.     {
  378.         foreach ([
  379.                      new PaymentMethods\FashionChequeGiftCardPaymentMethod(),
  380.                      new PaymentMethods\DeCadeaukaartGiftCardPaymentMethod(),
  381.                      new PaymentMethods\GenericGiftCardPaymentMethod()
  382.                  ] as $method) {
  383.             $this->addPaymentMethod(
  384.                 $method,
  385.                 $updateContext->getContext()
  386.             );
  387.             $this->setPaymentMethodIsActive(
  388.                 true,
  389.                 $updateContext->getContext(),
  390.                 $method
  391.             );
  392.         }
  393.         // Set the Savvy payment method to inactive
  394.         /** @var EntityRepository $paymentRepository */
  395.         $paymentRepository = $this->container->get('payment_method.repository');
  396.         $salesChannelPaymentRepository = $this->container->get('sales_channel_payment_method.repository');
  397.         $savvyPaymentMethodId = $this->getPaymentMethodId(
  398.             'Adyen\Shopware\Handlers\SavvyGiftCardPaymentMethodHandler'
  399.         );
  400.         // If savvy payment method is not found, return
  401.         if (!$savvyPaymentMethodId) {
  402.             return;
  403.         }
  404.         $paymentMethodData = [
  405.             'id' => $savvyPaymentMethodId,
  406.             'active' => false
  407.         ];
  408.         // Set the savvy payment method to inactive
  409.         $paymentRepository->update([$paymentMethodData], $updateContext->getContext());
  410.         $criteria = new Criteria();
  411.         $criteria->addFilter(new EqualsFilter('id', $savvyPaymentMethodId));
  412.         $criteria->addAssociation('salesChannels');
  413.         /** @var PaymentMethodEntity $savvyPaymentMethod */
  414.         $savvyPaymentMethod = $paymentRepository->search($criteria, $updateContext->getContext())->first();
  415.         $genericPaymentMethodId = $this->getPaymentMethodId(
  416.             GenericGiftCardPaymentMethodHandler::class
  417.         );
  418.         $salesChannelIds = [];
  419.         $salesChannels = $savvyPaymentMethod->getSalesChannels();
  420.         if (count($salesChannels) > 0) {
  421.             /** @var SalesChannelEntity $savvySalesChannel */
  422.             foreach ($savvyPaymentMethod->getSalesChannels() as $savvySalesChannel) {
  423.                 $salesChannelIds[] = ['id' => $savvySalesChannel->getId()];
  424.                 $salesChannelPaymentRepository->delete([
  425.                     [
  426.                         'salesChannelId' => $savvySalesChannel->getId(),
  427.                         'paymentMethodId' => $savvyPaymentMethodId
  428.                     ]
  429.                 ], $updateContext->getContext());
  430.             }
  431.             // Add new Generic giftcard links to the sales channel
  432.             $paymentRepository->update([
  433.                 [
  434.                     'id' => $genericPaymentMethodId,
  435.                     'salesChannels' => $salesChannelIds
  436.                 ]
  437.             ], $updateContext->getContext());
  438.         }
  439.     }
  440.     private function updateTo350(UpdateContext $updateContext): void
  441.     {
  442.         //Version 3.5.0 introduces Bancontact mobile
  443.         foreach ([
  444.                      new PaymentMethods\BancontactMobilePaymentMethod()
  445.                  ] as $method) {
  446.             $this->addPaymentMethod(
  447.                 $method,
  448.                 $updateContext->getContext()
  449.             );
  450.             $this->setPaymentMethodIsActive(
  451.                 true,
  452.                 $updateContext->getContext(),
  453.                 $method
  454.             );
  455.         }
  456.     }
  457.     private function updateTo370(UpdateContext $updateContext): void
  458.     {
  459.         /*
  460.          * Version 3.7.0 introduces following payment methods.
  461.          * MB Way, Multibanco, WeChat Pay, MobilePay, Vipps, Affirm & PayBright
  462.          */
  463.         foreach ([
  464.             new PaymentMethods\MbwayPaymentMethod(),
  465.             new PaymentMethods\MultibancoPaymentMethod(),
  466.             new PaymentMethods\WechatpayqrPaymentMethod(),
  467.             new PaymentMethods\WechatpaywebPaymentMethod(),
  468.             new PaymentMethods\MobilePayPaymentMethod(),
  469.             new PaymentMethods\VippsPaymentMethod(),
  470.             new PaymentMethods\AffirmPaymentMethod(),
  471.             new PaymentMethods\PayBrightPaymentMethod()
  472.         ] as $method) {
  473.             $this->addPaymentMethod(
  474.                 $method,
  475.                 $updateContext->getContext()
  476.             );
  477.             $this->setPaymentMethodIsActive(
  478.                 true,
  479.                 $updateContext->getContext(),
  480.                 $method
  481.             );
  482.         }
  483.     }
  484.     private function updateTo3100(UpdateContext $updateContext): void
  485.     {
  486.         /*
  487.          * Version 3.10.0 introduces following payment methods.
  488.          * Open Banking / Pay by Bank
  489.          */
  490.         foreach ([
  491.             new PaymentMethods\OpenBankingPaymentMethod(),
  492.         ] as $method) {
  493.             $this->addPaymentMethod(
  494.                 $method,
  495.                 $updateContext->getContext()
  496.             );
  497.             $this->setPaymentMethodIsActive(
  498.                 true,
  499.                 $updateContext->getContext(),
  500.                 $method
  501.             );
  502.         }
  503.     }
  504.     private function safeCopyAsset($source, $destination): bool
  505.     {
  506.         try {
  507.             return copy($source, $destination);
  508.         } catch (\Exception $e) {
  509.             return false;
  510.         }
  511.     }
  512. }
  513. if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
  514.     require_once __DIR__ . '/../vendor/autoload.php';
  515. }