Skip to content

Commit ea93be6

Browse files
egzosn@gmail.comegzosn@gmail.com
egzosn@gmail.com
authored and
egzosn@gmail.com
committed
针对拦截器进行优化,简化开发者判断处理
1 parent 7cfeaa0 commit ea93be6

File tree

18 files changed

+334
-101
lines changed

18 files changed

+334
-101
lines changed

pay-java-ali/src/main/java/com/egzosn/pay/ali/api/AliPayService.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import com.egzosn.pay.common.api.BasePayService;
77
import com.egzosn.pay.common.api.Callback;
88
import com.egzosn.pay.common.api.PayConfigStorage;
9-
import com.egzosn.pay.common.bean.MethodType;
10-
import com.egzosn.pay.common.bean.PayOrder;
11-
import com.egzosn.pay.common.bean.PayOutMessage;
12-
import com.egzosn.pay.common.bean.TransactionType;
9+
import com.egzosn.pay.common.bean.*;
1310
import com.egzosn.pay.common.bean.result.PayException;
1411
import com.egzosn.pay.common.exception.PayErrorException;
1512
import com.egzosn.pay.common.http.HttpConfigStorage;
@@ -36,11 +33,11 @@ public class AliPayService extends BasePayService {
3633
protected final Log log = LogFactory.getLog(AliPayService.class);
3734

3835
//正式测试环境
39-
private String httpsReqUrl = "https://openapi.alipay.com/gateway.do";
36+
private final static String httpsReqUrl = "https://openapi.alipay.com/gateway.do";
4037
//沙箱测试环境账号
41-
private String devReqUrl = "https://openapi.alipaydev.com/gateway.do";
38+
private final static String devReqUrl = "https://openapi.alipaydev.com/gateway.do";
4239
//兼容上一版本即时收款
43-
private String httpsReqUrlBefore = "https://mapi.alipay.com/gateway.do";
40+
private final static String httpsReqUrlBefore = "https://mapi.alipay.com/gateway.do";
4441

4542

4643
/**
@@ -297,6 +294,17 @@ public PayOutMessage getPayOutMessage(String code, String message) {
297294
return PayOutMessage.TEXT().content(code.toLowerCase()).build();
298295
}
299296

297+
/**
298+
* 获取成功输出消息,用户返回给支付端
299+
* 主要用于拦截器中返回
300+
* @param payMessage 支付回调消息
301+
* @return 返回输出消息
302+
*/
303+
@Override
304+
public PayOutMessage successPayOutMessage(PayMessage payMessage) {
305+
return PayOutMessage.TEXT().content("success").build();
306+
}
307+
300308
/**
301309
*
302310
* @param orderInfo 发起支付的订单信息
@@ -305,7 +313,6 @@ public PayOutMessage getPayOutMessage(String code, String message) {
305313
*/
306314
@Override
307315
public String buildRequest(Map<String, Object> orderInfo, MethodType method) {
308-
309316
StringBuffer formHtml = new StringBuffer();
310317
formHtml.append("<form id=\"_alipaysubmit_\" name=\"alipaysubmit\" action=\"");
311318
if (null == orderInfo.get("method")) {
@@ -332,11 +339,6 @@ public String buildRequest(Map<String, Object> orderInfo, MethodType method) {
332339

333340
formHtml.append("<input type=\"hidden\" name=\"biz_content\" value=\"" + biz_content.replace("\"", "&quot;") + "\"/>");
334341
}
335-
336-
337-
338-
//submit按钮控件请不要含有name属性
339-
// formHtml.append("<input type=\"submit\" value=\"\" style=\"display:none;\">");
340342
formHtml.append("</form>");
341343
formHtml.append("<script>document.forms['_alipaysubmit_'].submit();</script>");
342344

pay-java-ali/src/main/java/com/egzosn/pay/ali/before/api/AliPayService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean verify(Map<String, Object> params) {
8585
*/
8686
@Override
8787
public boolean verifySource(String id) {
88-
return "true".equals(requestTemplate.getForObject( getHttpsVerifyUrl() + "partner=" + payConfigStorage.getPid() + "&notify_id=" + id, String.class));
88+
return "true".equals(requestTemplate.getForObject( getHttpsVerifyUrl() + "&partner=" + payConfigStorage.getPid() + "&notify_id=" + id, String.class));
8989
}
9090

9191
/**
@@ -228,6 +228,15 @@ public Map<String, Object> getParameter2Map(Map<String, String[]> parameterMap,
228228
}
229229
//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
230230
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
231+
if (!valueStr.matches("\\w+")){
232+
try {
233+
if(valueStr.equals(new String(valueStr.getBytes("iso8859-1"), "iso8859-1"))){
234+
valueStr=new String(valueStr.getBytes("iso8859-1"), payConfigStorage.getInputCharset());
235+
}
236+
} catch (UnsupportedEncodingException e) {
237+
e.printStackTrace();
238+
}
239+
}
231240
params.put(name, valueStr);
232241
}
233242

pay-java-common/src/main/java/com/egzosn/pay/common/api/PayMessageRouterRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ protected PayOutMessage service(PayMessage payMessage,
288288
// 如果拦截器不通过
289289
for (PayMessageInterceptor interceptor : this.interceptors) {
290290
if (!interceptor.intercept(payMessage, context, payService)) {
291-
return null;
291+
//这里直接返回成功,解决外层判断问题
292+
return payService.successPayOutMessage(payMessage);
292293
}
293294
}
294295

pay-java-common/src/main/java/com/egzosn/pay/common/api/PayService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.egzosn.pay.common.api;
22

3-
import com.egzosn.pay.common.bean.MethodType;
4-
import com.egzosn.pay.common.bean.PayOrder;
5-
import com.egzosn.pay.common.bean.PayOutMessage;
6-
import com.egzosn.pay.common.bean.TransactionType;
3+
import com.egzosn.pay.common.bean.*;
74
import com.egzosn.pay.common.exception.PayErrorException;
85
import com.egzosn.pay.common.http.HttpConfigStorage;
96
import com.egzosn.pay.common.http.HttpRequestTemplate;
@@ -126,6 +123,14 @@ public interface PayService {
126123
*/
127124
PayOutMessage getPayOutMessage(String code, String message);
128125

126+
/**
127+
* 获取成功输出消息,用户返回给支付端
128+
* 主要用于拦截器中返回
129+
* @param payMessage 支付回调消息
130+
* @return 返回输出消息
131+
*/
132+
PayOutMessage successPayOutMessage(PayMessage payMessage);
133+
129134
/**
130135
* 获取输出消息,用户返回给支付端, 针对于web端
131136
*

pay-java-common/src/main/java/com/egzosn/pay/common/bean/PayOrder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class PayOrder {
2626
private String deviceInfo;
2727
//付款条码串 与设备号类似???
2828
private String authCode;
29+
//
30+
private String openid;
2931
//交易类型
3032
private TransactionType transactionType;
3133
//支付币种
@@ -145,8 +147,11 @@ public PayOrder(String subject, String body, BigDecimal price, String outTradeNo
145147
this.transactionType = transactionType;
146148
}
147149

150+
public String getOpenid() {
151+
return openid;
152+
}
148153

149-
150-
151-
154+
public void setOpenid(String openid) {
155+
this.openid = openid;
156+
}
152157
}

pay-java-demo/src/main/java/com/egzosn/pay/demo/controller/PayController.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ public String toPay(Integer payId, String transactionType, String bankType, BigD
8484
}
8585

8686

87+
/**
88+
* 公众号支付
89+
*
90+
*
91+
* @param payId 账户id
92+
* @param openid openid
93+
* @return 跳到支付页面
94+
*/
95+
@RequestMapping(value = "jsapi" )
96+
public Map toPay(Integer payId, String openid, BigDecimal price) {
97+
//获取对应的支付账户操作工具(可根据账户id)
98+
PayResponse payResponse = service.getPayResponse(payId);
99+
100+
PayOrder order = new PayOrder("订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""), PayType.valueOf(payResponse.getStorage().getPayType()).getTransactionType("JSAPI"));
101+
order.setOpenid(openid);
102+
103+
Map orderInfo = payResponse.getService().orderInfo(order);
104+
orderInfo.put("code", 0);
105+
106+
return orderInfo;
107+
}
108+
109+
87110
/**
88111
* 刷卡付,pos主动扫码付款(条码付)
89112
*

pay-java-demo/src/main/java/com/egzosn/pay/demo/dao/ApyAccountRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ApyAccountRepository {
3434
// TODO 2017/2/9 16:20 author: egan sign_type只有单一key时public_key与private_key相等,比如sign_type=MD5的情况
3535
apyAccount1.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB");
3636
apyAccount1.setPrivateKey("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKroe/8h5vC4L6T+B2WdXiVwGsMvUKgb2XsKix6VY3m2wcf6tyzpNRDCNykbIwGtaeo7FshN+qZxdXHLiIam9goYncBit/8ojfLGy2gLxO/PXfzGxYGs0KsDZ+ryVPPmE34ZZ8jiJpR0ygzCFl8pN3QJPJRGTJn5+FTT9EF/9zyZAgMBAAECgYAktngcYC35u7cQXDk+jMVyiVhWYU2ULxdSpPspgLGzrZyG1saOcTIi/XVX8Spd6+B6nmLQeF/FbU3rOeuD8U2clzul2Z2YMbJ0FYay9oVZFfp5gTEFpFRTVfzqUaZQBIjJe/xHL9kQVqc5xHlE/LVA27/Kx3dbC35Y7B4EVBDYAQJBAOhsX8ZreWLKPhXiXHTyLmNKhOHJc+0tFH7Ktise/0rNspojU7o9prOatKpNylp9v6kux7migcMRdVUWWiVe+4ECQQC8PqsuEz7B0yqirQchRg1DbHjh64bw9Kj82EN1/NzOUd53tP9tg+SO97EzsibK1F7tOcuwqsa7n2aY48mQ+y0ZAkBndA2xcRcnvOOjtAz5VO8G7R12rse181HjGfG6AeMadbKg30aeaGCyIxN1loiSfNR5xsPJwibGIBg81mUrqzqBAkB+K6rkaPXJR9XtzvdWb/N3235yPkDlw7Z4MiOVM3RzvR/VMDV7m8lXoeDde2zQyeMOMYy6ztwA6WgE1bhGOnQRAkEAouUBv1sVdSBlsexX15qphOmAevzYrpufKgJIRLFWQxroXMS7FTesj+f+FmGrpPCxIde1dqJ8lqYLTyJmbzMPYw==\n");
37-
apyAccount1.setNotifyUrl("http://pay.egan.in/payBack2.json");
37+
apyAccount1.setNotifyUrl("http://pay.egan.in/payBack1.json");
3838
// 无需同步回调可不填
3939
// apyAccount1.setReturnUrl("");
4040
apyAccount1.setInputCharset("UTF-8");

pay-java-demo/src/main/java/com/egzosn/pay/demo/service/PayResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.egzosn.pay.common.api.PayMessageHandler;
1717
import com.egzosn.pay.common.api.PayService;
1818
import com.egzosn.pay.common.bean.MsgType;
19+
import com.egzosn.pay.demo.service.interceptor.YoudianPayMessageInterceptor;
1920
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2021

2122
import javax.annotation.Resource;
@@ -85,8 +86,7 @@ private void buildRouter(Integer payId) {
8586
.async(false)
8687
.msgType(MsgType.text.name()) //消息类型
8788
.payType(PayType.aliPay.name()) //支付账户事件类型
88-
// .transactionType(AliTransactionType.UNAWARE.name())//交易类型,有关回调的可在这处理
89-
.interceptor(new AliPayMessageInterceptor(payId)) //拦截器
89+
.interceptor(new AliPayMessageInterceptor()) //拦截器
9090
.handler(autowire(new AliPayMessageHandler(payId))) //处理器
9191
.end()
9292
.rule()
@@ -99,6 +99,7 @@ private void buildRouter(Integer payId) {
9999
.async(false)
100100
.msgType(MsgType.json.name())
101101
.payType(PayType.youdianPay.name())
102+
.interceptor(new YoudianPayMessageInterceptor()) //拦截器
102103
.handler(autowire(new YouDianPayMessageHandler(payId)))
103104
.end()
104105
.rule()

pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/AliPayMessageHandler.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.egzosn.pay.common.bean.PayOutMessage;
66
import com.egzosn.pay.common.exception.PayErrorException;
77

8+
import java.math.BigDecimal;
89
import java.util.Map;
910

1011
/**
@@ -22,26 +23,25 @@ public AliPayMessageHandler(Integer payId) {
2223

2324
@Override
2425
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
26+
27+
Map<String, Object> message = payMessage.getPayMessage();
2528
//交易状态
26-
String trade_status = (String) payMessage.getPayMessage().get("trade_status");
27-
28-
if ("TRADE_SUCCESS".equals(trade_status)){
29-
//判断该笔订单是否在商户网站中已经做过处理
30-
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
31-
//如果有做过处理,不执行商户的业务程序
32-
//注意:
33-
//付款完成后,支付宝系统发送该交易状态通知
34-
35-
} else if("TRADE_FINISHED".equals(trade_status)) {
36-
//判断该笔订单是否在商户网站中已经做过处理
37-
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
38-
//如果有做过处理,不执行商户的业务程序
39-
//注意:
40-
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
41-
} else if ("WAIT_BUYER_PAY".equals(trade_status) || "TRADE_CLOSED".equals(trade_status)) {
42-
43-
}
44-
45-
return payService.getPayOutMessage("success", "成功");
29+
String trade_status = (String) message.get("trade_status");
30+
31+
//上下文对象中获取账单
32+
// AmtApply amtApply = (AmtApply)context.get("amtApply");
33+
//日志存储
34+
// amtPaylogService.createAmtPaylogByCallBack(amtApply, message.toString());
35+
//交易完成
36+
if ("TRADE_SUCCESS".equals(trade_status) || "TRADE_FINISHED".equals(trade_status)) {
37+
38+
BigDecimal payAmount = new BigDecimal((String) message.get("total_fee"));
39+
40+
return payService.getPayOutMessage("success", "成功");
41+
42+
}/* else if ("WAIT_BUYER_PAY".equals(trade_status) || "TRADE_CLOSED".equals(trade_status)) {
43+
44+
}*/
45+
return payService.getPayOutMessage("fail", "失败");
4646
}
4747
}

pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/FuiouPayMessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context,
2626
if ("0000".equals(payMessage.getPayMessage().get("order_pay_code"))){
2727
/////这里进行成功的处理
2828

29-
return PayOutMessage.JSON().content("order_pay_error","成功").build();
29+
return PayOutMessage.JSON().content("success","成功").build();
3030
}
3131

32-
return PayOutMessage.JSON().content("order_pay_error",payMessage.getPayMessage().get("order_pay_error")).build();
32+
return PayOutMessage.JSON().content("fail", "失败").build();
3333
}
3434
}

pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/YouDianPayMessageHandler.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.egzosn.pay.demo.service.handler;
22

3+
import com.alibaba.fastjson.JSON;
34
import com.egzosn.pay.common.api.PayService;
45
import com.egzosn.pay.common.bean.PayMessage;
56
import com.egzosn.pay.common.bean.PayOutMessage;
@@ -23,12 +24,17 @@ public YouDianPayMessageHandler(Integer payId) {
2324
@Override
2425
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
2526
//交易状态
26-
if ("0000".equals(payMessage.getPayMessage().get("order_pay_code"))){
27-
/////这里进行成功的处理
27+
Map<String, Object> message = payMessage.getPayMessage();
28+
//上下文对象中获取账单
29+
// AmtApply amtApply = (AmtApply)context.get("amtApply");
30+
//日志存储
31+
// amtPaylogService.createAmtPaylogByCallBack(amtApply, message.toString());
32+
33+
if ("SUCCESS".equals(message.get("return_code"))){
34+
/////这里进行成功的处理,因没有返回金额
2835

29-
return PayOutMessage.JSON().content("order_pay_error","成功").build();
3036
}
3137

32-
return PayOutMessage.JSON().content("order_pay_error",payMessage.getPayMessage().get("order_pay_error")).build();
38+
return PayOutMessage.TEXT().content(JSON.toJSONString(message)).build();
3339
}
3440
}

pay-java-demo/src/main/java/com/egzosn/pay/demo/service/interceptor/AliPayMessageInterceptor.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
*/
1818
public class AliPayMessageInterceptor implements PayMessageInterceptor {
1919

20-
//支付账户id
21-
private Integer payId;
22-
public AliPayMessageInterceptor(Integer payId) {
23-
24-
this.payId = payId;
25-
}
26-
2720
/**
2821
* 拦截支付消息
2922
*
@@ -37,6 +30,26 @@ public AliPayMessageInterceptor(Integer payId) {
3730
public boolean intercept(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
3831

3932
//这里进行拦截器处理,自行实现
33+
String outTradeNo = payMessage.getOutTradeNo();
34+
// 设置外部单号
35+
// amtApplyService.fillApplyoutId(outTradeNo, (String) payMessage.getPayMessage().get("trade_no"));
36+
37+
38+
//获取账单
39+
// AmtApply amtApply = amtApplyService.getAmtApplyByApplyId(outTradeNo);
40+
// if (null == amtApply){
41+
// Log4jUtil.info("app 阿里pay:" + outTradeNo);
42+
// return false;
43+
// }
44+
//
45+
// 重复回调不进行处理
46+
// if(amtApply.getApplyState().shortValue()== ApplyStateEnum.success.getCode()){
47+
// return false;
48+
// }
49+
//将账单存储至上下文对象中
50+
// context.put("amtApply", amtApply);
51+
52+
4053
return true;
4154
}
4255
}

0 commit comments

Comments
 (0)