Completed
Push — master ( 24e2fb...716e2b )
by Oleksandr
16s queued 14s
created

EasyCreditController::easyCreditPaymentAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Heidelpay\Controller;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...cessingResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\QuoteTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spryker\Service\UtilText\Model\Url\Url;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
19
/**
20
 * @method \SprykerEco\Yves\Heidelpay\HeidelpayFactory getFactory()
21
 * @method \SprykerEco\Client\Heidelpay\HeidelpayClientInterface getClient()
22
 */
23
class EasyCreditController extends BaseHeidelpayController
24
{
25
    /**
26
     * @param \Symfony\Component\HttpFoundation\Request $request
27
     *
28
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
29
     *
30
     * @return \Symfony\Component\HttpFoundation\Response
31
     */
32
    public function easyCreditPaymentAction(Request $request): Response
33
    {
34
        if (!$request->isMethod(Request::METHOD_POST)) {
35
            throw new NotFoundHttpException();
36
        }
37
38
        $responseArray = $this->getUrldecodedRequestBody($request);
39
        $processingResultTransfer = $this->processEasyCreditPaymentResponse(
40
            $this->getClient()->filterResponseParameters($responseArray)
41
        );
42
43
        if ($processingResultTransfer->getIsError()) {
44
            return new Response($this->getFailurePageUrl($processingResultTransfer));
45
        }
46
47
        $redirectUrl = $this->generateEasyCreditRedirectUrl($responseArray);
48
49
        return new Response($redirectUrl);
50
    }
51
52
    /**
53
     * @param \Symfony\Component\HttpFoundation\Request $request
54
     *
55
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
56
     */
57
    public function easyCreditInitializePaymentAction(Request $request): RedirectResponse
58
    {
59
        $quoteTransfer = $this->getClient()->getQuote();
60
        $paymentParameters = $request->query->all();
61
62
        $this->getFactory()
63
            ->createEasyCreditResponseToQuoteHydrator()
64
            ->hydrateQuoteTransferWithEasyCreditResponse($paymentParameters, $quoteTransfer);
65
66
        return $this->redirectResponseExternal(
67
            $this->getSummaryPageUrl()
68
        );
69
    }
70
71
    /**
72
     * @param array $requestParameters
73
     *
74
     * @return array
75
     */
76
    protected function getPaymentParametersFromEasyCreditResponseParameters(array $requestParameters): array
77
    {
78
        $paymentParameters = new ArrayObject();
79
        $this->getFactory()
80
            ->createEasyCreditResponseToGetParametersMapper()
81
            ->map($requestParameters, $paymentParameters);
82
83
        return $paymentParameters->getArrayCopy();
84
    }
85
86
    /**
87
     * @param array $requestAsArray
88
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
89
     *
90
     * @return void
91
     */
92
    protected function hydrateEasyCreditResponseToQuote(array $requestAsArray, QuoteTransfer $quoteTransfer): void
93
    {
94
        $this->getFactory()
95
            ->createEasyCreditResponseToQuoteHydrator()
96
            ->hydrateQuoteTransferWithEasyCreditResponse($requestAsArray, $quoteTransfer);
97
    }
98
99
    /**
100
     * @param array $requestArray
101
     *
102
     * @return \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer
103
     */
104
    protected function processEasyCreditPaymentResponse(array $requestArray): HeidelpayPaymentProcessingResponseTransfer
105
    {
106
        return $this
107
            ->getClient()
108
            ->processExternalEasyCreditPaymentResponse($requestArray);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getSummaryPageUrl(): string
115
    {
116
        return $this->getConfig()->getYvesCheckoutSummaryStepUrl();
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    protected function getInitializePaymentUrl(): string
123
    {
124
        return $this->getConfig()->getYvesInitializePaymentUrl();
125
    }
126
127
    /**
128
     * @param \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer
129
     *
130
     * @return string
131
     */
132
    protected function getFailurePageUrl(HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer): string
133
    {
134
        return sprintf(
135
            $this->getConfig()->getYvesCheckoutPaymentFailedUrl(),
136
            $processingResultTransfer->getError()->getCode()
137
        );
138
    }
139
140
    /**
141
     * @param array $responseArray
142
     *
143
     * @return string
144
     */
145
    protected function generateEasyCreditRedirectUrl(array $responseArray): string
146
    {
147
        return Url::generate(
148
            $this->getInitializePaymentUrl(),
149
            $this->getPaymentParametersFromEasyCreditResponseParameters($responseArray)
150
        )->build();
151
    }
152
}
153