|
2 | 2 | namespace Payum\YiiExtension; |
3 | 3 |
|
4 | 4 | use Payum\Core\Request\BinaryMaskStatusRequest; |
| 5 | +use Payum\Core\Request\InteractiveRequestInterface; |
5 | 6 | use Payum\Core\Request\Http\RedirectUrlInteractiveRequest; |
6 | 7 | use Payum\Core\Request\SecuredCaptureRequest; |
| 8 | +use Payum\Core\Exception\LogicException; |
7 | 9 |
|
8 | 10 | class PaymentController extends \CController |
9 | 11 | { |
| 12 | + public function init() |
| 13 | + { |
| 14 | + parent::init(); |
| 15 | + |
| 16 | + \Yii::app()->attachEventHandler('onException', array($this, 'handleException')); |
| 17 | + } |
| 18 | + |
10 | 19 | public function actionCapture() |
11 | 20 | { |
12 | 21 | $token = $this->getPayum()->getHttpRequestVerifier()->verify($_REQUEST); |
13 | 22 | $payment = $this->getPayum()->getRegistry()->getPayment($token->getPaymentName()); |
14 | 23 |
|
15 | | - $payment->execute($status = new BinaryMaskStatusRequest($token)); |
| 24 | + $status = new BinaryMaskStatusRequest($token); |
| 25 | + $payment->execute($status); |
16 | 26 |
|
17 | | - if ($interactiveRequest = $payment->execute(new SecuredCaptureRequest($token), true)) { |
18 | | - if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) { |
19 | | - $this->redirect($interactiveRequest->getUrl(), true); |
20 | | - } |
21 | | - |
22 | | - throw new \LogicException('Unsupported interactive request', null, $interactiveRequest); |
23 | | - } |
| 27 | + $capture = new SecuredCaptureRequest($token); |
| 28 | + $payment->execute($capture); |
24 | 29 |
|
25 | 30 | $this->getPayum()->getHttpRequestVerifier()->invalidate($token); |
26 | 31 |
|
27 | 32 | $this->redirect($token->getAfterUrl()); |
28 | 33 | } |
29 | 34 |
|
| 35 | + public function handleException(\CExceptionEvent $event) |
| 36 | + { |
| 37 | + if (false == $event->exception instanceof InteractiveRequestInterface) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $interactiveRequest = $event->exception; |
| 42 | + |
| 43 | + if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) { |
| 44 | + $this->redirect($interactiveRequest->getUrl(), true); |
| 45 | + $event->handled = true; |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $ro = new \ReflectionObject($interactiveRequest); |
| 50 | + |
| 51 | + $event->exception = new LogicException( |
| 52 | + sprintf('Cannot convert interactive request %s to Yii response.', $ro->getShortName()), |
| 53 | + null, |
| 54 | + $interactiveRequest |
| 55 | + ); |
| 56 | + } |
| 57 | + |
30 | 58 | /** |
31 | 59 | * @return \Payum\YiiExtension\PayumComponent |
32 | 60 | */ |
|
0 commit comments