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