0
点赞
收藏
分享

微信扫一扫

微信支付APIv3版,平台证书可视化下载工具

大家好,我是小悟

微信支付相关接口升级为APIv3版本,

相较于之前的微信支付API,主要区别是:

遵循统一的REST的设计风格,

使用JSON作为数据交互的格式,不再使用XML,

使用基于非对称密钥的SHA256-RSA的数字签名算法,不再使用MD5或HMAC-SHA256,

不再要求携带HTTPS客户端证书(仅需携带证书序列号),

使用AES-256-GCM,对回调中的关键信息进行加密保护。

微信支付APIv3版,平台证书可视化下载工具_微信支付

开发者需要通过接口下载微信支付平台证书,微信支付平台证书的作用主要有两个,

一是验证微信应答或回调签名的正确性,以确保应答或回调是由微信支付发送。

二是为了保证通信过程中敏感信息字段(如用户的住址、银行卡号、手机号码等)的机密性,微信支付API v3要求商户对上送的敏感信息字段进行加密。这样只有拥有私钥的微信支付才能对密文进行解密,从而保证了信息的机密性。

微信支付APIv3版,平台证书可视化下载工具_电商收付通_02

public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
try {
byte[] buffer = Base64.decode(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
}
}

所有用到APIv3版本的微信支付接口都需要使用平台证书,电商收付通作为微信支付的核心内容也不例外。都需要调用接口来下载,这样极其不方便,于是就做了一个可视化下载平台证书的工具。

微信支付APIv3版,平台证书可视化下载工具_电商收付通_03

只需要填写商户号、商户APIv3密钥、商户API证书序列号、商户API证书(apiclient_key.pem)就可以下载微信支付平台证书。证书接收邮箱是用来接收创建好的证书和证书序列号,会自动发送,所以请确保邮箱填写正确。

微信支付APIv3版,平台证书可视化下载工具_微信支付_04

private static CertificateResult saveCertificate(List<PlainCertificateItem> cert, String merchantId, String savePath) throws IOException {
File file = new File(savePath);
List<X509Certificate> x509Certs = new ArrayList<X509Certificate>();
file.mkdirs();
String outputAbsoluteFilename = "";
String fileName = "";
for (PlainCertificateItem item : cert) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(item.getPlainCertificate().getBytes(StandardCharsets.UTF_8));
X509Certificate x509Cert = CertificateUtils.getCertificate(inputStream);
x509Certs.add(x509Cert);
String expireTime = Date2Utils.dealDateFormat(item.getExpireTime());
expireTime = Date2Utils.dataFormat(expireTime);
fileName = merchantId + "_" + expireTime + "_wechat_pay_platform_" + item.getSerialNo() + ".pem";
outputAbsoluteFilename = file.getAbsolutePath() + File.separator + fileName;
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputAbsoluteFilename), StandardCharsets.UTF_8))) {
writer.write(item.getPlainCertificate());
}
logger.info("输出证书文件目录:" + outputAbsoluteFilename);
}
CertificateResult certificateResult = new CertificateResult();
certificateResult.setWechatPubKeyPath(outputAbsoluteFilename)
.setWechatPubKeyName(fileName);
return certificateResult;
}

为了参数安全起见,微信支付平台证书生成成功后,请到商户后台更换商户API证书和API证书序列号或者修改APIv3密钥。API证书、API证书序列号或者APIv3密钥变化不会影响之前生成好的平台证书。

公众号【悟空码字】后台回复【微信平台证书】获取创建平台证书地址。


您的一键三连,是我更新的最大动力,谢谢

山水有相逢,来日皆可期,谢谢阅读,我们再会

我手中的金箍棒,上能通天,下能探海

举报

相关推荐

0 条评论