custom/plugins/EnetCredit/src/EnetCredit.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EnetCredit;
  3. use Shopware\Core\Framework\Context;
  4. use EnetCredit\Service\UserCreditPayment;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\System\CustomField\CustomFieldTypes;
  9. class EnetCredit extends Plugin
  10. {
  11.     public function install(Plugin\Context\InstallContext $context): void
  12.     {
  13.         $this->createCustomFields($context);
  14.         $this->createPaymentMethod($context);
  15.     }
  16.     public function uninstall(Plugin\Context\UninstallContext $context): void
  17.     {
  18.         $this->setPaymentMethodIsActive(false$context->getContext());
  19.     }
  20.     public function activate(Plugin\Context\ActivateContext $context): void
  21.     {
  22.         $this->setPaymentMethodIsActive(true$context->getContext());
  23.         parent::activate($context);
  24.     }
  25.     public function deactivate(Plugin\Context\DeactivateContext $context): void
  26.     {
  27.         $this->setPaymentMethodIsActive(false$context->getContext());
  28.         parent::deactivate($context);
  29.     }
  30.     private function setPaymentMethodIsActive(bool $activeContext $context): void
  31.     {
  32.         $paymentRepository $this->container->get('payment_method.repository');
  33.         $paymentMethodId $this->getPaymentMethodId();
  34.         if (!$paymentMethodId) {
  35.             return;
  36.         }
  37.         $paymentMethod = [
  38.             'id' => $paymentMethodId,
  39.             'active' => $active,
  40.         ];
  41.         $paymentRepository->update([$paymentMethod], $context);
  42.     }
  43.     private function createPaymentMethod(Plugin\Context\InstallContext $context)
  44.     {
  45.         $paymentMehtodExists $this->getPaymentMethodId();
  46.         if ($paymentMehtodExists) {
  47.             return;
  48.         }
  49.         $pluginIdProvider $this->container->get(Plugin\Util\PluginIdProvider::class);
  50.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context->getContext());
  51.         $paymentData = [
  52.             'handlerIdentifier' => UserCreditPayment::class,
  53.             'name' => 'SmeasyCredit',
  54.             'description' => 'Payment Method to use Users Credit Volume',
  55.             'pluginId' => $pluginId,
  56.             'afterOrderEnabled' => false,
  57.         ];
  58.         $paymentRepository $this->container->get('payment_method.repository');
  59.         $paymentRepository->create([$paymentData], $context->getContext());
  60.     }
  61.     private function getPaymentMethodId(): ?string
  62.     {
  63.         $paymentRepository $this->container->get('payment_method.repository');
  64.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier'UserCreditPayment::class));
  65.         $paymentIds $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext());
  66.         if ($paymentIds->getTotal() === 0) {
  67.             return null;
  68.         }
  69.         return $paymentIds->getIds()[0];
  70.     }
  71.     private function createCustomFields(Plugin\Context\InstallContext $context)
  72.     {
  73.         $customFieldRepository $this->container->get('custom_field_set.repository');
  74.         $customFieldRepository->create(
  75.             [
  76.                 [
  77.                     'name' => 'enet_credit_product',
  78.                     'config' => [
  79.                         'label' => [
  80.                             'en-GB' => 'Credit Card Product',
  81.                             'de-DE' => 'Guthaben Product',
  82.                         ]
  83.                     ],
  84.                     'customFields' => [
  85.                         [
  86.                             'name' => 'enet_credit_product',
  87.                             'type' => CustomFieldTypes::BOOL,
  88.                             'config' => [
  89.                                 'componentName' => 'sw-field',
  90.                                 'customFieldType' => 'checkbox',
  91.                                 'label' => [
  92.                                     'en-GB' => 'Enable Product as Credit Product',
  93.                                     'de-DE' => 'Als Guthabenprodukt nutzen'
  94.                                 ]
  95.                             ],
  96.                             'active' => true,
  97.                         ],
  98.                         [
  99.                             'name' => 'enet_credit_bonus',
  100.                             'type' => CustomFieldTypes::TEXT,
  101.                             'config' => [
  102.                                 'componentName' => 'sw-field',
  103.                                 'customFieldType' => 'text',
  104.                                 'label' => [
  105.                                     'en-GB' => 'Additional Bonus added to Price',
  106.                                     'de-DE' => 'Zusätzliches Guthaben das zum Preis hinzugefügt wird'
  107.                                 ]
  108.                             ],
  109.                             'active' => true,
  110.                         ]
  111.                     ],
  112.                     'relations' => [
  113.                         ['entityName' => 'product']
  114.                     ]
  115.                 ]
  116.             ],
  117.             $context->getContext()
  118.         );
  119.         $customFieldRepository->create(
  120.             [
  121.                 [
  122.                     'name' => 'enet_credit_customer',
  123.                     'config' => [
  124.                         'label' => [
  125.                             'en-GB' => 'Credit Card',
  126.                             'de-DE' => 'Guthabenkarte',
  127.                         ]
  128.                     ],
  129.                     'customFields' => [
  130.                         [
  131.                             'name' => 'enet_credit_value',
  132.                             'type' => CustomFieldTypes::FLOAT,
  133.                             'config' => [
  134.                                 'componentName' => 'sw-field',
  135.                                 'customFieldType' => 'text',
  136.                                 'label' => [
  137.                                     'en-GB' => 'Credit Card Value',
  138.                                     'de-DE' => 'Kreditrahmen Guthaben Karte'
  139.                                 ]
  140.                             ],
  141.                             'active' => true,
  142.                         ]
  143.                     ],
  144.                     'relations' => [
  145.                         ['entityName' => 'customer']
  146.                     ]
  147.                 ]
  148.             ],
  149.             $context->getContext()
  150.         );
  151.     }
  152. }