<?php declare(strict_types=1);
namespace WabsAuth;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use WabsAuth\Service\CostCenterPayment;
use WabsAuth\Service\WabsInvoicePayment;
class WabsAuth extends Plugin
{
public function install(InstallContext $context): void
{
$this->addCostCenterPaymentMethod($context->getContext());
$this->updateInvoicePaymentMethod($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
// Only set the payment method to inactive when uninstalling. Removing the payment method would
// cause data consistency issues, since the payment method might have been used in several orders
$this->setCostCenterPaymentMethodIsActive(false, $context->getContext());
$this->setInvoicePaymentMethodIsActive(false, $context->getContext());
}
public function activate(ActivateContext $activateContext): void
{
$this->setCostCenterPaymentMethodIsActive(true, $activateContext->getContext());
$this->setInvoicePaymentMethodIsActive(true, $activateContext->getContext());
parent::activate($activateContext);
$activateContext->setAutoMigrate(true);
}
public function update(UpdateContext $updateContext): void
{
$this->addCostCenterPaymentMethod($updateContext->getContext());
$this->updateInvoicePaymentMethod($updateContext->getContext());
parent::update($updateContext);
$updateContext->setAutoMigrate(true);
}
public function deactivate(DeactivateContext $context): void
{
$this->setCostCenterPaymentMethodIsActive(false, $context->getContext());
$this->setInvoicePaymentMethodIsActive(false, $context->getContext());
parent::deactivate($context);
}
private function addCostCenterPaymentMethod(Context $context): void
{
$paymentMethodExists = $this->getCostCenterPaymentMethodId();
// Payment method exists already, no need to continue here
if ($paymentMethodExists) {
return;
}
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
$costCenterPayment = [
// payment handler will be selected by the identifier
'handlerIdentifier' => CostCenterPayment::class,
'name' => 'Kostenstelle',
'description' => 'Bezahlmethode Kostenstelle für Hauptamtliche Kunden',
'pluginId' => $pluginId,
'customFields' => [
'wabsauth_template' => '@Storefront/storefront/wabsauth/cost-center-payment.html.twig',
'cost_center_payment' => true,
],
];
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->create([$costCenterPayment], $context);
}
private function setCostCenterPaymentMethodIsActive(bool $active, Context $context): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentMethodId = $this->getCostCenterPaymentMethodId();
// Payment does not even exist, so nothing to (de-)activate here
if (!$paymentMethodId) {
return;
}
$paymentMethod = [
'id' => $paymentMethodId,
'active' => $active,
];
$paymentRepository->update([$paymentMethod], $context);
}
private function setInvoicePaymentMethodIsActive(bool $active, Context $context): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentMethodId = $this->getInvoicePaymentMethodId();
// Payment does not even exist, so nothing to (de-)activate here
if (!$paymentMethodId) {
return;
}
$paymentMethod = [
'id' => $paymentMethodId,
'active' => $active,
];
$paymentRepository->update([$paymentMethod], $context);
}
private function getCostCenterPaymentMethodId(): ?string
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
// Fetch ID for update
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier', CostCenterPayment::class));
return $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
}
private function getInvoicePaymentMethodId(): ?string
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
// Fetch ID for update
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('handlerIdentifier',
WabsInvoicePayment::class));
return $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
}
private function updateInvoicePaymentMethod(Context $context)
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('name', 'Rechnung'));
$invoicePaymentId = $paymentRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
if (!$invoicePaymentId) {
return;
}
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
$paymentRepository->update([
[
'id' => $invoicePaymentId,
'handlerIdentifier' => WabsInvoicePayment::class,
'pluginId' => $pluginId,
]
], $context);
}
}