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