|
@@ -17,8 +17,10 @@ import org.jeecg.modules.pay.config.WechatUrlConstants;
|
|
|
import org.jeecg.modules.pay.serverPay.WXPayUtility;
|
|
|
import org.jeecg.modules.system.app.entity.AppOrder;
|
|
|
import org.jeecg.modules.system.app.entity.AppOrderProInfo;
|
|
|
+import org.jeecg.modules.system.app.entity.AppOrderRefundsInfo;
|
|
|
import org.jeecg.modules.system.app.mapper.AppOrderMapper;
|
|
|
import org.jeecg.modules.system.app.mapper.AppOrderProInfoMapper;
|
|
|
+import org.jeecg.modules.system.app.mapper.AppOrderRefundsInfoMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -29,6 +31,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.BigInteger;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.GeneralSecurityException;
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
@@ -58,6 +61,9 @@ public class WeChatPayService {
|
|
|
@Resource
|
|
|
private AppOrderProInfoMapper appOrderProInfoMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private AppOrderRefundsInfoMapper appOrderRefundsInfoMapper;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 小程序支付拉起
|
|
@@ -233,7 +239,7 @@ public class WeChatPayService {
|
|
|
* @param out_trade_no 发起支付时创建的商户订单号
|
|
|
* @return null代表查询失败 SUCCESS-成功 USERPAYING和ACCEPT为中间态 其他为支付失败
|
|
|
*/
|
|
|
- public String orderQueryByOutTradeNo(String out_trade_no) {
|
|
|
+ public JSONObject orderQueryByOutTradeNo(String out_trade_no) {
|
|
|
String url = WechatUrlConstants.PAY_V3_QUERY_OUT;
|
|
|
url = url.replace("{out_trade_no}", WXPayUtility.urlEncode(out_trade_no));
|
|
|
Map<String, Object> args = new HashMap<>();
|
|
@@ -241,7 +247,7 @@ public class WeChatPayService {
|
|
|
args.put("sub_mchid", WechatConstants.WECHAT_SUB_MCH_ID);
|
|
|
url = url + "?" + WXPayUtility.urlEncode(args);
|
|
|
JSONObject res = wechatPayV3Utils.sendGet(url);
|
|
|
- return res == null ? null : res.getString("trade_state");
|
|
|
+ return res ;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -253,32 +259,53 @@ public class WeChatPayService {
|
|
|
log.info("进入退款接口------>");
|
|
|
log.info("执行操作的 原支付交易对应的商户订单号:{}", orderCode);
|
|
|
|
|
|
- Integer totalFee = 1;
|
|
|
- Integer total = 1;
|
|
|
+ BigDecimal totalFee = BigDecimal.ZERO;
|
|
|
+ BigDecimal total = BigDecimal.ZERO;
|
|
|
AppOrder appOrder = appOrderMapper.selectOne(Wrappers.<AppOrder>lambdaQuery().eq(AppOrder::getOrderCode, orderCode).last("limit 1"));
|
|
|
if (ObjectUtil.isNotEmpty(appOrder)) {
|
|
|
- total = appOrder.getPrice().intValue();
|
|
|
- totalFee = appOrder.getPrice().intValue();
|
|
|
+ total = appOrder.getPrice();
|
|
|
+ totalFee = total;
|
|
|
+ List<AppOrderProInfo> proInfoList = appOrderProInfoMapper.selectList(Wrappers.<AppOrderProInfo>lambdaQuery().eq(AppOrderProInfo::getOrderId, appOrder.getId()).eq(AppOrderProInfo::getType, CommonConstant.ORDER_PRO_INFO_TYPE_6));
|
|
|
+ if (ObjectUtil.isNotEmpty(proInfoList)) {
|
|
|
+ for (AppOrderProInfo appOrderProInfo : proInfoList) {
|
|
|
+ totalFee = totalFee.subtract(appOrderProInfo.getPrice());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
//退款单号
|
|
|
String out_refund_no = generateOrderNumber(1);
|
|
|
|
|
|
- //todo 创建退款订单
|
|
|
+ // 创建退款订单
|
|
|
+ AppOrderRefundsInfo appOrderRefundsInfo = new AppOrderRefundsInfo();
|
|
|
+
|
|
|
+ appOrderRefundsInfo.setOrderId(appOrder.getId());
|
|
|
+ appOrderRefundsInfo.setOrderCode(orderCode);
|
|
|
+ appOrderRefundsInfo.setOutRefundNo(out_refund_no);
|
|
|
+// appOrderRefundsInfo.setRefundId();
|
|
|
+ appOrderRefundsInfo.setTransactionId(appOrder.getTransactionId());
|
|
|
+ appOrderRefundsInfo.setReason(reason);
|
|
|
+// appOrderRefundsInfo.setSuccessTime();
|
|
|
+ appOrderRefundsInfo.setAmount(total);
|
|
|
+// appOrderRefundsInfo.setNotifyRequest();
|
|
|
+ appOrderRefundsInfo.setCreateTime(new Date());
|
|
|
+
|
|
|
+ appOrderRefundsInfoMapper.insert(appOrderRefundsInfo);
|
|
|
|
|
|
|
|
|
//todo 发起分账回退
|
|
|
|
|
|
try {
|
|
|
JSONObject params = new JSONObject();
|
|
|
+ params.put("sub_mchid", WechatConstants.WECHAT_SUB_MCH_ID);
|
|
|
params.put("out_trade_no", orderCode);//商户订单号
|
|
|
params.put("out_refund_no", out_refund_no);//商户退款单号
|
|
|
params.put("reason", reason);//退款原因
|
|
|
params.put("notify_url", WechatUrlConstants.PAY_V3_REFUND_NOTIFY);//退款通知
|
|
|
|
|
|
JSONObject amount = new JSONObject();
|
|
|
- amount.put("refund", (long) (totalFee * 100));//退款金额
|
|
|
+ amount.put("refund", (totalFee.multiply(BigDecimal.valueOf(100))).longValue());//退款金额
|
|
|
amount.put("currency", "CNY");
|
|
|
- amount.put("total", (long) (total * 100));//原订单金额
|
|
|
+ amount.put("total", (total.multiply(BigDecimal.valueOf(100))).longValue());//原订单金额
|
|
|
params.put("amount", amount);
|
|
|
// 执行请求POST 请求发送到微信退款接口
|
|
|
JSONObject res = wechatPayV3Utils.sendPost(WechatUrlConstants.PAY_V3_REFUND, params);
|
|
@@ -329,7 +356,7 @@ public class WeChatPayService {
|
|
|
public Map<String, Object> refundOrderNotify(String jsonData) throws IOException, GeneralSecurityException {
|
|
|
|
|
|
//转为map格式
|
|
|
- Map<String, String> jsonMap = JSONObject.parseObject(jsonData, Map.class);
|
|
|
+ Map jsonMap = JSONObject.parseObject(jsonData, Map.class);
|
|
|
|
|
|
//退款成功后返回一个加密字段resource,以下为解密
|
|
|
/**
|
|
@@ -345,6 +372,8 @@ public class WeChatPayService {
|
|
|
String resultStr = decryptToString(associated_data.getBytes(StandardCharsets.UTF_8), nonce.getBytes(StandardCharsets.UTF_8), ciphertext);
|
|
|
Map<String, String> reqInfo = JSONObject.parseObject(resultStr, Map.class);
|
|
|
|
|
|
+ log.info("微信返回的退款通知数据:" + reqInfo);
|
|
|
+
|
|
|
String refund_status = reqInfo.get("refund_status");//退款状态
|
|
|
String out_trade_no = reqInfo.get("out_trade_no"); //订单号
|
|
|
|
|
@@ -370,6 +399,17 @@ public class WeChatPayService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 创建退款订单
|
|
|
+ AppOrderRefundsInfo appOrderRefundsInfo = appOrderRefundsInfoMapper.selectOne(Wrappers.lambdaQuery(AppOrderRefundsInfo.class).eq(AppOrderRefundsInfo::getOrderId, order.getId()));
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(appOrderRefundsInfo)){
|
|
|
+ // appOrderRefundsInfo.setRefundId();
|
|
|
+ appOrderRefundsInfo.setSuccessTime(new Date());
|
|
|
+// appOrderRefundsInfo.setNotifyRequest();
|
|
|
+ appOrderRefundsInfoMapper.updateById(appOrderRefundsInfo);
|
|
|
+ }
|
|
|
+
|
|
|
parm.put("code", "SUCCESS");
|
|
|
parm.put("message", "成功");
|
|
|
} else {
|