Skip to content

Commit 3d79403

Browse files
fix: 支付宝响应空签名时签名验证逻辑错误的问题 (#998)
--------- Co-authored-by: yansongda <[email protected]>
1 parent 3683c69 commit 3d79403

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## TBD - v3.7.5
1+
## v3.7.5
2+
3+
### fixed
4+
5+
- fix: 支付宝响应空签名时签名验证逻辑错误的问题(#998)
26

37
### optimized
48

src/Exception/Exception.php

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Exception extends \Exception
4444

4545
public const RESPONSE_MISSING_NECESSARY_PARAMS = 9305;
4646

47+
public const RESPONSE_BUSINESS_CODE_WRONG = 9306;
48+
4749
/*
4850
* 关于配置.
4951
*/

src/Plugin/Alipay/V2/ResponsePlugin.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66

77
use Closure;
88
use Yansongda\Artful\Contract\PluginInterface;
9+
use Yansongda\Artful\Exception\InvalidResponseException;
910
use Yansongda\Artful\Logger;
1011
use Yansongda\Artful\Rocket;
12+
use Yansongda\Pay\Exception\Exception;
1113
use Yansongda\Supports\Collection;
1214

1315
use function Yansongda\Artful\should_do_http_request;
1416

1517
class ResponsePlugin implements PluginInterface
1618
{
19+
/**
20+
* @throws InvalidResponseException
21+
*/
1722
public function assembly(Rocket $rocket, Closure $next): Rocket
1823
{
1924
/* @var Rocket $rocket */
@@ -26,9 +31,16 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
2631
$resultKey = str_replace('.', '_', $payload->get('method')).'_response';
2732

2833
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+
2941
$rocket->setDestination(new Collection(array_merge(
30-
['_sign' => $destination->get('sign', '')],
31-
$destination->get($resultKey, $destination->all())
42+
['_sign' => $sign],
43+
$response
3244
)));
3345
}
3446

tests/Plugin/Alipay/V2/ResponsePluginTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Yansongda\Pay\Tests\Plugin\Alipay\V2;
44

5+
use Yansongda\Artful\Exception\InvalidResponseException;
6+
use Yansongda\Pay\Exception\Exception;
57
use Yansongda\Pay\Plugin\Alipay\V2\ResponsePlugin;
68
use Yansongda\Artful\Rocket;
79
use Yansongda\Pay\Tests\TestCase;
@@ -82,4 +84,27 @@ public function testErrorResponseWithNoMethodKey()
8284

8385
self::assertEquals(array_merge(['_sign' => '123'], $destination), $result->getDestination()->all());
8486
}
87+
88+
public function testErrorResponseWithEmptySignKey()
89+
{
90+
self::expectException(InvalidResponseException::class);
91+
self::expectExceptionCode(Exception::RESPONSE_BUSINESS_CODE_WRONG);
92+
self::expectExceptionMessage('支付宝网关响应异常: 无效的AppID参数');
93+
94+
$destination = [
95+
'alipay_fund_trans_uni_transfer_response' => [
96+
'code' => '40002',
97+
'msg' => 'Invalid Arguments',
98+
'sub_code' => 'isv.invalid-app-id',
99+
'sub_msg' => '无效的AppID参数',
100+
],
101+
'sign' => ''
102+
];
103+
104+
$rocket = (new Rocket())
105+
->mergePayload(['method' => 'alipay_fund_trans_uni_transfer'])
106+
->setDestination(new Collection($destination));
107+
108+
$this->plugin->assembly($rocket, function ($rocket) {return $rocket; });
109+
}
85110
}

0 commit comments

Comments
 (0)