引入WxPayException,替代原有的异常处理类,并做相应的优化

This commit is contained in:
Binary Wang
2017-06-06 18:57:52 +08:00
parent ea13197844
commit 92c8a86199
9 changed files with 228 additions and 69 deletions
@@ -3,7 +3,7 @@ package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.config.WxPayConfig;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.github.binarywang.wxpay.exception.WxPayException;
import java.io.File;
import java.util.Map;
@@ -33,7 +33,7 @@ public interface WxPayService {
* @param transactionId 微信订单号
* @param outTradeNo 商户系统内部的订单号,当没提供transactionId时需要传这个。
*/
WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException;
WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxPayException;
/**
* <pre>
@@ -49,7 +49,7 @@ public interface WxPayService {
*
* @param outTradeNo 商户系统内部的订单号
*/
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorException;
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;
/**
* 统一下单(详见http://com.github.binarywang.wechat.pay.bean.pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
@@ -58,7 +58,7 @@ public interface WxPayService {
*
* @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
*/
WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxErrorException;
WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
/**
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数
@@ -66,7 +66,7 @@ public interface WxPayService {
*
* @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
*/
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException;
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException;
/**
* 获取配置
@@ -88,7 +88,7 @@ public interface WxPayService {
* @param request 请求对象
* @return 退款操作结果
*/
WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException;
WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException;
/**
* <pre>
@@ -108,13 +108,13 @@ public interface WxPayService {
* @return 退款信息
*/
WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, String outRefundNo, String refundId)
throws WxErrorException;
throws WxPayException;
/**
* 读取支付结果通知
* 详见http://com.github.binarywang.wechat.pay.bean.pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
*/
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxErrorException;
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
/**
* 发送微信红包给个人用户
@@ -128,7 +128,7 @@ public interface WxPayService {
*
* @param request 请求对象
*/
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxErrorException;
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxPayException;
/**
* <pre>
@@ -141,7 +141,7 @@ public interface WxPayService {
*
* @param mchBillNo 商户发放红包的商户订单号,比如10000098201411111234567890
*/
WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxErrorException;
WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException;
/**
* <pre>
@@ -155,7 +155,7 @@ public interface WxPayService {
*
* @param request 请求对象
*/
WxEntPayResult entPay(WxEntPayRequest request) throws WxErrorException;
WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException;
/**
* <pre>
@@ -167,7 +167,7 @@ public interface WxPayService {
*
* @param partnerTradeNo 商户订单号
*/
WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxErrorException;
WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException;
/**
* <pre>
@@ -225,7 +225,7 @@ public interface WxPayService {
* 是否需要证书:不需要
* </pre>
*/
void report(WxPayReportRequest request) throws WxErrorException;
void report(WxPayReportRequest request) throws WxPayException;
/**
* <pre>
@@ -246,7 +246,7 @@ public interface WxPayService {
* @param deviceInfo 设备号 device_info 非必传参数,终端设备号
* @return 保存到本地的临时文件
*/
WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxErrorException;
WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxPayException;
/**
* <pre>
@@ -260,7 +260,7 @@ public interface WxPayService {
* 是否需要证书:不需要。
* </pre>
*/
WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxErrorException;
WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayException;
/**
* <pre>
@@ -276,7 +276,7 @@ public interface WxPayService {
* 是否需要证书:请求需要双向证书。
* </pre>
*/
WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) throws WxErrorException;
WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) throws WxPayException;
/**
* <pre>
@@ -291,7 +291,7 @@ public interface WxPayService {
*
* @param request 请求对象
*/
String shorturl(WxPayShorturlRequest request) throws WxErrorException;
String shorturl(WxPayShorturlRequest request) throws WxPayException;
/**
* <pre>
@@ -301,7 +301,7 @@ public interface WxPayService {
* @param longUrl 需要被压缩的网址
* @see WxPayService#shorturl(WxPayShorturlRequest)
*/
String shorturl(String longUrl) throws WxErrorException;
String shorturl(String longUrl) throws WxPayException;
/**
* <pre>
@@ -316,7 +316,7 @@ public interface WxPayService {
* @param request 请求对象
* @return openid
*/
String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException;
String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayException;
/**
* <pre>
@@ -327,5 +327,5 @@ public interface WxPayService {
* @return openid
* @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest)
*/
String authcode2Openid(String authCode) throws WxErrorException;
String authcode2Openid(String authCode) throws WxPayException;
}
@@ -4,14 +4,13 @@ import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.SignUtils;
import com.google.common.collect.Maps;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.net.SSLSocketHttpConnectionProvider;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@@ -55,7 +54,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException {
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
@@ -67,7 +66,7 @@ public class WxPayServiceImpl implements WxPayService {
@Override
public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, String outRefundNo, String refundId)
throws WxErrorException {
throws WxPayException {
WxPayRefundQueryRequest request = new WxPayRefundQueryRequest();
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
request.setTransactionId(StringUtils.trimToNull(transactionId));
@@ -85,24 +84,24 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxErrorException {
public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException {
try {
log.debug("微信支付回调参数详细:{}", xmlData);
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
log.debug("微信支付回调结果对象:{}", result);
result.checkResult(this);
return result;
} catch (WxErrorException e) {
} catch (WxPayException e) {
log.error(e.getMessage(), e);
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new WxErrorException(WxError.newBuilder().setErrorMsg("发生异常" + e.getMessage()).build());
throw new WxPayException("发生异常," + e.getMessage());
}
}
@Override
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxErrorException {
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack";
@@ -119,7 +118,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxErrorException {
public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException {
WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest();
request.setMchBillNo(mchBillNo);
request.setBillType("MCHT");
@@ -133,7 +132,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException {
public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxPayException {
WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
request.setTransactionId(StringUtils.trimToNull(transactionId));
@@ -142,7 +141,7 @@ public class WxPayServiceImpl implements WxPayService {
String url = this.getPayBaseUrl() + "/pay/orderquery";
String responseContent = this.post(url, request.toXML());
if (StringUtils.isBlank(responseContent)) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无响应结果").build());
throw new WxPayException("无响应结果");
}
WxPayOrderQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderQueryResult.class);
@@ -152,7 +151,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorException {
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException {
if (StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("out_trade_no不能为空");
}
@@ -170,7 +169,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxErrorException {
public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
@@ -181,7 +180,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException {
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException {
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
String prepayId = unifiedOrderResult.getPrepayId();
if (StringUtils.isBlank(prepayId)) {
@@ -225,7 +224,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxEntPayResult entPay(WxEntPayRequest request) throws WxErrorException {
public WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
@@ -236,7 +235,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxErrorException {
public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException {
WxEntPayQueryRequest request = new WxEntPayQueryRequest();
request.setPartnerTradeNo(partnerTradeNo);
request.checkAndSign(this.getConfig());
@@ -291,7 +290,7 @@ public class WxPayServiceImpl implements WxPayService {
return QrcodeUtils.createQrcode(content, sideLength, logoFile);
}
public void report(WxPayReportRequest request) throws WxErrorException {
public void report(WxPayReportRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/payitil/report";
@@ -301,7 +300,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxErrorException {
public WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxPayException {
WxPayDownloadBillRequest request = new WxPayDownloadBillRequest();
request.setBillType(billType);
request.setBillDate(billDate);
@@ -394,7 +393,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxErrorException {
public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/pay/micropay";
@@ -405,7 +404,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) throws WxErrorException {
public WxPayOrderReverseResult reverseOrder(WxPayOrderReverseRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/secapi/pay/reverse";
@@ -416,7 +415,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public String shorturl(WxPayShorturlRequest request) throws WxErrorException {
public String shorturl(WxPayShorturlRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/tools/shorturl";
@@ -427,12 +426,12 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public String shorturl(String longUrl) throws WxErrorException {
public String shorturl(String longUrl) throws WxPayException {
return this.shorturl(new WxPayShorturlRequest(longUrl));
}
@Override
public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxErrorException {
public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayException {
request.checkAndSign(this.getConfig());
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
@@ -443,7 +442,7 @@ public class WxPayServiceImpl implements WxPayService {
}
@Override
public String authcode2Openid(String authCode) throws WxErrorException {
public String authcode2Openid(String authCode) throws WxPayException {
return this.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode));
}
@@ -472,7 +471,7 @@ public class WxPayServiceImpl implements WxPayService {
/**
* ecoolper(20170418),修改为jodd-http方式
*/
private String postWithKey(String url, String requestStr) throws WxErrorException {
private String postWithKey(String url, String requestStr) throws WxPayException {
try {
SSLContext sslContext = this.getConfig().getSslContext();
if (null == sslContext) {
@@ -487,7 +486,7 @@ public class WxPayServiceImpl implements WxPayService {
return result;
} catch (Exception e) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
throw new WxPayException(e.getMessage());
}
}