1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yiiviet/yii2-payment |
4
|
|
|
* @copyright Copyright (c) 2017 Yii Viet |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yiiviet\payment\momo; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\validators\Validator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Lớp SignatureValidator hổ trợ xác minh chữ ký dữ liệu từ MoMo gửi sang. |
15
|
|
|
* |
16
|
|
|
* @author Vuong Minh <[email protected]> |
17
|
|
|
* @since 1.0.3 |
18
|
|
|
*/ |
19
|
|
|
class SignatureValidator extends Validator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var PaymentClient đối tượng dùng để xác minh tính hợp lệ của chữ ký dữ liệu. |
23
|
|
|
*/ |
24
|
|
|
public $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array cung cấp các attributes dùng để xác minh tính hợp lệ của chữ ký dữ liệu phản hồi từ MoMo. |
28
|
|
|
*/ |
29
|
|
|
public $dataSignAttributes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
* @throws InvalidConfigException |
34
|
|
|
*/ |
35
|
1 |
|
public function init() |
36
|
|
|
{ |
37
|
1 |
|
if ($this->client === null) { |
38
|
|
|
throw new InvalidConfigException('Property `client` must be set!'); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
if ($this->message === null) { |
42
|
1 |
|
$this->message = '{attribute} is invalid!'; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
parent::init(); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
* @throws \yii\base\NotSupportedException |
51
|
|
|
*/ |
52
|
|
|
public function validateAttribute($model, $attribute) |
53
|
|
|
{ |
54
|
|
|
$data = array_merge(array_fill_keys($this->dataSignAttributes, ''), $model->toArray()); |
55
|
|
|
$dataSign = array_intersect_key($data, array_flip($this->dataSignAttributes)); |
56
|
|
|
$actualSignature = urldecode(http_build_query($dataSign)); |
57
|
|
|
|
58
|
|
|
if (!$this->client->validateSignature($actualSignature, $model->$attribute ?? '')) { |
59
|
|
|
$this->addError($model, $attribute, $this->message); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|