|
|
@@ -5,46 +5,27 @@ import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
import com.wechat.pay.java.core.exception.HttpException;
|
|
|
import com.wechat.pay.java.core.exception.MalformedMessageException;
|
|
|
import com.wechat.pay.java.core.exception.ServiceException;
|
|
|
-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.core.util.PemUtil;
|
|
|
-import com.wechat.pay.java.service.partnerpayments.app.model.Transaction;
|
|
|
-import com.wechat.pay.java.service.payments.jsapi.model.Amount;
|
|
|
-import com.wechat.pay.java.service.payments.jsapi.model.Payer;
|
|
|
-import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
|
|
|
-import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.model.*;
|
|
|
+import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
import com.zhongshu.card.client.model.wechat.MiniAppUserInfoVo;
|
|
|
import com.zhongshu.payment.client.model.PrePayModel;
|
|
|
-import com.zhongshu.payment.client.model.param.WxTransactionsParam;
|
|
|
import com.zhongshu.payment.client.ret.ResultContent;
|
|
|
-import com.zhongshu.payment.client.ret.ResultState;
|
|
|
import com.zhongshu.payment.server.core.dataConfig.WxV3PayConfig;
|
|
|
import com.zhongshu.payment.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.payment.server.core.utils.wx.WechatCUtil;
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
-import jakarta.servlet.ServletInputStream;
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
-import jline.internal.Log;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStreamReader;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.Signature;
|
|
|
import java.util.Base64;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
-import static cn.hutool.crypto.SignUtil.sign;
|
|
|
-import static cn.hutool.crypto.SignUtil.signParamsSha256;
|
|
|
-import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.*;
|
|
|
-import static com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders.WECHAT_PAY_SIGNATURE;
|
|
|
|
|
|
/**
|
|
|
* @author TRX
|
|
|
@@ -60,7 +41,7 @@ public class WxPaymentService extends SuperService {
|
|
|
public static com.wechat.pay.java.service.payments.jsapi.JsapiService service;
|
|
|
|
|
|
// 初始化商户配置
|
|
|
- public Config RSAAutoCertificateConfig(){
|
|
|
+ public static Config RSAAutoCertificateConfig(){
|
|
|
return new RSAAutoCertificateConfig.Builder()
|
|
|
.merchantId(WxV3PayConfig.Mch_ID)
|
|
|
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
|
|
@@ -98,21 +79,16 @@ public class WxPaymentService extends SuperService {
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
- public ResultContent prepay(WxTransactionsParam param){
|
|
|
+ public ResultContent<PrePayModel> prepay(PrepayRequest request){
|
|
|
+
|
|
|
|
|
|
- String openId = getOpenId(param.getJscode());
|
|
|
- if (StringUtil.isNullOrEmpty(openId)){
|
|
|
- return ResultContent.buildFail("获取用户openid失败");
|
|
|
- }
|
|
|
// 初始化商户配置
|
|
|
Config config = RSAAutoCertificateConfig();
|
|
|
// 初始化服务
|
|
|
service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
// ... 调用接口
|
|
|
try {
|
|
|
- PrepayRequest request = new PrepayRequest();
|
|
|
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
- // 调用接口
|
|
|
request.setAppid("wx92ae04fb0f325887");
|
|
|
request.setMchid("1680033836");
|
|
|
request.setDescription("充值服务");
|
|
|
@@ -125,6 +101,7 @@ public class WxPaymentService extends SuperService {
|
|
|
Payer payer = new Payer();
|
|
|
payer.setOpenid("oSd8L7Wlucyn7a8g3SZCOeLrSUKY");
|
|
|
request.setPayer(payer);
|
|
|
+ // 调用接口
|
|
|
PrepayResponse response = service.prepay(request);
|
|
|
|
|
|
PrePayModel prePayModel = new PrePayModel();
|
|
|
@@ -140,22 +117,120 @@ public class WxPaymentService extends SuperService {
|
|
|
return ResultContent.buildContent(prePayModel);
|
|
|
} catch (HttpException e) { // 发送HTTP请求失败
|
|
|
// 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
|
|
|
- Log.info("请求失败1:", e.getHttpRequest());
|
|
|
+ log.info("请求失败1:{}", e.getHttpRequest());
|
|
|
return ResultContent.buildFail("发送HTTP请求失败");
|
|
|
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
|
|
// 调用e.getResponseBody()获取返回体打印日志或上报监控,更多方法见ServiceException定义
|
|
|
- Log.info("请求失败2:", e.getResponseBody());
|
|
|
+ log.info("请求失败2:{}", e.getResponseBody());
|
|
|
return ResultContent.buildFail(e.getErrorMessage());
|
|
|
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
// 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
|
|
|
- Log.info("请求失败3:", e.getMessage());
|
|
|
+ log.info("请求失败3:{}", e.getMessage());
|
|
|
return ResultContent.buildFail("服务返回成功,返回体类型不合法,或者解析返回体失败");
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
- Log.info("请求失败4:", e.getMessage());
|
|
|
+ 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();
|
|
|
+ // 初始化服务
|
|
|
+ service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
+ try {
|
|
|
+ CloseOrderRequest request = new CloseOrderRequest();
|
|
|
+ // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ // 调用接口
|
|
|
+ request.setMchid(WxV3PayConfig.Mch_ID);
|
|
|
+ request.setOutTradeNo(outTradeNo);
|
|
|
+ service.closeOrder(request);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ } catch (HttpException e) { // 发送HTTP请求失败
|
|
|
+ // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
|
|
|
+ log.info("请求失败1:{}", e.getHttpRequest());
|
|
|
+ return ResultContent.buildFail("发送HTTP请求失败");
|
|
|
+ } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
|
|
+ log.info("请求失败2:{}", e.getResponseBody());
|
|
|
+ return ResultContent.buildFail(e.getErrorMessage());
|
|
|
+ } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
+ // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
|
|
|
+ log.info("请求失败3:{}", e.getMessage());
|
|
|
+ return ResultContent.buildFail("服务返回成功,返回体类型不合法,或者解析返回体失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @Author wjf
|
|
|
+ * @Description //TODO 商户订单号查询订单
|
|
|
+ * @Date 2024/7/24
|
|
|
+ * @Param 商户订单号 outTradeNo
|
|
|
+ * @return
|
|
|
+ **/
|
|
|
+ public ResultContent<Transaction> queryOrderByOutTradeNo(String outTradeNo){
|
|
|
+ Config config = RSAAutoCertificateConfig();
|
|
|
+ // 初始化服务
|
|
|
+ service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
+ try {
|
|
|
+ QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
|
|
|
+ // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ // 调用接口
|
|
|
+ request.setMchid(WxV3PayConfig.Mch_ID);
|
|
|
+ request.setOutTradeNo(outTradeNo);
|
|
|
+ Transaction transaction = service.queryOrderByOutTradeNo(request);
|
|
|
+ return ResultContent.buildSuccess(transaction);
|
|
|
+ } catch (HttpException e) { // 发送HTTP请求失败
|
|
|
+ // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
|
|
|
+ log.info("请求失败1:{}", e.getHttpRequest());
|
|
|
+ return ResultContent.buildFail("发送HTTP请求失败");
|
|
|
+ } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
|
|
+ log.info("请求失败2:{}", e.getResponseBody());
|
|
|
+ return ResultContent.buildFail(e.getErrorMessage());
|
|
|
+ } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
+ // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
|
|
|
+ log.info("请求失败3:{}", e.getMessage());
|
|
|
+ return ResultContent.buildFail("服务返回成功,返回体类型不合法,或者解析返回体失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @Author wjf
|
|
|
+ * @Description //TODO 微信支付订单号查询订单
|
|
|
+ * @Date 2024/7/24
|
|
|
+ * @Param 商户订单号 outTradeNo
|
|
|
+ * @return
|
|
|
+ **/
|
|
|
+ public ResultContent queryOrderById(String transactionId){
|
|
|
+ Config config = RSAAutoCertificateConfig();
|
|
|
+ // 初始化服务
|
|
|
+ service = new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(config).build();
|
|
|
+ try {
|
|
|
+ QueryOrderByIdRequest request = new QueryOrderByIdRequest();
|
|
|
+ // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ // 调用接口
|
|
|
+ request.setMchid(WxV3PayConfig.Mch_ID);
|
|
|
+ request.setTransactionId(transactionId);
|
|
|
+ Transaction transaction = service.queryOrderById(request);
|
|
|
+ return ResultContent.buildSuccess(transaction);
|
|
|
+ } catch (HttpException e) { // 发送HTTP请求失败
|
|
|
+ // 调用e.getHttpRequest()获取请求打印日志或上报监控,更多方法见HttpException定义
|
|
|
+ log.info("请求失败1:{}", e.getHttpRequest());
|
|
|
+ return ResultContent.buildFail("发送HTTP请求失败");
|
|
|
+ } catch (ServiceException e) { // 服务返回状态小于200或大于等于300,例如500
|
|
|
+ log.info("请求失败2:{}", e.getResponseBody());
|
|
|
+ return ResultContent.buildFail(e.getErrorMessage());
|
|
|
+ } catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
+ // 调用e.getMessage()获取信息打印日志或上报监控,更多方法见MalformedMessageException定义
|
|
|
+ log.info("请求失败3:{}", e.getMessage());
|
|
|
+ return ResultContent.buildFail("服务返回成功,返回体类型不合法,或者解析返回体失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
@SneakyThrows
|
|
|
public String sign(String privateKeyPath, byte[] message){
|
|
|
Signature sign = Signature.getInstance("SHA256withRSA");
|