Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transformRequest for GET requests #146

Merged
merged 6 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 变更历史

## [1.4.11](../../compare/v1.4.9...v1.4.10) - 2024-12-23
## [1.4.12](../../compare/v1.4.11...v1.4.12) - 2025-01-27

- 修正`APIv2`特殊`GET`请求抛异常问题,相关记录见[这里](https://github.com/wechatpay-apiv3/wechatpay-php/issues/146)。

## [1.4.11](../../compare/v1.4.10...v1.4.11) - 2024-12-27

- 对`APIv2`服务端返回值做精细判断,对于`return_code`(返回状态码)及/或`result_code`(业务结果)有key且值不为`SUCCESS`的情形,抛出客户端`RejectionException`异常,并加入[AuthcodetoopenidTest.php](./tests/OpenAPI/V2/Tools/AuthcodetoopenidTest.php)异常处理示例。

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

## 项目状态

当前版本为 `1.4.11` 版。
当前版本为 `1.4.12` 版。
项目版本遵循 [语义化版本号](https://semver.org/lang/zh-CN/)。
如果你使用的版本 `<=v1.3.2`,升级前请参考 [升级指南](UPGRADING.md)。

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatpay/wechatpay",
"version": "1.4.11",
"version": "1.4.12",
"description": "[A]Sync Chainable WeChatPay v2&v3's OpenAPI SDK for PHP",
"type": "library",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/ClientDecoratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ClientDecoratorInterface
/**
* @var string - This library version
*/
public const VERSION = '1.4.11';
public const VERSION = '1.4.12';

/**
* @var string - The HTTP transfer `xml` based protocol
Expand Down
11 changes: 9 additions & 2 deletions src/ClientXmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Query;
use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
Expand Down Expand Up @@ -82,7 +83,13 @@ public static function transformRequest(
{
return static function (callable $handler) use ($mchid, $secret, $merchant): callable {
return static function (RequestInterface $request, array $options = []) use ($handler, $mchid, $secret, $merchant): PromiseInterface {
$data = $options['xml'] ?? [];
$methodIsGet = $request->getMethod() === 'GET';

if ($methodIsGet) {
$queryParams = Query::parse($request->getUri()->getQuery());
}

$data = $options['xml'] ?? ($queryParams ?? []);

if ($mchid && $mchid !== ($inputMchId = $data['mch_id'] ?? $data['mchid'] ?? $data['combine_mch_id'] ?? null)) {
throw new Exception\InvalidArgumentException(sprintf(Exception\EV2_REQ_XML_NOTMATCHED_MCHID, $inputMchId ?? '', $mchid));
Expand All @@ -94,7 +101,7 @@ public static function transformRequest(

$data['sign'] = Crypto\Hash::sign($type, Formatter::queryStringLike(Formatter::ksort($data)), $secret);

$modify = ['body' => Transformer::toXml($data)];
$modify = $methodIsGet ? ['query' => Query::build($data)] : ['body' => Transformer::toXml($data)];

// for security request, it was required the merchant's private_key and certificate
if (isset($options['security']) && true === $options['security']) {
Expand Down
131 changes: 131 additions & 0 deletions tests/OpenAPI/V2/Papay/H5entrustwebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php declare(strict_types=1);

namespace WeChatPay\Tests\OpenAPI\V2\Papay;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use WeChatPay\Builder;
use WeChatPay\Crypto\Hash;
use WeChatPay\Formatter;
use WeChatPay\Transformer;
use PHPUnit\Framework\TestCase;

class H5entrustwebTest extends TestCase
{
/** @var MockHandler $mock */
private $mock;

private function guzzleMockStack(): HandlerStack
{
$this->mock = new MockHandler();

return HandlerStack::create($this->mock);
}

/**
* @param string $mchid
* @return array{\WeChatPay\BuilderChainable}
*/
private function prepareEnvironment(string $mchid, string $secret): array
{
$instance = Builder::factory([
'mchid' => $mchid,
'serial' => 'nop',
'privateKey' => 'any',
'certs' => ['any' => null],
'secret' => $secret,
'handler' => $this->guzzleMockStack(),
]);

$endpoint = $instance->chain('v2/papay/h5entrustweb');

return [$endpoint];
}

/**
* @return array<string,array{string,string,array<string,int|string>,ResponseInterface}>
*/
public function mockRequestsDataProvider(): array
{
$mchid = '1230000109';
$secret = Formatter::nonce(32);
$queryData = [
'appid' => 'wxcbda96de0b165486',
'mch_id' => $mchid,
'plan_id' => '12535',
'contract_code' => '100000',
'request_serial' => 1000,
'contract_display_account' => '微信代扣',
'notify_url' => 'https://weixin.qq.com',
'version' => '1.0',
'sign_type' => 'HMAC-SHA256',
'timestamp' => Formatter::timestamp(),
'return_appid' => 'wxcbda96de0b165486',
];
$responseData = [
'return_code' => 'SUCCESS',
'return_msg' => '',
'result_code' => 'SUCCESS',
'result_msg' => '',
'redirect_url' => 'https://payapp.weixin.qq.com',
];

return [
'return_code=SUCCESS' => [$mchid, $secret, $queryData, new Response(200, [], Transformer::toXml($responseData))],
];
}

/**
* @dataProvider mockRequestsDataProvider
* @param string $mchid
* @param string $secret
* @param array<string,int|string> $query
* @param ResponseInterface $respondor
*/
public function testGet(string $mchid, string $secret, array $query, ResponseInterface $respondor): void
{
[$endpoint] = $this->prepareEnvironment($mchid, $secret);

$this->mock->reset();
$this->mock->append($respondor);

$res = $endpoint->get(['nonceless' => true, 'query' => $query]);
self::responseAssertion($res);
}

/**
* @param ResponseInterface $response
*/
private static function responseAssertion(ResponseInterface $response): void
{
$txt = (string) $response->getBody();
$array = Transformer::toArray($txt);
static::assertArrayHasKey('redirect_url', $array);
static::assertArrayHasKey('return_code', $array);
static::assertArrayHasKey('result_code', $array);
}

/**
* @dataProvider mockRequestsDataProvider
* @param string $mchid
* @param string $secret
* @param array<string,string> $query
* @param ResponseInterface $respondor
*/
public function testGetAsync(string $mchid, string $secret, array $query, ResponseInterface $respondor): void
{
[$endpoint] = $this->prepareEnvironment($mchid, $secret);

$this->mock->reset();
$this->mock->append($respondor);

$endpoint->getAsync([
'nonceless' => true,
'query' => $query,
])->then(static function(ResponseInterface $res) {
self::responseAssertion($res);
})->wait();
}
}
Loading