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\NotSupportedException; |
11
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
12
|
|
|
|
13
|
|
|
use vxm\gatewayclients\RequestData as BaseRequestData; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Lớp RequestData cung cấp dữ liệu đã được kiểm tra tính trọn vẹn khi tạo [[request()]] ở [[PaymentGateway]]. |
17
|
|
|
* |
18
|
|
|
* @method PaymentClient getClient() đối tượng client đã dùng để thực thi request. |
19
|
|
|
* |
20
|
|
|
* @property PaymentClient $client đối tượng client đã dùng để thực thi request. |
21
|
|
|
* |
22
|
|
|
* @author Vuong Minh <[email protected]> |
23
|
|
|
* @since 1.0.3 |
24
|
|
|
*/ |
25
|
|
|
class RequestData extends BaseRequestData |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
8 |
|
public function behaviors() |
31
|
|
|
{ |
32
|
|
|
$attributeTypes = [ |
33
|
8 |
|
'orderId' => 'string' |
34
|
|
|
]; |
35
|
|
|
|
36
|
8 |
|
if (in_array($this->command, [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_REFUND], true)) { |
37
|
6 |
|
$attributeTypes['amount'] = 'string'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return [ |
41
|
8 |
|
'typeCast' => [ |
42
|
|
|
'class' => AttributeTypecastBehavior::class, |
43
|
8 |
|
'attributeTypes' => $attributeTypes |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
8 |
|
public function rules() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
8 |
|
[['amount'], 'required', 'on' => [PaymentGateway::RC_REFUND, PaymentGateway::RC_PURCHASE]], |
55
|
|
|
[['returnUrl', 'notifyUrl'], 'required', 'on' => PaymentGateway::RC_PURCHASE], |
56
|
|
|
[['transId'], 'required', 'on' => PaymentGateway::RC_REFUND], |
57
|
|
|
[['partnerCode', 'accessKey', 'requestId', 'orderId', 'signature', 'requestType'], 'required', 'on' => [ |
58
|
|
|
PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_REFUND, PaymentGateway::RC_QUERY_REFUND |
59
|
|
|
]], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
* @throws NotSupportedException |
66
|
|
|
*/ |
67
|
8 |
|
protected function ensureAttributes(array &$attributes) |
68
|
|
|
{ |
69
|
8 |
|
parent::ensureAttributes($attributes); |
70
|
8 |
|
$client = $this->getClient(); |
71
|
8 |
|
$command = $this->getCommand(); |
72
|
8 |
|
$attributes['partnerCode'] = $client->partnerCode; |
73
|
8 |
|
$attributes['accessKey'] = $client->accessKey; |
74
|
|
|
|
75
|
8 |
|
if ($command === PaymentGateway::RC_PURCHASE) { |
76
|
5 |
|
$attributes['orderInfo'] = $attributes['orderInfo'] ?? ''; |
77
|
5 |
|
$attributes['extraData'] = $attributes['extraData'] ?? ''; |
78
|
5 |
|
$attributes['notifyUrl'] = $attributes['notifyUrl'] ?? ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
$attributes['signature'] = $this->getSignature($attributes); |
82
|
8 |
|
$attributes['requestType'] = $this->getRequestType(); |
83
|
8 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Phương thức hổ trợ lấy `requestType` tương ứng với [[getCommand()]] khi gửi dữ liệu đến MOMO. |
87
|
|
|
* |
88
|
|
|
* @return string `requestType` gửi đến MOMO |
89
|
|
|
* @throws NotSupportedException |
90
|
|
|
*/ |
91
|
8 |
|
protected function getRequestType(): string |
92
|
|
|
{ |
93
|
8 |
|
switch ($command = $this->getCommand()) { |
94
|
|
|
case PaymentGateway::RC_PURCHASE: |
95
|
5 |
|
return PaymentGateway::REQUEST_TYPE_PURCHASE; |
96
|
|
|
case PaymentGateway::RC_QUERY_DR: |
97
|
2 |
|
return PaymentGateway::REQUEST_TYPE_QUERY_DR; |
98
|
|
|
case PaymentGateway::RC_REFUND: |
99
|
2 |
|
return PaymentGateway::REQUEST_TYPE_REFUND; |
100
|
|
|
case PaymentGateway::RC_QUERY_REFUND: |
101
|
2 |
|
return PaymentGateway::REQUEST_TYPE_QUERY_REFUND; |
102
|
|
|
default: |
103
|
|
|
throw new NotSupportedException("Not supported command: `$command`"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Phương thức hồ trợ ký dữ liệu gửi đến MOMO. |
109
|
|
|
* |
110
|
|
|
* @param array $attributes mảng chứa các thông tin dùng để tạo chữ ký. |
111
|
|
|
* @return string chữ ký dữ liệu. |
112
|
|
|
* @throws NotSupportedException |
113
|
|
|
*/ |
114
|
8 |
|
protected function getSignature(array $attributes): string |
115
|
|
|
{ |
116
|
8 |
|
switch ($command = $this->getCommand()) { |
117
|
|
|
case PaymentGateway::RC_PURCHASE: |
118
|
|
|
$dataSign = [ |
119
|
5 |
|
'partnerCode' => $attributes['partnerCode'], |
120
|
5 |
|
'accessKey' => $attributes['accessKey'], |
121
|
5 |
|
'requestId' => $attributes['requestId'], |
122
|
5 |
|
'amount' => $attributes['amount'], |
123
|
5 |
|
'orderId' => $attributes['orderId'], |
124
|
5 |
|
'orderInfo' => $attributes['orderInfo'], |
125
|
5 |
|
'returnUrl' => $attributes['returnUrl'], |
126
|
5 |
|
'notifyUrl' => $attributes['notifyUrl'], |
127
|
5 |
|
'extraData' => $attributes['extraData'], |
128
|
|
|
]; |
129
|
5 |
|
break; |
130
|
|
|
case PaymentGateway::RC_QUERY_DR: |
131
|
|
|
case PaymentGateway::RC_QUERY_REFUND: |
132
|
|
|
$dataSign = [ |
133
|
4 |
|
'partnerCode' => $attributes['partnerCode'], |
134
|
4 |
|
'accessKey' => $attributes['accessKey'], |
135
|
4 |
|
'requestId' => $attributes['requestId'], |
136
|
4 |
|
'orderId' => $attributes['orderId'], |
137
|
4 |
|
'requestType' => $this->getRequestType() |
138
|
|
|
]; |
139
|
4 |
|
break; |
140
|
|
|
case PaymentGateway::RC_REFUND: |
141
|
|
|
$dataSign = [ |
142
|
2 |
|
'partnerCode' => $attributes['partnerCode'], |
143
|
2 |
|
'accessKey' => $attributes['accessKey'], |
144
|
2 |
|
'requestId' => $attributes['requestId'], |
145
|
2 |
|
'amount' => $attributes['amount'], |
146
|
2 |
|
'orderId' => $attributes['orderId'], |
147
|
2 |
|
'transId' => $attributes['transId'], |
148
|
2 |
|
'requestType' => $this->getRequestType() |
149
|
|
|
]; |
150
|
2 |
|
break; |
151
|
|
|
default: |
152
|
|
|
throw new NotSupportedException("Not supported command: `$command`"); |
153
|
|
|
} |
154
|
|
|
|
155
|
8 |
|
$dataSign = urldecode(http_build_query($dataSign)); |
156
|
8 |
|
return $this->getClient()->signature($dataSign); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|