custom/plugins/PevrPayeverIntegration/src/PevrPayeverIntegration.php line 56

Open in your IDE?
  1. <?php
  2. /**
  3.  * payever GmbH
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * DISCLAIMER
  26.  *
  27.  * Do not edit or add to this file if you wish to upgrade payever Shopware package
  28.  * to newer versions in the future.
  29.  *
  30.  * @category    Payever
  31.  * @author      payever GmbH <service@payever.de>
  32.  * @copyright   Copyright (c) 2021 payever GmbH (http://www.payever.de)
  33.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  34.  */
  35. declare(strict_types=1);
  36. namespace Payever\PayeverPayments;
  37. // phpcs:disable PSR1.Files.SideEffects
  38. require_once __DIR__ '/../vendor/autoload.php';
  39. // phpcs:enable PSR1.Files.SideEffects
  40. use Payever\Sdk\Plugins\PluginsApiClient;
  41. use Shopware\Core\Framework\Context;
  42. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  43. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  44. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  45. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  46. use Shopware\Core\Framework\Plugin;
  47. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  48. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  49. use Shopware\Core\System\CustomField\CustomFieldTypes;
  50. class PevrPayeverIntegration extends Plugin
  51. {
  52.     public const PLUGIN_NAME 'PevrPayeverIntegration';
  53.     public const CUSTOM_FIELD_TRANSACTION_ID 'payever_transaction_id';
  54.     public const CUSTOM_FIELD_TRANSACTION_AMOUNT 'payever_transaction_amount';
  55.     public const CUSTOM_FIELD_PAN_ID 'payever_pan_id';
  56.     public const CUSTOM_FIELD_NOTIFICATION_TIMESTAMP 'payever_notification_timestamp';
  57.     public const CUSTOM_FIELD_ACCEPT_FEE 'payever_accept_fee';
  58.     public const CUSTOM_FIELD_FIXED_FEE 'payever_fixed_fee';
  59.     public const CUSTOM_FIELD_VARIABLE_FEE 'payever_variable_fee';
  60.     public const CUSTOM_FIELD_METHOD_CODE 'payever_method_code';
  61.     public const CUSTOM_FIELD_VARIANT_ID 'payever_variant_id';
  62.     public const CUSTOM_FIELD_IS_REDIRECT_METHOD 'payever_is_redirect_method';
  63.     /**
  64.      * @var array[]
  65.      */
  66.     private $customFields = [
  67.         [
  68.             'name' => self::CUSTOM_FIELD_METHOD_CODE,
  69.             'type' => CustomFieldTypes::TEXT,
  70.         ],
  71.         [
  72.             'name' => self::CUSTOM_FIELD_VARIANT_ID,
  73.             'type' => CustomFieldTypes::TEXT,
  74.         ],
  75.         [
  76.             'name' => self::CUSTOM_FIELD_TRANSACTION_ID,
  77.             'type' => CustomFieldTypes::TEXT,
  78.         ],
  79.         [
  80.             'name' => self::CUSTOM_FIELD_PAN_ID,
  81.             'type' => CustomFieldTypes::TEXT,
  82.         ],
  83.         [
  84.             'name' => self::CUSTOM_FIELD_NOTIFICATION_TIMESTAMP,
  85.             'type' => CustomFieldTypes::INT,
  86.         ],
  87.         [
  88.             'name' => self::CUSTOM_FIELD_ACCEPT_FEE,
  89.             'type' => CustomFieldTypes::BOOL,
  90.         ],
  91.         [
  92.             'name' => self::CUSTOM_FIELD_FIXED_FEE,
  93.             'type' => CustomFieldTypes::FLOAT,
  94.         ],
  95.         [
  96.             'name' => self::CUSTOM_FIELD_VARIABLE_FEE,
  97.             'type' => CustomFieldTypes::FLOAT,
  98.         ],
  99.         [
  100.             'name' => self::CUSTOM_FIELD_TRANSACTION_AMOUNT,
  101.             'type' => CustomFieldTypes::FLOAT,
  102.         ],
  103.         [
  104.             'name' => self::CUSTOM_FIELD_IS_REDIRECT_METHOD,
  105.             'type' => CustomFieldTypes::BOOL,
  106.         ],
  107.     ];
  108.     /**
  109.      * @param ActivateContext $context
  110.      */
  111.     public function activate(ActivateContext $context): void
  112.     {
  113.         $shopwareContext $context->getContext();
  114.         $this->activateOrderTransactionCustomField($shopwareContext);
  115.         $this->registerPlugin();
  116.         parent::activate($context);
  117.     }
  118.     /**
  119.      * @param DeactivateContext $context
  120.      */
  121.     public function deactivate(DeactivateContext $context): void
  122.     {
  123.         $shopwareContext $context->getContext();
  124.         $this->deactivateOrderTransactionCustomField($shopwareContext);
  125.         $this->unregisterPlugin();
  126.         parent::deactivate($context);
  127.     }
  128.     /**
  129.      * Registers plugin
  130.      */
  131.     private function registerPlugin(): void
  132.     {
  133.         try {
  134.             if ($this->container->has(PluginsApiClient::class)) {
  135.                 /** @var PluginsApiClient $pluginsApiClient */
  136.                 $pluginsApiClient $this->container->get(PluginsApiClient::class);
  137.                 $pluginsApiClient->registerPlugin();
  138.             }
  139.         } catch (\Exception $exception) {
  140.             // silent
  141.         }
  142.     }
  143.     /**
  144.      * Unregisters plugin
  145.      */
  146.     private function unregisterPlugin(): void
  147.     {
  148.         try {
  149.             if ($this->container->has(PluginsApiClient::class)) {
  150.                 /** @var PluginsApiClient $pluginsApiClient */
  151.                 $pluginsApiClient $this->container->get(PluginsApiClient::class);
  152.                 $pluginsApiClient->unregisterPlugin();
  153.             }
  154.         } catch (\Exception $exception) {
  155.             // silent
  156.         }
  157.     }
  158.     /**
  159.      * @param Context $context
  160.      */
  161.     private function activateOrderTransactionCustomField(Context $context): void
  162.     {
  163.         /** @var EntityRepository $customFieldRepository */
  164.         $customFieldRepository $this->container->get('custom_field.repository');
  165.         $customFieldRepository->upsert($this->customFields$context);
  166.     }
  167.     /**
  168.      * @param Context $context
  169.      */
  170.     private function deactivateOrderTransactionCustomField(Context $context): void
  171.     {
  172.         /** @var EntityRepository $customFieldRepository */
  173.         $customFieldRepository $this->container->get('custom_field.repository');
  174.         $ids = [];
  175.         foreach ($this->customFields as $customField) {
  176.             $customFieldId $this->getCustomFieldId($customField['name'], $customFieldRepository$context);
  177.             if ($customFieldId->firstId()) {
  178.                 $ids[] = ['id' => $customFieldId->firstId()];
  179.             }
  180.         }
  181.         if (!count($ids)) {
  182.             return;
  183.         }
  184.         $customFieldRepository->delete($ids$context);
  185.     }
  186.     /**
  187.      * @param string $customFieldName
  188.      * @param EntityRepository $customFieldRepository
  189.      * @param Context $context
  190.      * @return IdSearchResult
  191.      */
  192.     private function getCustomFieldId(
  193.         string $customFieldName,
  194.         EntityRepository $customFieldRepository,
  195.         Context $context
  196.     ): IdSearchResult {
  197.         $criteria = new Criteria();
  198.         $criteria->addFilter(new EqualsFilter('name'$customFieldName));
  199.         return $customFieldRepository->searchIds($criteria$context);
  200.     }
  201. }