|
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\Client; |
|
18
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
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
|
|
|
public function __construct(Application $app) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->app = $app; |
|
43
|
|
|
|
|
44
|
|
|
if ($this->app->inSandbox()) { |
|
45
|
|
|
$config = $this->app['http_client']->getConfig(); |
|
46
|
|
|
$config['base_uri'] = new Uri($this->app['config']->get('http.base_uri').'/sandboxnew/'); |
|
47
|
|
|
|
|
48
|
|
|
$client = new Client($config); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->setHttpClient($client ?? $this->app['http_client']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Extra request params. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function prepends() |
|
60
|
|
|
{ |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Make a API request. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $endpoint |
|
68
|
|
|
* @param array $params |
|
69
|
|
|
* @param string $method |
|
70
|
|
|
* @param array $options |
|
71
|
|
|
* @param bool $returnResponse |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function request(string $endpoint, array $params = [], $method = 'post', array $options = [], $returnResponse = false) |
|
76
|
|
|
{ |
|
77
|
|
|
$base = [ |
|
78
|
|
|
'appid' => $this->app['config']['app_id'], |
|
79
|
|
|
'mch_id' => $this->app['config']['mch_id'], |
|
80
|
|
|
'nonce_str' => uniqid(), |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$params = array_filter(array_merge($base, $this->prepends(), $params)); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->app->inSandbox() && !$this->app['sandbox']->except($endpoint)) { |
|
86
|
|
|
$key = $this->app['sandbox']->key(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$params['sign'] = Support\generate_sign($params, $key ?? $this->app['config']->key); |
|
90
|
|
|
|
|
91
|
|
|
$options = array_merge([ |
|
92
|
|
|
'body' => Support\XML::build($params), |
|
93
|
|
|
], $options); |
|
94
|
|
|
|
|
95
|
|
|
$response = $this->performRequest($endpoint, $method, $options); |
|
96
|
|
|
|
|
97
|
|
|
return $returnResponse ? $response : $this->resolveResponse($response, $this->app->config->get('response_type')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Make a request and return raw response. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $endpoint |
|
104
|
|
|
* @param array $params |
|
105
|
|
|
* @param string $method |
|
106
|
|
|
* @param array $options |
|
107
|
|
|
* |
|
108
|
|
|
* @return array|Support\Collection|object|ResponseInterface|string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function requestRaw($endpoint, array $params = [], $method = 'post', array $options = []) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->request($endpoint, $params, $method, $options, true); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Request with SSL. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $endpoint |
|
119
|
|
|
* @param array $params |
|
120
|
|
|
* @param string $method |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function safeRequest($endpoint, array $params, $method = 'post') |
|
125
|
|
|
{ |
|
126
|
|
|
$options = [ |
|
127
|
|
|
'cert' => $this->app['config']->get('cert_path'), |
|
128
|
|
|
'ssl_key' => $this->app['config']->get('key_path'), |
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
|
|
return $this->request($endpoint, $params, $method, $options); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|