Completed
Push — master ( 0ed61c...03ecff )
by Joachim
15:52
created

CallbackController::handleCallback()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 65
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 8.6195
c 0
b 0
f 0
cc 5
eloc 49
nc 6
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Controller;
4
5
use Loevgaard\AltaPay\Callback\Xml as XmlCallback;
6
use Loevgaard\AltaPay\Entity\Transaction;
7
use Loevgaard\DandomainAltapayBundle\Annotation\LogHttpTransaction;
8
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
9
use Loevgaard\DandomainAltapayBundle\Exception\CallbackException;
10
use Loevgaard\DandomainAltapayBundle\Exception\NotAllowedIpException;
11
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException;
12
use Loevgaard\DandomainAltapayBundle\Manager\PaymentManager;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * @Route("/callback")
22
 */
23
class CallbackController extends Controller
24
{
25
    /**
26
     * @Method("POST")
27
     * @Route("/form", name="loevgaard_dandomain_altapay_callback_form")
28
     *
29
     * @LogHttpTransaction()
30
     *
31
     * @param Request $request
32
     *
33
     * @return Response
34
     */
35
    public function formAction(Request $request)
36
    {
37
        $payment = $this->handleCallback($request);
38
39
        return $this->render('@LoevgaardDandomainAltapay/callback/form.html.twig', [
40
            'payment' => $payment,
41
        ]);
42
    }
43
44
    /**
45
     * @Method("POST")
46
     * @Route("/ok", name="loevgaard_dandomain_altapay_callback_ok")
47
     *
48
     * @LogHttpTransaction()
49
     *
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54
    public function okAction(Request $request)
55
    {
56
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
58
        return $this->render('@LoevgaardDandomainAltapay/callback/ok.html.twig');
59
    }
60
61
    /**
62
     * @Method("POST")
63
     * @Route("/fail", name="loevgaard_dandomain_altapay_callback_fail")
64
     *
65
     * @LogHttpTransaction()
66
     *
67
     * @param Request $request
68
     *
69
     * @return Response
70
     */
71
    public function failAction(Request $request)
72
    {
73
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
75
        return $this->render('@LoevgaardDandomainAltapay/callback/fail.html.twig');
76
    }
77
78
    /**
79
     * @Method("POST")
80
     * @Route("/redirect", name="loevgaard_dandomain_altapay_callback_redirect")
81
     *
82
     * @LogHttpTransaction()
83
     *
84
     * @param Request $request
85
     *
86
     * @return Response
87
     */
88
    public function redirectAction(Request $request)
89
    {
90
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
92
        return $this->render('@LoevgaardDandomainAltapay/callback/redirect.html.twig');
93
    }
94
95
    /**
96
     * @Method("POST")
97
     * @Route("/open", name="loevgaard_dandomain_altapay_callback_open")
98
     *
99
     * @LogHttpTransaction()
100
     *
101
     * @param Request $request
102
     *
103
     * @return Response
104
     */
105
    public function openAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        return $this->render('@LoevgaardDandomainAltapay/callback/open.html.twig');
108
    }
109
110
    /**
111
     * @Method("POST")
112
     * @Route("/notification", name="loevgaard_dandomain_altapay_callback_notification")
113
     *
114
     * @LogHttpTransaction()
115
     *
116
     * @param Request $request
117
     *
118
     * @return Response
119
     */
120
    public function notificationAction(Request $request)
121
    {
122
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123
124
        return new Response('OK');
125
    }
126
127
    /**
128
     * @Method("POST")
129
     * @Route("/verify-order", name="loevgaard_dandomain_altapay_callback_verify_order")
130
     *
131
     * @LogHttpTransaction()
132
     *
133
     * @param Request $request
134
     *
135
     * @return Response
136
     */
137
    public function verifyOrderAction(Request $request)
138
    {
139
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
141
        return new Response('OK');
142
    }
143
144
    /**
145
     * @param Request $request
146
     *
147
     * @return Payment
148
     *
149
     * @throws PaymentException
150
     */
151
    protected function handleCallback(Request $request)
152
    {
153
        $payment = $this->getPaymentFromRequest($request);
154
        $callbackHandler = $this->get('loevgaard_dandomain_altapay.altapay_callback_handler');
155
156
        // convert symfony request to PSR7 request
157
        $psr7Factory = new DiactorosFactory();
158
        $psrRequest = $psr7Factory->createRequest($request);
159
        $callback = $callbackHandler->handleCallback($psrRequest);
160
161
        if ($callback instanceof XmlCallback) {
162
            $transactions = $callback->getTransactions();
163
            if (isset($transactions[0])) {
164
                /** @var Transaction $transaction */
165
                $transaction = $transactions[0];
166
167
                $paymentManager = $this->getPaymentManager();
168
169
                $payment
170
                    ->setAltapayId($transaction->getPaymentId())
171
                    ->setCardStatus($transaction->getCardStatus())
172
                    ->setCreditCardToken($transaction->getCreditCardToken())
173
                    ->setCreditCardMaskedPan($transaction->getCreditCardMaskedPan())
174
                    ->setThreeDSecureResult($transaction->getThreeDSecureResult())
175
                    ->setLiableForChargeback($transaction->getLiableForChargeback())
176
                    ->setBlacklistToken($transaction->getBlacklistToken())
177
                    ->setShop($transaction->getShop())
178
                    ->setTerminal($transaction->getTerminal())
179
                    ->setTransactionStatus($transaction->getTransactionStatus())
180
                    ->setReasonCode($transaction->getReasonCode())
181
                    ->setMerchantCurrency($transaction->getMerchantCurrency())
182
                    ->setMerchantCurrencyAlpha($transaction->getMerchantCurrencyAlpha())
183
                    ->setCardHolderCurrency($transaction->getCardHolderCurrency())
184
                    ->setCardHolderCurrencyAlpha($transaction->getCardHolderCurrencyAlpha())
185
                    ->setReservedAmount($transaction->getReservedAmount())
186
                    ->setCapturedAmount($transaction->getCapturedAmount())
187
                    ->setRefundedAmount($transaction->getRefundedAmount())
188
                    ->setRecurringDefaultAmount($transaction->getRecurringDefaultAmount())
189
                    ->setCreatedDate($transaction->getCreatedDate())
190
                    ->setUpdatedDate($transaction->getUpdatedDate())
191
                    ->setPaymentNature($transaction->getPaymentNature())
192
                    ->setSupportsRefunds($transaction->getPaymentNatureService()->isSupportsRefunds())
193
                    ->setSupportsRelease($transaction->getPaymentNatureService()->isSupportsRelease())
194
                    ->setSupportsMultipleCaptures($transaction->getPaymentNatureService()->isSupportsMultipleCaptures())
195
                    ->setSupportsMultipleRefunds($transaction->getPaymentNatureService()->isSupportsMultipleRefunds())
196
                    ->setFraudRiskScore($transaction->getFraudRiskScore())
197
                    ->setFraudExplanation($transaction->getFraudExplanation())
198
                ;
199
                $paymentManager->update($payment);
200
            }
201
        }
202
203
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
204
        $callback = $callbackManager->createCallbackFromRequest($request);
205
        $callback->setPayment($payment);
206
207
        $callbackManager->update($callback);
208
209
        $allowedIps = $this->container->getParameter('loevgaard_dandomain_altapay.altapay_ips');
210
        if ('prod' === $this->container->get('kernel')->getEnvironment() && !in_array($request->getClientIp(), $allowedIps)) {
211
            throw NotAllowedIpException::create('IP `'.$request->getClientIp().'` is not an allowed IP.', $request, $payment);
212
        }
213
214
        return $payment;
215
    }
216
217
    /**
218
     * @param Request $request
219
     *
220
     * @return Payment
221
     *
222
     * @throws CallbackException
223
     */
224
    protected function getPaymentFromRequest(Request $request)
225
    {
226
        $paymentId = $request->cookies->getInt($this->getParameter('loevgaard_dandomain_altapay.cookie_payment_id'));
227
        $paymentManager = $this->getPaymentManager();
228
229
        /** @var Payment $payment */
230
        $payment = $paymentManager->getRepository()->find($paymentId);
231
232
        if (!$payment) {
233
            throw new CallbackException('Payment '.$paymentId.' does not exist');
234
        }
235
236
        return $payment;
237
    }
238
239
    /**
240
     * Add a callback request to the payment for logging purposes.
241
     *
242
     * @param Payment $payment
243
     * @param Request $request
244
     */
245
    protected function logCallback($payment, Request $request)
246
    {
247
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
248
        $callback = $callbackManager->create();
249
        $callback->setPayment($payment)
250
            ->setRequest((string) $request);
251
252
        $callbackManager->update($callback);
253
    }
254
255
    /**
256
     * @return PaymentManager
257
     */
258
    protected function getPaymentManager()
259
    {
260
        return $this->container->get('loevgaard_dandomain_altapay.payment_manager');
261
    }
262
}
263