1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay\V2; |
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\ServiceNotFoundException; |
12
|
|
|
use Yansongda\Artful\Logger; |
13
|
|
|
use Yansongda\Artful\Rocket; |
14
|
|
|
use Yansongda\Supports\Collection; |
15
|
|
|
|
16
|
|
|
use function Yansongda\Artful\get_radar_method; |
17
|
|
|
use function Yansongda\Pay\get_alipay_url; |
18
|
|
|
use function Yansongda\Pay\get_provider_config; |
19
|
|
|
|
20
|
|
|
class AddRadarPlugin implements PluginInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws ContainerException |
24
|
|
|
* @throws ServiceNotFoundException |
25
|
|
|
*/ |
26
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
27
|
|
|
{ |
28
|
|
|
Logger::debug('[Alipay][AddRadarPlugin] 插件开始装载', ['rocket' => $rocket]); |
29
|
|
|
|
30
|
|
|
$params = $rocket->getParams(); |
31
|
|
|
$config = get_provider_config('alipay', $params); |
32
|
|
|
$payload = $rocket->getPayload(); |
33
|
|
|
|
34
|
|
|
$rocket->setRadar(new Request( |
35
|
|
|
// 这里因为支付宝的 payload 里不包含 _method,所以需要取 params 中的 |
36
|
|
|
get_radar_method(new Collection($params)) ?? 'POST', |
37
|
|
|
get_alipay_url($config, $payload), |
38
|
|
|
$this->getHeaders(), |
39
|
|
|
// 不能用 packer,支付宝接收的是 x-www-form-urlencoded 返回的又是 json,packer 用的是返回. |
40
|
|
|
$payload?->query() ?? '', |
41
|
|
|
)); |
42
|
|
|
|
43
|
|
|
Logger::info('[Alipay][AddRadarPlugin] 插件装载完毕', ['rocket' => $rocket]); |
44
|
|
|
|
45
|
|
|
return $next($rocket); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getHeaders(): array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
52
|
|
|
'User-Agent' => 'yansongda/pay-v3', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|