<?php declare(strict_types=1);
namespace EnetCredit;
use Shopware\Core\Framework\Context;
use EnetCredit\Service\UserCreditPayment;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class EnetCredit extends Plugin
{
public function install(Plugin\Context\InstallContext $context): void
{
$this->createCustomFields($context);
$this->createPaymentMethod($context);
}
public function uninstall(Plugin\Context\UninstallContext $context): void
{
$this->setPaymentMethodIsActive(false, $context->getContext());
}
public function activate(Plugin\Context\ActivateContext $context): void
{
$this->setPaymentMethodIsActive(true, $context->getContext());
parent::activate($context);
}
public function deactivate(Plugin\Context\DeactivateContext $context): void
{
$this->setPaymentMethodIsActive(false, $context->getContext());
parent::deactivate($context);
}
private function setPaymentMethodIsActive(bool $active, Context $context): void
{
$paymentRepository = $this->container->get('payment_method.repository');
$paymentMethodId = $this->getPaymentMethodId();
if (!$paymentMethodId) {
return;
}
$paymentMethod = [
'id' => $paymentMethodId,
'active' => $active,
];
$paymentRepository->update([$paymentMethod], $context);
}
private function createPaymentMethod(Plugin\Context\InstallContext $context)
{
$paymentMehtodExists = $this->getPaymentMethodId();
if ($paymentMehtodExists) {
return;
}
$pluginIdProvider = $this->container->get(Plugin\Util\PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context->getContext());
$paymentData = [
'handlerIdentifier' => UserCreditPayment::class,
'name' => 'SmeasyCredit',
'description' => 'Payment Method to use Users Credit Volume',
'pluginId' => $pluginId,
'afterOrderEnabled' => false,
];
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->create([$paymentData], $context->getContext());
}
private function getPaymentMethodId(): ?string
{
$paymentRepository = $this->container->get('payment_method.repository');
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier', UserCreditPayment::class));
$paymentIds = $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext());
if ($paymentIds->getTotal() === 0) {
return null;
}
return $paymentIds->getIds()[0];
}
private function createCustomFields(Plugin\Context\InstallContext $context)
{
$customFieldRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository->create(
[
[
'name' => 'enet_credit_product',
'config' => [
'label' => [
'en-GB' => 'Credit Card Product',
'de-DE' => 'Guthaben Product',
]
],
'customFields' => [
[
'name' => 'enet_credit_product',
'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'label' => [
'en-GB' => 'Enable Product as Credit Product',
'de-DE' => 'Als Guthabenprodukt nutzen'
]
],
'active' => true,
],
[
'name' => 'enet_credit_bonus',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'label' => [
'en-GB' => 'Additional Bonus added to Price',
'de-DE' => 'Zusätzliches Guthaben das zum Preis hinzugefügt wird'
]
],
'active' => true,
]
],
'relations' => [
['entityName' => 'product']
]
]
],
$context->getContext()
);
$customFieldRepository->create(
[
[
'name' => 'enet_credit_customer',
'config' => [
'label' => [
'en-GB' => 'Credit Card',
'de-DE' => 'Guthabenkarte',
]
],
'customFields' => [
[
'name' => 'enet_credit_value',
'type' => CustomFieldTypes::FLOAT,
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'label' => [
'en-GB' => 'Credit Card Value',
'de-DE' => 'Kreditrahmen Guthaben Karte'
]
],
'active' => true,
]
],
'relations' => [
['entityName' => 'customer']
]
]
],
$context->getContext()
);
}
}