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\InvalidCallException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait SignatureValidatorTrait cung cấp phương thức xác minh chữ ký dữ liệu từ MOMO. |
14
|
|
|
* |
15
|
|
|
* @author Vuong Minh <[email protected]> |
16
|
|
|
* @since 1.0.3 |
17
|
|
|
*/ |
18
|
|
|
trait SignatureValidatorTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ MOMO gửi sang. |
22
|
|
|
* |
23
|
|
|
* @param string $attribute có giá trị là chữ ký cần kiểm tra. |
24
|
|
|
* @param array $params thiết lập từ rule |
25
|
|
|
* @param \yii\validators\InlineValidator $validator |
26
|
|
|
* @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException|InvalidCallException |
27
|
|
|
*/ |
28
|
4 |
|
public function signatureValidator($attribute, $params, \yii\validators\InlineValidator $validator) |
|
|
|
|
29
|
|
|
{ |
30
|
4 |
|
$dataSignAttributes = $this->getDataSignAttributes(); |
31
|
4 |
|
$data = array_merge(array_fill_keys($dataSignAttributes, ''), $this->get(false)); |
|
|
|
|
32
|
4 |
|
$dataSign = array_intersect_key($data, array_flip($dataSignAttributes)); |
33
|
4 |
|
$dataSign = urldecode(http_build_query($dataSign)); |
34
|
|
|
|
35
|
4 |
|
$client = $this->getClient(); |
|
|
|
|
36
|
|
|
|
37
|
4 |
|
if (!$client->validateSignature($dataSign, $this->$attribute ?? '')) { |
38
|
|
|
$validator->addError($this, $attribute, $validator->message); |
39
|
|
|
} |
40
|
4 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Phương thức hồ trợ 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. |
44
|
|
|
* |
45
|
|
|
* @return array attributes dùng để xác minh tính hợp lệ. |
46
|
|
|
*/ |
47
|
|
|
protected function getDataSignAttributes(): array |
48
|
|
|
{ |
49
|
|
|
throw new InvalidCallException(__METHOD__ . ' must be override by class used it!'); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.