custom/plugins/SwagPayPal/src/SwagPayPal.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal;
  8. use Doctrine\DBAL\Connection;
  9. use Shopware\Core\Content\Media\File\FileSaver;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  18. use Shopware\Core\System\CustomField\CustomFieldDefinition;
  19. use Shopware\Core\System\SystemConfig\SystemConfigService;
  20. use Swag\PayPal\Pos\Setting\Service\InformationDefaultService;
  21. use Swag\PayPal\Pos\Webhook\WebhookService as PosWebhookService;
  22. use Swag\PayPal\Util\Lifecycle\ActivateDeactivate;
  23. use Swag\PayPal\Util\Lifecycle\Installer\CurrencyInstaller;
  24. use Swag\PayPal\Util\Lifecycle\Installer\MediaInstaller;
  25. use Swag\PayPal\Util\Lifecycle\Installer\PaymentMethodInstaller;
  26. use Swag\PayPal\Util\Lifecycle\Installer\PosInstaller;
  27. use Swag\PayPal\Util\Lifecycle\Installer\SettingsInstaller;
  28. use Swag\PayPal\Util\Lifecycle\InstallUninstall;
  29. use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  30. use Swag\PayPal\Util\Lifecycle\State\PaymentMethodStateService;
  31. use Swag\PayPal\Util\Lifecycle\Update;
  32. use Swag\PayPal\Webhook\WebhookService;
  33. use Symfony\Component\DependencyInjection\ContainerInterface;
  34. class SwagPayPal extends Plugin
  35. {
  36.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_TRANSACTION_ID 'swag_paypal_transaction_id';
  37.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_TOKEN 'swag_paypal_token';
  38.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_PUI_INSTRUCTION 'swag_paypal_pui_payment_instruction';
  39.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_ORDER_ID 'swag_paypal_order_id';
  40.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_PARTNER_ATTRIBUTION_ID 'swag_paypal_partner_attribution_id';
  41.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_RESOURCE_ID 'swag_paypal_resource_id';
  42.     public const SHIPPING_METHOD_CUSTOM_FIELDS_CARRIER 'swag_paypal_carrier';
  43.     public const SALES_CHANNEL_TYPE_POS '1ce0868f406d47d98cfe4b281e62f099';
  44.     public const SALES_CHANNEL_POS_EXTENSION 'paypalPosSalesChannel';
  45.     public const PRODUCT_LOG_POS_EXTENSION 'paypalPosLog';
  46.     public const PRODUCT_SYNC_POS_EXTENSION 'paypalPosSync';
  47.     public const POS_PARTNER_CLIENT_ID '48804990-9c6d-4579-9c39-eae6d93e5f94';
  48.     public const POS_PARTNER_IDENTIFIER 'shopware';
  49.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ 'swag_paypal_pos_sales_channel:read';
  50.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE 'swag_paypal_pos_sales_channel:update';
  51.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE 'swag_paypal_pos_sales_channel:create';
  52.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE 'swag_paypal_pos_sales_channel:delete';
  53.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ 'swag_paypal_pos_sales_channel_run:read';
  54.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE 'swag_paypal_pos_sales_channel_run:update';
  55.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE 'swag_paypal_pos_sales_channel_run:create';
  56.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE 'swag_paypal_pos_sales_channel_run:delete';
  57.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ 'swag_paypal_pos_sales_channel_run_log:read';
  58.     private ActivateDeactivate $activateDeactivate;
  59.     /**
  60.      * @Required
  61.      */
  62.     public function setActivateDeactivate(ActivateDeactivate $activateDeactivate): void
  63.     {
  64.         $this->activateDeactivate $activateDeactivate;
  65.     }
  66.     public function install(InstallContext $installContext): void
  67.     {
  68.         $this->getInstaller()->install($installContext->getContext());
  69.         parent::install($installContext);
  70.     }
  71.     public function uninstall(UninstallContext $uninstallContext): void
  72.     {
  73.         if (!$uninstallContext->keepUserData()) {
  74.             $this->getInstaller()->uninstall($uninstallContext->getContext());
  75.         }
  76.         parent::uninstall($uninstallContext);
  77.     }
  78.     public function update(UpdateContext $updateContext): void
  79.     {
  80.         /** @var EntityRepositoryInterface $customFieldRepository */
  81.         $customFieldRepository $this->container->get(\sprintf('%s.repository', (new CustomFieldDefinition())->getEntityName()));
  82.         /** @var EntityRepositoryInterface $paymentMethodRepository */
  83.         $paymentMethodRepository $this->container->get('payment_method.repository');
  84.         /** @var WebhookService|null $webhookService */
  85.         $webhookService $this->container->get(WebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  86.         /** @var EntityRepositoryInterface $salesChannelRepository */
  87.         $salesChannelRepository $this->container->get('sales_channel.repository');
  88.         /** @var EntityRepositoryInterface $salesChannelTypeRepository */
  89.         $salesChannelTypeRepository $this->container->get('sales_channel_type.repository');
  90.         /** @var InformationDefaultService|null $informationDefaultService */
  91.         $informationDefaultService $this->container->get(InformationDefaultService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  92.         /** @var EntityRepositoryInterface $shippingRepository */
  93.         $shippingRepository $this->container->get('shipping_method.repository');
  94.         /** @var PosWebhookService|null $posWebhookService */
  95.         $posWebhookService $this->container->get(PosWebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  96.         /** @var EntityRepositoryInterface $ruleRepository */
  97.         $ruleRepository $this->container->get('rule.repository');
  98.         /** @var EntityRepositoryInterface $ruleConditionRepository */
  99.         $ruleConditionRepository $this->container->get('rule_condition.repository');
  100.         /** @var EntityRepositoryInterface $mediaRepository */
  101.         $mediaRepository $this->container->get('media.repository');
  102.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  103.         $mediaFolderRepository $this->container->get('media_folder.repository');
  104.         /** @var EntityRepositoryInterface $currencyRepository */
  105.         $currencyRepository $this->container->get('currency.repository');
  106.         /** @var PaymentMethodInstaller|null $paymentMethodInstaller */
  107.         $paymentMethodInstaller $this->container->get(PaymentMethodInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  108.         /** @var PaymentMethodStateService|null $paymentMethodStateService */
  109.         $paymentMethodStateService $this->container->get(PaymentMethodStateService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  110.         /** @var MediaInstaller|null $mediaInstaller */
  111.         $mediaInstaller $this->container->get(MediaInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  112.         $paymentMethodDataRegistry = new PaymentMethodDataRegistry($paymentMethodRepository$this->container);
  113.         (new Update(
  114.             $this->container->get(SystemConfigService::class),
  115.             $paymentMethodRepository,
  116.             $customFieldRepository,
  117.             $webhookService,
  118.             $salesChannelRepository,
  119.             $salesChannelTypeRepository,
  120.             $informationDefaultService,
  121.             $shippingRepository,
  122.             $posWebhookService,
  123.             $paymentMethodInstaller ?? new PaymentMethodInstaller(
  124.                 $paymentMethodRepository,
  125.                 $ruleRepository,
  126.                 $ruleConditionRepository,
  127.                 $this->container->get(PluginIdProvider::class),
  128.                 $paymentMethodDataRegistry,
  129.                 $mediaInstaller ?? new MediaInstaller(
  130.                     $mediaRepository,
  131.                     $mediaFolderRepository,
  132.                     $paymentMethodRepository,
  133.                     $this->container->get(FileSaver::class),
  134.                 ),
  135.             ),
  136.             $paymentMethodStateService ?? new PaymentMethodStateService(
  137.                 $paymentMethodDataRegistry,
  138.                 $paymentMethodRepository,
  139.             ),
  140.             new CurrencyInstaller($currencyRepository),
  141.         ))->update($updateContext);
  142.         parent::update($updateContext);
  143.     }
  144.     public function activate(ActivateContext $activateContext): void
  145.     {
  146.         $this->activateDeactivate->activate($activateContext->getContext());
  147.         parent::activate($activateContext);
  148.     }
  149.     public function deactivate(DeactivateContext $deactivateContext): void
  150.     {
  151.         $this->activateDeactivate->deactivate($deactivateContext->getContext());
  152.         parent::deactivate($deactivateContext);
  153.     }
  154.     public function enrichPrivileges(): array
  155.     {
  156.         return [
  157.             'sales_channel.viewer' => [
  158.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ,
  159.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ,
  160.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE,
  161.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE,
  162.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ,
  163.                 'sales_channel_payment_method:read',
  164.             ],
  165.             'sales_channel.editor' => [
  166.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE,
  167.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE,
  168.                 'payment_method:update',
  169.             ],
  170.             'sales_channel.creator' => [
  171.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE,
  172.                 'payment_method:create',
  173.                 'shipping_method:create',
  174.                 'delivery_time:create',
  175.             ],
  176.             'sales_channel.deleter' => [
  177.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE,
  178.             ],
  179.         ];
  180.     }
  181.     private function getInstaller(): InstallUninstall
  182.     {
  183.         /** @var EntityRepositoryInterface $systemConfigRepository */
  184.         $systemConfigRepository $this->container->get('system_config.repository');
  185.         /** @var EntityRepositoryInterface $paymentMethodRepository */
  186.         $paymentMethodRepository $this->container->get('payment_method.repository');
  187.         /** @var EntityRepositoryInterface $ruleRepository */
  188.         $ruleRepository $this->container->get('rule.repository');
  189.         /** @var EntityRepositoryInterface $ruleConditionRepository */
  190.         $ruleConditionRepository $this->container->get('rule_condition.repository');
  191.         /** @var EntityRepositoryInterface $mediaRepository */
  192.         $mediaRepository $this->container->get('media.repository');
  193.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  194.         $mediaFolderRepository $this->container->get('media_folder.repository');
  195.         /** @var EntityRepositoryInterface $currencyRepository */
  196.         $currencyRepository $this->container->get('currency.repository');
  197.         return new InstallUninstall(
  198.             new PaymentMethodInstaller(
  199.                 $paymentMethodRepository,
  200.                 $ruleRepository,
  201.                 $ruleConditionRepository,
  202.                 $this->container->get(PluginIdProvider::class),
  203.                 new PaymentMethodDataRegistry(
  204.                     $paymentMethodRepository,
  205.                     $this->container,
  206.                 ),
  207.                 new MediaInstaller(
  208.                     $mediaRepository,
  209.                     $mediaFolderRepository,
  210.                     $paymentMethodRepository,
  211.                     $this->container->get(FileSaver::class)
  212.                 ),
  213.             ),
  214.             new SettingsInstaller($systemConfigRepository$this->container->get(SystemConfigService::class)),
  215.             new PosInstaller($this->container->get(Connection::class)),
  216.             new CurrencyInstaller($currencyRepository),
  217.         );
  218.     }
  219. }