<?php
declare(strict_types=1);
namespace Chann\Channelpilot;
use Chann\Channelpilot\Service\ChannelpilotPaymentHandler;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
class Channelpilot extends Plugin
{
public function install(InstallContext $context): void
{
// Ignore InstallContext and Choose defaultContext.
// installContext in backend-based install has no system privileges,
// so addPaymentMethod() fails.
$systemContext = Context::createDefaultContext();
$this->addPaymentMethod(true, $systemContext);
$this->addCPPaymentToSalesChannels($systemContext);
$this->addOrderCustomFields($context->getContext());
}
public function update(UpdateContext $updateContext): void
{
$this->addOrderCustomFields($updateContext->getContext());
$this->addPaymentMethod(true, $updateContext->getContext());
}
protected function addPaymentMethod(bool $active, Context $context): void
{
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$paymentRepository->upsert($this->getPaymentDefinitions($context), $context);
}
protected function getPaymentDefinitions($context): array
{
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
$pluginId = $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
return [
[
'id' => md5('ChannelPilot'),
'handlerIdentifier' => ChannelpilotPaymentHandler::class,
'pluginId' => $pluginId,
'afterOrderEnabled' => false,
'active' => false,
'translations' => [
'de-DE' => [
'name' => 'Channel Pilot Pro',
],
'en-GB' => [
'name' => 'Channel Pilot Pro',
],
],
]
];
}
/**
* Create custom fields.
* All IDs fixed for idempotency on reinstallations.
*/
protected function addOrderCustomFields(Context $context): void
{
$custom_field_set_id = md5('Channelpilot custom_field_set');
$this->container->get('custom_field_set.repository')->upsert(
[
[
'id' => $custom_field_set_id,
'name' => 'custom_channelpilot',
'config' => [
"label" => ["de-DE" => "Channel Pilot Pro", "en-GB" => "Channel Pilot Pro"],
"translated" => false
],
'active' => true,
'global' => false,
],
],
$context
);
$this->container->get('custom_field_set_relation.repository')->upsert(
[
[
'id' => md5('ChannelPilot custom_field_set_relation'),
'customFieldSetId' => $custom_field_set_id,
'entityName' => 'order',
],
],
$context
);
$this->container->get('custom_field.repository')->upsert(
[
[
'id' => md5('custom_channelpilot_token'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_token',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Token für Storno und Versand", "en-GB" => "Channel Pilot Pro Token for cancellation and shipping"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_orderjson'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_orderjson',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro serialisierte Bestellung", "en-GB" => "Channel Pilot Pro serialized order"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_external_order_id'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_external_order_id',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Marktplatz Order-ID", "en-GB" => "Channel Pilot Pro Marketplace Order-ID"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_source'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_source',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Quelle", "en-GB" => "Channel Pilot Pro Source"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_delivery2send'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_delivery2send',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Offene Versandmitteilung", "en-GB" => "Channel Pilot Pro pending delivery message"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_cancel2send'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_cancel2send',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Offene Stornomitteilung", "en-GB" => "Channel Pilot Pro pending cancellation message"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
[
'id' => md5('custom_channelpilot_refund2send'),
'customFieldSetId' => $custom_field_set_id,
'name' => 'custom_channelpilot_refund2send',
'type' => 'text',
'config' => [
'type' => 'text',
"label" => ["de-DE" => "Channel Pilot Pro Offene Erstattung", "en-GB" => "Channel Pilot Pro pending refund message"],
"translated" => false,
"componentName" => "sw-field",
"customFieldType" => "text",
"customFieldPosition" => 1
],
'active' => true,
],
],
$context
);
}
protected function addCPPaymentToSalesChannels(Context $context): void
{
foreach ($this->getPaymentDefinitions($context) as $paymentDefinition) {
$paymentMethodId = $paymentDefinition['id'];
$salesChannelsToChange = $this->getSalesChannelsToChange($context);
$updateData = [];
foreach ($salesChannelsToChange as $salesChannel) {
$salesChannelUpdateData = [
'id' => $salesChannel->getId(),
];
$paymentMethodCollection = $salesChannel->getPaymentMethods();
if ($paymentMethodCollection === null || $paymentMethodCollection->get($paymentMethodId) === null) {
$salesChannelUpdateData['paymentMethods'][] = [
'id' => $paymentMethodId,
];
}
$updateData[] = $salesChannelUpdateData;
}
$this->container->get('sales_channel.repository')->update($updateData, $context);
}
}
protected function getSalesChannelsToChange(Context $context): EntityCollection
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsAnyFilter('typeId', [
Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
Defaults::SALES_CHANNEL_TYPE_API,
])
);
$criteria->addAssociation('paymentMethods');
return $this->container->get('sales_channel.repository')->search($criteria, $context)->getEntities();
}
}