|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\MicroMerchant\Kernel; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Support; |
|
15
|
|
|
use EasyWeChat\Kernel\Traits\HasHttpRequests; |
|
16
|
|
|
use EasyWeChat\MicroMerchant\Application; |
|
17
|
|
|
use EasyWeChat\MicroMerchant\Kernel\Exceptions\EncryptException; |
|
18
|
|
|
use GuzzleHttp\MessageFormatter; |
|
19
|
|
|
use GuzzleHttp\Middleware; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class BaseClient. |
|
23
|
|
|
* |
|
24
|
|
|
* @author overtrue <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class BaseClient |
|
27
|
|
|
{ |
|
28
|
|
|
use HasHttpRequests { |
|
|
|
|
|
|
29
|
|
|
request as performRequest; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $microCertificates; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $certificates; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \EasyWeChat\MicroMerchant\Application |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $app; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* BaseClient constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \EasyWeChat\MicroMerchant\Application $app |
|
51
|
|
|
*/ |
|
52
|
22 |
|
public function __construct(Application $app) |
|
53
|
|
|
{ |
|
54
|
22 |
|
$this->app = $app; |
|
55
|
|
|
|
|
56
|
22 |
|
$this->microCertificates = $this->app['config']->mch_id.'_micro_certificates'; |
|
57
|
|
|
|
|
58
|
22 |
|
$this->setHttpClient($this->app['http_client']); |
|
59
|
22 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Extra request params. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
4 |
|
protected function prepends() |
|
67
|
|
|
{ |
|
68
|
4 |
|
return []; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* request. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $endpoint |
|
75
|
|
|
* @param array $params |
|
76
|
|
|
* @param string $method |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* @param bool $returnResponse |
|
79
|
|
|
* |
|
80
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
83
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
84
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
|
85
|
|
|
*/ |
|
86
|
4 |
|
protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false) |
|
87
|
|
|
{ |
|
88
|
|
|
$base = [ |
|
89
|
4 |
|
'mch_id' => $this->app['config']['mch_id'], |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
4 |
|
$params = array_filter(array_merge($base, $this->prepends(), $params)); |
|
93
|
|
|
|
|
94
|
4 |
|
$secretKey = $this->app->getKey(); |
|
95
|
4 |
|
if ('HMAC-SHA256' === ($params['sign_type'] ?? 'MD5')) { |
|
96
|
1 |
|
$encryptMethod = function ($str) use ($secretKey) { |
|
97
|
1 |
|
return hash_hmac('sha256', $str, $secretKey); |
|
98
|
1 |
|
}; |
|
99
|
|
|
} else { |
|
100
|
3 |
|
$encryptMethod = 'md5'; |
|
101
|
|
|
} |
|
102
|
4 |
|
$params['sign'] = Support\generate_sign($params, $secretKey, $encryptMethod); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
4 |
|
$options = array_merge([ |
|
105
|
4 |
|
'body' => Support\XML::build($params), |
|
106
|
|
|
], $options); |
|
107
|
|
|
|
|
108
|
4 |
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
|
109
|
|
|
|
|
110
|
4 |
|
$response = $this->performRequest($endpoint, $method, $options); |
|
111
|
4 |
|
$response = $returnResponse ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
112
|
|
|
// auto verify signature |
|
113
|
4 |
|
if (!$returnResponse && 'array' === ($this->app->config->get('response_type') ?? 'array')) { |
|
114
|
1 |
|
$this->app->verifySignature($response); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
4 |
|
return $response; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Log the request. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Closure |
|
124
|
|
|
*/ |
|
125
|
4 |
|
protected function logMiddleware() |
|
126
|
|
|
{ |
|
127
|
4 |
|
$formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG); |
|
128
|
|
|
|
|
129
|
4 |
|
return Middleware::log($this->app['logger'], $formatter); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make a request and return raw response. |
|
134
|
|
|
* |
|
135
|
|
|
* @param $endpoint |
|
136
|
|
|
* @param array $params |
|
137
|
|
|
* @param string $method |
|
138
|
|
|
* @param array $options |
|
139
|
|
|
* |
|
140
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
141
|
|
|
* |
|
142
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
143
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
144
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
|
145
|
|
|
*/ |
|
146
|
4 |
|
protected function requestRaw($endpoint, array $params = [], $method = 'post', array $options = []) |
|
147
|
|
|
{ |
|
148
|
4 |
|
return $this->request($endpoint, $params, $method, $options, true); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Request with SSL. |
|
153
|
|
|
* |
|
154
|
|
|
* @param $endpoint |
|
155
|
|
|
* @param array $params |
|
156
|
|
|
* @param string $method |
|
157
|
|
|
* @param array $options |
|
158
|
|
|
* |
|
159
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
162
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
163
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
|
164
|
|
|
*/ |
|
165
|
1 |
|
protected function safeRequest($endpoint, array $params, $method = 'post', array $options = []) |
|
166
|
|
|
{ |
|
167
|
1 |
|
$options = array_merge([ |
|
168
|
1 |
|
'cert' => $this->app['config']->get('cert_path'), |
|
169
|
1 |
|
'ssl_key' => $this->app['config']->get('key_path'), |
|
170
|
|
|
], $options); |
|
171
|
|
|
|
|
172
|
1 |
|
return $this->request($endpoint, $params, $method, $options); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* processing parameters contain fields that require sensitive information encryption. |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $params |
|
179
|
|
|
* |
|
180
|
|
|
* @return array |
|
181
|
|
|
* |
|
182
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
183
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
184
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\EncryptException |
|
185
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidExtensionException |
|
186
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException |
|
187
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
188
|
|
|
*/ |
|
189
|
1 |
|
protected function processParams(array $params) |
|
190
|
|
|
{ |
|
191
|
1 |
|
$cert = $this->app->getCertficates(); |
|
|
|
|
|
|
192
|
1 |
|
$this->certificates = $cert['certificates']; |
|
193
|
1 |
|
$params['cert_sn'] = $cert['serial_no']; |
|
|
|
|
|
|
194
|
1 |
|
$sensitive_fields = $this->getSensitiveFieldsName(); |
|
195
|
1 |
|
foreach ($params as $k => $v) { |
|
196
|
1 |
|
if (in_array($k, $sensitive_fields, true)) { |
|
197
|
1 |
|
$params[$k] = $this->encryptSensitiveInformation($v); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
1 |
|
return $params; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* To id card, mobile phone number and other fields sensitive information encryption. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $string |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
* |
|
211
|
|
|
* @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\EncryptException |
|
212
|
|
|
*/ |
|
213
|
1 |
|
protected function encryptSensitiveInformation(string $string) |
|
214
|
|
|
{ |
|
215
|
1 |
|
$encrypted = ''; |
|
|
|
|
|
|
216
|
1 |
|
$publicKeyResource = openssl_get_publickey($this->certificates); |
|
217
|
1 |
|
$f = openssl_public_encrypt($string, $encrypted, $publicKeyResource); |
|
|
|
|
|
|
218
|
1 |
|
openssl_free_key($publicKeyResource); |
|
219
|
1 |
|
if ($f) { |
|
220
|
1 |
|
return base64_encode($encrypted); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
throw new EncryptException('Encryption of sensitive information failed'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* get sensitive fields name. |
|
228
|
|
|
* |
|
229
|
|
|
* @return array |
|
230
|
|
|
*/ |
|
231
|
1 |
|
protected function getSensitiveFieldsName() |
|
232
|
|
|
{ |
|
233
|
|
|
return [ |
|
234
|
1 |
|
'id_card_name', |
|
235
|
|
|
'id_card_number', |
|
236
|
|
|
'account_name', |
|
237
|
|
|
'account_number', |
|
238
|
|
|
'contact', |
|
239
|
|
|
'contact_phone', |
|
240
|
|
|
'contact_email', |
|
241
|
|
|
'legal_person', |
|
242
|
|
|
'mobile_phone', |
|
243
|
|
|
'email', |
|
244
|
|
|
]; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|