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
|
|
|
'sub_mch_id' => $this->app['config']['sub_mch_id'], |
82
|
|
|
'sub_appid' => $this->app['config']['sub_appid'], |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$params = array_filter(array_merge($base, $this->prepends(), $params)); |
86
|
|
|
|
87
|
|
|
$params['sign'] = Support\generate_sign($params, $this->getKey($endpoint)); |
88
|
|
|
|
89
|
|
|
$options = array_merge([ |
90
|
|
|
'body' => Support\XML::build($params), |
91
|
|
|
], $options); |
92
|
|
|
|
93
|
|
|
$response = $this->performRequest($endpoint, $method, $options); |
94
|
|
|
|
95
|
|
|
return $returnResponse ? $response : $this->resolveResponse($response, $this->app->config->get('response_type')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Make a request and return raw response. |
100
|
|
|
* |
101
|
|
|
* @param string $endpoint |
102
|
|
|
* @param array $params |
103
|
|
|
* @param string $method |
104
|
|
|
* @param array $options |
105
|
|
|
* |
106
|
|
|
* @return array|Support\Collection|object|ResponseInterface|string |
107
|
|
|
*/ |
108
|
|
|
protected function requestRaw($endpoint, array $params = [], $method = 'post', array $options = []) |
109
|
|
|
{ |
110
|
|
|
return $this->request($endpoint, $params, $method, $options, true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Request with SSL. |
115
|
|
|
* |
116
|
|
|
* @param string $endpoint |
117
|
|
|
* @param array $params |
118
|
|
|
* @param string $method |
119
|
|
|
* |
120
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
121
|
|
|
*/ |
122
|
|
|
protected function safeRequest($endpoint, array $params, $method = 'post') |
123
|
|
|
{ |
124
|
|
|
$options = [ |
125
|
|
|
'cert' => $this->app['config']->get('cert_path'), |
126
|
|
|
'ssl_key' => $this->app['config']->get('key_path'), |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
return $this->request($endpoint, $params, $method, $options); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $endpoint |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function getKey(string $endpoint) |
138
|
|
|
{ |
139
|
|
|
if ($this->app->inSandbox() && !$this->app['sandbox']->except($endpoint)) { |
140
|
|
|
return $this->app['sandbox']->key(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->app['config']->key; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|