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 $mediaRepository */
  98.         $mediaRepository $this->container->get('media.repository');
  99.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  100.         $mediaFolderRepository $this->container->get('media_folder.repository');
  101.         /** @var PaymentMethodInstaller|null $paymentMethodInstaller */
  102.         $paymentMethodInstaller $this->container->get(PaymentMethodInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  103.         /** @var PaymentMethodStateService|null $paymentMethodStateService */
  104.         $paymentMethodStateService $this->container->get(PaymentMethodStateService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  105.         /** @var MediaInstaller|null $mediaInstaller */
  106.         $mediaInstaller $this->container->get(MediaInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  107.         $paymentMethodDataRegistry = new PaymentMethodDataRegistry($paymentMethodRepository$this->container);
  108.         (new Update(
  109.             $this->container->get(SystemConfigService::class),
  110.             $paymentMethodRepository,
  111.             $customFieldRepository,
  112.             $webhookService,
  113.             $salesChannelRepository,
  114.             $salesChannelTypeRepository,
  115.             $informationDefaultService,
  116.             $shippingRepository,
  117.             $posWebhookService,
  118.             $paymentMethodInstaller ?? new PaymentMethodInstaller(
  119.                 $paymentMethodRepository,
  120.                 $ruleRepository,
  121.                 $this->container->get(PluginIdProvider::class),
  122.                 $paymentMethodDataRegistry,
  123.                 $mediaInstaller ?? new MediaInstaller(
  124.                     $mediaRepository,
  125.                     $mediaFolderRepository,
  126.                     $paymentMethodRepository,
  127.                     $this->container->get(FileSaver::class),
  128.                 ),
  129.             ),
  130.             $paymentMethodStateService ?? new PaymentMethodStateService(
  131.                 $paymentMethodDataRegistry,
  132.                 $paymentMethodRepository,
  133.             )
  134.         ))->update($updateContext);
  135.         parent::update($updateContext);
  136.     }
  137.     public function activate(ActivateContext $activateContext): void
  138.     {
  139.         $this->activateDeactivate->activate($activateContext->getContext());
  140.         parent::activate($activateContext);
  141.     }
  142.     public function deactivate(DeactivateContext $deactivateContext): void
  143.     {
  144.         $this->activateDeactivate->deactivate($deactivateContext->getContext());
  145.         parent::deactivate($deactivateContext);
  146.     }
  147.     public function enrichPrivileges(): array
  148.     {
  149.         return [
  150.             'sales_channel.viewer' => [
  151.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ,
  152.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ,
  153.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE,
  154.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE,
  155.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ,
  156.                 'sales_channel_payment_method:read',
  157.             ],
  158.             'sales_channel.editor' => [
  159.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE,
  160.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE,
  161.                 'payment_method:update',
  162.             ],
  163.             'sales_channel.creator' => [
  164.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE,
  165.                 'payment_method:create',
  166.                 'shipping_method:create',
  167.                 'delivery_time:create',
  168.             ],
  169.             'sales_channel.deleter' => [
  170.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE,
  171.             ],
  172.         ];
  173.     }
  174.     private function getInstaller(): InstallUninstall
  175.     {
  176.         /** @var EntityRepositoryInterface $systemConfigRepository */
  177.         $systemConfigRepository $this->container->get('system_config.repository');
  178.         /** @var EntityRepositoryInterface $paymentMethodRepository */
  179.         $paymentMethodRepository $this->container->get('payment_method.repository');
  180.         /** @var EntityRepositoryInterface $ruleRepository */
  181.         $ruleRepository $this->container->get('rule.repository');
  182.         /** @var EntityRepositoryInterface $mediaRepository */
  183.         $mediaRepository $this->container->get('media.repository');
  184.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  185.         $mediaFolderRepository $this->container->get('media_folder.repository');
  186.         return new InstallUninstall(
  187.             new PaymentMethodInstaller(
  188.                 $paymentMethodRepository,
  189.                 $ruleRepository,
  190.                 $this->container->get(PluginIdProvider::class),
  191.                 new PaymentMethodDataRegistry(
  192.                     $paymentMethodRepository,
  193.                     $this->container,
  194.                 ),
  195.                 new MediaInstaller(
  196.                     $mediaRepository,
  197.                     $mediaFolderRepository,
  198.                     $paymentMethodRepository,
  199.                     $this->container->get(FileSaver::class)
  200.                 ),
  201.             ),
  202.             new SettingsInstaller($systemConfigRepository$this->container->get(SystemConfigService::class)),
  203.             new PosInstaller($this->container->get(Connection::class)),
  204.         );
  205.     }
  206. }