- wechatpay-apache-httpclient,适用于使用Apache HttpClient处理HTTP的Java开发者。
- wechatpay-guzzle-middleware,适用于PHP开发者。
这里用的是PHP,所以使用 wechatpay-guzzle-middleware 。
// 该文件复制自 \vendor\wechatpay\wechatpay-guzzle-middleware\tool
下载微信支付平台证书主要文件 CertificateDownloader.php
<?php
namespace App\Http\Controllers\WxPay;
// 该文件复制自 \vendor\wechatpay\wechatpay-guzzle-middleware\tool\CertificateDownloader.php
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Validator;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
use WechatPay\GuzzleMiddleware\Util\AesUtil;
use WechatPay\GuzzleMiddleware\Auth\CertificateVerifier;
use WechatPay\GuzzleMiddleware\Auth\WechatPay2Validator;
class CertificateDownloader extends Controller
{
const VERSION = '0.1.0';
public function index() {
$opts=[
'mchid'=> config('globaldatas.mchid'),
'serialno'=> config('globaldatas.serial_no'),
'privatekey'=> "apiclient_cert.pem",
'wechatpay-cert'=> null,
'key'=> config('globaldatas.apiv'),
'output'=> '微信平台证书存放目录'
];
$this->downloadCert($opts);
}
public function run()
{
$opts = $this->parseOpts();
if (!$opts) {
$this->printHelp();
exit(1);
}
if (isset($opts['help'])) {
$this->printHelp();
exit(0);
}
if (isset($opts['version'])) {
echo self::VERSION . "\n";
exit(0);
}
$this->downloadCert($opts);
}
private function parseOpts()
{
$opts = [
[ 'key', 'k', true ],
[ 'mchid', 'm', true ],
[ 'privatekey', 'f', true ],
[ 'serialno', 's', true ],
[ 'output', 'o', true ],
[ 'wechatpay-cert', 'c', false ],
];
$shortopts = 'hV';
$longopts = [ 'help', 'version' ];
foreach ($opts as $opt) {
$shortopts .= $opt[1].':';
$longopts[] = $opt[0].':';
}
$parsed = getopt($shortopts, $longopts);
if (!$parsed) {
return false;
}
$args = [];
foreach ($opts as $opt) {
if (isset($parsed[$opt[0]])) {
$args[$opt[0]] = $parsed[$opt[0]];
}
else if (isset($parsed[$opt[1]])) {
$args[$opt[0]] = $parsed[$opt[1]];
}
else if ($opt[2]) {
return false;
}
}
if (isset($parsed['h']) || isset($parsed['help'])) {
$args['help'] = true;
}
if (isset($parsed['V']) || isset($parsed['version'])) {
$args['version'] = true;
}
return $args;
}
private function printHelp()
{
echo <<<EOD
Usage: 微信支付平台证书下载工具 [-hV] [-c=<wechatpayCertificatePath>]
-f=<privateKeyFilePath> -k=<apiV3key> -m=<merchantId>
-o=<outputFilePath> -s=<serialNo>
-m, --mchid=<merchantId> 商户号
-s, --serialno=<serialNo> 商户证书的序列号
-f, --privatekey=<privateKeyFilePath>
商户的私钥文件
-k, --key=<apiV3key> ApiV3Key
-c, --wechatpay-cert=<wechatpayCertificatePath>
微信支付平台证书,验证签名
-o, --output=<outputFilePath>
下载成功后保存证书的路径
-V, --version Print version information and exit.
-h, --help Show this help message and exit.
EOD;
}
}