vendor/store.shopware.com/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.         \assert($this->container instanceof ContainerInterface'Container is not set yet, please call setContainer() before calling boot(), see `platform/Core/Kernel.php:186`.');
  91.         /** @var WebhookService|null $webhookService */
  92.         $webhookService $this->container->get(WebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  93.         /** @var InformationDefaultService|null $informationDefaultService */
  94.         $informationDefaultService $this->container->get(InformationDefaultService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  95.         /** @var PosWebhookService|null $posWebhookService */
  96.         $posWebhookService $this->container->get(PosWebhookService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  97.         /** @var PaymentMethodInstaller|null $paymentMethodInstaller */
  98.         $paymentMethodInstaller $this->container->get(PaymentMethodInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  99.         /** @var PaymentMethodStateService|null $paymentMethodStateService */
  100.         $paymentMethodStateService $this->container->get(PaymentMethodStateService::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  101.         /** @var MediaInstaller|null $mediaInstaller */
  102.         $mediaInstaller $this->container->get(MediaInstaller::class, ContainerInterface::NULL_ON_INVALID_REFERENCE);
  103.         $paymentMethodDataRegistry = new PaymentMethodDataRegistry(
  104.             $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  105.             $this->container
  106.         );
  107.         (new Update(
  108.             $this->container->get(SystemConfigService::class),
  109.             $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  110.             $this->getRepository($this->containerCustomFieldDefinition::ENTITY_NAME),
  111.             $webhookService,
  112.             $this->getRepository($this->containerSalesChannelDefinition::ENTITY_NAME),
  113.             $this->getRepository($this->containerSalesChannelTypeDefinition::ENTITY_NAME),
  114.             $informationDefaultService,
  115.             $this->getRepository($this->containerShippingMethodDefinition::ENTITY_NAME),
  116.             $posWebhookService,
  117.             $paymentMethodInstaller ?? new PaymentMethodInstaller(
  118.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  119.                 $this->getRepository($this->containerRuleDefinition::ENTITY_NAME),
  120.                 $this->container->get(PluginIdProvider::class),
  121.                 $paymentMethodDataRegistry,
  122.                 $mediaInstaller ?? new MediaInstaller(
  123.                     $this->getRepository($this->containerMediaDefinition::ENTITY_NAME),
  124.                     $this->getRepository($this->containerMediaFolderDefinition::ENTITY_NAME),
  125.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  126.                     $this->container->get(FileSaver::class),
  127.                 ),
  128.             ),
  129.             $paymentMethodStateService ?? new PaymentMethodStateService(
  130.                 $paymentMethodDataRegistry,
  131.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  132.             )
  133.         ))->update($updateContext);
  134.         parent::update($updateContext);
  135.     }
  136.     public function activate(ActivateContext $activateContext): void
  137.     {
  138.         $this->activateDeactivate->activate($activateContext->getContext());
  139.         parent::activate($activateContext);
  140.     }
  141.     public function deactivate(DeactivateContext $deactivateContext): void
  142.     {
  143.         $this->activateDeactivate->deactivate($deactivateContext->getContext());
  144.         parent::deactivate($deactivateContext);
  145.     }
  146.     public function enrichPrivileges(): array
  147.     {
  148.         return [
  149.             'sales_channel.viewer' => [
  150.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_READ,
  151.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_READ,
  152.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_UPDATE,
  153.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_CREATE,
  154.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_LOG_READ,
  155.                 'sales_channel_payment_method:read',
  156.             ],
  157.             'sales_channel.editor' => [
  158.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_UPDATE,
  159.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_RUN_DELETE,
  160.                 'payment_method:update',
  161.             ],
  162.             'sales_channel.creator' => [
  163.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_CREATE,
  164.                 'payment_method:create',
  165.                 'shipping_method:create',
  166.                 'delivery_time:create',
  167.             ],
  168.             'sales_channel.deleter' => [
  169.                 self::PAYPAL_POS_SALES_CHANNEL_PRIVILEGE_DELETE,
  170.             ],
  171.         ];
  172.     }
  173.     private function getInstaller(): InstallUninstall
  174.     {
  175.         \assert($this->container instanceof ContainerInterface'Container is not set yet, please call setContainer() before calling boot(), see `platform/Core/Kernel.php:186`.');
  176.         return new InstallUninstall(
  177.             new PaymentMethodInstaller(
  178.                 $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  179.                 $this->getRepository($this->containerRuleDefinition::ENTITY_NAME),
  180.                 $this->container->get(PluginIdProvider::class),
  181.                 new PaymentMethodDataRegistry(
  182.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  183.                     $this->container,
  184.                 ),
  185.                 new MediaInstaller(
  186.                     $this->getRepository($this->containerMediaDefinition::ENTITY_NAME),
  187.                     $this->getRepository($this->containerMediaFolderDefinition::ENTITY_NAME),
  188.                     $this->getRepository($this->containerPaymentMethodDefinition::ENTITY_NAME),
  189.                     $this->container->get(FileSaver::class)
  190.                 ),
  191.             ),
  192.             new SettingsInstaller(
  193.                 $this->getRepository($this->containerSystemConfigDefinition::ENTITY_NAME),
  194.                 $this->container->get(SystemConfigService::class)
  195.             ),
  196.             new PosInstaller($this->container->get(Connection::class)),
  197.         );
  198.     }
  199.     private function getRepository(ContainerInterface $containerstring $entityName): EntityRepository
  200.     {
  201.         $repository $container->get(\sprintf('%s.repository'$entityName), ContainerInterface::NULL_ON_INVALID_REFERENCE);
  202.         if (\interface_exists(EntityRepositoryInterface::class) && $repository instanceof EntityRepositoryInterface) {
  203.             return new EntityRepositoryDecorator($repository);
  204.         }
  205.         if (!$repository instanceof EntityRepository) {
  206.             throw new ServiceNotFoundException(\sprintf('%s.repository'$entityName));
  207.         }
  208.         return $repository;
  209.     }
  210. }