From eb3a94efc86bb466e79fc9c3b180c44ddbba30e2 Mon Sep 17 00:00:00 2001 From: egzosn Date: Tue, 17 Oct 2017 10:47:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/ali/before/api/AliPayService.java | 20 ++++---- .../pay/common/api/PayMessageRouter.java | 34 +++++++++---- .../pay/common/http/ClientHttpRequest.java | 8 ++-- .../java/com/egzosn/pay/common/util/XML.java | 11 +++-- .../wx/youdian/api/WxYouDianPayService.java | 32 ++++++------- .../com/egzosn/pay/wx/api/WxPayService.java | 48 ++++++++++++------- 6 files changed, 89 insertions(+), 64 deletions(-) diff --git a/pay-java-ali/src/main/java/com/egzosn/pay/ali/before/api/AliPayService.java b/pay-java-ali/src/main/java/com/egzosn/pay/ali/before/api/AliPayService.java index 263d014..bc64224 100644 --- a/pay-java-ali/src/main/java/com/egzosn/pay/ali/before/api/AliPayService.java +++ b/pay-java-ali/src/main/java/com/egzosn/pay/ali/before/api/AliPayService.java @@ -33,11 +33,11 @@ import java.util.*; * @see com.egzosn.pay.ali.api.AliPayService */ public class AliPayService extends BasePayService { - protected final Log log = LogFactory.getLog(AliPayService.class); + protected final Log LOG = LogFactory.getLog(AliPayService.class); - private String httpsReqUrl = "https://mapi.alipay.com/gateway.do"; - private String queryReqUrl = "https://openapi.alipay.com/gateway.do"; + private String HTTPS_REQ_URL = "https://mapi.alipay.com/gateway.do"; + private String QUERY_REQ_URL = "https://openapi.alipay.com/gateway.do"; public AliPayService(PayConfigStorage payConfigStorage) { super(payConfigStorage); @@ -49,7 +49,7 @@ public class AliPayService extends BasePayService { public String getHttpsVerifyUrl() { - return httpsReqUrl + "?service=notify_verify"; + return HTTPS_REQ_URL + "?service=notify_verify"; } /** @@ -62,14 +62,14 @@ public class AliPayService extends BasePayService { public boolean verify(Map params) { if (params.get("sign") == null || params.get("notify_id") == null) { - log.debug("支付宝支付异常:params:" + params); + LOG.debug("支付宝支付异常:params:" + params); return false; } try { return signVerify(params, (String) params.get("sign")) && verifySource((String) params.get("notify_id")); } catch (PayErrorException e) { - e.printStackTrace(); + LOG.error(e); } return false; @@ -299,7 +299,7 @@ public class AliPayService extends BasePayService { StringBuffer formHtml = new StringBuffer(); formHtml.append("
rules = new ArrayList(); - + /** + * 支付服务 + */ private final PayService payService; - + /** + * 异步线程处理器 + */ private ExecutorService executorService; - + /** + * 支付异常处理器 + */ private PayErrorExceptionHandler exceptionHandler; + /** + * 根据支付服务创建路由 + * @param payService 支付服务 + */ public PayMessageRouter(PayService payService) { this.payService = payService; this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); @@ -138,7 +152,7 @@ public class PayMessageRouter { } else { res = rule.service(payMessage, payService, exceptionHandler); // 在同步操作结束,session访问结束 - log.debug("End session access: async=false, fromPay=" + payMessage.getFromPay()); + LOG.debug("End session access: async=false, fromPay=" + payMessage.getFromPay()); } } @@ -149,12 +163,12 @@ public class PayMessageRouter { for (Future future : futures) { try { future.get(); - log.debug("End session access: async=true, fromPay=" + payMessage.getFromPay()); + LOG.debug("End session access: async=true, fromPay=" + payMessage.getFromPay()); } catch (InterruptedException e) { - log.error("Error happened when wait task finish", e); + LOG.error("Error happened when wait task finish", e); } catch (ExecutionException e) { - log.error("Error happened when wait task finish", e); + LOG.error("Error happened when wait task finish", e); } } } diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java index a0673b8..774a057 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java @@ -153,11 +153,11 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme entity.writeTo((OutputStream)t); return t; } catch (InstantiationException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("InstantiationException", e.getMessage())); } catch (IllegalAccessException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("IllegalAccessException", e.getMessage())); } - throw new HttpResponseException(statusLine.getStatusCode(), responseType + " 无法进行类型转换"); + } } String charset = "UTF-8"; @@ -174,7 +174,7 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme try { return JSON.parseObject(result, responseType); }catch (JSONException e){ - throw new PayErrorException(new PayException("failure", "类型转化异常,contentType:" + entity.getContentType().getValue(), result)); + throw new PayErrorException(new PayException("failure", String.format("类型转化异常,contentType: %s\n%s", entity.getContentType().getValue(), e.getMessage() ), result)); } } diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java b/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java index c3a565a..56cf186 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/util/XML.java @@ -2,6 +2,8 @@ package com.egzosn.pay.common.util; import com.alibaba.fastjson.JSONObject; +import com.egzosn.pay.common.bean.result.PayException; +import com.egzosn.pay.common.exception.PayErrorException; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; @@ -38,9 +40,9 @@ public class XML { try (InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"))){ return (JSONObject) inputStream2Map(in, null); } catch (IOException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("IOException", e.getMessage())); } - return null; + } @@ -58,9 +60,8 @@ public class XML { try { return (JSONObject)inputStream2Map(in, null); } catch (IOException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("IOException", e.getMessage())); } - return null; } @@ -95,7 +96,7 @@ public class XML { m.put(k, v); } } catch (JDOMException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("JDOMException", e.getMessage())); } return m; } diff --git a/pay-java-wx-youdian/src/main/java/com/egzosn/pay/wx/youdian/api/WxYouDianPayService.java b/pay-java-wx-youdian/src/main/java/com/egzosn/pay/wx/youdian/api/WxYouDianPayService.java index 39155db..ae27f28 100644 --- a/pay-java-wx-youdian/src/main/java/com/egzosn/pay/wx/youdian/api/WxYouDianPayService.java +++ b/pay-java-wx-youdian/src/main/java/com/egzosn/pay/wx/youdian/api/WxYouDianPayService.java @@ -32,15 +32,15 @@ import java.util.concurrent.locks.Lock; * date 2017/01/12 22:58 */ public class WxYouDianPayService extends BasePayService { - protected final Log log = LogFactory.getLog(WxYouDianPayService.class); + protected static final Log LOG = LogFactory.getLog(WxYouDianPayService.class); //登录获取授权码 - public final static String loginUrl = "http://life.51youdian.com/Api/CheckoutCounter/login"; + public final static String LOGIN_URL = "http://life.51youdian.com/Api/CheckoutCounter/login"; //刷新授权码 - public final static String resetLoginUrl = "http://life.51youdian.com/Api/CheckoutCounter/resetLogin"; + public final static String RESET_LOGIN_URL = "http://life.51youdian.com/Api/CheckoutCounter/resetLogin"; //查看付款订单状态 - public final static String unifiedorderStatusUrl = "http://life.51youdian.com/Api/CheckoutCounter/unifiedorderStatus"; + public final static String UNIFIEDORDER_STATUS_URL = "http://life.51youdian.com/Api/CheckoutCounter/unifiedorderStatus"; //预下单链接 - public final static String unifiedOrderUrl = "http://life.51youdian.com/Api/CheckoutCounter/unifiedorder"; + public final static String UNIFIED_ORDER_URL = "http://life.51youdian.com/Api/CheckoutCounter/unifiedorder"; /** @@ -51,9 +51,8 @@ public class WxYouDianPayService extends BasePayService { try { return getAccessToken(false); } catch (PayErrorException e) { - e.printStackTrace(); + throw e; } - return null; } /** @@ -80,7 +79,7 @@ public class WxYouDianPayService extends BasePayService { StringBuilder param = new StringBuilder().append("access_token=").append(payConfigStorage.getAccessToken()); String sign = createSign(param.toString() + apbNonce, payConfigStorage.getInputCharset()); param.append("&apb_nonce=").append(apbNonce).append("&sign=").append(sign); - JSONObject json = execute(resetLoginUrl + "?" + param.toString(), MethodType.GET, null ); + JSONObject json = execute(RESET_LOGIN_URL + "?" + param.toString(), MethodType.GET, null ); int errorcode = json.getIntValue("errorcode"); if (0 == errorcode){ payConfigStorage.updateAccessToken(payConfigStorage.getAccessToken(), 7200); @@ -112,7 +111,7 @@ public class WxYouDianPayService extends BasePayService { String sign = createSign(SignUtils.parameterText(data, "") + apbNonce, payConfigStorage.getInputCharset()); String queryParam = SignUtils.parameterText(data) + "&apb_nonce=" + apbNonce + "&sign=" + sign; - JSONObject json = execute(loginUrl + "?" + queryParam, MethodType.GET, null); + JSONObject json = execute(LOGIN_URL + "?" + queryParam, MethodType.GET, null); payConfigStorage.updateAccessToken(json.getString("access_token"), json.getLongValue("viptime")); return json; } @@ -124,7 +123,7 @@ public class WxYouDianPayService extends BasePayService { * @return 请求地址 */ public String getHttpsVerifyUrl() { - return unifiedorderStatusUrl; + return UNIFIEDORDER_STATUS_URL; } /** * 回调校验 @@ -135,15 +134,15 @@ public class WxYouDianPayService extends BasePayService { @Override public boolean verify(Map params) { if (!"SUCCESS".equals(params.get("return_code"))){ - log.debug(String.format("友店微信支付异常:return_code=%s,参数集=%s", params.get("return_code"), params)); + LOG.debug(String.format("友店微信支付异常:return_code=%s,参数集=%s", params.get("return_code"), params)); return false; } - if(params.get("sign") == null) {log.debug("友店微信支付异常:签名为空!out_trade_no=" + params.get("out_trade_no"));} + if(params.get("sign") == null) {LOG.debug("友店微信支付异常:签名为空!out_trade_no=" + params.get("out_trade_no"));} try { return signVerify(params, (String) params.get("sign")) && verifySource((String)params.get("out_trade_no")); } catch (PayErrorException e) { - e.printStackTrace(); + LOG.error(e.getMessage()); } return false; } @@ -217,7 +216,7 @@ public class WxYouDianPayService extends BasePayService { if ("401".equals(error.getErrorCode()) || "500".equals(error.getErrorCode())) { try { int sleepMillis = retrySleepMillis * (1 << retryTimes); - log.debug(String.format("友店微信系统繁忙,(%s)ms 后重试(第%s次)", sleepMillis, retryTimes + 1)); + LOG.debug(String.format("友店微信系统繁忙,(%s)ms 后重试(第%s次)", sleepMillis, retryTimes + 1)); Thread.sleep(sleepMillis); } catch (InterruptedException e1) { throw new PayErrorException(new YdPayError(-1, "友店支付服务端重试失败", e1.getMessage())); @@ -255,14 +254,13 @@ public class WxYouDianPayService extends BasePayService { data.put("PayMoney", data.remove("paymoney")); String params = SignUtils.parameterText(data) + "&apb_nonce=" + apbNonce + "&sign=" + sign; try { - JSONObject json = execute(unifiedOrderUrl+ "?" + params, MethodType.GET, null); + JSONObject json = execute(UNIFIED_ORDER_URL+ "?" + params, MethodType.GET, null); //友店比较特殊,需要在下完预订单后,自己存储 order_sn 对应 微信官方文档 out_trade_no order.setOutTradeNo(json.getString("order_sn")); return json; } catch (PayErrorException e) { - e.printStackTrace(); + throw e; } - return null; } diff --git a/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java b/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java index 58a6c0e..853d4d1 100644 --- a/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java +++ b/pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java @@ -10,6 +10,7 @@ import com.egzosn.pay.common.exception.PayErrorException; import com.egzosn.pay.common.http.HttpConfigStorage; import com.egzosn.pay.common.util.MatrixToImageWriter; import com.egzosn.pay.common.util.sign.SignUtils; +import com.egzosn.pay.common.util.str.StringUtils; import com.egzosn.pay.wx.bean.WxPayError; import com.egzosn.pay.wx.bean.WxTransactionType; import com.egzosn.pay.common.util.XML; @@ -32,21 +33,29 @@ import java.util.*; * */ public class WxPayService extends BasePayService { - protected final Log log = LogFactory.getLog(WxPayService.class); + protected final Log LOG = LogFactory.getLog(WxPayService.class); - - - public final static String uri = "https://api.mch.weixin.qq.com/"; + /** + * 微信请求地址 + */ + public final static String URI = "https://api.mch.weixin.qq.com/"; // public final static String unifiedOrderUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // public final static String orderqueryUrl = "https://api.mch.weixin.qq.com/pay/orderquery"; - + /** + * 创建支付服务 + * @param payConfigStorage 微信对应的支付配置 + */ public WxPayService(PayConfigStorage payConfigStorage) { super(payConfigStorage); } - + /** + * 创建支付服务 + * @param payConfigStorage 微信对应的支付配置 + * @param configStorage 微信对应的网络配置,包含代理配置、ssl证书配置 + */ public WxPayService(PayConfigStorage payConfigStorage, HttpConfigStorage configStorage) { super(payConfigStorage, configStorage); } @@ -58,7 +67,7 @@ public class WxPayService extends BasePayService { */ private String getUrl(TransactionType transactionType){ - return uri + transactionType.getMethod(); + return URI + transactionType.getMethod(); } /** @@ -70,19 +79,19 @@ public class WxPayService extends BasePayService { @Override public boolean verify(Map params) { if (!"SUCCESS".equals(params.get("return_code"))){ - log.debug(String.format("微信支付异常:return_code=%s,参数集=%s", params.get("return_code"), params)); + LOG.debug(String.format("微信支付异常:return_code=%s,参数集=%s", params.get("return_code"), params)); return false; } if(null == params.get("sign")) { - log.debug("微信支付异常:签名为空!out_trade_no=" + params.get("out_trade_no")); + LOG.debug("微信支付异常:签名为空!out_trade_no=" + params.get("out_trade_no")); return false; } try { return signVerify(params, (String) params.get("sign")) && verifySource((String)params.get("out_trade_no")); } catch (PayErrorException e) { - e.printStackTrace(); + LOG.error(e); } return false; } @@ -183,7 +192,7 @@ public class WxPayService extends BasePayService { parameters.put("sign", sign); String requestXML = XML.getMap2Xml(parameters); - log.debug("requestXML:" + requestXML); + LOG.debug("requestXML:" + requestXML); //调起支付的参数列表 JSONObject result = requestTemplate.postForObject(getUrl(order.getTransactionType()), requestXML, JSONObject.class); @@ -268,9 +277,9 @@ public class WxPayService extends BasePayService { try { return XML.inputStream2Map(is, map); } catch (IOException e) { - e.printStackTrace(); + throw new PayErrorException(new PayException("IOException", e.getMessage())); } - return map; + } /** @@ -544,7 +553,7 @@ public class WxPayService extends BasePayService { * @return 返回支付方对应接口的结果 */ @Override - public T secondaryInterface(Object transactionIdOrBillDate, String outTradeNoBillType, TransactionType transactionType, Callback callback) { + public T secondaryInterface(Object transactionIdOrBillDate, String outTradeNoBillType, TransactionType transactionType, Callback callback) { if (transactionType == WxTransactionType.REFUND){ throw new PayErrorException(new PayException("failure", "通用接口不支持:" + transactionType)); @@ -558,13 +567,13 @@ public class WxPayService extends BasePayService { throw new PayErrorException(new PayException("failure", "非法类型异常:" + transactionIdOrBillDate.getClass())); } - if (!(transactionIdOrBillDate instanceof String)){ + if (!(null == transactionIdOrBillDate || transactionIdOrBillDate instanceof String)){ throw new PayErrorException(new PayException("failure", "非法类型异常:" + transactionIdOrBillDate.getClass())); } //获取公共参数 - Map parameters = getPublicParameters(); - if (null == transactionIdOrBillDate || "".equals(transactionIdOrBillDate)){ + Map parameters = new HashMap<>();//getPublicParameters(); + if (StringUtils.isEmpty((String)transactionIdOrBillDate)){ parameters.put("out_trade_no", outTradeNoBillType); }else { parameters.put("transaction_id", transactionIdOrBillDate); @@ -574,6 +583,9 @@ public class WxPayService extends BasePayService { return callback.perform(requestTemplate.postForObject(getUrl(transactionType), XML.getMap2Xml(parameters) , JSONObject.class)); } - + public static void main(String[] args) { + WxPayService service = new WxPayService(null); + service.secondaryInterface(null, "aaa", WxTransactionType.MWEB, null); + } }