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
|
45 |
|
public function __construct(Application $app) |
41
|
|
|
{ |
42
|
45 |
|
$this->app = $app; |
43
|
|
|
|
44
|
45 |
|
$this->setHttpClient($this->app['http_client']); |
45
|
45 |
|
} |
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
|
|
|
*/ |
72
|
4 |
|
protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false) |
73
|
|
|
{ |
74
|
|
|
$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
|
4 |
|
'sub_appid' => $this->app['config']['sub_appid'], |
79
|
|
|
]; |
80
|
|
|
|
81
|
4 |
|
$params = array_filter(array_merge($base, $this->prepends(), $params), 'strlen'); |
82
|
|
|
|
83
|
4 |
|
$secretKey = $this->app->getKey($endpoint); |
84
|
4 |
|
if ('HMAC-SHA256' === ($params['sign_type'] ?? 'MD5')) { |
85
|
|
|
$encryptMethod = function ($str) use ($secretKey) { |
86
|
1 |
|
return hash_hmac('sha256', $str, $secretKey); |
87
|
1 |
|
}; |
88
|
|
|
} else { |
89
|
3 |
|
$encryptMethod = 'md5'; |
90
|
|
|
} |
91
|
4 |
|
$params['sign'] = Support\generate_sign($params, $secretKey, $encryptMethod); |
|
|
|
|
92
|
|
|
|
93
|
4 |
|
$options = array_merge([ |
94
|
4 |
|
'body' => Support\XML::build($params), |
95
|
|
|
], $options); |
96
|
|
|
|
97
|
4 |
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
98
|
|
|
|
99
|
4 |
|
$response = $this->performRequest($endpoint, $method, $options); |
100
|
|
|
|
101
|
4 |
|
return $returnResponse ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Log the request. |
106
|
|
|
* |
107
|
|
|
* @return \Closure |
108
|
|
|
*/ |
109
|
8 |
|
protected function logMiddleware() |
110
|
|
|
{ |
111
|
8 |
|
$formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG); |
112
|
|
|
|
113
|
8 |
|
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
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
129
|
|
|
*/ |
130
|
8 |
|
protected function requestRaw(string $endpoint, array $params = [], $method = 'post', array $options = []) |
131
|
|
|
{ |
132
|
|
|
/** @var ResponseInterface $response */ |
133
|
8 |
|
$response = $this->request($endpoint, $params, $method, $options, true); |
134
|
|
|
|
135
|
8 |
|
return $response; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Make a request and return an array. |
140
|
|
|
* |
141
|
|
|
* @param string $endpoint |
142
|
|
|
* @param array $params |
143
|
|
|
* @param string $method |
144
|
|
|
* @param array $options |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
* |
148
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
149
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
150
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
151
|
|
|
*/ |
152
|
|
|
protected function requestArray(string $endpoint, array $params = [], $method = 'post', array $options = []): array |
153
|
|
|
{ |
154
|
|
|
$response = $this->requestRaw($endpoint, $params, $method, $options); |
155
|
|
|
|
156
|
|
|
return $this->castResponseToType($response, 'array'); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Request with SSL. |
161
|
|
|
* |
162
|
|
|
* @param string $endpoint |
163
|
|
|
* @param array $params |
164
|
|
|
* @param string $method |
165
|
|
|
* @param array $options |
166
|
|
|
* |
167
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
168
|
|
|
* |
169
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
170
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
171
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
172
|
|
|
*/ |
173
|
2 |
|
protected function safeRequest($endpoint, array $params, $method = 'post', array $options = []) |
174
|
|
|
{ |
175
|
2 |
|
$options = array_merge([ |
176
|
2 |
|
'cert' => $this->app['config']->get('cert_path'), |
177
|
2 |
|
'ssl_key' => $this->app['config']->get('key_path'), |
178
|
|
|
], $options); |
179
|
|
|
|
180
|
2 |
|
return $this->request($endpoint, $params, $method, $options); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Wrapping an API endpoint. |
185
|
|
|
* |
186
|
|
|
* @param string $endpoint |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
15 |
|
protected function wrap(string $endpoint): string |
191
|
|
|
{ |
192
|
15 |
|
return $this->app->inSandbox() ? "sandboxnew/{$endpoint}" : $endpoint; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|