Passed
Push — trunk ( 49986f...44ff76 )
by Christian
22:08 queued 08:23
created

AsyncTestPaymentHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A finalize() 0 15 2
A __construct() 0 2 1
A pay() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Integration\PaymentHandler;
4
5
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;
6
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct;
7
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface;
8
use Shopware\Core\Checkout\Payment\Exception\CustomerCanceledAsyncPaymentException;
9
use Shopware\Core\Framework\Log\Package;
10
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
11
use Shopware\Core\System\SalesChannel\SalesChannelContext;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * @internal
17
 */
18
#[Package('checkout')]
19
class AsyncTestPaymentHandler implements AsynchronousPaymentHandlerInterface
20
{
21
    final public const REDIRECT_URL = 'https://shopware.com';
22
23
    public function __construct(private readonly OrderTransactionStateHandler $transactionStateHandler)
24
    {
25
    }
26
27
    public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse
28
    {
29
        $context = $salesChannelContext->getContext();
30
31
        $this->transactionStateHandler->process($transaction->getOrderTransaction()->getId(), $context);
32
33
        return new RedirectResponse(self::REDIRECT_URL);
34
    }
35
36
    public function finalize(
37
        AsyncPaymentTransactionStruct $transaction,
38
        Request $request,
39
        SalesChannelContext $salesChannelContext
40
    ): void {
41
        $context = $salesChannelContext->getContext();
42
43
        if ($request->query->getBoolean('cancel')) {
44
            throw new CustomerCanceledAsyncPaymentException(
45
                $transaction->getOrderTransaction()->getId(),
46
                'Async Test Payment canceled'
47
            );
48
        }
49
50
        $this->transactionStateHandler->paid($transaction->getOrderTransaction()->getId(), $context);
51
    }
52
}
53