接口文档中说要先获取账单,通过API v3标准对download_url进行签名 我使用了文档中的demo 但是一直报错:"Http头Authorization值格式错误,请参考《微信支付商户REST API签名规则》"
生成连接:
https://api.mch.weixin.qq.com/v3/billdownload/file?token=ICNRgTSPv6dxsvlmhQhy9d1xLCQFf2mhNMqTxrBbHKRe2ZViVtb6XCSvEgssNTpT -H 'Authorization: WECHATPAY2-SHA256-RSA2048 mchid="1610661703",nonce_str="593BEC0C930BF1AFEB40B4A08C8FB242",signature="jYyzep3KWGnCxB43CnObJUhKqZjNMRK6ThfOqilgFWmdg4WnpvlcuDb0ul/Avbgny4n7/rL35QWOsOzgs5lSkKk8jmnp/of7emVbFg5Ji1v2tB8sraqMNMJE2/L6Ohuq/cePjDsBceJmna3OO988xTSDnsAz3MjcgeD6FquUo0LwuoPLI2LCii2mi4N4mS4Pcz9JEO98zKtBTyXP9p7IEIWDkQNUaTUTkQQFYWnWW9CazjJ1o7buXSt1cewtui7NMz5OmbUUxuD0X/9wfnkTam8NhtBthSIZrkp6S2YdBkyYPA0+ajUItY4psYw/btWSjL/hbDkJEE5ajYy5YJlGUg==",timestamp="1635398934",serial_no="401F9FABBB243E274F3C278582DB8F7FF946E5A8"'
我在用的实例,供参考:
<?php require_once('./vendor/autoload.php'); use WeChatPay\Builder; use WeChatPay\Crypto\Rsa; use WeChatPay\Crypto\Hash; use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Utils; use GuzzleHttp\Psr7\InflateStream; use Psr\Http\Message\ResponseInterface; $instance = Builder::factory([ 'mchid' => '', 'serial' => '', 'privateKey' => $privateKey, 'certs' => ['' => $publicKey], ]); $handler = clone $instance->getDriver()->select()->getConfig('handler'); $handler->remove('verifier'); $tradeBill = $instance ->v3->bill->tradebill ->getAsync([ 'query' => [ 'bill_date' => $billDate, 'bill_type' => 'ALL', 'tar_type' => 'GZIP', ], ]) ->then(static function(ResponseInterface $response) { return (array) json_decode($response->getBody()->getContents(), true); }) ->wait(); $hashAlgo = strtolower($tradeBill['hash_type'] ?? 'sha1'); $hashValue = $tradeBill['hash_value'] ?? null; $previous = new Uri($tradeBill['download_url'] ?? ''); $baseUri = $previous->composeComponents($previous->getScheme(), $previous->getAuthority(), '/', '', ''); $savedTo = fopen('/tmp/all.' . $billDate . '.csv.gz', 'w+'); $stream = Utils::streamFor($savedTo); $message = $instance ->chain(ltrim($previous->getPath(), '/')) ->getAsync([ 'sink' => $stream, 'handler' => $handler, 'query' => $previous->getQuery(), 'base_uri' => $baseUri, ]) ->then(static function(ResponseInterface $response) use($hashValue, $hashAlgo) { if (Hash::equals(Utils::hash(new InflateStream($response->getBody()), $hashAlgo), $hashValue)) { return 'Verified with digest(' . $hashValue . ') OK'; } $response->getBody()->close(); throw new UnexpectedValueException('Bad digest verification'); }) ->wait(); print_r($message);