Completed
Push — master ( 7c1eaa...80d841 )
by Joachim
15:53
created

CallbackController::handleCallback()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 21
nc 16
nop 1
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Controller;
4
5
use Loevgaard\DandomainAltapayBundle\Annotation\LogHttpTransaction;
6
use Loevgaard\DandomainAltapayBundle\Entity\Payment;
7
use Loevgaard\DandomainAltapayBundle\Exception\CallbackException;
8
use Loevgaard\DandomainAltapayBundle\Exception\NotAllowedIpException;
9
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException;
10
use Loevgaard\DandomainAltapayBundle\Manager\PaymentManager;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @Route("/callback")
19
 */
20
class CallbackController extends Controller
21
{
22
    /**
23
     * @Method("POST")
24
     * @Route("/form", name="loevgaard_dandomain_altapay_callback_form")
25
     *
26
     * @LogHttpTransaction()
27
     *
28
     * @param Request $request
29
     *
30
     * @return Response
31
     */
32
    public function formAction(Request $request)
33
    {
34
        $payment = $this->handleCallback($request);
35
36
        return $this->render('@LoevgaardDandomainAltapay/callback/form.html.twig', [
37
            'payment' => $payment,
38
        ]);
39
    }
40
41
    /**
42
     * @Method("POST")
43
     * @Route("/ok", name="loevgaard_dandomain_altapay_callback_ok")
44
     *
45
     * @LogHttpTransaction()
46
     *
47
     * @param Request $request
48
     *
49
     * @return Response
50
     */
51
    public function okAction(Request $request)
52
    {
53
        $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...
54
55
        return $this->render('@LoevgaardDandomainAltapay/callback/ok.html.twig');
56
    }
57
58
    /**
59
     * @Method("POST")
60
     * @Route("/fail", name="loevgaard_dandomain_altapay_callback_fail")
61
     *
62
     * @LogHttpTransaction()
63
     *
64
     * @param Request $request
65
     *
66
     * @return Response
67
     */
68
    public function failAction(Request $request)
69
    {
70
        $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...
71
72
        return $this->render('@LoevgaardDandomainAltapay/callback/fail.html.twig');
73
    }
74
75
    /**
76
     * @Method("POST")
77
     * @Route("/redirect", name="loevgaard_dandomain_altapay_callback_redirect")
78
     *
79
     * @LogHttpTransaction()
80
     *
81
     * @param Request $request
82
     *
83
     * @return Response
84
     */
85
    public function redirectAction(Request $request)
86
    {
87
        $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...
88
89
        return $this->render('@LoevgaardDandomainAltapay/callback/redirect.html.twig');
90
    }
91
92
    /**
93
     * @Method("POST")
94
     * @Route("/open", name="loevgaard_dandomain_altapay_callback_open")
95
     *
96
     * @LogHttpTransaction()
97
     *
98
     * @param Request $request
99
     *
100
     * @return Response
101
     */
102
    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...
103
    {
104
        return $this->render('@LoevgaardDandomainAltapay/callback/open.html.twig');
105
    }
106
107
    /**
108
     * @Method("POST")
109
     * @Route("/notification", name="loevgaard_dandomain_altapay_callback_notification")
110
     *
111
     * @LogHttpTransaction()
112
     *
113
     * @param Request $request
114
     *
115
     * @return Response
116
     */
117
    public function notificationAction(Request $request)
118
    {
119
        $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...
120
121
        return new Response('OK');
122
    }
123
124
    /**
125
     * @Method("POST")
126
     * @Route("/verify-order", name="loevgaard_dandomain_altapay_callback_verify_order")
127
     *
128
     * @LogHttpTransaction()
129
     *
130
     * @param Request $request
131
     *
132
     * @return Response
133
     */
134
    public function verifyOrderAction(Request $request)
135
    {
136
        $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...
137
138
        return new Response('OK');
139
    }
140
141
    /**
142
     * @param Request $request
143
     *
144
     * @return Payment
145
     *
146
     * @throws PaymentException
147
     */
148
    protected function handleCallback(Request $request)
149
    {
150
        $payment = $this->getPaymentFromRequest($request);
151
152
        // @todo this should be placed somewhere in the altapay php sdk
153
        $paymentId = null;
154
155
        if($request->request->has('xml')) {
156
            $xml = new \SimpleXMLElement($request->request->get('xml'));
157
            if(isset($xml->Body->Transactions->Transaction) && !empty($xml->Body->Transactions->Transaction)) {
158
                foreach ($xml->Body->Transactions->Transaction as $transaction) {
159
                    $paymentId = (string)$transaction->PaymentId;
160
                    break;
161
                }
162
            }
163
        }
164
165
        if($paymentId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $paymentId of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
166
            $paymentManager = $this->getPaymentManager();
167
            $payment->setAltapayId($paymentId);
168
            $paymentManager->update($payment);
169
        }
170
171
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
172
        $callback = $callbackManager->createCallbackFromRequest($request);
173
        $callback->setPayment($payment);
174
175
        $callbackManager->update($callback);
176
177
        $allowedIps = $this->container->getParameter('loevgaard_dandomain_altapay.altapay_ips');
178
        if ('prod' === $this->container->get('kernel')->getEnvironment() && !in_array($request->getClientIp(), $allowedIps)) {
179
            throw NotAllowedIpException::create('IP `'.$request->getClientIp().'` is not an allowed IP.', $request, $payment);
180
        }
181
182
        return $payment;
183
    }
184
185
    /**
186
     * @param Request $request
187
     *
188
     * @return Payment
189
     *
190
     * @throws CallbackException
191
     */
192
    protected function getPaymentFromRequest(Request $request)
193
    {
194
        $paymentId = $request->cookies->getInt($this->getParameter('loevgaard_dandomain_altapay.cookie_payment_id'));
195
        $paymentManager = $this->getPaymentManager();
196
197
        /** @var Payment $payment */
198
        $payment = $paymentManager->getRepository()->find($paymentId);
199
200
        if (!$payment) {
201
            throw new CallbackException('Payment '.$paymentId.' does not exist');
202
        }
203
204
        return $payment;
205
    }
206
207
    /**
208
     * Add a callback request to the payment for logging purposes.
209
     *
210
     * @param Payment $payment
211
     * @param Request $request
212
     */
213
    protected function logCallback($payment, Request $request)
214
    {
215
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
216
        $callback = $callbackManager->create();
217
        $callback->setPayment($payment)
218
            ->setRequest((string) $request);
219
220
        $callbackManager->update($callback);
221
    }
222
223
    /**
224
     * @return PaymentManager
225
     */
226
    protected function getPaymentManager()
227
    {
228
        return $this->container->get('loevgaard_dandomain_altapay.payment_manager');
229
    }
230
}
231