新增回调参数对象,回调处理

This commit is contained in:
egan
2021-08-18 22:49:57 +08:00
parent f761e9e2b8
commit aae4ffe61d
18 changed files with 402 additions and 117 deletions

View File

@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.egzosn.pay.common.bean.BillType;
import com.egzosn.pay.common.bean.DefaultNoticeRequest;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.NoticeParams;
import com.egzosn.pay.common.bean.NoticeRequest;
@@ -56,7 +57,7 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
/**
* 支付消息拦截器
*/
protected List<PayMessageInterceptor> interceptors = new ArrayList<PayMessageInterceptor>();
protected List<PayMessageInterceptor<PayMessage, PayService>> interceptors = new ArrayList<PayMessageInterceptor<PayMessage, PayService>>();
;
/**
@@ -191,6 +192,17 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
*/
@Override
public Map<String, Object> getParameter2Map(Map<String, String[]> parameterMap, InputStream is) {
return getNoticeParams(new DefaultNoticeRequest(parameterMap, is)).getBody();
}
/**
* 将请求参数或者请求流转化为 Map
*
* @param request 通知请求
* @return 获得回调的请求参数
*/
@Override
public NoticeParams getNoticeParams(NoticeRequest request) {
final Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> params = new TreeMap<String, Object>();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
@@ -212,10 +224,9 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
}
params.put(name, valueStr);
}
return params;
return new NoticeParams(params);
}
/**
* 交易查询接口,带处理器
*
@@ -399,16 +410,7 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
interceptors.add(interceptor);
}
/**
* 将请求参数或者请求流转化为 Map
*
* @param request 通知请求
* @return 获得回调的请求参数
*/
@Override
public NoticeParams getNoticeParams(NoticeRequest request) {
return null;
}
/**
* 将请求参数或者请求流转化为 Map
@@ -417,16 +419,29 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
* @param is 请求流
* @return 获得回调响应信息
*/
@Deprecated
@Override
public PayOutMessage payBack(Map<String, String[]> parameterMap, InputStream is) {
Map<String, Object> data = getParameter2Map(parameterMap, is);
return payBack(new DefaultNoticeRequest(parameterMap, is));
}
/**
* 回调处理
*
* @param request 请求参数
* @return 获得回调响应信息
*/
@Override
public PayOutMessage payBack(NoticeRequest request) {
final NoticeParams noticeParams = getNoticeParams(request);
if (LOG.isDebugEnabled()) {
LOG.debug("回调响应:" + JSON.toJSONString(data));
LOG.debug("回调响应:{}" , JSON.toJSONString(noticeParams));
}
if (!verify(data)) {
if (!verify(noticeParams)) {
return getPayOutMessage("fail", "失败");
}
PayMessage payMessage = this.createMessage(data);
PayMessage payMessage = this.createMessage(noticeParams.getBody());
Map<String, Object> context = new HashMap<String, Object>();
for (PayMessageInterceptor interceptor : interceptors) {
if (!interceptor.intercept(payMessage, context, this)) {

View File

@@ -351,13 +351,21 @@ public interface PayService<PC extends PayConfigStorage> {
<T> T transferQuery(String outNo, String tradeNo, Callback<T> callback);
/**
* 将请求参数或者请求流转化为 Map
* 回调处理
*
* @param parameterMap 请求参数
* @param is 请求流
* @return 获得回调响应信息
* 过时方法,详情查看 {@link #payBack(NoticeRequest)}
*/
@Deprecated
PayOutMessage payBack(Map<String, String[]> parameterMap, InputStream is);
/**
* 回调处理
* @param request 请求参数
* @return 获得回调响应信息
*/
PayOutMessage payBack(NoticeRequest request);
/**
* 使用转换过的参数进行回调处理
@@ -371,7 +379,7 @@ public interface PayService<PC extends PayConfigStorage> {
* 设置支付消息处理器,这里用于处理具体的支付业务
*
* @param handler 消息处理器
* 配合{@link com.egzosn.pay.common.api.PayService#payBack(java.util.Map, java.io.InputStream)}进行使用
* 配合{@link com.egzosn.pay.common.api.PayService#payBack(NoticeRequest)}进行使用
* <p>
* 默认使用{@link com.egzosn.pay.common.api.DefaultPayMessageHandler }进行实现
*/
@@ -381,7 +389,7 @@ public interface PayService<PC extends PayConfigStorage> {
* 设置支付消息处理器,这里用于处理具体的支付业务
*
* @param interceptor 消息拦截器
* 配合{@link com.egzosn.pay.common.api.PayService#payBack(java.util.Map, java.io.InputStream)}进行使用
* 配合{@link com.egzosn.pay.common.api.PayService#payBack(NoticeRequest)}进行使用
* <p>
* 默认使用{@link com.egzosn.pay.common.api.DefaultPayMessageHandler }进行实现
*/

View File

@@ -0,0 +1,95 @@
package com.egzosn.pay.common.bean;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
/**`
*
* 默认的通知请求
* @author Egan
* <pre>
* email egzosn@gmail.com
* date 2021/8/18
* </pre>
*/
public class DefaultNoticeRequest implements NoticeRequest {
private Map<String, String[]> parameterMap;
private InputStream inputStream;
private Map<String, List<String>> headers;
public DefaultNoticeRequest(Map<String, String[]> parameterMap, InputStream inputStream) {
this.parameterMap = parameterMap;
this.inputStream = inputStream;
}
public DefaultNoticeRequest(Map<String, String[]> parameterMap, InputStream inputStream, Map<String, List<String>> headers) {
this.parameterMap = parameterMap;
this.inputStream = inputStream;
this.headers = headers;
}
public DefaultNoticeRequest(InputStream inputStream, Map<String, List<String>> headers) {
this.inputStream = inputStream;
this.headers = headers;
}
/**
* 根据请求头名称获取请求头信息
*
* @param name 名称
* @return 请求头值
*/
@Override
public String getHeader(String name) {
List<String> value = this.headers.get(name);
return (null == value || value.isEmpty()) ? null : value.get(0);
}
/**
* 根据请求头名称获取请求头信息
*
* @param name 名称
* @return 请求头值
*/
@Override
public Enumeration<String> getHeaders(String name) {
return Collections.enumeration(this.headers.get(name));
}
/**
* 获取所有的请求头名称
*
* @return 请求头名称
*/
@Override
public Enumeration<String> getHeaderNames() {
return Collections.enumeration(this.headers.keySet());
}
/**
* 输入流
*
* @return 输入流
* @throws IOException IOException
*/
@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}
/**
* 获取所有的请求参数
*
* @return 请求参数
*/
@Override
public Map<String, String[]> getParameterMap() {
return parameterMap;
}
}

View File

@@ -47,6 +47,8 @@ public class NoticeParams {
*/
private Map<String, Object> attr;
public NoticeParams() {
}
public NoticeParams(Map<String, Object> body) {
this.body = body;

View File

@@ -20,7 +20,7 @@ public class MapGen<K, V> {
keyValue(key, value);
}
public MapGen keyValue(K key, V value) {
public MapGen<K, V> keyValue(K key, V value) {
attr.put(key, value);
return this;
}

View File

@@ -2,6 +2,8 @@ package com.egzosn.pay.common.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Map;
public class Util {
/**
@@ -69,9 +71,11 @@ public class Util {
if (n.toByteArray().length == 33) {
tmpd = new byte[32];
System.arraycopy(n.toByteArray(), 1, tmpd, 0, 32);
} else if (n.toByteArray().length == 32) {
}
else if (n.toByteArray().length == 32) {
tmpd = n.toByteArray();
} else {
}
else {
tmpd = new byte[32];
for (int i = 0; i < 32 - n.toByteArray().length; i++) {
tmpd[i] = 0;
@@ -348,7 +352,8 @@ public class Util {
int algorism = 0;
if (c >= '0' && c <= '9') {
algorism = c - '0';
} else {
}
else {
algorism = c - 55;
}
result += Math.pow(16, max - i) * algorism;
@@ -493,7 +498,8 @@ public class Util {
int i = 0;
try {
i = Integer.parseInt(s, radix);
} catch (NumberFormatException ex) {
}
catch (NumberFormatException ex) {
i = defaultInt;
}
return i;
@@ -510,7 +516,8 @@ public class Util {
int i = 0;
try {
i = Integer.parseInt(s);
} catch (NumberFormatException ex) {
}
catch (NumberFormatException ex) {
i = defaultInt;
}
return i;
@@ -574,7 +581,7 @@ public class Util {
/**
* 一百
*/
public static final BigDecimal HUNDRED = new BigDecimal(100);
public static final BigDecimal HUNDRED = new BigDecimal(100);
/**
* 元转分
@@ -597,4 +604,13 @@ public class Util {
}
public static <K, V> boolean isEmpty(Map<K, V> map) {
return null == map || map.isEmpty();
}
public static <V> boolean isEmpty(Collection<V> collection) {
return null == collection || collection.isEmpty();
}
}