|
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
|
|
|
|
|
9
|
|
|
namespace yiiviet\payment; |
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
|
|
13
|
|
|
use GatewayClients\DataInterface; |
|
14
|
|
|
|
|
15
|
|
|
use yii\base\ActionFilter; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
use yii\di\Instance; |
|
18
|
|
|
use yii\web\ForbiddenHttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Lớp VerifyFilter dùng để xác thực tính hợp lệ của dữ liệu đầu vào. |
|
22
|
|
|
* |
|
23
|
|
|
* @property null|DataInterface $verifiedData đối tượng chứa các thuộc tính dữ liệu đã được xác minh tính hợp lệ. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Vuong Minh <[email protected]> |
|
26
|
|
|
* @since 1.0.2 |
|
27
|
|
|
*/ |
|
28
|
|
|
class VerifyFilter extends ActionFilter |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Ánh xạ của [[BasePaymentGateway::VRC_IPN]] hổ trợ khai báo thuộc tính `commands` dễ dàng hơn. |
|
33
|
|
|
*/ |
|
34
|
|
|
const VRC_IPN = BasePaymentGateway::VRC_IPN; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Ánh xạ của [[BasePaymentGateway::VRC_PURCHASE_SUCCESS]] hổ trợ khai báo thuộc tính `commands` dễ dàng hơn. |
|
38
|
|
|
*/ |
|
39
|
|
|
const VRC_PURCHASE_SUCCESS = BasePaymentGateway::VRC_PURCHASE_SUCCESS; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \yiiviet\payment\PaymentGatewayInterface Đối tượng cổng thanh toán dùng để xác thực tính hợp lệ của dữ liệu đầu vào, |
|
43
|
|
|
* bạn có thể thiết lập nó thông qua `id component` trong `app`. |
|
44
|
|
|
*/ |
|
45
|
|
|
public $gateway; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string|int id của client thuộc [[gateway]] dùng để xác thực. Nếu không thiết lập sẽ sử dụng `clientId` mặc định của `gateway`. |
|
49
|
|
|
* @since 1.0.4 |
|
50
|
|
|
*/ |
|
51
|
|
|
public $clientId; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array chứa các action `id` map với `command` cần verify, lưu ý rằng chỉ cần action `id` chứ không phải là action `uniqueId`. |
|
55
|
|
|
* Ví dụ: |
|
56
|
|
|
* |
|
57
|
|
|
* ```php |
|
58
|
|
|
* [ |
|
59
|
|
|
* 'ipn' => 'IPN', |
|
60
|
|
|
* 'purchase-success' => 'purchaseSuccess', |
|
61
|
|
|
* ] |
|
62
|
|
|
*/ |
|
63
|
|
|
public $commands = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var \yii\web\Request đối tượng request dùng để lấy các dữ liệu đầu vào, nếu không thiết lập mặc định sẽ lấy `request` component trong `app`. |
|
67
|
|
|
*/ |
|
68
|
|
|
public $request = 'request'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var bool tự động tắt kiểm tra `csrf` của controller hiện tại. |
|
72
|
|
|
* @since 1.0.3 |
|
73
|
|
|
*/ |
|
74
|
|
|
public $autoDisableControllerCsrfValidation = true; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
* @throws InvalidConfigException |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function init() |
|
81
|
|
|
{ |
|
82
|
4 |
|
if ($this->gateway === null) { |
|
83
|
|
|
|
|
84
|
1 |
|
throw new InvalidConfigException('`gateway` property must be set!'); |
|
85
|
|
|
} else { |
|
86
|
|
|
|
|
87
|
3 |
|
$this->gateway = Instance::ensure($this->gateway, 'yiiviet\payment\PaymentGatewayInterface'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
if (empty($this->commands)) { |
|
91
|
|
|
|
|
92
|
1 |
|
throw new InvalidConfigException('`commands` property must be set!'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$this->request = Instance::ensure($this->request, 'yii\web\Request'); |
|
96
|
|
|
|
|
97
|
2 |
|
parent::init(); |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
* @throws ForbiddenHttpException|InvalidConfigException |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function beforeAction($action) |
|
105
|
|
|
{ |
|
106
|
2 |
|
$actionId = $action->id; |
|
107
|
2 |
|
$command = $this->commands[$actionId] ?? null; |
|
108
|
|
|
|
|
109
|
2 |
|
if ($command === null) { |
|
110
|
|
|
|
|
111
|
|
|
throw new InvalidConfigException("Can't find verify command of action `$actionId`"); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
$verifiedData = $this->gateway->verifyRequest($command, $this->request, $this->clientId); |
|
115
|
|
|
|
|
116
|
2 |
|
if (!$verifiedData instanceof DataInterface) { |
|
117
|
|
|
|
|
118
|
1 |
|
throw new ForbiddenHttpException( 'You are not allowed to perform this action.'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$this->_verifiedData = $verifiedData; |
|
122
|
|
|
|
|
123
|
1 |
|
if ($this->autoDisableControllerCsrfValidation) { |
|
124
|
1 |
|
$action->controller->enableCsrfValidation = false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @var DataInterface đối tượng tập hợp các thuộc tính dữ liệu đã xác thực. |
|
133
|
|
|
* @see [[getVerifiedData()]] |
|
134
|
|
|
*/ |
|
135
|
|
|
private $_verifiedData; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Phương thức hổ trợ lấy dữ liệu đã xác thực tính hợp lệ. |
|
139
|
|
|
* |
|
140
|
|
|
* @return null|DataInterface đối tượng chứa các thuộc tính dữ liệu đã xác thực. |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function getVerifiedData(): ?DataInterface |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->_verifiedData; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|