refactor(payment): 重构微信支付私钥处理逻辑
- 将方法参数从 keyPath 修改为 key,移除对文件路径的依赖 - 更新方法签名和文档注释,以适应新的参数 - 删除 WechatPaymentSetting 中未使用的证书字段
This commit is contained in:
		
							parent
							
								
									586a507bb1
								
							
						
					
					
						commit
						9225b4ff10
					
				@ -313,16 +313,16 @@ public class PayKit {
 | 
				
			|||||||
     * v3 接口创建签名
 | 
					     * v3 接口创建签名
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param signMessage 待签名的参数
 | 
					     * @param signMessage 待签名的参数
 | 
				
			||||||
     * @param keyPath     key.pem 证书路径
 | 
					     * @param key     key.pem 证书
 | 
				
			||||||
     * @return 生成 v3 签名
 | 
					     * @return 生成 v3 签名
 | 
				
			||||||
     * @throws Exception 异常信息
 | 
					     * @throws Exception 异常信息
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static String createSign(String signMessage, String keyPath) throws Exception {
 | 
					    public static String createSign(String signMessage, String key) throws Exception {
 | 
				
			||||||
        if (StrUtil.isEmpty(signMessage)) {
 | 
					        if (StrUtil.isEmpty(signMessage)) {
 | 
				
			||||||
            return null;
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        //获取商户私钥
 | 
					        //获取商户私钥
 | 
				
			||||||
        PrivateKey privateKey = PayKit.getPrivateKey(keyPath);
 | 
					        PrivateKey privateKey = PayKit.getPrivateKey(key);
 | 
				
			||||||
        //生成签名
 | 
					        //生成签名
 | 
				
			||||||
        return RsaKit.encryptByPrivateKey(signMessage, privateKey);
 | 
					        return RsaKit.encryptByPrivateKey(signMessage, privateKey);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -378,13 +378,13 @@ public class PayKit {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 获取商户私钥
 | 
					     * 获取商户私钥
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param keyPath 商户私钥证书路径
 | 
					     * @param key 商户私钥证书
 | 
				
			||||||
     * @return {@link PrivateKey} 商户私钥
 | 
					     * @return {@link PrivateKey} 商户私钥
 | 
				
			||||||
     * @throws Exception 异常信息
 | 
					     * @throws Exception 异常信息
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static PrivateKey getPrivateKey(String keyPath) throws Exception {
 | 
					    public static PrivateKey getPrivateKey(String key) throws Exception {
 | 
				
			||||||
        String originalKey = FileUtil.readUtf8String(keyPath);
 | 
					//        String originalKey = FileUtil.readUtf8String(keyPath);
 | 
				
			||||||
        String privateKey = originalKey
 | 
					        String privateKey = key
 | 
				
			||||||
                .replace("-----BEGIN PRIVATE KEY-----", "")
 | 
					                .replace("-----BEGIN PRIVATE KEY-----", "")
 | 
				
			||||||
                .replace("-----END PRIVATE KEY-----", "")
 | 
					                .replace("-----END PRIVATE KEY-----", "")
 | 
				
			||||||
                .replaceAll("\\s+", "");
 | 
					                .replaceAll("\\s+", "");
 | 
				
			||||||
 | 
				
			|||||||
@ -449,7 +449,7 @@ public class WxPayKit {
 | 
				
			|||||||
     * @param urlSuffix 可通过 WxApiType 来获取,URL挂载参数需要自行拼接
 | 
					     * @param urlSuffix 可通过 WxApiType 来获取,URL挂载参数需要自行拼接
 | 
				
			||||||
     * @param mchId     商户Id
 | 
					     * @param mchId     商户Id
 | 
				
			||||||
     * @param serialNo  商户 API 证书序列号
 | 
					     * @param serialNo  商户 API 证书序列号
 | 
				
			||||||
     * @param keyPath   key.pem 证书路径
 | 
					     * @param key   key.pem 证书
 | 
				
			||||||
     * @param body      接口请求参数
 | 
					     * @param body      接口请求参数
 | 
				
			||||||
     * @param nonceStr  随机字符库
 | 
					     * @param nonceStr  随机字符库
 | 
				
			||||||
     * @param timestamp 时间戳
 | 
					     * @param timestamp 时间戳
 | 
				
			||||||
@ -458,11 +458,11 @@ public class WxPayKit {
 | 
				
			|||||||
     * @throws Exception 异常信息
 | 
					     * @throws Exception 异常信息
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static String buildAuthorization(RequestMethodEnums method, String urlSuffix, String mchId,
 | 
					    public static String buildAuthorization(RequestMethodEnums method, String urlSuffix, String mchId,
 | 
				
			||||||
                                            String serialNo, String keyPath, String body, String nonceStr,
 | 
					                                            String serialNo, String key, String body, String nonceStr,
 | 
				
			||||||
                                            long timestamp, String authType) throws Exception {
 | 
					                                            long timestamp, String authType) throws Exception {
 | 
				
			||||||
        //构建签名参数
 | 
					        //构建签名参数
 | 
				
			||||||
        String buildSignMessage = PayKit.buildSignMessage(method, urlSuffix, timestamp, nonceStr, body);
 | 
					        String buildSignMessage = PayKit.buildSignMessage(method, urlSuffix, timestamp, nonceStr, body);
 | 
				
			||||||
        String signature = PayKit.createSign(buildSignMessage, keyPath);
 | 
					        String signature = PayKit.createSign(buildSignMessage, key);
 | 
				
			||||||
        //根据平台规则生成请求头 authorization
 | 
					        //根据平台规则生成请求头 authorization
 | 
				
			||||||
        return PayKit.getAuthorization(mchId, serialNo, nonceStr, String.valueOf(timestamp), signature, authType);
 | 
					        return PayKit.getAuthorization(mchId, serialNo, nonceStr, String.valueOf(timestamp), signature, authType);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -165,7 +165,7 @@ public class WechatApi {
 | 
				
			|||||||
     * @param mchId        商户Id
 | 
					     * @param mchId        商户Id
 | 
				
			||||||
     * @param serialNo     商户 API 证书序列号
 | 
					     * @param serialNo     商户 API 证书序列号
 | 
				
			||||||
     * @param platSerialNo 平台序列号,接口中包含敏感信息时必传
 | 
					     * @param platSerialNo 平台序列号,接口中包含敏感信息时必传
 | 
				
			||||||
     * @param keyPath      apiclient_key.pem 证书路径
 | 
					     * @param keyPath      apiclient_key.pem 证书
 | 
				
			||||||
     * @param body         接口请求参数
 | 
					     * @param body         接口请求参数
 | 
				
			||||||
     * @param nonceStr     随机字符库
 | 
					     * @param nonceStr     随机字符库
 | 
				
			||||||
     * @param timestamp    时间戳
 | 
					     * @param timestamp    时间戳
 | 
				
			||||||
@ -175,12 +175,12 @@ public class WechatApi {
 | 
				
			|||||||
     * @throws Exception 接口执行异常
 | 
					     * @throws Exception 接口执行异常
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static PaymentHttpResponse v3(RequestMethodEnums method, String urlPrefix, String urlSuffix,
 | 
					    public static PaymentHttpResponse v3(RequestMethodEnums method, String urlPrefix, String urlSuffix,
 | 
				
			||||||
                                         String mchId, String serialNo, String platSerialNo, String keyPath,
 | 
					                                         String mchId, String serialNo, String platSerialNo, String key,
 | 
				
			||||||
                                         String body, String nonceStr, long timestamp, String authType,
 | 
					                                         String body, String nonceStr, long timestamp, String authType,
 | 
				
			||||||
                                         File file) throws Exception {
 | 
					                                         File file) throws Exception {
 | 
				
			||||||
        //构建 Authorization
 | 
					        //构建 Authorization
 | 
				
			||||||
        String authorization = WxPayKit.buildAuthorization(method, urlSuffix, mchId, serialNo,
 | 
					        String authorization = WxPayKit.buildAuthorization(method, urlSuffix, mchId, serialNo,
 | 
				
			||||||
                keyPath, body, nonceStr, timestamp, authType);
 | 
					                key, body, nonceStr, timestamp, authType);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (StrUtil.isEmpty(platSerialNo)) {
 | 
					        if (StrUtil.isEmpty(platSerialNo)) {
 | 
				
			||||||
            platSerialNo = serialNo;
 | 
					            platSerialNo = serialNo;
 | 
				
			||||||
@ -252,17 +252,17 @@ public class WechatApi {
 | 
				
			|||||||
     * @param mchId        商户Id
 | 
					     * @param mchId        商户Id
 | 
				
			||||||
     * @param serialNo     商户 API 证书序列号
 | 
					     * @param serialNo     商户 API 证书序列号
 | 
				
			||||||
     * @param platSerialNo 平台序列号
 | 
					     * @param platSerialNo 平台序列号
 | 
				
			||||||
     * @param keyPath      apiclient_key.pem 证书路径
 | 
					     * @param key      apiclient_key.pem 证书
 | 
				
			||||||
     * @param body         接口请求参数
 | 
					     * @param body         接口请求参数
 | 
				
			||||||
     * @return {@link PaymentHttpResponse} 请求返回的结果
 | 
					     * @return {@link PaymentHttpResponse} 请求返回的结果
 | 
				
			||||||
     * @throws Exception 接口执行异常
 | 
					     * @throws Exception 接口执行异常
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static PaymentHttpResponse v3(RequestMethodEnums method, String urlPrefix, String urlSuffix, String mchId,
 | 
					    public static PaymentHttpResponse v3(RequestMethodEnums method, String urlPrefix, String urlSuffix, String mchId,
 | 
				
			||||||
                                         String serialNo, String platSerialNo, String keyPath, String body) throws Exception {
 | 
					                                         String serialNo, String platSerialNo, String key, String body) throws Exception {
 | 
				
			||||||
        long timestamp = System.currentTimeMillis() / 1000;
 | 
					        long timestamp = System.currentTimeMillis() / 1000;
 | 
				
			||||||
        String authType = "WECHATPAY2-SHA256-RSA2048";
 | 
					        String authType = "WECHATPAY2-SHA256-RSA2048";
 | 
				
			||||||
        String nonceStr = WxPayKit.generateStr();
 | 
					        String nonceStr = WxPayKit.generateStr();
 | 
				
			||||||
        return v3(method, urlPrefix, urlSuffix, mchId, serialNo, platSerialNo, keyPath, body, nonceStr, timestamp, authType, null);
 | 
					        return v3(method, urlPrefix, urlSuffix, mchId, serialNo, platSerialNo, key, body, nonceStr, timestamp, authType, null);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 | 
				
			|||||||
@ -41,14 +41,14 @@ public class WechatPaymentSetting {
 | 
				
			|||||||
     * 私钥
 | 
					     * 私钥
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private String apiclient_key;
 | 
					    private String apiclient_key;
 | 
				
			||||||
    /**
 | 
					//    /**
 | 
				
			||||||
     * pem 证书
 | 
					//     * pem 证书
 | 
				
			||||||
     */
 | 
					//     */
 | 
				
			||||||
    private String apiclient_cert_pem;
 | 
					//    private String apiclient_cert_pem;
 | 
				
			||||||
    /**
 | 
					//    /**
 | 
				
			||||||
     * p12 证书
 | 
					//     * p12 证书
 | 
				
			||||||
     */
 | 
					//     */
 | 
				
			||||||
    private String apiclient_cert_p12;
 | 
					//    private String apiclient_cert_p12;
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 商户证书序列号
 | 
					     * 商户证书序列号
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user