-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClickController.php
264 lines (217 loc) · 9.79 KB
/
ClickController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace app\controllers;
use app\components\ClickData;
use app\models\ClickTransactions;
use app\models\Orders;
use app\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class ClickController extends Controller
{
private $reqData = [];
private $user; // init into validateData()
private $userID = 5;
public function beforeAction($action)
{
if ($action->id == "prepare" || $action->id == "complete") {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
/**
* @return string
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\NotSupportedException
* @throws \yii\db\Exception
* @throws \yii\db\StaleObjectException
*/
public function actionPrepare()
{
$this->reqData = $_POST;
$this->validateData();
$checkExists = ClickTransactions::find()
->where(['click_trans_id' => $this->reqData['click_trans_id']])->one();
if ($checkExists !== NULL) {
if ($checkExists->status == ClickTransactions::STATUS_CANCEL) {
//Transaction cancelled
die(json_encode(ClickData::getMessage('9')));
} //Already paid
else die(json_encode(ClickData::getMessage('4')));
}
//Error in request from click
if (!$this->reqData['error'] == 0) die(json_encode(ClickData::getMessage('8')));
$newTransaction = new ClickTransactions;
$newTransaction->user_id = $this->reqData['merchant_trans_id'];
$newTransaction->click_trans_id = $this->reqData['click_trans_id'];
$newTransaction->service_id = $this->reqData['service_id'];
$newTransaction->amount = $this->reqData['amount'];
$newTransaction->sign_time = $this->reqData['sign_time'];
$newTransaction->click_paydoc_id = $this->reqData['click_paydoc_id'];
$newTransaction->create_time = time();
$newTransaction->status = ClickTransactions::STATUS_INACTIVE;
if ($newTransaction->save(false)) {
$merchant_prepare_id = $newTransaction->id;
$return_array = array(
'click_trans_id' => $this->reqData['click_trans_id'], // ID Click Trans
'merchant_trans_id' => $this->reqData['merchant_trans_id'], // ID платежа в биллинге Поставщика
'merchant_prepare_id' => $merchant_prepare_id // ID платежа для подтверждения
);
$result = array_merge(ClickData::getMessage('0'), $return_array);
die(json_encode($result));
}
// other case report: Unknown Error
die(json_encode(1));
}
public function actionComplete()
{
$this->reqData = $_POST;
//if not validated it is end point
//-------------------------------------------
$this->validateData();
//-------------------------------------------
//Error in request from click
if (empty($this->reqData['merchant_prepare_id'])) die(json_encode(ClickData::getMessage('8')));
// --------------------------------------------------------------------------- Start trasaction DB
$transaction = ClickTransactions::findOne(
[
'id' => $this->reqData['merchant_prepare_id'],
'user_id' => $this->reqData['merchant_trans_id'],
'click_trans_id' => $this->reqData['click_trans_id'],
'click_paydoc_id' => $this->reqData['click_paydoc_id'],
'service_id' => $this->reqData['service_id'],
]
);
if ($transaction !== NULL) {
if ($this->reqData['error'] == 0) {
if ($this->reqData['amount'] == $transaction->amount) {
if ($transaction->status == ClickTransactions::STATUS_INACTIVE) {
$db = \Yii::$app->db;
$db_transaction = $db->beginTransaction();
$transaction->status = ClickTransactions::STATUS_ACTIVE;
if (!$transaction->save(false)) {
$db_transaction->rollback();
die(json_encode(ClickData::getMessage('n')));
}
$db_transaction->commit();
$order = Orders::findOne($transaction->user_id); // if pay success -> Change Order status to 2
if(!empty($order)){
$order->state = 2;
$order->save(false);
}
$return_array = [
'click_trans_id' => $transaction->click_trans_id,
'merchant_trans_id' => $transaction->user_id,
'merchant_confirm_id' => $transaction->id,
];
$result = array_merge(ClickData::getMessage('0'), $return_array);
die(json_encode($result));
} elseif ($transaction->status == ClickTransactions::STATUS_CANCEL) {
//"Transaction cancelled"
die(json_encode(ClickData::getMessage('9')));
} elseif ($transaction->status == ClickTransactions::STATUS_ACTIVE) {
die(json_encode(ClickData::getMessage('4')));
} else die(json_encode(ClickData::getMessage('n')));
} else {
if ($transaction->status == ClickTransactions::STATUS_INACTIVE)
//$transaction->delete();
//"Incorrect parameter amount"
die(json_encode(ClickData::getMessage('2')));
}
} elseif ($this->reqData['error'] < 0) {
if ($this->reqData['error'] == -5017) { // "Transaction cancelled"
if ($transaction->status != ClickTransactions::STATUS_ACTIVE) {
$transaction->status = ClickTransactions::STATUS_CANCEL;
if ($transaction->save(false)) {
// "Transaction cancelled"
$this->send_mail_complete($this->reqData, true);
die(json_encode(ClickData::getMessage('9')));
}
die(json_encode(ClickData::getMessage('n')));
} else die(json_encode(ClickData::getMessage('n')));
} elseif ($this->reqData['error'] == -1 && $transaction->status == ClickTransactions::STATUS_ACTIVE) {
die(json_encode(ClickData::getMessage('4')));
} else die(json_encode(ClickData::getMessage('n')));
} // error > 0
else {
die(json_encode(ClickData::getMessage('n')));
}
} // Transaction is null
else {
// Transaction does not exist
die(json_encode(ClickData::getMessage('6')));
}
// echo "Hello from Complete // ";
print_r(ClickData::getMessage(0));
// var_dump(ClickData::$messages);
}
private function validateData()
{
//check complete parameters: Unknown Error
if ((!isset($this->reqData['click_trans_id'])) ||
(!isset($this->reqData['service_id'])) ||
(!isset($this->reqData['click_paydoc_id'])) ||
(!isset($this->reqData['merchant_trans_id'])) ||
(!isset($this->reqData['amount'])) ||
(!isset($this->reqData['action'])) ||
(!isset($this->reqData['sign_time'])) ||
(!isset($this->reqData['sign_string'])) ||
(!isset($this->reqData['error']))
) {
die(json_encode(ClickData::getMessage('n')));
}
// Формирование ХЭШ подписи
$sign_string_veryfied = md5(
$this->reqData['click_trans_id'] .
$this->reqData['service_id'] .
ClickData::SECRET_KEY .
$this->reqData['merchant_trans_id'] .
(($this->reqData['action'] == 1) ? $this->reqData['merchant_prepare_id'] : '') .
$this->reqData['amount'] .
$this->reqData['action'] .
$this->reqData['sign_time']
);
if ($this->reqData['sign_string'] != $sign_string_veryfied) {
die(json_encode(ClickData::getMessage('1')));
}
// Check Actions: Action not found
if (!in_array($this->reqData['action'], [0, 1])) die(json_encode(ClickData::getMessage('3')));
// Check sum: Incorrect parameter amount
if (($this->reqData['amount'] < ClickData::MIN_AMOUNT) || ($this->reqData['amount'] > ClickData::MAX_AMOUNT)) {
die(json_encode(ClickData::getMessage('2')));
}
//
$this->user = User::findOne($this->reqData['merchant_trans_id']);
if ($this->user === NULL) {
// User does not exist
die(json_encode(ClickData::getMessage('5')));
}
}
private function send_mail_complete($data, $notcomplete = false)
{
if (!$notcomplete) {
$message = <<<MESSAGE
<p>Message</p>
MESSAGE;
$subject_text = 'Оплата CLICK';
} else {
$message = <<<MESSAGE
<p>Message</p>
MESSAGE;
$subject_text = 'Отмена CLICK';
}
Yii::$app->mailer->compose()
->setFrom('')
->setTo([''])
->setSubject($subject_text)
->setHtmlBody($message)
->send();
}
/**
* Lists all ClickTransactions models.
* @return mixed
*/
public function actionIndex()
{
return $this->render('click');
}
}