|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Yii2 extension for payment processing with Omnipay, Payum and more later. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-merchant |
|
6
|
|
|
* @package yii2-merchant |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\merchant\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\yii2\merchant\actions\RequestAction; |
|
14
|
|
|
use hiqdev\yii2\merchant\models\DepositForm; |
|
15
|
|
|
use hiqdev\yii2\merchant\models\DepositRequest; |
|
16
|
|
|
use hiqdev\yii2\merchant\Module; |
|
17
|
|
|
use hiqdev\yii2\merchant\transactions\Transaction; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\InvalidConfigException; |
|
20
|
|
|
use yii\web\BadRequestHttpException; |
|
21
|
|
|
use yii\web\Response; |
|
22
|
|
|
|
|
23
|
|
|
class PayController extends \yii\web\Controller |
|
24
|
|
|
{ |
|
25
|
|
|
public function actions() |
|
26
|
|
|
{ |
|
27
|
|
|
return array_merge(parent::actions(), [ |
|
28
|
|
|
'request' => [ |
|
29
|
|
|
'class' => RequestAction::class |
|
30
|
|
|
] |
|
31
|
|
|
]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Module|\yii\base\Module |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getMerchantModule() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->module; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Disable CSRF validation for POST requests we receive from outside |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function beforeAction($action) |
|
47
|
|
|
{ |
|
48
|
|
|
if (in_array($this->action->id, ['notify', 'return', 'cancel', 'proxy-notification'], true)) { |
|
49
|
|
|
Yii::$app->controller->enableCsrfValidation = false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return parent::beforeAction($action); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return Response |
|
57
|
|
|
*/ |
|
58
|
|
|
public function actionCancel() |
|
59
|
|
|
{ |
|
60
|
|
|
Yii::$app->session->addFlash('error', Yii::t('merchant', 'Payment failed or cancelled')); |
|
61
|
|
|
|
|
62
|
|
|
return $this->redirect($this->getMerchantModule()->previousUrl() ?: ['deposit']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function actionReturn() |
|
69
|
|
|
{ |
|
70
|
|
|
$transaction = $this->checkNotify(); |
|
71
|
|
|
|
|
72
|
|
|
return $this->render('return', [ |
|
73
|
|
|
'transactionId' => $transaction->getId(), |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $transactionId |
|
79
|
|
|
* @throws BadRequestHttpException |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function actionCheckReturn($transactionId) |
|
83
|
|
|
{ |
|
84
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
85
|
|
|
$transaction = $this->getMerchantModule()->findTransaction($transactionId); |
|
86
|
|
|
|
|
87
|
|
|
if ($transaction->getParameter('username') !== $this->getMerchantModule()->getUsername()) { |
|
88
|
|
|
throw new BadRequestHttpException('Access denied', 403); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return [ |
|
92
|
|
|
'status' => $transaction->getSuccess(), |
|
93
|
|
|
'url' => $transaction->isConfirmed() |
|
94
|
|
|
? $transaction->getParameter('finishUrl') |
|
95
|
|
|
: $transaction->getParameter('cancelUrl'), |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Action is designed to get the system notification from payment system, |
|
101
|
|
|
* process it and report success or error for the payment system. |
|
102
|
|
|
* @return null|string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function actionNotify() |
|
105
|
|
|
{ |
|
106
|
|
|
$transaction = $this->checkNotify(); |
|
107
|
|
|
if ($transaction === null) { |
|
108
|
|
|
return 'Unknown transaction'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW; |
|
112
|
|
|
|
|
113
|
|
|
return $transaction->isConfirmed() ? 'OK' : $transaction->getParameter('error'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Check notifications. |
|
118
|
|
|
* TODO: implement actual request check and proper handling. |
|
119
|
|
|
* @return Transaction |
|
120
|
|
|
*/ |
|
121
|
|
|
public function checkNotify() |
|
122
|
|
|
{ |
|
123
|
|
|
throw new InvalidConfigException('Method checkNotify must be implemented'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function actionProxyNotification() |
|
127
|
|
|
{ |
|
128
|
|
|
throw new InvalidConfigException('Method actionProxyNotification must be implemented'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function actionDeposit() |
|
132
|
|
|
{ |
|
133
|
|
|
$model = Yii::createObject($this->getMerchantModule()->depositFromClass); |
|
134
|
|
|
$request = Yii::$app->request; |
|
135
|
|
|
if ($model->load($request->isPost ? $request->post() : $request->get()) && $model->validate()) { |
|
136
|
|
|
return $this->renderDeposit($model); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->render('deposit-form', [ |
|
140
|
|
|
'model' => $model, |
|
141
|
|
|
'availableMerchants' => $this->getMerchantModule()->getPurchaseRequestCollection()->getItems(), |
|
142
|
|
|
]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Renders depositing buttons for given request data. |
|
147
|
|
|
* |
|
148
|
|
|
* @param DepositForm $form request data: |
|
149
|
|
|
* @return \yii\web\Response |
|
150
|
|
|
*/ |
|
151
|
|
|
public function renderDeposit($form) |
|
152
|
|
|
{ |
|
153
|
|
|
$request = new DepositRequest(); |
|
154
|
|
|
$request->amount = $form->amount; |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
$requests = $this->getMerchantModule()->getPurchaseRequestCollection($request)->getItems(); |
|
157
|
|
|
|
|
158
|
|
|
return $this->render('deposit', [ |
|
159
|
|
|
'requests' => $requests, |
|
160
|
|
|
'depositForm' => $form |
|
161
|
|
|
]); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.