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 | |||
85 | 4 | $encryptMethod = Support\get_encrypt_method(Support\Arr::get($params, 'sign_type', 'MD5'), $secretKey); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
86 | |||
87 | 4 | $params['sign'] = Support\generate_sign($params, $secretKey, $encryptMethod); |
|
88 | |||
89 | 4 | $options = array_merge([ |
|
90 | 4 | 'body' => Support\XML::build($params), |
|
91 | ], $options); |
||
92 | |||
93 | 4 | $this->pushMiddleware($this->logMiddleware(), 'log'); |
|
94 | |||
95 | 4 | $response = $this->performRequest($endpoint, $method, $options); |
|
96 | |||
97 | 4 | return $returnResponse ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Log the request. |
||
102 | * |
||
103 | * @return \Closure |
||
104 | */ |
||
105 | 8 | protected function logMiddleware() |
|
106 | { |
||
107 | 8 | $formatter = new MessageFormatter($this->app['config']['http.log_template'] ?? MessageFormatter::DEBUG); |
|
108 | |||
109 | 8 | return Middleware::log($this->app['logger'], $formatter); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Make a request and return raw response. |
||
114 | * |
||
115 | * @param string $endpoint |
||
116 | * @param array $params |
||
117 | * @param string $method |
||
118 | * @param array $options |
||
119 | * |
||
120 | * @return ResponseInterface |
||
121 | * |
||
122 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
||
123 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
124 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
125 | */ |
||
126 | 8 | protected function requestRaw(string $endpoint, array $params = [], $method = 'post', array $options = []) |
|
127 | { |
||
128 | /** @var ResponseInterface $response */ |
||
129 | 8 | $response = $this->request($endpoint, $params, $method, $options, true); |
|
130 | |||
131 | 8 | return $response; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Make a request and return an array. |
||
136 | * |
||
137 | * @param string $endpoint |
||
138 | * @param array $params |
||
139 | * @param string $method |
||
140 | * @param array $options |
||
141 | * |
||
142 | * @return array |
||
143 | * |
||
144 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
145 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
||
146 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
147 | */ |
||
148 | protected function requestArray(string $endpoint, array $params = [], $method = 'post', array $options = []): array |
||
149 | { |
||
150 | $response = $this->requestRaw($endpoint, $params, $method, $options); |
||
151 | |||
152 | return $this->castResponseToType($response, 'array'); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Request with SSL. |
||
157 | * |
||
158 | * @param string $endpoint |
||
159 | * @param array $params |
||
160 | * @param string $method |
||
161 | * @param array $options |
||
162 | * |
||
163 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
164 | * |
||
165 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
||
166 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
167 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
168 | */ |
||
169 | 2 | protected function safeRequest($endpoint, array $params, $method = 'post', array $options = []) |
|
170 | { |
||
171 | 2 | $options = array_merge([ |
|
172 | 2 | 'cert' => $this->app['config']->get('cert_path'), |
|
173 | 2 | 'ssl_key' => $this->app['config']->get('key_path'), |
|
174 | ], $options); |
||
175 | |||
176 | 2 | return $this->request($endpoint, $params, $method, $options); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Wrapping an API endpoint. |
||
181 | * |
||
182 | * @param string $endpoint |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 15 | protected function wrap(string $endpoint): string |
|
187 | { |
||
188 | 15 | return $this->app->inSandbox() ? "sandboxnew/{$endpoint}" : $endpoint; |
|
189 | } |
||
190 | } |
||
191 |