|
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\Payment\Kernel; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Support; |
|
15
|
|
|
use EasyWeChat\Kernel\Traits\HasHttpRequests; |
|
16
|
|
|
use EasyWeChat\Payment\Application; |
|
17
|
|
|
use GuzzleHttp\MessageFormatter; |
|
18
|
|
|
use GuzzleHttp\Middleware; |
|
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class BaseClient. |
|
23
|
|
|
* |
|
24
|
|
|
* @author overtrue <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class BaseClient |
|
27
|
|
|
{ |
|
28
|
|
|
use HasHttpRequests { request as performRequest; } |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \EasyWeChat\Payment\Application |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $app; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \EasyWeChat\Payment\Application $app |
|
39
|
|
|
*/ |
|
40
|
44 |
|
public function __construct(Application $app) |
|
41
|
|
|
{ |
|
42
|
44 |
|
$this->app = $app; |
|
43
|
|
|
|
|
44
|
44 |
|
$this->setHttpClient($this->app['http_client']); |
|
45
|
44 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Extra request params. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
4 |
|
protected function prepends() |
|
53
|
|
|
{ |
|
54
|
4 |
|
return []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Make a API request. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $endpoint |
|
61
|
|
|
* @param array $params |
|
62
|
|
|
* @param string $method |
|
63
|
|
|
* @param array $options |
|
64
|
|
|
* @param bool $returnResponse |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
69
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
70
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
71
|
4 |
|
*/ |
|
72
|
|
|
protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false) |
|
73
|
|
|
{ |
|
74
|
4 |
|
$base = [ |
|
75
|
4 |
|
'mch_id' => $this->app['config']['mch_id'], |
|
76
|
4 |
|
'nonce_str' => uniqid(), |
|
77
|
4 |
|
'sub_mch_id' => $this->app['config']['sub_mch_id'], |
|
78
|
|
|
'sub_appid' => $this->app['config']['sub_appid'], |
|
79
|
|
|
]; |
|
80
|
4 |
|
|
|
81
|
|
|
$params = array_filter(array_merge($base, $this->prepends(), $params), 'strlen'); |
|
82
|
4 |
|
|
|
83
|
4 |
|
$secretKey = $this->app->getKey($endpoint); |
|
84
|
1 |
|
if ('HMAC-SHA256' === ($params['sign_type'] ?? 'MD5')) { |
|
85
|
1 |
|
$encryptMethod = function ($str) use ($secretKey) { |
|
86
|
1 |
|
return hash_hmac('sha256', $str, $secretKey); |
|
87
|
|
|
}; |
|
88
|
3 |
|
} else { |
|
89
|
|
|
$encryptMethod = 'md5'; |
|
90
|
4 |
|
} |
|
91
|
|
|
$params['sign'] = Support\generate_sign($params, $secretKey, $encryptMethod); |
|
|
|
|
|
|
92
|
4 |
|
|
|
93
|
4 |
|
$options = array_merge([ |
|
94
|
|
|
'body' => Support\XML::build($params), |
|
95
|
|
|
], $options); |
|
96
|
4 |
|
|
|
97
|
|
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
|
98
|
4 |
|
|
|
99
|
|
|
$response = $this->performRequest($endpoint, $method, $options); |
|
100
|
4 |
|
|
|
101
|
|
|
return $returnResponse ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Log the request. |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Closure |
|
108
|
8 |
|
*/ |
|
109
|
|
|
protected function logMiddleware() |
|
110
|
8 |
|
{ |
|
111
|
|
|
$formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG); |
|
112
|
8 |
|
|
|
113
|
|
|
return Middleware::log($this->app['logger'], $formatter); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Make a request and return raw response. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $endpoint |
|
120
|
|
|
* @param array $params |
|
121
|
|
|
* @param string $method |
|
122
|
|
|
* @param array $options |
|
123
|
|
|
* |
|
124
|
|
|
* @return ResponseInterface |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
127
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
128
|
8 |
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
129
|
|
|
*/ |
|
130
|
8 |
|
protected function requestRaw(string $endpoint, array $params = [], $method = 'post', array $options = []) |
|
131
|
|
|
{ |
|
132
|
|
|
$response = $this->request($endpoint, $params, $method, $options, true); |
|
133
|
|
|
|
|
134
|
|
|
// Fix phpstan error |
|
135
|
|
|
assert($response instanceof ResponseInterface); |
|
136
|
|
|
|
|
137
|
|
|
return $response; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Make a request and return an array. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $endpoint |
|
144
|
|
|
* @param array $params |
|
145
|
|
|
* @param string $method |
|
146
|
2 |
|
* @param array $options |
|
147
|
|
|
* |
|
148
|
2 |
|
* @return array |
|
149
|
2 |
|
* |
|
150
|
2 |
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
151
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
152
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
153
|
2 |
|
*/ |
|
154
|
|
|
protected function requestArray(string $endpoint, array $params = [], $method = 'post', array $options = []): array |
|
155
|
|
|
{ |
|
156
|
|
|
$response = $this->requestRaw($endpoint, $params, $method, $options); |
|
157
|
|
|
|
|
158
|
|
|
return $this->castResponseToType($response, 'array'); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Request with SSL. |
|
163
|
14 |
|
* |
|
164
|
|
|
* @param string $endpoint |
|
165
|
14 |
|
* @param array $params |
|
166
|
|
|
* @param string $method |
|
167
|
|
|
* @param array $options |
|
168
|
|
|
* |
|
169
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
170
|
|
|
* |
|
171
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
172
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
173
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function safeRequest($endpoint, array $params, $method = 'post', array $options = []) |
|
176
|
|
|
{ |
|
177
|
|
|
$options = array_merge([ |
|
178
|
|
|
'cert' => $this->app['config']->get('cert_path'), |
|
179
|
|
|
'ssl_key' => $this->app['config']->get('key_path'), |
|
180
|
|
|
], $options); |
|
181
|
|
|
|
|
182
|
|
|
return $this->request($endpoint, $params, $method, $options); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Wrapping an API endpoint. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $endpoint |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function wrap(string $endpoint): string |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->app->inSandbox() ? "sandboxnew/{$endpoint}" : $endpoint; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|