安装:
composer require wechatpay/wechatpay
Using version ^1.4 for wechatpay/wechatpay
./composer.json has been updated
Running composer update wechatpay/wechatpay
Loading composer repositories with package information
Updating dependencies
Lock file operations: 11 installs, 0 updates, 0 removals
- Locking guzzlehttp/guzzle (7.4.1)
- Locking guzzlehttp/promises (1.5.1)
- Locking guzzlehttp/psr7 (2.1.0)
- Locking guzzlehttp/uri-template (v1.0.1)
- Locking psr/http-client (1.0.1)
- Locking psr/http-factory (1.0.1)
- Locking psr/http-message (1.0.1)
- Locking ralouphie/getallheaders (3.0.3)
- Locking symfony/deprecation-contracts (v2.5.0)
- Locking symfony/polyfill-php80 (v1.25.0)
- Locking wechatpay/wechatpay (1.4.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 11 installs, 0 updates, 0 removals
- Installing guzzlehttp/promises (1.5.1): Extracting archive
- Installing ralouphie/getallheaders (3.0.3): Extracting archive
- Installing psr/http-message (1.0.1): Extracting archive
- Installing psr/http-factory (1.0.1): Extracting archive
- Installing guzzlehttp/psr7 (2.1.0): Extracting archive
- Installing psr/http-client (1.0.1): Extracting archive
- Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
- Installing symfony/polyfill-php80 (v1.25.0): Extracting archive
- Installing guzzlehttp/uri-template (v1.0.1): Extracting archive
- Installing guzzlehttp/guzzle (7.4.1): Extracting archive
- Installing wechatpay/wechatpay (1.4.3): Extracting archive
PHP版本:7.4.28
PHP代码:
require '..../classes/vendor/autoload.php';
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
use WeChatPay\Util\MediaUtil;
.....
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformCertificateSerial => $platformPublicKeyInstance,
],
]);
$resp = $instance->chain('v3/certificates')->get();
运行结果:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https:
{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_ (truncated...)
in .../classes/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
Stack trace:
#0 .../classes/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL)
#1 .../classes/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 .../classes/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL)
#3 .../classes/vendor/guzzlehttp/p in .../classes/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
按照文档:https://github.com/wechatpay-apiv3/wechatpay-guzzle-middleware
“开始”部分一切正常。
在“上传媒体文件”部分:
// 参考上述指引说明,并引入 `MediaUtil` 正常初始化,无额外条件 use WechatPay\GuzzleMiddleware\Util\MediaUtil; // 实例化一个媒体文件流,注意文件后缀名需符合接口要求 $media = new MediaUtil('/your/file/path/with.extension');
执行new MediaUtil的时候,报错:
Fatal error: Uncaught Error: Call to undefined function GuzzleHttp\Psr7\hash() in .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php:91 Stack trace: #0 .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php(75): WechatPay\GuzzleMiddleware\Util\MediaUtil->composeStream() #1 .../classes/util/util_wxpay.class.php(43): WechatPay\GuzzleMiddleware\Util\MediaUtil->__construct('/home/www/websi...') #7 {main} thro in .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php on line 91
已解决,修改wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php第91行
原:'sha256' => \GuzzleHttp\Psr7\hash($stream, 'sha256'),
改:'sha256' => \GuzzleHttp\Psr7\Utils::hash($stream, 'sha256'),
直接修改composer包代码也不合理啊