|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/phpviet/omnipay-vnpay |
|
4
|
|
|
* @copyright (c) PHP Viet |
|
5
|
|
|
* @license [MIT](http://www.opensource.org/licenses/MIT) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Omnipay\VNPay\Support; |
|
9
|
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Vuong Minh <[email protected]> |
|
14
|
|
|
* @since 1.0.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class Signature |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Khóa bí mật dùng để tạo và kiểm tra chữ ký dữ liệu. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $hashSecret; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Loại thuật toán mã hóa sẽ sử dụng. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $hashType; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Khởi tạo đối tượng DataSignature. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $hashSecret |
|
36
|
|
|
* @param string $hashType |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(string $hashSecret, string $hashType = 'sha256') |
|
40
|
|
|
{ |
|
41
|
|
|
if (! $this->isSupportHashType($hashType)) { |
|
42
|
|
|
throw new InvalidArgumentException(sprintf('Hash type: `%s` is not supported by VNPay', $hashType)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->hashType = $hashType; |
|
46
|
|
|
$this->hashSecret = $hashSecret; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Trả về chữ ký dữ liệu của dữ liệu truyền vào. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $data |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function generate(array $data): string |
|
56
|
|
|
{ |
|
57
|
|
|
ksort($data); |
|
58
|
|
|
$dataSign = $this->hashSecret.urldecode(http_build_query($data)); |
|
59
|
|
|
|
|
60
|
|
|
return hash($this->hashType, $dataSign); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Kiểm tra tính hợp lệ của chữ ký dữ liệu so với dữ liệu truyền vào. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $data |
|
67
|
|
|
* @param string $expect |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function validate(array $data, string $expect): bool |
|
71
|
|
|
{ |
|
72
|
|
|
$actual = $this->generate($data); |
|
73
|
|
|
|
|
74
|
|
|
return 0 === strcasecmp($expect, $actual); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Phương thức cho biết loại mã hóa truyền vào có được VNPay hổ trợ hay không. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $type |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function isSupportHashType(string $type): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return 0 === strcasecmp($type, 'md5') || 0 === strcasecmp($type, 'sha256'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|