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

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\Checkout\Payment\PaymentMethodDefinition;
  10. use Shopware\Core\Checkout\Shipping\ShippingMethodDefinition;
  11. use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderDefinition;
  12. use Shopware\Core\Content\Media\File\FileSaver;
  13. use Shopware\Core\Content\Media\MediaDefinition;
  14. use Shopware\Core\Content\Rule\RuleDefinition;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\Plugin;
  18. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  19. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  20. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  21. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  22. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  23. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  24. use Shopware\Core\System\CustomField\CustomFieldDefinition;
  25. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelType\SalesChannelTypeDefinition;
  26. use Shopware\Core\System\SalesChannel\SalesChannelDefinition;
  27. use Shopware\Core\System\SystemConfig\SystemConfigDefinition;
  28. use Shopware\Core\System\SystemConfig\SystemConfigService;
  29. use Swag\PayPal\Pos\Setting\Service\InformationDefaultService;
  30. use Swag\PayPal\Pos\Webhook\WebhookService as PosWebhookService;
  31. use Swag\PayPal\Util\Compatibility\EntityRepositoryDecorator;
  32. use Swag\PayPal\Util\Lifecycle\ActivateDeactivate;
  33. use Swag\PayPal\Util\Lifecycle\Installer\MediaInstaller;
  34. use Swag\PayPal\Util\Lifecycle\Installer\PaymentMethodInstaller;
  35. use Swag\PayPal\Util\Lifecycle\Installer\PosInstaller;
  36. use Swag\PayPal\Util\Lifecycle\Installer\SettingsInstaller;
  37. use Swag\PayPal\Util\Lifecycle\InstallUninstall;
  38. use Swag\PayPal\Util\Lifecycle\Method\PaymentMethodDataRegistry;
  39. use Swag\PayPal\Util\Lifecycle\State\PaymentMethodStateService;
  40. use Swag\PayPal\Util\Lifecycle\Update;
  41. use Swag\PayPal\Webhook\WebhookService;
  42. use Symfony\Component\DependencyInjection\ContainerInterface;
  43. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  44. class SwagPayPal extends Plugin
  45. {
  46.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_TRANSACTION_ID 'swag_paypal_transaction_id';
  47.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_TOKEN 'swag_paypal_token';
  48.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_PUI_INSTRUCTION 'swag_paypal_pui_payment_instruction';
  49.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_ORDER_ID 'swag_paypal_order_id';
  50.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_PARTNER_ATTRIBUTION_ID 'swag_paypal_partner_attribution_id';
  51.     public const ORDER_TRANSACTION_CUSTOM_FIELDS_PAYPAL_RESOURCE_ID 'swag_paypal_resource_id';
  52.     public const SHIPPING_METHOD_CUSTOM_FIELDS_CARRIER 'swag_paypal_carrier';
  53.     public const SALES_CHANNEL_TYPE_POS '1ce0868f406d47d98cfe4b281e62f099';
  54.     public const SALES_CHANNEL_POS_EXTENSION 'paypalPosSalesChannel';
  55.     public const PRODUCT_LOG_POS_EXTENSION 'paypalPosLog';
  56.     public const PRODUCT_SYNC_POS_EXTENSION 'paypalPosSync';
  57.     public const POS_PARTNER_CLIENT_ID '48804990-9c6d-4579-9c39-eae6d93e5f94';
  58.     public const POS_PARTNER_IDENTIFIER 'shopware';
  59.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ 'swag_paypal_pos_sales_channel:read';
  60.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE 'swag_paypal_pos_sales_channel:update';
  61.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE 'swag_paypal_pos_sales_channel:create';
  62.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE 'swag_paypal_pos_sales_channel:delete';
  63.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ 'swag_paypal_pos_sales_channel_run:read';
  64.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE 'swag_paypal_pos_sales_channel_run:update';
  65.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE 'swag_paypal_pos_sales_channel_run:create';
  66.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE 'swag_paypal_pos_sales_channel_run:delete';
  67.     private const PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ 'swag_paypal_pos_sales_channel_run_log:read';
  68.     private ActivateDeactivate $activateDeactivate;
  69.     /**
  70.      * @Required
  71.      */
  72.     public function setActivateDeactivate(ActivateDeactivate $activateDeactivate): void
  73.     {
  74.         $this->activateDeactivate $activateDeactivate;
  75.     }
  76.     public function install(InstallContext $installContext): void
  77.     {
  78.         $this->getInstaller()->install($installContext->getContext());
  79.         parent::install($installContext);
  80.     }
  81.     public function uninstall(UninstallContext $uninstallContext): void
  82.     {
  83.         if (!$uninstallContext->keepUserData()) {
  84.             $this->getInstaller()->uninstall($uninstallContext->getContext());
  85.         }
  86.         parent::uninstall($uninstallContext);
  87.     }
  88.     public function update(UpdateContext $updateContext): void
  89.     {
  90.         /** @var WebhookService|null $webhookService */
  91.         $webhookService $this->container->get(WebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  92.         /** @var InformationDefaultService|null $informationDefaultService */
  93.         $informationDefaultService $this->container->get(InformationDefaultService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  94.         /** @var PosWebhookService|null $posWebhookService */
  95.         $posWebhookService $this->container->get(PosWebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  96.         /** @var PaymentMethodInstaller|null $paymentMethodInstaller */
  97.         $paymentMethodInstaller $this->container->get(PaymentMethodInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  98.         /** @var PaymentMethodStateService|null $paymentMethodStateService */
  99.         $paymentMethodStateService $this->container->get(PaymentMethodStateService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  100.         /** @var MediaInstaller|null $mediaInstaller */
  101.         $mediaInstaller $this->container->get(MediaInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  102.         $paymentMethodDataRegistry = new PaymentMethodDataRegistry(
  103.             $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  104.             $this->container
  105.         );
  106.         (new Update(
  107.             $this->container->get(SystemConfigService::class),
  108.             $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  109.             $this->getRepository($this->containerCustomFieldDefinition::ENTITY_NAME),
  110.             $webhookService,
  111.             $this->getRepository($this->containerSalesChannelDefinition::ENTITY_NAME),
  112.             $this->getRepository($this->containerSalesChannelTypeDefinition::ENTITY_NAME),
  113.             $informationDefaultService,
  114.             $this->getRepository($this->containerShippingMethodDefinition::ENTITY_NAME),
  115.             $posWebhookService,
  116.             $paymentMethodInstaller ?? new PaymentMethodInstaller(
  117.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  118.                 $this->getRepository($this->containerRuleDefinition::ENTITY_NAME),
  119.                 $this->container->get(PluginIdProvider::class),
  120.                 $paymentMethodDataRegistry,
  121.                 $mediaInstaller ?? new MediaInstaller(
  122.                     $this->getRepository($this->containerMediaDefinition::ENTITY_NAME),
  123.                     $this->getRepository($this->containerMediaFolderDefinition::ENTITY_NAME),
  124.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  125.                     $this->container->get(FileSaver::class),
  126.                 ),
  127.             ),
  128.             $paymentMethodStateService ?? new PaymentMethodStateService(
  129.                 $paymentMethodDataRegistry,
  130.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  131.             )
  132.         ))->update($updateContext);
  133.         parent::update($updateContext);
  134.     }
  135.     public function activate(ActivateContext $activateContext): void
  136.     {
  137.         $this->activateDeactivate->activate($activateContext->getContext());
  138.         parent::activate($activateContext);
  139.     }
  140.     public function deactivate(DeactivateContext $deactivateContext): void
  141.     {
  142.         $this->activateDeactivate->deactivate($deactivateContext->getContext());
  143.         parent::deactivate($deactivateContext);
  144.     }
  145.     public function enrichPrivileges(): array
  146.     {
  147.         return [
  148.             'sales_channel.viewer' => [
  149.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ,
  150.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ,
  151.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE,
  152.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE,
  153.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ,
  154.                 'sales_channel_payment_method:read',
  155.             ],
  156.             'sales_channel.editor' => [
  157.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE,
  158.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE,
  159.                 'payment_method:update',
  160.             ],
  161.             'sales_channel.creator' => [
  162.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE,
  163.                 'payment_method:create',
  164.                 'shipping_method:create',
  165.                 'delivery_time:create',
  166.             ],
  167.             'sales_channel.deleter' => [
  168.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE,
  169.             ],
  170.         ];
  171.     }
  172.     private function getInstaller(): InstallUninstall
  173.     {
  174.         return new InstallUninstall(
  175.             new PaymentMethodInstaller(
  176.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  177.                 $this->getRepository($this->containerRuleDefinition::ENTITY_NAME),
  178.                 $this->container->get(PluginIdProvider::class),
  179.                 new PaymentMethodDataRegistry(
  180.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  181.                     $this->container,
  182.                 ),
  183.                 new MediaInstaller(
  184.                     $this->getRepository($this->containerMediaDefinition::ENTITY_NAME),
  185.                     $this->getRepository($this->containerMediaFolderDefinition::ENTITY_NAME),
  186.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  187.                     $this->container->get(FileSaver::class)
  188.                 ),
  189.             ),
  190.             new SettingsInstaller(
  191.                 $this->getRepository($this->containerSystemConfigDefinition::ENTITY_NAME),
  192.                 $this->container->get(SystemConfigService::class)
  193.             ),
  194.             new PosInstaller($this->container->get(Connection::class)),
  195.         );
  196.     }
  197.     private function getRepository(ContainerInterface $containerstring $entityName): EntityRepository
  198.     {
  199.         $repository $container->get(\sprintf('%s.repository'$entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
  200.         if (\interface_exists(EntityRepositoryInterface::class) && $repository instanceof EntityRepositoryInterface) {
  201.             return new EntityRepositoryDecorator($repository);
  202.         }
  203.         if (!$repository instanceof EntityRepository) {
  204.             throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  205.         }
  206.         return $repository;
  207.     }
  208. }