Skip to content

Commit

Permalink
针对拦截器进行优化,简化开发者判断处理
Browse files Browse the repository at this point in the history
  • Loading branch information
egzosn@gmail.com authored and egzosn@gmail.com committed Jun 9, 2017
1 parent 7cfeaa0 commit ea93be6
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.egzosn.pay.common.api.BasePayService;
import com.egzosn.pay.common.api.Callback;
import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.bean.*;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.http.HttpConfigStorage;
Expand All @@ -36,11 +33,11 @@ public class AliPayService extends BasePayService {
protected final Log log = LogFactory.getLog(AliPayService.class);

//正式测试环境
private String httpsReqUrl = "https://openapi.alipay.com/gateway.do";
private final static String httpsReqUrl = "https://openapi.alipay.com/gateway.do";
//沙箱测试环境账号
private String devReqUrl = "https://openapi.alipaydev.com/gateway.do";
private final static String devReqUrl = "https://openapi.alipaydev.com/gateway.do";
//兼容上一版本即时收款
private String httpsReqUrlBefore = "https://mapi.alipay.com/gateway.do";
private final static String httpsReqUrlBefore = "https://mapi.alipay.com/gateway.do";


/**
Expand Down Expand Up @@ -297,6 +294,17 @@ public PayOutMessage getPayOutMessage(String code, String message) {
return PayOutMessage.TEXT().content(code.toLowerCase()).build();
}

/**
* 获取成功输出消息,用户返回给支付端
* 主要用于拦截器中返回
* @param payMessage 支付回调消息
* @return 返回输出消息
*/
@Override
public PayOutMessage successPayOutMessage(PayMessage payMessage) {
return PayOutMessage.TEXT().content("success").build();
}

/**
*
* @param orderInfo 发起支付的订单信息
Expand All @@ -305,7 +313,6 @@ public PayOutMessage getPayOutMessage(String code, String message) {
*/
@Override
public String buildRequest(Map<String, Object> orderInfo, MethodType method) {

StringBuffer formHtml = new StringBuffer();
formHtml.append("<form id=\"_alipaysubmit_\" name=\"alipaysubmit\" action=\"");
if (null == orderInfo.get("method")) {
Expand All @@ -332,11 +339,6 @@ public String buildRequest(Map<String, Object> orderInfo, MethodType method) {

formHtml.append("<input type=\"hidden\" name=\"biz_content\" value=\"" + biz_content.replace("\"", "&quot;") + "\"/>");
}



//submit按钮控件请不要含有name属性
// formHtml.append("<input type=\"submit\" value=\"\" style=\"display:none;\">");
formHtml.append("</form>");
formHtml.append("<script>document.forms['_alipaysubmit_'].submit();</script>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean verify(Map<String, Object> params) {
*/
@Override
public boolean verifySource(String id) {
return "true".equals(requestTemplate.getForObject( getHttpsVerifyUrl() + "partner=" + payConfigStorage.getPid() + "&notify_id=" + id, String.class));
return "true".equals(requestTemplate.getForObject( getHttpsVerifyUrl() + "&partner=" + payConfigStorage.getPid() + "&notify_id=" + id, String.class));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ protected PayOutMessage service(PayMessage payMessage,
// 如果拦截器不通过
for (PayMessageInterceptor interceptor : this.interceptors) {
if (!interceptor.intercept(payMessage, context, payService)) {
return null;
//这里直接返回成功,解决外层判断问题
return payService.successPayOutMessage(payMessage);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.egzosn.pay.common.api;

import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.bean.*;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.http.HttpRequestTemplate;
Expand Down Expand Up @@ -126,6 +123,14 @@ public interface PayService {
*/
PayOutMessage getPayOutMessage(String code, String message);

/**
* 获取成功输出消息,用户返回给支付端
* 主要用于拦截器中返回
* @param payMessage 支付回调消息
* @return 返回输出消息
*/
PayOutMessage successPayOutMessage(PayMessage payMessage);

/**
* 获取输出消息,用户返回给支付端, 针对于web端
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class PayOrder {
private String deviceInfo;
//付款条码串 与设备号类似???
private String authCode;
//
private String openid;
//交易类型
private TransactionType transactionType;
//支付币种
Expand Down Expand Up @@ -145,8 +147,11 @@ public PayOrder(String subject, String body, BigDecimal price, String outTradeNo
this.transactionType = transactionType;
}

public String getOpenid() {
return openid;
}




public void setOpenid(String openid) {
this.openid = openid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ public String toPay(Integer payId, String transactionType, String bankType, BigD
}


/**
* 公众号支付
*
*
* @param payId 账户id
* @param openid openid
* @return 跳到支付页面
*/
@RequestMapping(value = "jsapi" )
public Map toPay(Integer payId, String openid, BigDecimal price) {
//获取对应的支付账户操作工具(可根据账户id)
PayResponse payResponse = service.getPayResponse(payId);

PayOrder order = new PayOrder("订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""), PayType.valueOf(payResponse.getStorage().getPayType()).getTransactionType("JSAPI"));
order.setOpenid(openid);

Map orderInfo = payResponse.getService().orderInfo(order);
orderInfo.put("code", 0);

return orderInfo;
}


/**
* 刷卡付,pos主动扫码付款(条码付)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ApyAccountRepository {
// TODO 2017/2/9 16:20 author: egan sign_type只有单一key时public_key与private_key相等,比如sign_type=MD5的情况
apyAccount1.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB");
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");
apyAccount1.setNotifyUrl("http://pay.egan.in/payBack2.json");
apyAccount1.setNotifyUrl("http://pay.egan.in/payBack1.json");
// 无需同步回调可不填
// apyAccount1.setReturnUrl("");
apyAccount1.setInputCharset("UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MsgType;
import com.egzosn.pay.demo.service.interceptor.YoudianPayMessageInterceptor;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import javax.annotation.Resource;
Expand Down Expand Up @@ -85,8 +86,7 @@ private void buildRouter(Integer payId) {
.async(false)
.msgType(MsgType.text.name()) //消息类型
.payType(PayType.aliPay.name()) //支付账户事件类型
// .transactionType(AliTransactionType.UNAWARE.name())//交易类型,有关回调的可在这处理
.interceptor(new AliPayMessageInterceptor(payId)) //拦截器
.interceptor(new AliPayMessageInterceptor()) //拦截器
.handler(autowire(new AliPayMessageHandler(payId))) //处理器
.end()
.rule()
Expand All @@ -99,6 +99,7 @@ private void buildRouter(Integer payId) {
.async(false)
.msgType(MsgType.json.name())
.payType(PayType.youdianPay.name())
.interceptor(new YoudianPayMessageInterceptor()) //拦截器
.handler(autowire(new YouDianPayMessageHandler(payId)))
.end()
.rule()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;

import java.math.BigDecimal;
import java.util.Map;

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

@Override
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {

Map<String, Object> message = payMessage.getPayMessage();
//交易状态
String trade_status = (String) payMessage.getPayMessage().get("trade_status");

if ("TRADE_SUCCESS".equals(trade_status)){
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
//注意:
//付款完成后,支付宝系统发送该交易状态通知

} else if("TRADE_FINISHED".equals(trade_status)) {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
} else if ("WAIT_BUYER_PAY".equals(trade_status) || "TRADE_CLOSED".equals(trade_status)) {

}

return payService.getPayOutMessage("success", "成功");
String trade_status = (String) message.get("trade_status");

//上下文对象中获取账单
// AmtApply amtApply = (AmtApply)context.get("amtApply");
//日志存储
// amtPaylogService.createAmtPaylogByCallBack(amtApply, message.toString());
//交易完成
if ("TRADE_SUCCESS".equals(trade_status) || "TRADE_FINISHED".equals(trade_status)) {

BigDecimal payAmount = new BigDecimal((String) message.get("total_fee"));

return payService.getPayOutMessage("success", "成功");

}/* else if ("WAIT_BUYER_PAY".equals(trade_status) || "TRADE_CLOSED".equals(trade_status)) {
}*/
return payService.getPayOutMessage("fail", "失败");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context,
if ("0000".equals(payMessage.getPayMessage().get("order_pay_code"))){
/////这里进行成功的处理

return PayOutMessage.JSON().content("order_pay_error","成功").build();
return PayOutMessage.JSON().content("success","成功").build();
}

return PayOutMessage.JSON().content("order_pay_error",payMessage.getPayMessage().get("order_pay_error")).build();
return PayOutMessage.JSON().content("fail", "失败").build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.egzosn.pay.demo.service.handler;

import com.alibaba.fastjson.JSON;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayMessage;
import com.egzosn.pay.common.bean.PayOutMessage;
Expand All @@ -23,12 +24,17 @@ public YouDianPayMessageHandler(Integer payId) {
@Override
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
//交易状态
if ("0000".equals(payMessage.getPayMessage().get("order_pay_code"))){
/////这里进行成功的处理
Map<String, Object> message = payMessage.getPayMessage();
//上下文对象中获取账单
// AmtApply amtApply = (AmtApply)context.get("amtApply");
//日志存储
// amtPaylogService.createAmtPaylogByCallBack(amtApply, message.toString());

if ("SUCCESS".equals(message.get("return_code"))){
/////这里进行成功的处理,因没有返回金额

return PayOutMessage.JSON().content("order_pay_error","成功").build();
}

return PayOutMessage.JSON().content("order_pay_error",payMessage.getPayMessage().get("order_pay_error")).build();
return PayOutMessage.TEXT().content(JSON.toJSONString(message)).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
*/
public class AliPayMessageInterceptor implements PayMessageInterceptor {

//支付账户id
private Integer payId;
public AliPayMessageInterceptor(Integer payId) {

this.payId = payId;
}

/**
* 拦截支付消息
*
Expand All @@ -37,6 +30,26 @@ public AliPayMessageInterceptor(Integer payId) {
public boolean intercept(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {

//这里进行拦截器处理,自行实现
String outTradeNo = payMessage.getOutTradeNo();
// 设置外部单号
// amtApplyService.fillApplyoutId(outTradeNo, (String) payMessage.getPayMessage().get("trade_no"));


//获取账单
// AmtApply amtApply = amtApplyService.getAmtApplyByApplyId(outTradeNo);
// if (null == amtApply){
// Log4jUtil.info("app 阿里pay:" + outTradeNo);
// return false;
// }
//
// 重复回调不进行处理
// if(amtApply.getApplyState().shortValue()== ApplyStateEnum.success.getCode()){
// return false;
// }
//将账单存储至上下文对象中
// context.put("amtApply", amtApply);


return true;
}
}
Loading

0 comments on commit ea93be6

Please sign in to comment.