Completed
Push — payment ( a2694a )
by Torben
42:06
created

PaymentController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
c 1
b 0
f 1
lcom 2
cbo 1
dl 0
loc 295
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A injectPaymentService() 0 4 1
A injectHashService() 0 4 1
A injectRegistrationRepository() 0 4 1
A processRequest() 0 9 2
B redirectAction() 0 35 2
B successAction() 0 27 2
B failureAction() 0 27 2
B cancelAction() 0 27 2
B notifyAction() 0 25 2
A processWithAction() 0 10 1
A validateHmacForAction() 0 5 1
A getPaymentUriForAction() 0 13 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository;
18
use DERHANSEN\SfEventMgt\Payment\Exception\PaymentException;
19
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
22
use TYPO3\CMS\Extbase\Mvc\ResponseInterface;
23
use DERHANSEN\SfEventMgt\Service\PaymentService;
24
use TYPO3\CMS\Extbase\Security\Exception\InvalidHashException;
25
26
/**
27
 * PaymentController
28
 *
29
 * @author Torben Hansen <[email protected]>
30
 */
31
class PaymentController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
32
{
33
34
    /**
35
     * PaymentService
36
     *
37
     * @var PaymentService
38
     */
39
    protected $paymentService;
40
41
    /**
42
     * Hash Service
43
     *
44
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
45
     */
46
    protected $hashService;
47
48
    /**
49
     * Registration repository
50
     *
51
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
52
     */
53
    protected $registrationRepository = null;
54
55
    /**
56
     * DI for paymentService
57
     *
58
     * @param PaymentService $paymentService
59
     */
60
    public function injectPaymentService(PaymentService $paymentService)
61
    {
62
        $this->paymentService = $paymentService;
63
    }
64
65
    /**
66
     * DI for hashService
67
     *
68
     * @param HashService $hashService
69
     */
70
    public function injectHashService(HashService $hashService)
71
    {
72
        $this->hashService = $hashService;
73
    }
74
75
    /**
76
     * DI for registrationRepository
77
     *
78
     * @param RegistrationRepository $registrationRepository
79
     */
80
    public function injectRegistrationRepository(RegistrationRepository $registrationRepository)
81
    {
82
        $this->registrationRepository = $registrationRepository;
83
    }
84
85
    /**
86
     * Catches all PaymentExceptions and sets the Exception message to the response content
87
     *
88
     * @param RequestInterface $request
89
     * @param ResponseInterface $response
90
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
91
     */
92
    public function processRequest(RequestInterface $request, ResponseInterface $response)
93
    {
94
        // @todo Catch Hash Exceptions
95
        try {
96
            parent::processRequest($request, $response);
97
        } catch (\DERHANSEN\SfEventMgt\Exception $e) {
98
            $response->setContent('<div class="payment-error">' . $e->getMessage() . '</div>');
99
        }
100
    }
101
102
    /**
103
     * Redirect to payment provider
104
     *
105
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
106
     * @param string $hmac
107
     */
108
    public function redirectAction($registration, $hmac)
109
    {
110
        // @todo Validate hmac
111
112
        // @todo Check if action is enabled for payment method
113
        $this->processWithAction($registration, $this->actionMethodName);
114
115
        $values = [
116
            'successUrl' => $this->getPaymentUriForAction('successAction', $registration),
117
            'failureUrl' => $this->getPaymentUriForAction('failureAction', $registration),
118
            'cancelUrl' => $this->getPaymentUriForAction('cancelAction', $registration),
119
            'notifyUrl' => $this->getPaymentUriForAction('notifyAction', $registration),
120
            'registration' => $registration,
121
            'html' => ''
122
        ];
123
124
        // @todo: Get from Registration!
125
        $paymentMethod = 'paypal';
126
127
        /**
128
         * If true, the externally called BeforeRedirect method was successful and the registration can be updated
129
         */
130
        $updateRegistration = false;
131
        $this->signalSlotDispatcher->dispatch(
132
            __CLASS__,
133
            __FUNCTION__ . 'BeforeRedirect' . ucfirst($paymentMethod),
134
            [&$values, &$updateRegistration, $registration, $this]
135
        );
136
137
        if ($updateRegistration) {
138
            $this->registrationRepository->update($registration);
139
        }
140
141
        $this->view->assign('result', $values);
142
    }
143
144
    /**
145
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
146
     * @param string $hmac
147
     */
148
    public function successAction($registration, $hmac)
149
    {
150
        // @todo Validate hmac
151
152
        // @todo Check if action is enabled for payment method
153
154
        $values = ['html' => ''];
155
156
        // @todo: Get from Registration!
157
        $paymentMethod = 'paypal';
158
159
        /**
160
         * If true, the externally called ProcessSuccess method was successful and the registration can be updated
161
         */
162
        $updateRegistration = false;
163
        $this->signalSlotDispatcher->dispatch(
164
            __CLASS__,
165
            __FUNCTION__ . 'ProcessSuccess' . ucfirst($paymentMethod),
166
            [&$values, &$updateRegistration, $registration, GeneralUtility::_GET(), $this]
167
        );
168
169
        if ($updateRegistration) {
170
            $this->registrationRepository->update($registration);
171
        }
172
173
        $this->view->assign('result', $values);
174
    }
175
176
    /**
177
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
178
     * @param string $hmac
179
     */
180
    public function failureAction($registration, $hmac)
181
    {
182
        // @todo Validate hmac
183
184
        // @todo Check if action is enabled for payment method
185
186
        $values = ['html' => ''];
187
188
        // @todo: Get from Registration!
189
        $paymentMethod = 'paypal';
190
191
        /**
192
         * If true, the externally called ProcessSuccess method was successful and the registration can be updated
193
         */
194
        $updateRegistration = false;
195
        $this->signalSlotDispatcher->dispatch(
196
            __CLASS__,
197
            __FUNCTION__ . 'ProcessFailure' . ucfirst($paymentMethod),
198
            [&$values, &$updateRegistration, $registration, GeneralUtility::_GET(), $this]
199
        );
200
201
        if ($updateRegistration) {
202
            $this->registrationRepository->update($registration);
203
        }
204
205
        $this->view->assign('result', $values);
206
    }
207
208
    /**
209
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
210
     * @param string $hmac
211
     */
212
    public function cancelAction($registration, $hmac)
213
    {
214
        // @todo Validate hmac
215
216
        // @todo Check if action is enabled for payment method
217
218
        $values = ['html' => ''];
219
220
        // @todo: Get from Registration!
221
        $paymentMethod = 'paypal';
222
223
        /**
224
         * If true, the externally called ProcessSuccess method was successful and the registration can be updated
225
         */
226
        $updateRegistration = false;
227
        $this->signalSlotDispatcher->dispatch(
228
            __CLASS__,
229
            __FUNCTION__ . 'ProcessCancel' . ucfirst($paymentMethod),
230
            [&$values, &$updateRegistration, $registration, GeneralUtility::_GET(), $this]
231
        );
232
233
        if ($updateRegistration) {
234
            $this->registrationRepository->update($registration);
235
        }
236
237
        $this->view->assign('result', $values);
238
    }
239
240
    /**
241
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
242
     * @param string $hmac
243
     */
244
    public function notifyAction($registration, $hmac)
245
    {
246
        // @todo Validate hmac
247
248
        $values = ['html' => ''];
249
250
        // @todo: Get from Registration!
251
        $paymentMethod = 'paypal';
252
253
        /**
254
         * If true, the externally called ProcessSuccess method was successful and the registration can be updated
255
         */
256
        $updateRegistration = false;
257
        $this->signalSlotDispatcher->dispatch(
258
            __CLASS__,
259
            __FUNCTION__ . 'ProcessNotify' . ucfirst($paymentMethod),
260
            [&$values, &$updateRegistration, $registration, GeneralUtility::_GET(), $this]
261
        );
262
263
        if ($updateRegistration) {
264
            $this->registrationRepository->update($registration);
265
        }
266
267
        $this->view->assign('result', $values);
268
    }
269
270
    /**
271
     * Checks if the given action may be called for the given registration / event
272
     *
273
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration$registration
274
     * @param string $actionName
275
     * @throws PaymentException
276
     * @return void
277
     */
278
    protected function processWithAction($registration, $actionName)
279
    {
280
        // @todo - Should do the following:
281
        // 1. Check, if payment is enabled for Event
282
        // 2. Check, if payment method is enabled for Event
283
        // 3. Check, if action is configured for payment method
284
        // 4. Check, if "paid" is false for registration
285
        // If a condition does not match, throw a PaymentException
286
        // @todo - move to service
287
    }
288
289
    /**
290
     * Checks the HMAC for the given action and registration
291
     *
292
     * @param $registration
293
     * @param $hmac
294
     * @param $action
295
     * @throws InvalidHashException
296
     */
297
    protected function validateHmacForAction($registration, $hmac, $action)
298
    {
299
        // Should throw an InvalidHmac exception
300
        // @todo - move to service
301
    }
302
303
    /**
304
     * Returns the payment Uri for the given action and registration
305
     *
306
     * @param string $action
307
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration
308
     * @return string
309
     * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
310
     */
311
    protected function getPaymentUriForAction($action, $registration)
312
    {
313
        return $this->uriBuilder->uriFor(
314
            $action,
315
            [
316
                'registration' => $registration,
317
                'hmac' => $this->hashService->generateHmac($action . '-' . $registration->getUid())
318
            ],
319
            'payment',
320
            'sfeventmgt',
321
            'Pieventpayment'
322
        );
323
    }
324
325
}
326
327