diff --git a/src/Plugin/Epay/AddRadarPlugin.php b/src/Plugin/Epay/AddRadarPlugin.php index fa98ad02a..99092fc30 100644 --- a/src/Plugin/Epay/AddRadarPlugin.php +++ b/src/Plugin/Epay/AddRadarPlugin.php @@ -42,11 +42,6 @@ public function assembly(Rocket $rocket, Closure $next): Rocket return $next($rocket); } - protected function getMethod(array $params): string - { - return strtoupper($params['_method'] ?? 'POST'); - } - protected function getHeaders(): array { return [ diff --git a/tests/Plugin/Epay/AddRadarPluginTest.php b/tests/Plugin/Epay/AddRadarPluginTest.php index 07cae3fc0..58deb7255 100644 --- a/tests/Plugin/Epay/AddRadarPluginTest.php +++ b/tests/Plugin/Epay/AddRadarPluginTest.php @@ -29,5 +29,12 @@ public function testRadarPostNormal() self::assertEquals('https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm', (string) $result->getRadar()->getUri()); self::stringContains('name=yansongda', (string) $result->getRadar()->getBody()); self::assertEquals('POST', $result->getRadar()->getMethod()); + + //是否存在Content-Type和User-Agent + self::assertArrayHasKey('Content-Type', $result->getRadar()->getHeaders()); + self::assertArrayHasKey('User-Agent', $result->getRadar()->getHeaders()); + //验证值 + self::assertEquals('text/html', $result->getRadar()->getHeader('Content-Type')[0]); + self::assertEquals('yansongda/pay-v3', $result->getRadar()->getHeader('User-Agent')[0]); } } diff --git a/tests/Plugin/Epay/CallbackPluginTest.php b/tests/Plugin/Epay/CallbackPluginTest.php index cff4fc3e1..5e42e9333 100644 --- a/tests/Plugin/Epay/CallbackPluginTest.php +++ b/tests/Plugin/Epay/CallbackPluginTest.php @@ -3,10 +3,17 @@ namespace Yansongda\Pay\Tests\Plugin\Epay; use GuzzleHttp\Psr7\ServerRequest; +use Yansongda\Artful\Contract\ConfigInterface; +use Yansongda\Artful\Exception\InvalidConfigException; +use Yansongda\Artful\Exception\InvalidParamsException; +use Yansongda\Artful\Exception\InvalidResponseException; use Yansongda\Artful\Rocket; +use Yansongda\Pay\Exception\Exception; +use Yansongda\Pay\Pay; use Yansongda\Pay\Plugin\Epay\CallbackPlugin; use Yansongda\Pay\Tests\TestCase; use Yansongda\Supports\Collection; +use Yansongda\Supports\Config; class CallbackPluginTest extends TestCase { @@ -44,4 +51,102 @@ public function testNormal() self::assertNotEmpty($result->getPayload()->all()); } + public function testErrorSign() + { + self::expectException(InvalidConfigException::class); + self::expectExceptionCode(Exception::SIGN_ERROR); + $payload = [ + 'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710', + 'orderStatus'=> '1', + 'totalFee'=> '0.02', + 'outTradeNo'=> 'RC240613164110030315', + 'orderNo'=> '20240613164114400729509', + 'field1'=> '2', + 'field2'=> '', + 'field3'=> '20240613164139|20240613164134400800219', + 'signType'=> 'RSA', + 'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0=' + ]; + $request = new ServerRequest('POST', 'http://localhost'); + $request = $request->withParsedBody($payload); + + $rocket = new Rocket(); + $rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]); + + $result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;}); + + self::assertNotEmpty($result->getPayload()->all()); + } + + public function testEmptySign() + { + self::expectException(InvalidResponseException::class); + self::expectExceptionCode(Exception::SIGN_ERROR); + $payload = [ + 'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710', + 'orderStatus'=> '1', + 'totalFee'=> '0.02', + 'outTradeNo'=> 'RC240613164110030316', + 'orderNo'=> '20240613164114400729509', + 'field1'=> '2', + 'field2'=> '', + 'field3'=> '20240613164139|20240613164134400800219', + 'signType'=> 'RSA', + 'sign'=> '' + ]; + $request = new ServerRequest('POST', 'http://localhost'); + $request = $request->withParsedBody($payload); + + $rocket = new Rocket(); + $rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]); + + $result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;}); + + self::assertNotEmpty($result->getPayload()->all()); + } + + public function testErrorRequestType() + { + self::expectException(InvalidParamsException::class); + self::expectExceptionCode(Exception::PARAMS_CALLBACK_REQUEST_INVALID); + $payload = [ + ]; + $request = new ServerRequest('POST', 'http://localhost'); + $request = $request->withParsedBody($payload); + + $rocket = new Rocket(); + $rocket->setParams(['request'=>$request->getParsedBody()]); + + $result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;}); + + self::assertNotEmpty($result->getPayload()->all()); + } + + public function testMissingEpayPublicCertPath() + { + self::expectException(InvalidConfigException::class); + self::expectExceptionCode(Exception::CONFIG_EPAY_INVALID); + Pay::set(ConfigInterface::class, new Config()); + $payload = [ + 'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710', + 'orderStatus'=> '1', + 'totalFee'=> '0.02', + 'outTradeNo'=> 'RC240613164110030316', + 'orderNo'=> '20240613164114400729509', + 'field1'=> '2', + 'field2'=> '', + 'field3'=> '20240613164139|20240613164134400800219', + 'signType'=> 'RSA', + 'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0=' + ]; + $request = new ServerRequest('POST', 'http://localhost'); + $request = $request->withParsedBody($payload); + + $rocket = new Rocket(); + $rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]); + + $result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;}); + + self::assertNotEmpty($result->getPayload()->all()); + } } diff --git a/tests/Plugin/Epay/ResponsePluginTest.php b/tests/Plugin/Epay/ResponsePluginTest.php index c7dc28705..28edcc8a9 100644 --- a/tests/Plugin/Epay/ResponsePluginTest.php +++ b/tests/Plugin/Epay/ResponsePluginTest.php @@ -45,6 +45,7 @@ public function testHttpWrong() { self::expectException(InvalidResponseException::class); self::expectExceptionCode(Exception::RESPONSE_ERROR); + self::expectExceptionMessage('epay返回状态码异常,请检查参数是否错误'); $body = 'errCode=1&field1=&field2=&field3=&orderNo=20240617144526400259379&orderStatus=1&outTradeNo=YC202406170003&partnerId=6a13eab71c4f4b0aa4757eda6fc59710&payUrl=http://weixintest.jsbchina.cn/epcs/qr/login.htm?qrCode=2018060611052793473720240617144526688568&respBizDate=20240617&respCode=000000&respMsg=交易成功&totalFee=0.01&validTime=2&signType=RSA&sign=jN3Ha6J9UUIe9M0L/XeexEdaRL9GB6nMV12wNC7LQvTS6V4nKHj4Qzw6M8cNsA9L0Tb3QFT83B0qO3FJnruDrcHKqBLZb4FkoKKN/WiDBuA2UZQjG4+CBejoGJWfpkWSsei9tXUk36TB27lc2ZlYXSEwuuDwM7M9yvlYysc3fjg='; $rocket = (new Rocket())->setDestinationOrigin(new Response(500, [], $body)); diff --git a/tests/Plugin/Epay/VerifySignaturePluginTest.php b/tests/Plugin/Epay/VerifySignaturePluginTest.php index d55fd3fcf..e54535e8e 100644 --- a/tests/Plugin/Epay/VerifySignaturePluginTest.php +++ b/tests/Plugin/Epay/VerifySignaturePluginTest.php @@ -47,6 +47,8 @@ public function testMulconnectorSign() public function testEmptySign() { self::expectException(InvalidSignException::class); + self::expectExceptionCode(Exception::RESPONSE_MISSING_NECESSARY_PARAMS); + $body = 'errCode=1&field1=&field2=&field3=&orderNo=20240617144526400259379&orderStatus=1&outTradeNo=YC202406170003&partnerId=6a13eab71c4f4b0aa4757eda6fc59710&payUrl=http://weixintest.jsbchina.cn/epcs/qr/login.htm?qrCode=2018060611052793473720240617144526688568&respBizDate=20240617&respCode=000000&respMsg=交易成功&totalFee=0.01&validTime=2'; $rocket = (new Rocket())->setDestinationOrigin(new Response(200, [], $body)); diff --git a/tests/Provider/EpayTest.php b/tests/Provider/EpayTest.php index a278f19d6..a4a4d7dc5 100644 --- a/tests/Provider/EpayTest.php +++ b/tests/Provider/EpayTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\ServerRequest; use Mockery; use Psr\Http\Message\ResponseInterface; use Yansongda\Artful\Contract\HttpClientInterface; @@ -125,7 +126,7 @@ public function testClose() { $this->expectException(InvalidParamsException::class); - Pay::epay()->cancel([ + Pay::epay()->close([ 'outTradeNo' => 'YC202406170003', ]); } @@ -153,6 +154,55 @@ public function testCallback() } + public function testCallbackByFromGlobals() + { + $payload = [ + 'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710', + 'orderStatus'=> '1', + 'totalFee'=> '0.02', + 'outTradeNo'=> 'RC240613164110030316', + 'orderNo'=> '20240613164114400729509', + 'field1'=> '2', + 'field2'=> '', + 'field3'=> '20240613164139|20240613164134400800219', + 'signType'=> 'RSA', + 'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0=' + ]; + $_POST = array_merge($_POST, $payload); + $result = Pay::epay()->callback(); + self::assertNotEmpty($result->all()); + self::assertArrayHasKey('outTradeNo', $result->all()); + self::assertArrayHasKey('orderNo', $result->all()); + self::assertArrayHasKey('orderStatus', $result->all()); + self::assertArrayHasKey('field3', $result->all()); + + } + + public function testCallbackByServerRequest() + { + $payload = [ + 'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710', + 'orderStatus'=> '1', + 'totalFee'=> '0.02', + 'outTradeNo'=> 'RC240613164110030316', + 'orderNo'=> '20240613164114400729509', + 'field1'=> '2', + 'field2'=> '', + 'field3'=> '20240613164139|20240613164134400800219', + 'signType'=> 'RSA', + 'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0=' + ]; + $serverRequest = new ServerRequest('POST', 'http://localhost', [], null); + $serverRequest = $serverRequest->withParsedBody($payload); + $result = Pay::epay()->callback($serverRequest); + self::assertNotEmpty($result->all()); + self::assertArrayHasKey('outTradeNo', $result->all()); + self::assertArrayHasKey('orderNo', $result->all()); + self::assertArrayHasKey('orderStatus', $result->all()); + self::assertArrayHasKey('field3', $result->all()); + + } + public function testSuccess() { $result = Pay::epay()->success();