|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the wannanbigpig/alipay. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) wannanbigpig <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace EasyAlipay\Kernel; |
|
12
|
|
|
|
|
13
|
|
|
use EasyAlipay\Kernel\Contracts\App; |
|
14
|
|
|
use EasyAlipay\Kernel\Providers\AppServiceProvider; |
|
15
|
|
|
use EasyAlipay\Kernel\Providers\ConfigServiceProvider; |
|
16
|
|
|
use EasyAlipay\Kernel\Providers\HttpClientServiceProvider; |
|
17
|
|
|
use EasyAlipay\Kernel\Providers\LogServiceProvider; |
|
18
|
|
|
use EasyAlipay\Kernel\Providers\RequestServiceProvider; |
|
19
|
|
|
use Pimple\Container; |
|
20
|
|
|
use WannanBigPig\Supports\Exceptions\RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ServiceContainer |
|
24
|
|
|
* |
|
25
|
|
|
* @property \WannanBigPig\Supports\Config $config |
|
26
|
|
|
* |
|
27
|
|
|
* @author liuml <[email protected]> |
|
28
|
|
|
* @DateTime 2019-07-18 16:13 |
|
29
|
|
|
*/ |
|
30
|
|
|
class ServiceContainer extends Container implements App |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const NORMAL_ENV = 'normal'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
const DEV_ENV = 'dev'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $gateway = [ |
|
46
|
|
|
self::NORMAL_ENV => 'https://openapi.alipay.com/gateway.do', |
|
47
|
|
|
self::DEV_ENV => 'https://openapi.alipaydev.com/gateway.do', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $providers = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $userConfig = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $defaultConfig = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Application constructor. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
*/ |
|
70
|
20 |
|
public function __construct(array $config) |
|
71
|
|
|
{ |
|
72
|
20 |
|
parent::__construct(['app_client_providers' => $this->providers]); |
|
73
|
20 |
|
$this->init($config); |
|
74
|
20 |
|
$this->registerProviders($this->getProviders()); |
|
75
|
20 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* __get. |
|
79
|
|
|
* |
|
80
|
|
|
* @param $name |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \WannanBigPig\Supports\Exceptions\RuntimeException |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function __get($name) |
|
87
|
|
|
{ |
|
88
|
4 |
|
if (isset($this[$name])) { |
|
89
|
4 |
|
return $this[$name]; |
|
90
|
|
|
} |
|
91
|
|
|
throw new RuntimeException(sprintf('Identifier "%s" is not defined', $name)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* init application config. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $config |
|
98
|
|
|
*/ |
|
99
|
20 |
|
public function init(array $config) |
|
100
|
|
|
{ |
|
101
|
20 |
|
$this->userConfig = $config; |
|
102
|
20 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* getConfig. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array|mixed |
|
108
|
|
|
*/ |
|
109
|
15 |
|
public function getConfig() |
|
110
|
|
|
{ |
|
111
|
|
|
$base = [ |
|
112
|
|
|
'http' => [ |
|
113
|
15 |
|
'timeout' => 30.0, |
|
114
|
15 |
|
'base_uri' => $this->getGateway(), |
|
115
|
15 |
|
'connect_timeout' => 6.0, |
|
116
|
15 |
|
'log_template' => "\n>>>>>>>>request\n--------\n{request}\n--------\n>>>>>>>>response\n--------\n{response}\n--------\n>>>>>>>>error\n--------\n{error}\n--------\n", |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
15 |
|
return array_replace_recursive($base, $this->defaultConfig, $this->userConfig); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the common request parameters of Alipay interface. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $endpoint |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
4 |
|
public function apiCommonConfig(string $endpoint): array |
|
131
|
|
|
{ |
|
132
|
4 |
|
$this->config->set('api_method', $endpoint); |
|
133
|
|
|
|
|
134
|
4 |
|
return array_merge([ |
|
135
|
4 |
|
'app_id' => $this->config['sys_params.app_id'], |
|
136
|
4 |
|
'method' => $endpoint, |
|
137
|
4 |
|
'format' => 'JSON', |
|
138
|
4 |
|
'charset' => $this->config->get('sys_params.charset', 'utf-8'), |
|
139
|
4 |
|
'sign_type' => $this->config->get('sys_params.sign_type', 'RSA2'), |
|
140
|
4 |
|
'sign' => '', |
|
141
|
4 |
|
'timestamp' => date('Y-m-d H:i:s'), |
|
142
|
4 |
|
'version' => '1.0', |
|
143
|
4 |
|
'app_auth_token' => $this->config->get('sys_params.app_auth_token', ''), |
|
144
|
4 |
|
], $this->config->get($endpoint.'config', [])); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* setEndpointConfig. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $endpoint |
|
151
|
|
|
* @param array $config |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
4 |
|
public function setEndpointConfig(string $endpoint, array $config) |
|
156
|
|
|
{ |
|
157
|
4 |
|
$this->config->set($endpoint.'config', $config); |
|
158
|
|
|
|
|
159
|
4 |
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set version. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $version |
|
166
|
|
|
* |
|
167
|
|
|
* @return $this |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setVersion(string $version) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->config->set('version', $version); |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Acquisition of development environment. |
|
178
|
|
|
* |
|
179
|
|
|
* @return mixed|string |
|
180
|
|
|
*/ |
|
181
|
15 |
|
public function getEnv() |
|
182
|
|
|
{ |
|
183
|
15 |
|
return isset($this->userConfig['env']) ? $this->userConfig['env'] : self::NORMAL_ENV; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get Alipay gateway address. |
|
188
|
|
|
* |
|
189
|
|
|
* @return mixed |
|
190
|
|
|
*/ |
|
191
|
15 |
|
public function getGateway() |
|
192
|
|
|
{ |
|
193
|
15 |
|
if (isset($this->gateway[$this->getEnv()])) { |
|
194
|
15 |
|
return $this->gateway[$this->getEnv()]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->gateway[self::NORMAL_ENV]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* setAppAuthToken. |
|
202
|
|
|
* |
|
203
|
|
|
* @param $appAuthToken |
|
204
|
|
|
* |
|
205
|
|
|
* @return $this |
|
206
|
|
|
*/ |
|
207
|
|
|
public function setAppAuthToken($appAuthToken) |
|
208
|
|
|
{ |
|
209
|
|
|
$this->config->set('app_auth_token', $appAuthToken); |
|
210
|
|
|
|
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Return all providers. |
|
216
|
|
|
* |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
20 |
|
public function getProviders() |
|
220
|
|
|
{ |
|
221
|
20 |
|
return array_merge([ |
|
222
|
20 |
|
ConfigServiceProvider::class, |
|
223
|
|
|
HttpClientServiceProvider::class, |
|
224
|
|
|
LogServiceProvider::class, |
|
225
|
|
|
RequestServiceProvider::class, |
|
226
|
|
|
AppServiceProvider::class, |
|
227
|
|
|
]); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param array $providers |
|
232
|
|
|
*/ |
|
233
|
20 |
|
public function registerProviders(array $providers) |
|
234
|
|
|
{ |
|
235
|
20 |
|
foreach ($providers as $provider) { |
|
236
|
20 |
|
parent::register(new $provider()); |
|
237
|
|
|
} |
|
238
|
20 |
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|