1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Wechat; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use GuzzleHttp\Psr7\Request; |
9
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
10
|
|
|
use Yansongda\Artful\Exception\ContainerException; |
11
|
|
|
use Yansongda\Artful\Exception\InvalidParamsException; |
12
|
|
|
use Yansongda\Artful\Exception\ServiceNotFoundException; |
13
|
|
|
use Yansongda\Artful\Logger; |
14
|
|
|
use Yansongda\Artful\Rocket; |
15
|
|
|
use Yansongda\Supports\Collection; |
16
|
|
|
|
17
|
|
|
use function Yansongda\Pay\get_provider_config; |
18
|
|
|
use function Yansongda\Pay\get_wechat_body; |
19
|
|
|
use function Yansongda\Pay\get_wechat_method; |
20
|
|
|
use function Yansongda\Pay\get_wechat_url; |
21
|
|
|
|
22
|
|
|
class AddRadarPlugin implements PluginInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @throws ContainerException |
26
|
|
|
* @throws InvalidParamsException |
27
|
|
|
* @throws ServiceNotFoundException |
28
|
|
|
*/ |
29
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
30
|
|
|
{ |
31
|
|
|
Logger::debug('[Wechat][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]); |
32
|
|
|
|
33
|
|
|
$params = $rocket->getParams(); |
34
|
|
|
$payload = $rocket->getPayload(); |
35
|
|
|
$config = get_provider_config('wechat', $params); |
36
|
|
|
|
37
|
|
|
$rocket->setRadar(new Request( |
38
|
|
|
get_wechat_method($payload), |
39
|
|
|
get_wechat_url($config, $payload), |
40
|
|
|
$this->getHeaders($payload), |
41
|
|
|
get_wechat_body($payload), |
42
|
|
|
)); |
43
|
|
|
|
44
|
|
|
Logger::info('[Wechat][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]); |
45
|
|
|
|
46
|
|
|
return $next($rocket); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getHeaders(?Collection $payload): array |
50
|
|
|
{ |
51
|
|
|
$headers = [ |
52
|
|
|
'Accept' => 'application/json, text/plain, application/x-gzip', |
53
|
|
|
'User-Agent' => 'yansongda/pay-v3', |
54
|
|
|
'Content-Type' => 'application/json; charset=utf-8', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
// 当 body 里有加密内容时,需要传递此参数用于微信区分 |
58
|
|
|
if (!empty($serialNo = $payload?->get('_serial_no'))) { |
59
|
|
|
$headers['Wechatpay-Serial'] = $serialNo; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!empty($authorization = $payload?->get('_authorization'))) { |
63
|
|
|
$headers['Authorization'] = $authorization; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($contentType = $payload?->get('_content_type'))) { |
67
|
|
|
$headers['Content-Type'] = $contentType; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!empty($accept = $payload?->get('_accept'))) { |
71
|
|
|
$headers['Accept'] = $accept; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $headers; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|