Customize payment provider

Overview

In this guide you'll learn how to customize an existing payment provider. In this example we are customizing a SynchronousPaymentHandler, but the procedure also applies to AsynchronousPaymentHandler.

Prerequisites

As most guides, this guide is also built upon the Plugin base guide, but you don't necessarily need that. It is helpful to have looked at the guide about adding a custom payment method beforehand. Furthermore, decorating a service is also not explained here, but it's covered in our guide about adjusting a service, so having this open in another tab won't hurt.

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 has to accept an instance of OrderTransactionStateHandler like the original service and additionally an instance of DebitPayment that we want to decorate.
<plugin root>/src/Service/ExampleDebitPayment.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Service;
4
5
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
6
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment;
7
use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct;
8
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
9
use Shopware\Core\System\SalesChannel\SalesChannelContext;
10
11
class ExampleDebitPayment extends DebitPayment
12
{
13
private DebitPayment $decorated;
14
15
public function __construct(OrderTransactionStateHandler $transactionStateHandler, DebitPayment $decorated)
16
{
17
parent::__construct($transactionStateHandler);
18
$this->decorated = $decorated;
19
}
20
21
public function getDecorated(): DebitPayment
22
{
23
return $this->decorated;
24
}
25
26
public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void
27
{
28
// do some custom stuff here
29
30
$this->transactionStateHandler->process($transaction->getOrderTransaction()->getId(), $salesChannelContext->getContext());
31
}
32
}
Copied!
After we've created our customized payment provider class, we have to register it to the DI-container.
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
7
<services>
8
<service id="Swag\BasicExample\Service\ExampleDebitPayment" decorates="Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DebitPayment">
9
<argument type="service" id="Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler"/>
10
<argument type="service" id="Swag\BasicExample\Service\ExampleDebitPayment.inner"/>
11
</service>
12
</services>
13
</container>
Copied!