@@ -1,7 +1,6 @@
package com.starry.admin.modules.weichat.controller ;
import cn.hutool.core.util.IdUtil ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson2.JSONObject ;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse ;
@@ -28,9 +27,15 @@ import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document ;
import org.dom4j.DocumentHelper ;
import org.dom4j.Element ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.RestController ;
import javax.annotation.Resource ;
import javax.servlet.http.HttpServletRequest ;
import java.io.ByteArrayOutputStream ;
import java.io.InputStream ;
import java.math.BigDecimal ;
import java.util.HashMap ;
import java.util.List ;
@@ -71,67 +76,55 @@ public class WxPlayController {
* @author admin
* @since 2024/5/8 11:25
**/
@Ge tMapping ( " /jsCallback " )
public String wxPayNotify ( @ RequestBody String xmlData ) {
log . info ( " ****************接受到微信支付回调:{} " , xmlData ) ;
this . dealNotify ( xmlData ) ;
@Reques tMapping ( " /jsCallback " )
public String wxPayNotify ( HttpServlet Request request ) {
try ( InputStream inStream = request . getInputStream ( ) ; ByteArrayOutputStream outSteam = new ByteArrayOutputStream ( ) ) {
byte [ ] buffer = new byte [ 1024 ] ;
int len ;
while ( ( len = inStream . read ( buffer ) ) ! = - 1 ) {
outSteam . write ( buffer , 0 , len ) ;
}
log . info ( " 微信支付回调开始-参数解析-开始 " ) ;
// 获取微信调用我们notify_url的返回信息
String result = outSteam . toString ( " utf-8 " ) ;
log . info ( " 微信支付回调开始-参数解析-结束,result={} " , result ) ;
this . dealNotify ( result ) ;
} catch ( Exception e ) {
log . error ( " 微信支付回调业务处理异常 " , e ) ;
}
return WxPayNotifyResponse . success ( " 成功 " ) ;
}
private void dealNotify ( String xmlData ) {
public void dealNotify ( String xmlData ) {
try {
Map < String , String > orderMap = readStringXmlOut ( xmlData ) ;
String outTradeNo = orderMap . get ( " out_trade_no " ) ;
PlayOrderInfoEntity orderInfoEntity = orderInfoService . getById ( outTradeNo ) ;
String tenantId = orderMap . get ( " attach " ) ;
if ( StrUtil . isBlank ( tenantId ) ) {
log . error ( " 支付对应业务处理异常-未查询到租户信息,xmlData={} " , xmlData ) ;
}
SecurityUtils . setTenantId ( tenantId ) ;
PlayOrderInfoEntity orderInfoEntity = orderInfoService . queryByOrderNo ( outTradeNo ) ;
if ( Objects . isNull ( orderInfoEntity ) ) {
log . error ( " ********* 未查询到对应的支付记录,订单号: {}" , outTradeNo ) ;
log . error ( " 支付对应业务处理异常- 未查询到对应的支付记录,xmlData= {}" , xmlData ) ;
return ;
}
// // TODO如果支付状态不是待 支付
// if (! orderInfoEntity. getOrderStatus().equals(RepairStatusEnum.NOT_PAY.name())) {
// log.error("*********支付记录状态异常,支付记录:{}", JSONObject.toJSONString(repair)) ;
// return;
// }
// Date nowDate = new Date();
// repair.setPaySuccessTime(nowDate) ;
// repair.setPayStatus(RepairStatusEnum.PAID.name());
// repair.setStatus(RepairStatusEnum.REPAIRING.name());
// TODO如果订单不是支付订单,并且 支付状态不为未 支付
if ( ! " 0 " . equals ( orderInfoEntity. getOrderType ( ) ) | | ! " 0 " . equals ( orderInfoEntity . getPayState ( ) ) ) {
log. error ( " 支付对应业务处理异常-支付记录状态异常, 支付记录: xmlData={} " , xmlData ) ;
return ;
}
//修养订单状态
orderInfoEntity . setPayState ( " 1 " ) ;
orderInfoService . updateById ( orderInfoEntity ) ;
//修改账户余额
customUserInfoService . customAccountBalanceRecharge ( orderInfoEntity . getOrderMoney ( ) , orderInfoEntity . getPurchaserBy ( ) , orderInfoEntity . getId ( ) ) ;
log . info ( " *********支付处理完成 " ) ;
} catch ( Exception e ) {
log . error ( e . getMessage ( ) ) ;
log . error ( " 订单回调业务处理异常,xmlData={} " , xmlData ) ;
}
}
//public void jsCallback(HttpServletRequest request, HttpServletResponse response) throws Exception {
// // 读取回调数据
// InputStream inputStream = request.getInputStream();
// StringBuilder sb = new StringBuilder();
// String s;
// BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
// while ((s = in.readLine()) != null) {
// sb.append(s);
// }
// in.close();
// inputStream.close();
//
// // 解析xml成map
// Map<String, Object> m = XmlUtil.xmlToMap(sb.toString());
// // 过滤空 设置 TreeMap
// SortedMap<Object, Object> packageParams = new TreeMap<>();
// for (String parameter : m.keySet()) {
// Object parameterValue = m.get(parameter);
// String v = "";
// if (null != parameterValue) {
// v = parameterValue.toString().trim();
// }
// packageParams.put(parameter, v);
// }
// log.info("packageParams=" + packageParams);
// String resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
// response.getWriter().write(resXml);
//}
@CustomUserLogin
@@ -161,24 +154,24 @@ public class WxPlayController {
// 订单总金额,单位为分
long totalFee = getTotalFee ( money ) ;
// 创建订单信息
// String orderNo = playOrderInfoService. getOrderNo() ;
String orderId = IdUtil . fastSimpleUUID ( ) ;
orderInfoService . createRechargeOrder ( orderId , new BigDecimal ( totalFee * 1 . 0 / 100 ) , new BigDecimal ( totalFee * 1 . 0 / 100 ) , customUserInfo . getId ( ) ) ;
customUserInfoService . customAccountBalanceRecharge ( new BigDecimal ( money ) , ThreadLocalRequestDetail . getCustomUserInfo ( ) . getId ( ) , orderId ) ;
// return R.ok("充值成功");
String orderNo = playOrderInfoService. getOrderNo( ) ;
orderInfoService . createRechargeOrder ( orderNo , new BigDecimal ( totalFee * 1 . 0 / 100 ) , new BigDecimal ( totalFee * 1 . 0 / 100 ) , customUserInfo . getId ( ) ) ;
WxPayService wxPayService = mpService . getWxPay ( ) ;
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest ( ) ;
request . setOpenid ( customUserInfo . getOpenid ( ) ) ;
// 订单总金额, 单位为分( 开发阶段固定设置为支付1分钱)
request . setAttach ( tenantId ) ;
request . setTotalFee ( 1 ) ;
request . setOutTradeNo ( orderId ) ;
request . setOutTradeNo ( orderNo ) ;
request . setTradeType ( " JSAPI " ) ;
request . setSpbillCreateIp ( " 101.43.206.16 " ) ;
request . setNotifyUrl ( wxPayService . getConfig ( ) . getNotifyUrl ( ) ) ;
request . setBody ( " 船票充值 " ) ;
WxPayUnifiedOrderResult orderResult ;
try {
//统一下单, 商户系统先调用该接口在微信支付服务后台生成预支付交易单, 返回正确的预支付交易会话标识后再在APP里面调起支付
orderResult = wxPayService . unifiedOrder ( request ) ;
} catch ( WxPayException e ) {
throw new RuntimeException ( e ) ;