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

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