|
|
@@ -9,8 +9,10 @@ import com.wechat.pay.java.core.util.NonceUtil;
|
|
|
import com.wechat.pay.java.core.util.PemUtil;
|
|
|
import com.wechat.pay.java.service.payments.jsapi.model.*;
|
|
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
+import com.zhongshu.card.client.model.payment.paySetting.WxPayConfigModel;
|
|
|
import com.zhongshu.card.client.model.wechat.MiniAppUserInfoVo;
|
|
|
import com.zhongshu.payment.client.model.PrePayModel;
|
|
|
+import com.zhongshu.payment.client.model.param.WxPrepayParam;
|
|
|
import com.zhongshu.payment.client.ret.ResultContent;
|
|
|
import com.zhongshu.payment.server.core.dataConfig.WxV3PayConfig;
|
|
|
import com.zhongshu.payment.server.core.service.base.SuperService;
|
|
|
@@ -41,13 +43,13 @@ public class WxPaymentService extends SuperService {
|
|
|
public static com.wechat.pay.java.service.payments.jsapi.JsapiService service;
|
|
|
|
|
|
// 初始化商户配置
|
|
|
- public static Config RSAAutoCertificateConfig(){
|
|
|
+ public static Config RSAAutoCertificateConfig(String mchId, String privateKeyPath, String mchSerialNo, String apiV3Key){
|
|
|
return new RSAAutoCertificateConfig.Builder()
|
|
|
- .merchantId(WxV3PayConfig.Mch_ID)
|
|
|
+ .merchantId(mchId)
|
|
|
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
|
|
- .privateKeyFromPath(WxV3PayConfig.privateKeyPath)
|
|
|
- .merchantSerialNumber(WxV3PayConfig.mchSerialNo)
|
|
|
- .apiV3Key(WxV3PayConfig.apiV3Key)
|
|
|
+ .privateKeyFromPath(privateKeyPath)
|
|
|
+ .merchantSerialNumber(mchSerialNo)
|
|
|
+ .apiV3Key(apiV3Key)
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
@@ -73,29 +75,24 @@ public class WxPaymentService extends SuperService {
|
|
|
return miniAppUserInfo.getOpenid();
|
|
|
}
|
|
|
|
|
|
- /** @Author wjf
|
|
|
- * @Description //TODO 预支付下单
|
|
|
- * @Date 2024/7/24
|
|
|
- * @Param
|
|
|
- * @return
|
|
|
- **/
|
|
|
- public ResultContent<PrePayModel> prepay(PrepayRequest request){
|
|
|
+ public ResultContent<PrePayModel> testPrepay(WxPrepayParam param){
|
|
|
|
|
|
|
|
|
// 初始化商户配置
|
|
|
- Config config = RSAAutoCertificateConfig();
|
|
|
+ Config config = RSAAutoCertificateConfig(WxV3PayConfig.Mch_ID, WxV3PayConfig.privateKeyPath, WxV3PayConfig.mchSerialNo, WxV3PayConfig.apiV3Key);
|
|
|
// 初始化服务
|
|
|
service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
// ... 调用接口
|
|
|
try {
|
|
|
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ PrepayRequest request = new PrepayRequest();
|
|
|
request.setAppid("wx92ae04fb0f325887");
|
|
|
request.setMchid("1680033836");
|
|
|
request.setDescription("充值服务");
|
|
|
- request.setOutTradeNo("A1111111");
|
|
|
- request.setNotifyUrl("http://8.137.120.225:8080/paymentserver-wjf/wechat/v3/notify/payNotify");
|
|
|
+ request.setOutTradeNo("A111112");
|
|
|
+ request.setNotifyUrl("https://api.dev.qk.zonelife.cn/paymentserver-wjf/wechat/v3/notify/payNotify");
|
|
|
Amount amount = new Amount();
|
|
|
- amount.setTotal(10);
|
|
|
+ amount.setTotal(1);
|
|
|
amount.setCurrency("CNY");
|
|
|
request.setAmount(amount);
|
|
|
Payer payer = new Payer();
|
|
|
@@ -133,14 +130,69 @@ public class WxPaymentService extends SuperService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /** @Author wjf
|
|
|
+ * @Description //TODO 预支付下单
|
|
|
+ * @Date 2024/7/24
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ **/
|
|
|
+ public ResultContent<PrePayModel> prepay(PrepayRequest request, WxPayConfigModel configModel){
|
|
|
+
|
|
|
+ String privateKeyPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + configModel.getPrivateKeyPath();
|
|
|
+
|
|
|
+ // 初始化商户配置
|
|
|
+ Config config = RSAAutoCertificateConfig(configModel.getMchId(), privateKeyPath, configModel.getMchSerialNo(), configModel.getApiV3Key());
|
|
|
+ // 初始化服务
|
|
|
+ service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
+ // ... 调用接口
|
|
|
+ try {
|
|
|
+ // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ request.setAppid(configModel.getAppId());
|
|
|
+ request.setMchid(configModel.getMchId());
|
|
|
+ request.setNotifyUrl(configModel.getNotifyUrl());
|
|
|
+ // 调用接口
|
|
|
+ PrepayResponse response = service.prepay(request);
|
|
|
+
|
|
|
+ PrePayModel prePayModel = new PrePayModel();
|
|
|
+ prePayModel.setNonceStr(NonceUtil.createNonce(10));
|
|
|
+ prePayModel.setPackAge("prepay_id=" + response.getPrepayId());
|
|
|
+ prePayModel.setSignType("RSA");
|
|
|
+ long timeStamp = System.currentTimeMillis() / 1000;
|
|
|
+ prePayModel.setTimeStamp(Long.toString(timeStamp));
|
|
|
+
|
|
|
+ String sign = buildMessage(WxV3PayConfig.APP_ID, timeStamp, prePayModel.getNonceStr(), prePayModel.getPackAge());
|
|
|
+ String signature = sign(privateKeyPath, sign.getBytes("utf-8"));
|
|
|
+ prePayModel.setPaySign(signature);
|
|
|
+ return ResultContent.buildContent(prePayModel);
|
|
|
+ } catch (HttpException e) { // 发送HTTP请求失败
|
|
|
+ // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
|
|
|
+ log.info("请求失败1:{}", e.getHttpRequest());
|
|
|
+ return ResultContent.buildFail("发送HTTP请求失败");
|
|
|
+ } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
|
|
+ // 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
|
|
|
+ log.info("请求失败2:{}", e.getResponseBody());
|
|
|
+ return ResultContent.buildFail(e.getErrorMessage());
|
|
|
+ } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
+ // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
|
|
|
+ log.info("请求失败3:{}", e.getMessage());
|
|
|
+ return ResultContent.buildFail("服务返回成功,返回体类型不合法,或者解析返回体失败");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.info("请求失败4:{}", e.getMessage());
|
|
|
+ return ResultContent.buildFail("编码错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/** @Author wjf
|
|
|
* @Description //TODO 关闭订单
|
|
|
* @Date 2024/7/24
|
|
|
* @Param 商户订单号 outTradeNo
|
|
|
* @return
|
|
|
**/
|
|
|
- public ResultContent closeOrder(String outTradeNo){
|
|
|
- Config config = RSAAutoCertificateConfig();
|
|
|
+ public ResultContent closeOrder(String outTradeNo, WxPayConfigModel configModel){
|
|
|
+ String privateKeyPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + configModel.getPrivateKeyPath();
|
|
|
+
|
|
|
+ // 初始化商户配置
|
|
|
+ Config config = RSAAutoCertificateConfig(configModel.getMchId(), privateKeyPath, configModel.getMchSerialNo(), configModel.getApiV3Key());
|
|
|
// 初始化服务
|
|
|
service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
try {
|
|
|
@@ -171,8 +223,11 @@ public class WxPaymentService extends SuperService {
|
|
|
* @Param 商户订单号 outTradeNo
|
|
|
* @return
|
|
|
**/
|
|
|
- public ResultContent<Transaction> queryOrderByOutTradeNo(String outTradeNo){
|
|
|
- Config config = RSAAutoCertificateConfig();
|
|
|
+ public ResultContent<Transaction> queryOrderByOutTradeNo(String outTradeNo, WxPayConfigModel configModel){
|
|
|
+ String privateKeyPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + configModel.getPrivateKeyPath();
|
|
|
+
|
|
|
+ // 初始化商户配置
|
|
|
+ Config config = RSAAutoCertificateConfig(configModel.getMchId(), privateKeyPath, configModel.getMchSerialNo(), configModel.getApiV3Key());
|
|
|
// 初始化服务
|
|
|
service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
try {
|
|
|
@@ -203,8 +258,11 @@ public class WxPaymentService extends SuperService {
|
|
|
* @Param 商户订单号 outTradeNo
|
|
|
* @return
|
|
|
**/
|
|
|
- public ResultContent queryOrderById(String transactionId){
|
|
|
- Config config = RSAAutoCertificateConfig();
|
|
|
+ public ResultContent queryOrderById(String transactionId, WxPayConfigModel configModel){
|
|
|
+ String privateKeyPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + configModel.getPrivateKeyPath();
|
|
|
+
|
|
|
+ // 初始化商户配置
|
|
|
+ Config config = RSAAutoCertificateConfig(configModel.getMchId(), privateKeyPath, configModel.getMchSerialNo(), configModel.getApiV3Key());
|
|
|
// 初始化服务
|
|
|
service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
try {
|
|
|
@@ -234,7 +292,7 @@ public class WxPaymentService extends SuperService {
|
|
|
@SneakyThrows
|
|
|
public String sign(String privateKeyPath, byte[] message){
|
|
|
Signature sign = Signature.getInstance("SHA256withRSA");
|
|
|
- PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(WxV3PayConfig.privateKeyPath);
|
|
|
+ PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(privateKeyPath);
|
|
|
sign.initSign(privateKey);
|
|
|
sign.update(message);
|
|
|
return Base64.getEncoder().encodeToString(sign.sign());
|