|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Wechat; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Yansongda\Pay\Contract\PluginInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function Yansongda\Pay\get_wechat_base_uri; |
|
12
|
|
|
use function Yansongda\Pay\get_wechat_config; |
|
13
|
|
|
|
|
14
|
|
|
use Yansongda\Pay\Logger; |
|
15
|
|
|
use Yansongda\Pay\Pay; |
|
16
|
|
|
use Yansongda\Pay\Request; |
|
17
|
|
|
use Yansongda\Pay\Rocket; |
|
18
|
|
|
|
|
19
|
|
|
abstract class GeneralPlugin implements PluginInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
23
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
26
|
|
|
{ |
|
27
|
|
|
Logger::info('[wechat][GeneralPlugin] 通用插件开始装载', ['rocket' => $rocket]); |
|
28
|
|
|
|
|
29
|
|
|
$rocket->setRadar($this->getRequest($rocket)); |
|
30
|
|
|
$this->doSomething($rocket); |
|
31
|
|
|
|
|
32
|
|
|
Logger::info('[wechat][GeneralPlugin] 通用插件装载完毕', ['rocket' => $rocket]); |
|
33
|
|
|
|
|
34
|
|
|
return $next($rocket); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
39
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getRequest(Rocket $rocket): RequestInterface |
|
42
|
|
|
{ |
|
43
|
|
|
return new Request( |
|
44
|
|
|
$this->getMethod(), |
|
45
|
|
|
$this->getUrl($rocket), |
|
46
|
|
|
$this->getHeaders(), |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getMethod(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return 'POST'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws \Yansongda\Pay\Exception\ContainerException |
|
57
|
|
|
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getUrl(Rocket $rocket): string |
|
60
|
|
|
{ |
|
61
|
|
|
$params = $rocket->getParams(); |
|
62
|
|
|
|
|
63
|
|
|
$url = Pay::MODE_SERVICE === (get_wechat_config($params)['mode'] ?? null) ? $this->getPartnerUri($rocket) : $this->getUri($rocket); |
|
64
|
|
|
|
|
65
|
|
|
return 0 === strpos($url, 'http') ? $url : (get_wechat_base_uri($params).$url); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getHeaders(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
|
|
'Accept' => 'application/json, text/plain, application/x-gzip', |
|
72
|
|
|
'User-Agent' => 'yansongda/pay-v3', |
|
73
|
|
|
'Content-Type' => 'application/json; charset=utf-8', |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function getConfigKey(array $params): string |
|
78
|
|
|
{ |
|
79
|
|
|
$key = ($params['_type'] ?? 'mp').'_app_id'; |
|
80
|
|
|
|
|
81
|
|
|
if ('app_app_id' === $key) { |
|
82
|
|
|
$key = 'app_id'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $key; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
abstract protected function doSomething(Rocket $rocket): void; |
|
89
|
|
|
|
|
90
|
|
|
abstract protected function getUri(Rocket $rocket): string; |
|
91
|
|
|
|
|
92
|
|
|
protected function getPartnerUri(Rocket $rocket): string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->getUri($rocket); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|