支付宝支付h5 无法支付成功,检查时回调参数格式问题,现重新整理上线

This commit is contained in:
Chopper 2021-06-17 16:31:40 +08:00
parent 897600335e
commit e01c279cfe
4 changed files with 83 additions and 15 deletions

View File

@ -1,10 +1,7 @@
package cn.lili.common.security.filter; package cn.lili.common.security.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
@ -15,8 +12,8 @@ import java.io.IOException;
* @version v1.0 * @version v1.0
* 2021-06-04 10:37 * 2021-06-04 10:37
*/ */
@WebFilter //@WebFilter
@Component //@Component
public class XssFilter implements Filter { public class XssFilter implements Filter {
FilterConfig filterConfig = null; FilterConfig filterConfig = null;

View File

@ -1,5 +1,7 @@
package cn.lili.common.utils; package cn.lili.common.utils;
import cn.hutool.json.JSONUtil;
import cn.lili.modules.payment.kit.dto.PayParam;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -56,4 +58,75 @@ public class BeanUtil {
return null; return null;
} }
} }
/**
* 将对象转换为key value
* A=a&B=b&C=c 格式
*/
public static String formatKeyValuePair(Object object) {
//准备接受的字符串
StringBuilder stringBuffer = new StringBuilder();
//获取对象字段
String[] fieldNames = BeanUtil.getFiledName(object);
//遍历所有属性
for (int j = 0; j < fieldNames.length; j++) {
//不是第一个并且不是最后一个拼接&
if (j != 0) {
stringBuffer.append("&");
}
//获取属性的名字
String key = fieldNames[j];
//获取值
Object value = BeanUtil.getFieldValueByName(key, object);
stringBuffer.append(key).append("=").append(value.toString());
}
return stringBuffer.toString();
}
/**
* key value键值对 转换为 对象
* A=a&B=b&C=c 格式 转换为对象
*/
public static <T> T formatKeyValuePair(String str, T t) {
//填写对参数键值对
String[] params = str.split("&");
//获取对象字段
String[] fieldNames = BeanUtil.getFiledName(t);
try {
//循环每个参数
for (String param : params) {
String[] keyValues = param.split("=");
for (int i = 0; i < fieldNames.length; i++) {
if (fieldNames[i].equals(keyValues[0])) {
Field f = t.getClass().getDeclaredField(fieldNames[i]);
f.setAccessible(true);
//长度为2 才转换否则不转
if (keyValues.length == 2) {
f.set(t, keyValues[1]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public static void main(String[] args) throws IllegalAccessException {
PayParam payParam = new PayParam();
payParam.setClientType("client");
payParam.setOrderType("");
payParam.setSn("sn");
String val = formatKeyValuePair(payParam);
System.out.println(val);
PayParam param = formatKeyValuePair(val, new PayParam());
System.out.println(JSONUtil.toJsonStr(param));
}
} }

View File

@ -255,6 +255,7 @@ public class StringUtils extends StrUtil {
/** /**
* 过滤特殊字符串 * 过滤特殊字符串
*
* @param str * @param str
* @return * @return
*/ */

View File

@ -6,11 +6,11 @@ import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.ResultCode; import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil; import cn.lili.common.enums.ResultUtil;
import cn.lili.common.exception.ServiceException; import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.BeanUtil;
import cn.lili.common.utils.SnowFlake; import cn.lili.common.utils.SnowFlake;
import cn.lili.common.utils.StringUtils; import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.ResultMessage; import cn.lili.common.vo.ResultMessage;
import cn.lili.config.properties.ApiProperties; import cn.lili.config.properties.ApiProperties;
import cn.lili.config.properties.DomainProperties;
import cn.lili.modules.payment.entity.RefundLog; import cn.lili.modules.payment.entity.RefundLog;
import cn.lili.modules.payment.kit.CashierSupport; import cn.lili.modules.payment.kit.CashierSupport;
import cn.lili.modules.payment.kit.Payment; import cn.lili.modules.payment.kit.Payment;
@ -63,9 +63,6 @@ public class AliPayPlugin implements Payment {
//API域名 //API域名
@Autowired @Autowired
private ApiProperties apiProperties; private ApiProperties apiProperties;
//域名配置
@Autowired
private DomainProperties domainProperties;
@Override @Override
public ResultMessage<Object> h5pay(HttpServletRequest request, HttpServletResponse response, PayParam payParam) { public ResultMessage<Object> h5pay(HttpServletRequest request, HttpServletResponse response, PayParam payParam) {
@ -79,7 +76,7 @@ public class AliPayPlugin implements Payment {
payModel.setSubject(cashierParam.getDetail()); payModel.setSubject(cashierParam.getDetail());
payModel.setTotalAmount(cashierParam.getPrice() + ""); payModel.setTotalAmount(cashierParam.getPrice() + "");
//回传数据 //回传数据
payModel.setPassbackParams(URLEncoder.createAll().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8)); payModel.setPassbackParams(URLEncoder.createAll().encode(BeanUtil.formatKeyValuePair(payParam), StandardCharsets.UTF_8));
//3分钟超时 //3分钟超时
payModel.setTimeoutExpress("3m"); payModel.setTimeoutExpress("3m");
payModel.setOutTradeNo(outTradeNo); payModel.setOutTradeNo(outTradeNo);
@ -118,7 +115,7 @@ public class AliPayPlugin implements Payment {
//3分钟超时 //3分钟超时
payModel.setTimeoutExpress("3m"); payModel.setTimeoutExpress("3m");
//回传数据 //回传数据
payModel.setPassbackParams(URLEncoder.createAll().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8)); payModel.setPassbackParams(URLEncoder.createAll().encode(BeanUtil.formatKeyValuePair(payParam), StandardCharsets.UTF_8));
payModel.setOutTradeNo(outTradeNo); payModel.setOutTradeNo(outTradeNo);
payModel.setProductCode("QUICK_MSECURITY_PAY"); payModel.setProductCode("QUICK_MSECURITY_PAY");
@ -151,7 +148,7 @@ public class AliPayPlugin implements Payment {
payModel.setTotalAmount(cashierParam.getPrice() + ""); payModel.setTotalAmount(cashierParam.getPrice() + "");
//回传数据 //回传数据
payModel.setPassbackParams(URLEncoder.createAll().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8)); payModel.setPassbackParams(URLEncoder.createAll().encode(BeanUtil.formatKeyValuePair(payParam), StandardCharsets.UTF_8));
// payModel.setStoreId("store_id"); // payModel.setStoreId("store_id");
payModel.setTimeoutExpress("3m"); payModel.setTimeoutExpress("3m");
payModel.setOutTradeNo(outTradeNo); payModel.setOutTradeNo(outTradeNo);
@ -256,7 +253,7 @@ public class AliPayPlugin implements Payment {
String payParamStr = map.get("passback_params"); String payParamStr = map.get("passback_params");
String payParamJson = URLDecoder.decode(payParamStr, StandardCharsets.UTF_8); String payParamJson = URLDecoder.decode(payParamStr, StandardCharsets.UTF_8);
PayParam payParam = JSONUtil.toBean(payParamJson, PayParam.class); PayParam payParam = BeanUtil.formatKeyValuePair(payParamJson, new PayParam());
if (verifyResult) { if (verifyResult) {