wechatpay-apiv3/wechatpay-php

微信支付平台证书异常

Closed this issue · 2 comments

tashuo commented

运行环境

- OS: CentOS 7.9.2009
- PHP: 7.4.5
- wechatpay-php: 1.4.8

描述你的问题现象

  1. 微信支付平台证书我是在本机(Mac)上使用CertificateDownloader.php工具生成并上传至服务器
  2. 在使用文档中native下单的示例代码时报错: UnexpectedValueException: Read the $certificate failed, please check it whether or nor correct in file /var/www/vendor/wechatpay/wechatpay/src/Util/PemUtil.php on line 90

商户api私钥apiclient_key.pem处理没问题,这个是微信支付平台证书的报错,请问是因为平台(mac -> linux)的原因导致证书处理异常吗?还是其他原因?

附生成的微信支付平台证书格式:

-----BEGIN CERTIFICATE-----
xxxx
xxxx
xxxx
-----END CERTIFICATE-----

wechatpay-php/README.md

Lines 90 to 95 in fbe8d7c

// 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
$platformCertificateFilePath = 'file:///path/to/wechatpay/cert.pem';
$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
// 从「微信支付平台证书」中获取「证书序列号」
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);

public static function parseCertificateSerialNo($certificate): string
{
$info = openssl_x509_parse($certificate);
if (false === $info || !isset($info['serialNumberHex'])) {
throw new UnexpectedValueException('Read the $certificate failed, please check it whether or nor correct');
}
return strtoupper($info['serialNumberHex']);
}

README上的L91行决定了L95行的解析,进而影响PemUtil.php:L90行,请检查你初始化参数平台证书路径是否带了file://协议。

tashuo commented

wechatpay-php/README.md

Lines 90 to 95 in fbe8d7c

// 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
$platformCertificateFilePath = 'file:///path/to/wechatpay/cert.pem';
$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
// 从「微信支付平台证书」中获取「证书序列号」
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);

public static function parseCertificateSerialNo($certificate): string
{
$info = openssl_x509_parse($certificate);
if (false === $info || !isset($info['serialNumberHex'])) {
throw new UnexpectedValueException('Read the $certificate failed, please check it whether or nor correct');
}
return strtoupper($info['serialNumberHex']);
}

README上的L91行决定了L95行的解析,进而影响PemUtil.php:L90行,请检查你初始化参数平台证书路径是否带了file://协议。

加了file://前缀的,商户私钥证书都可以解析出来,后来直接用openssl_pkey_get_public(file_get_contents($platformCertificateFilePath))的方式可以正常解析,刚试回file://的形式突然又没问题了,多谢解答!