1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yansongda\Pay\Plugin\Jsb; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Yansongda\Artful\Contract\PluginInterface; |
10
|
|
|
use Yansongda\Artful\Exception\InvalidResponseException; |
11
|
|
|
use Yansongda\Artful\Logger; |
12
|
|
|
use Yansongda\Artful\Rocket; |
13
|
|
|
use Yansongda\Pay\Exception\Exception; |
14
|
|
|
use Yansongda\Supports\Collection; |
15
|
|
|
|
16
|
|
|
class ResponsePlugin implements PluginInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @throws InvalidResponseException |
20
|
|
|
*/ |
21
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket |
22
|
|
|
{ |
23
|
|
|
/* @var Rocket $rocket */ |
24
|
|
|
$rocket = $next($rocket); |
25
|
|
|
|
26
|
|
|
Logger::debug('[Jsb][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]); |
27
|
|
|
|
28
|
|
|
$this->validateResponse($rocket); |
29
|
|
|
|
30
|
|
|
Logger::info('[Jsb][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]); |
31
|
|
|
|
32
|
|
|
return $rocket; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws InvalidResponseException |
37
|
|
|
*/ |
38
|
|
|
protected function validateResponse(Rocket $rocket): void |
39
|
|
|
{ |
40
|
|
|
$destination = $rocket->getDestination(); |
41
|
|
|
$destinationOrigin = $rocket->getDestinationOrigin(); |
42
|
|
|
|
43
|
|
|
if ($destinationOrigin instanceof ResponseInterface |
44
|
|
|
&& ($destinationOrigin->getStatusCode() < 200 || $destinationOrigin->getStatusCode() >= 300)) { |
45
|
|
|
throw new InvalidResponseException(Exception::RESPONSE_CODE_WRONG, '江苏银行返回状态码异常,请检查参数是否错误', $rocket->getDestination()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($destination instanceof Collection && '000000' !== $destination->get('respCode')) { |
49
|
|
|
throw new InvalidResponseException(Exception::RESPONSE_BUSINESS_CODE_WRONG, sprintf('江苏银行返回错误: respCode:%s respMsg:%s', $destination->get('respCode'), $destination->get('respMsg')), $rocket->getDestination()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|