支付配置改为RSAPublicKeyConfig,增加公钥配置,但是WXCertUtil.exe 生成的apiclient_cert.pem格式不正确

This commit is contained in:
jkcdev 2025-05-12 18:10:11 +08:00
parent 17d4235e42
commit e2f9b2356f
9 changed files with 39 additions and 21 deletions

View File

@ -74,6 +74,13 @@
<version>${hutool.version}</version>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>

View File

@ -1,7 +1,7 @@
package com.cdzy.payment.config;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.cipher.RSASigner;
import com.wechat.pay.java.core.util.PemUtil;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
@ -33,9 +33,17 @@ public class WxPayConfig {
*/
private String merchantId;
/**
* 微信支付商户钥文件路径
* 微信支付商户钥文件路径
*/
private String privateKeyPath;
/**
* 微信支付商户公钥文件路径
*/
private String publicKeyPath;
/**
* 微信支付商户公钥ID
*/
private String publicKeyId;
/**
* 微信支付商户证书序列号
*/
@ -58,13 +66,16 @@ public class WxPayConfig {
*/
private Integer expireMinute = 5;
@Bean
public Config certificateConfig() {
return new RSAAutoCertificateConfig.Builder()
return new RSAPublicKeyConfig.Builder()
.merchantId(merchantId)
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥商户私钥会用来生成请求的签名
.privateKeyFromPath(privateKeyPath)
.publicKeyFromPath(publicKeyPath)
.merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3Key)
.publicKeyId(publicKeyId)
.build();
}

View File

@ -4,8 +4,8 @@ import com.alibaba.fastjson2.JSONObject;
import com.cdzy.payment.model.dto.HandleNotifyResult;
import com.cdzy.payment.service.WxPayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@ -75,7 +75,7 @@ public class EbikeWxPaymentController {
}
/**
* 调用微信退款接口
* 调用退款接口
*
* @param orderId 订单id
* @return 退款成功返回true否则返回false

View File

@ -2,7 +2,6 @@ package com.cdzy.payment.service;
import com.mybatisflex.core.service.IService;
import com.cdzy.payment.model.entity.EbikeRefund;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import java.util.List;

View File

@ -2,12 +2,11 @@ package com.cdzy.payment.service;
import com.alibaba.fastjson2.JSONObject;
import com.cdzy.payment.model.dto.AmountRefundDto;
import com.cdzy.payment.model.dto.HandleNotifyResult;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import jakarta.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
/**
* 微信支付服务类JSAPI支付小程序

View File

@ -1,6 +1,5 @@
package com.cdzy.payment.service.impl;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.cdzy.common.model.JsonResult;
@ -27,6 +26,7 @@ import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.core.util.NonceUtil;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.GoodsDetail;
@ -38,7 +38,7 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.*;
@ -114,7 +114,7 @@ public class WxPayServiceImpl implements WxPayService {
r.put("error", "查询订单失败, "+result.getMessage());
return r;
}
EbikePaymentDto paymentDto = JSON.parseObject(JSON.toJSONString(result.getData()), EbikePaymentDto.class);
EbikePaymentDto paymentDto = JSON.parseObject(JSONObject.toJSONString(result.getData()), EbikePaymentDto.class);
return prepay(paymentDto, ebikePayment);
}
@ -188,7 +188,7 @@ public class WxPayServiceImpl implements WxPayService {
WxJsapiPromptDto wxJsapiPromptDto = new WxJsapiPromptDto();
wxJsapiPromptDto.setAppId(wxPayConfig.getAppId());
wxJsapiPromptDto.setTimeStamp(String.valueOf(Instant.now().getEpochSecond()));
wxJsapiPromptDto.setNonceStr(RandomUtil.randomStringUpper(32));
wxJsapiPromptDto.setNonceStr(NonceUtil.createNonce(32));
wxJsapiPromptDto.setExtension("prepay_id=" + payId);
return wxJsapiPromptDto.toJson(wxRsaSigner);
}

View File

@ -1,7 +1,7 @@
package com.cdzy.payment.utils;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

View File

@ -2,7 +2,7 @@ server:
port: 10017
spring:
application:
name: ebike-pay
name: ebike-payment
cloud:
nacos:
server-addr: 127.0.0.1:8848 # nacos
@ -33,11 +33,13 @@ mybatis-flex:
mapper-locations: classpath:mapper/*.xml
payment:
wx-pay:
app-id: wx097b305458b0757c
merchant-id: 1668000369
private-key-path: D:\Projects\eBIKE\docs\wechatpay\apiclient_key.pem
merchant-serial-number: 5157F09EFDC096DE15EBE81A47057A72********
api-v3-key: 5157F09EFDC096DE15EBE81A47057A72********
app-id: wx327d788d7bd6eddf
merchant-id: 1715147005
private-key-path: D:/Projects/eBIKE/docs/secrets/apiclient_key.pem
public-key-path: D:/Projects/eBIKE/docs/secrets/apiclient_cert.pem
merchant-serial-number: 7873E3E694ADD0368EA3E9FAC929F496EECB8DF9
api-v3-key: 1715147005V3Key20250425174554633
public-key-id: PUB_KEY_ID_0117151470052025042500331704000601
pay-notify_url: http://192.168.2.156:10017/wxPayment/pay-notify
refund-notify_url: http://192.168.2.156:10017/wxPayment/refund-notify
expire-minutes: 5