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

Conversation

saber-x
Copy link
Contributor

@saber-x saber-x commented Jan 25, 2025

问题描述
在使用微信支付APIv2的PHP库时,发现H5纯签约接口(/v2/papay/h5entrustweb)在进行GET请求时,代码错误地将请求参数转换为XML格式,导致请求失败。具体错误信息为:
PHP Fatal error: Uncaught WeChatPay\Exception\InvalidArgumentException: The xml's structure[mch_id()] doesn't matched the init one mchid(**********).

根据微信支付APIv2的文档,H5纯签约接口的GET请求仅需要通过queryString传递参数,而当前代码在处理GET请求时,错误地将参数转换为XML格式,这与API的要求不符。
修复方案

  1. 为了解决上述问题,对transformRequest方法进行了修复。修复后的代码逻辑如下:
  • 区分GET请求与非GET请求:

  • 对于GET请求,直接返回请求,不进行XML转换。

  • 对于非GET请求,保留原有的XML转换和签名逻辑。

  1. 参数处理:
  • 使用GuzzleHttp\Psr7\Query::parse解析GET请求的查询字符串,确保参数正确传递。

  • 对于非GET请求,继续按照原有逻辑生成XML格式的请求体。

  1. 商户号匹配检查:
  • 修复后的代码保留了对mchid的检查逻辑,确保传入的商户号与初始化时的商户号一致。

@saber-x saber-x force-pushed the fix-transform-request branch from 8393eb4 to 072ba66 Compare January 25, 2025 10:44
@TheNorthMemory
Copy link
Collaborator

PHP7.1下校验未通过:

 ------ --------------------------------------------- 
  Line   src/ClientXmlTrait.php (in context of class  
         WeChatPay\ClientDecorator)                   
 ------ --------------------------------------------- 
  87     Right side of && is always false.            
 ------ --------------------------------------------- 

@saber-x
Copy link
Contributor Author

saber-x commented Jan 25, 2025

@TheNorthMemory 已修改重新提交。

src/ClientXmlTrait.php Outdated Show resolved Hide resolved
src/ClientXmlTrait.php Outdated Show resolved Hide resolved
Copy link

@Eric-Lee-Handyman Eric-Lee-Handyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* Fix transformRequest for GET requests
* Refactored to use if statement for parsing queryParams
* optimize request parameter parsing and handling
* sample tests on `v2/papay/h5entrustweb` request
@TheNorthMemory TheNorthMemory merged commit bd2148e into wechatpay-apiv3:main Jan 26, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants