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
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
$attributes['requestType'] = $this->getRequestType(); |
81
|
8 |
|
$attributes['signature'] = $this->getSignature($attributes); |
82
|
8 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Phương thức hổ trợ lấy `requestType` tương ứng với [[getCommand()]] khi gửi dữ liệu đến MOMO. |
86
|
|
|
* |
87
|
|
|
* @return string `requestType` gửi đến MOMO |
88
|
|
|
* @throws NotSupportedException |
89
|
|
|
*/ |
90
|
8 |
|
protected function getRequestType(): string |
91
|
|
|
{ |
92
|
8 |
|
switch ($command = $this->getCommand()) { |
93
|
8 |
|
case PaymentGateway::RC_PURCHASE: |
94
|
5 |
|
return 'captureMoMoWallet'; |
95
|
6 |
|
case PaymentGateway::RC_QUERY_DR: |
96
|
2 |
|
return 'transactionStatus'; |
97
|
4 |
|
case PaymentGateway::RC_REFUND: |
98
|
2 |
|
return 'refundMoMoWallet'; |
99
|
2 |
|
case PaymentGateway::RC_QUERY_REFUND: |
100
|
2 |
|
return 'refundStatus'; |
101
|
|
|
default: |
102
|
|
|
throw new NotSupportedException("Not supported command: `$command`"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Phương thức hồ trợ ký dữ liệu gửi đến MOMO. |
108
|
|
|
* |
109
|
|
|
* @param array $attributes mảng chứa các thông tin dùng để tạo chữ ký. |
110
|
|
|
* @return string chữ ký dữ liệu. |
111
|
|
|
* @throws NotSupportedException |
112
|
|
|
*/ |
113
|
8 |
|
protected function getSignature(array $attributes): string |
114
|
|
|
{ |
115
|
8 |
|
switch ($command = $this->getCommand()) { |
116
|
8 |
|
case PaymentGateway::RC_PURCHASE: |
117
|
|
|
$attributesSign = [ |
118
|
5 |
|
'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'returnUrl', 'notifyUrl', 'extraData' |
119
|
|
|
]; |
120
|
5 |
|
break; |
121
|
6 |
|
case PaymentGateway::RC_QUERY_DR: |
122
|
4 |
View Code Duplication |
case PaymentGateway::RC_QUERY_REFUND: |
|
|
|
|
123
|
|
|
$attributesSign = [ |
124
|
4 |
|
'partnerCode', 'accessKey', 'requestId', 'orderId', 'requestType' |
125
|
|
|
]; |
126
|
4 |
|
break; |
127
|
2 |
View Code Duplication |
case PaymentGateway::RC_REFUND: |
|
|
|
|
128
|
|
|
$attributesSign = [ |
129
|
2 |
|
'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'transId', 'requestType' |
130
|
|
|
]; |
131
|
2 |
|
break; |
132
|
|
|
default: |
133
|
|
|
throw new NotSupportedException("Not supported command: `$command`"); |
134
|
|
|
} |
135
|
|
|
|
136
|
8 |
|
$data = []; |
137
|
|
|
|
138
|
8 |
|
foreach ($attributesSign as $attributeSign) { |
139
|
8 |
|
if (isset($attributes[$attributeSign])) { |
140
|
8 |
|
$data[$attributeSign] = $attributes[$attributeSign]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
8 |
|
$strSign = urldecode(http_build_query($data)); |
145
|
|
|
|
146
|
8 |
|
return $this->getClient()->signature($strSign); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.