Customize Payment Provider
Overview
This guide covers how to customize an existing payment provider. In this example we are customizing a synchronous payment flow, but the procedure also applies to an asynchronous approach.
Prerequisites
Review the Plugin base guide. The guides for adding a custom payment method and adjusting a service, which provides information about decorating services, are also helpful.
Customize the payment provider
First, we create a new class that extends from the provider we want to customise. In this example we customise the class Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment and name our class ExampleDebitPayment. The constructor accepts an instance of OrderTransactionStateHandler for custom processing and an instance of DebitPayment that we want to decorate.
After we've created our customized payment provider class, we have to register it to the DI-container via the services.php.
<?php declare(strict_types=1);
namespace Swag\BasicExample\Service;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment;
use Shopware\Core\Checkout\Payment\Cart\PaymentTransactionStruct;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Struct\Struct;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class ExampleDebitPayment extends DebitPayment
{
private DebitPayment $decorated;
private OrderTransactionStateHandler $transactionStateHandler;
public function __construct(OrderTransactionStateHandler $transactionStateHandler, DebitPayment $decorated)
{
$this->transactionStateHandler = $transactionStateHandler;
$this->decorated = $decorated;
}
public function getDecorated(): DebitPayment
{
return $this->decorated;
}
public function pay(Request $request, PaymentTransactionStruct $transaction, Context $context, ?Struct $validateStruct): ?RedirectResponse
{
// do some custom stuff here
$this->transactionStateHandler->process($transaction->getOrderTransactionId(), $context);
return null;
}
}<?php declare(strict_types=1);
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment;
use Swag\BasicExample\Service\ExampleDebitPayment;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
return static function (ContainerConfigurator $configurator): void {
$services = $configurator->services();
$services->set(ExampleDebitPayment::class)
->decorate(DebitPayment::class)
->args([
service(OrderTransactionStateHandler::class),
service('.inner'),
]);
};