|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Alipay\V2; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
|
9
|
|
|
use Yansongda\Artful\Exception\InvalidResponseException; |
|
10
|
|
|
use Yansongda\Artful\Logger; |
|
11
|
|
|
use Yansongda\Artful\Rocket; |
|
12
|
|
|
use Yansongda\Pay\Exception\Exception; |
|
13
|
|
|
use Yansongda\Supports\Collection; |
|
14
|
|
|
|
|
15
|
|
|
use function Yansongda\Artful\should_do_http_request; |
|
16
|
|
|
|
|
17
|
|
|
class ResponsePlugin implements PluginInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @throws InvalidResponseException |
|
21
|
|
|
*/ |
|
22
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var Rocket $rocket */ |
|
25
|
|
|
$rocket = $next($rocket); |
|
26
|
|
|
|
|
27
|
|
|
Logger::debug('[Alipay][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]); |
|
28
|
|
|
|
|
29
|
|
|
$destination = $rocket->getDestination(); |
|
30
|
|
|
$payload = $rocket->getPayload(); |
|
31
|
|
|
$resultKey = str_replace('.', '_', $payload->get('method')).'_response'; |
|
32
|
|
|
|
|
33
|
|
|
if (should_do_http_request($rocket->getDirection()) && $destination instanceof Collection) { |
|
34
|
|
|
$sign = $destination->get('sign', ''); |
|
35
|
|
|
$response = $destination->get($resultKey, $destination->all()); |
|
36
|
|
|
|
|
37
|
|
|
if (empty($sign) && '10000' !== ($response['code'] ?? 'null')) { |
|
38
|
|
|
throw new InvalidResponseException(Exception::RESPONSE_BUSINESS_CODE_WRONG, '支付宝网关响应异常: '.($response['sub_msg'] ?? $response['msg'] ?? '未知错误,请查看支付宝原始响应'), $rocket->getDestination()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$rocket->setDestination(new Collection(array_merge( |
|
42
|
|
|
['_sign' => $sign], |
|
43
|
|
|
$response |
|
44
|
|
|
))); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
Logger::info('[Alipay][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]); |
|
48
|
|
|
|
|
49
|
|
|
return $rocket; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|