1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Alipay |
4
|
|
|
* |
5
|
|
|
* @author liuml <[email protected]> |
6
|
|
|
* @DateTime 2019-04-04 15:40 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace WannanBigPig\Alipay; |
10
|
|
|
|
11
|
|
|
use WannanBigPig\Alipay\Kernel\Listeners\KernelLogSubscriber; |
12
|
|
|
use WannanBigPig\Alipay\Kernel\Support\Support; |
13
|
|
|
use WannanBigPig\Supports\Config; |
14
|
|
|
use WannanBigPig\Supports\Events; |
15
|
|
|
use WannanBigPig\Supports\Exceptions\ApplicationException; |
16
|
|
|
use WannanBigPig\Supports\Logs\Log; |
17
|
|
|
use WannanBigPig\Supports\Str; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Alipay |
21
|
|
|
* |
22
|
|
|
* @author liuml <[email protected]> |
23
|
|
|
* @DateTime 2019-04-04 16:53 |
24
|
|
|
* |
25
|
|
|
* @package WannanBigPig\Alipay |
26
|
|
|
* |
27
|
|
|
* @method static payment(array $config) |
28
|
|
|
* @method static notify(array $config) |
29
|
|
|
*/ |
30
|
|
|
class Alipay |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Const mode_normal. |
35
|
|
|
*/ |
36
|
|
|
const ENV_NORMAL = 'normal'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Const mode_dev. |
40
|
|
|
*/ |
41
|
|
|
const ENV_DEV = 'dev'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Const url. |
45
|
|
|
*/ |
46
|
|
|
const URL = [ |
47
|
|
|
self::ENV_NORMAL => 'https://openapi.alipay.com/gateway.do', |
48
|
|
|
self::ENV_DEV => 'https://openapi.alipaydev.com/gateway.do', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* commonParams |
53
|
|
|
* |
54
|
|
|
* @param Config $config |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
3 |
|
public function commonParams(Config $config) |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
3 |
|
'app_id' => $config->get('payload.app_id'), |
62
|
3 |
|
'method' => '', |
63
|
3 |
|
'format' => $config->get('payload.format', 'JSON'), |
64
|
3 |
|
'charset' => $config->get('payload.charset', 'utf-8'), |
65
|
3 |
|
'sign_type' => $config->get('payload.sign_type', 'RSA2'), |
66
|
3 |
|
'version' => $config->get('payload.version', '1.0'), |
67
|
3 |
|
'return_url' => $config->get('payload.return_url'), |
68
|
3 |
|
'notify_url' => $config->get('payload.notify_url'), |
69
|
3 |
|
'timestamp' => date('Y-m-d H:i:s'), |
70
|
3 |
|
'sign' => '', |
71
|
3 |
|
'biz_content' => '', |
72
|
3 |
|
'app_auth_token' => $config->get('payload.app_auth_token'), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* create |
78
|
|
|
* |
79
|
|
|
* @param $name |
80
|
|
|
* @param array $config |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
* |
84
|
|
|
* @throws \WannanBigPig\Supports\Exceptions\ApplicationException |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
3 |
|
public function create($name, array $config) |
88
|
|
|
{ |
89
|
3 |
|
$name = Str::studly($name); |
90
|
3 |
|
$application = __NAMESPACE__ . "\\{$name}\\Application"; |
91
|
3 |
|
if (class_exists($application)) { |
92
|
|
|
// 设置配置数组 |
93
|
3 |
|
$this->config($config); |
94
|
|
|
|
95
|
|
|
// 实例化应用 |
96
|
3 |
|
return $this->make($application); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new ApplicationException("Application [{$name}] does not exist"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* make |
104
|
|
|
* |
105
|
|
|
* @param $application |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
3 |
|
public function make($application) |
110
|
|
|
{ |
111
|
3 |
|
return new $application(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* config |
116
|
|
|
* |
117
|
|
|
* @param array $config |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
* |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
3 |
|
public function config(array $config) |
124
|
|
|
{ |
125
|
|
|
// 创建配置 |
126
|
3 |
|
$config = Support::createConfig($config); |
127
|
|
|
|
128
|
|
|
// 设置支付宝公共参数 |
129
|
3 |
|
$config->set('payload', $this->commonParams($config)); |
130
|
|
|
|
131
|
|
|
// 设置支付宝网关 |
132
|
3 |
|
$base_uri = self::URL[$config->get('env', self::ENV_NORMAL)]; |
133
|
3 |
|
$config->set('base_uri', $base_uri); |
134
|
|
|
|
135
|
3 |
|
$this->registerLogService(); |
136
|
3 |
|
$this->registerEventService(); |
137
|
|
|
|
138
|
3 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @static __callStatic |
143
|
|
|
* |
144
|
|
|
* @param $name |
145
|
|
|
* @param $arguments |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
* |
149
|
|
|
* @throws ApplicationException |
150
|
|
|
*/ |
151
|
3 |
|
public static function __callStatic($name, $arguments) |
152
|
|
|
{ |
153
|
3 |
|
$app = new self(); |
154
|
3 |
|
if (method_exists($app, $name)) { |
155
|
|
|
return $app->{$name}(...$arguments); |
156
|
|
|
} |
157
|
3 |
|
return $app->create($name, ...$arguments); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* registerLogService |
162
|
|
|
* |
163
|
|
|
* @throws \Exception |
164
|
|
|
*/ |
165
|
3 |
|
protected function registerLogService() |
166
|
|
|
{ |
167
|
3 |
|
$logger = Log::createLogger( |
168
|
3 |
|
Support::$config->get('log.file'), |
169
|
3 |
|
'wannanbigpig.alipay', |
170
|
3 |
|
Support::$config->get('log.level', 'warning'), |
171
|
3 |
|
Support::$config->get('log.type', 'daily'), |
172
|
3 |
|
Support::$config->get('log.max_file', 30) |
173
|
|
|
); |
174
|
|
|
|
175
|
3 |
|
Log::setLogger($logger); |
176
|
3 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* registerEventService |
180
|
|
|
*/ |
181
|
3 |
|
protected function registerEventService() |
182
|
|
|
{ |
183
|
3 |
|
Events::addSubscriber(new KernelLogSubscriber()); |
184
|
3 |
|
} |
185
|
|
|
} |
186
|
|
|
|