From dae8e40dc0c4b4eb0b089569e93b84a03ea5a3e3 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Mon, 26 Sep 2016 18:56:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=B7=B2=E6=9C=89=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjar/weixin/mp/api/WxMpPayService.java | 13 +- .../mp/api/impl/WxMpPayServiceImpl.java | 170 +++++------------- 2 files changed, 51 insertions(+), 132 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java index dae03f43..3a4128fe 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java @@ -30,20 +30,25 @@ public interface WxMpPayService { * @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP * @param ip 发起支付的客户端IP * @param notifyUrl 通知地址 + * @throws WxErrorException * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map) instead */ @Deprecated - WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl); + WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, + String body, String tradeType, String ip, String notifyUrl) + throws WxErrorException; /** * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" * * @param parameters All required/optional parameters for weixin payment + * @throws WxErrorException * @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.unifiedOrder(WxUnifiedOrderRequest) instead */ @Deprecated - WxMpPrepayIdResult getPrepayId(Map parameters); + WxMpPrepayIdResult getPrepayId(Map parameters) + throws WxErrorException; /** * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) @@ -98,9 +103,11 @@ public interface WxMpPayService { /** * 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 + * @throws WxErrorException * */ - WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo); + WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) + throws WxErrorException; /** * 读取支付结果通知 diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java index e3e628c7..b8ec8acb 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java @@ -1,6 +1,5 @@ package me.chanjar.weixin.mp.api.impl; -import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; import java.util.List; @@ -10,16 +9,9 @@ import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.http.Consts; -import org.apache.http.HttpHost; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; import org.joor.Reflect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.slf4j.helpers.MessageFormatter; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -29,9 +21,9 @@ import com.thoughtworks.xstream.annotations.XStreamAlias; import me.chanjar.weixin.common.annotation.Required; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; import me.chanjar.weixin.common.util.xml.XStreamInitializer; import me.chanjar.weixin.mp.api.WxMpPayService; +import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.pay.WxMpPayCallback; import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult; import me.chanjar.weixin.mp.bean.pay.WxMpPayResult; @@ -54,19 +46,17 @@ public class WxMpPayServiceImpl implements WxMpPayService { private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", "notify_url", "trade_type" }; - private HttpHost httpProxy; - private WxMpServiceImpl wxMpService; + private WxMpService wxMpService; - public WxMpPayServiceImpl(WxMpServiceImpl wxMpService) { + public WxMpPayServiceImpl(WxMpService wxMpService) { this.wxMpService = wxMpService; - this.httpProxy = wxMpService.getHttpProxy(); } @Override @Deprecated public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, - String callbackUrl) { + String callbackUrl) throws WxErrorException { Map packageParams = new HashMap<>(); packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); @@ -85,7 +75,8 @@ public class WxMpPayServiceImpl implements WxMpPayService { @Override @Deprecated - public WxMpPrepayIdResult getPrepayId(final Map parameters) { + public WxMpPrepayIdResult getPrepayId(final Map parameters) + throws WxErrorException { final SortedMap packageParams = new TreeMap<>(parameters); packageParams.put("appid", this.wxMpService.getWxMpConfigStorage().getAppId()); @@ -106,29 +97,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { request.append(""); - HttpPost httpPost = new HttpPost( - "https://api.mch.weixin.qq.com/pay/unifiedorder"); - if (this.httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) - .build(); - httpPost.setConfig(config); - } - - StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); - httpPost.setEntity(entity); - try (CloseableHttpResponse response = this.wxMpService.getHttpclient() - .execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE - .handleResponse(response); - XStream xstream = XStreamInitializer.getInstance(); - xstream.alias("xml", WxMpPrepayIdResult.class); - return (WxMpPrepayIdResult) xstream.fromXML(responseContent); - } catch (IOException e) { - throw new RuntimeException("Failed to get prepay id due to IO exception.", - e); - } finally { - httpPost.releaseConnection(); - } + String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; + String responseContent = this.wxMpService.post(url, request.toString()); + XStream xstream = XStreamInitializer.getInstance(); + xstream.alias("xml", WxMpPrepayIdResult.class); + return (WxMpPrepayIdResult) xstream.fromXML(responseContent); } private void checkParameters(Map parameters) { @@ -238,7 +211,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { @Override public WxMpPayResult getJSSDKPayResult(String transactionId, - String outTradeNo) { + String outTradeNo) throws WxErrorException { String nonce_str = System.currentTimeMillis() + ""; SortedMap packageParams = new TreeMap<>(); @@ -267,27 +240,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { } request.append(""); - HttpPost httpPost = new HttpPost( - "https://api.mch.weixin.qq.com/pay/orderquery"); - if (this.httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) - .build(); - httpPost.setConfig(config); - } - - StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); - httpPost.setEntity(entity); - try (CloseableHttpResponse response = this.wxMpService.getHttpclient() - .execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE - .handleResponse(response); - XStream xstream = XStreamInitializer.getInstance(); - xstream.alias("xml", WxMpPayResult.class); - return (WxMpPayResult) xstream.fromXML(responseContent); - } catch (IOException e) { - throw new RuntimeException("Failed to query order due to IO exception.", - e); - } + String url = "https://api.mch.weixin.qq.com/pay/orderquery"; + String responseContent = this.wxMpService.post(url, request.toString()); + XStream xstream = XStreamInitializer.getInstance(); + xstream.alias("xml", WxMpPayResult.class); + return (WxMpPayResult) xstream.fromXML(responseContent); } @Override @@ -325,49 +282,26 @@ public class WxMpPayServiceImpl implements WxMpPayService { } request.append(""); - HttpPost httpPost = new HttpPost( - "https://api.mch.weixin.qq.com/secapi/pay/refund"); - if (this.httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) - .build(); - httpPost.setConfig(config); - } - - StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); - httpPost.setEntity(entity); - try (CloseableHttpResponse response = this.wxMpService.getHttpclient() - .execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE - .handleResponse(response); - XStream xstream = XStreamInitializer.getInstance(); - xstream.processAnnotations(WxMpPayRefundResult.class); - WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream - .fromXML(responseContent); - - if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) - || !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { - WxError error = new WxError(); - error.setErrorCode(-1); - error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() - + ";return_msg:" + wxMpPayRefundResult.getReturnMsg() - + ";result_code:" + wxMpPayRefundResult.getResultCode() - + ";err_code" + wxMpPayRefundResult.getErrCode() + ";err_code_des" - + wxMpPayRefundResult.getErrCodeDes()); - throw new WxErrorException(error); - } + String url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; + String responseContent = this.wxMpService.post(url, request.toString()); + XStream xstream = XStreamInitializer.getInstance(); + xstream.processAnnotations(WxMpPayRefundResult.class); + WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream + .fromXML(responseContent); - return wxMpPayRefundResult; - } catch (IOException e) { - String message = MessageFormatter - .format("Exception happened when sending refund '{}'.", - request.toString()) - .getMessage(); - this.log.error(message, e); - throw new WxErrorException( - WxError.newBuilder().setErrorMsg(message).build()); - } finally { - httpPost.releaseConnection(); + if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) + || !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { + WxError error = new WxError(); + error.setErrorCode(-1); + error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() + + ";return_msg:" + wxMpPayRefundResult.getReturnMsg() + + ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code" + + wxMpPayRefundResult.getErrCode() + ";err_code_des" + + wxMpPayRefundResult.getErrCodeDes()); + throw new WxErrorException(error); } + + return wxMpPayRefundResult; } @Override @@ -400,34 +334,12 @@ public class WxMpPayServiceImpl implements WxMpPayService { request.append(""); - HttpPost httpPost = new HttpPost( - "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); - if (this.httpProxy != null) { - RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) - .build(); - httpPost.setConfig(config); - } + String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; - StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); - httpPost.setEntity(entity); - try (CloseableHttpResponse response = this.wxMpService.getHttpclient() - .execute(httpPost)) { - String responseContent = Utf8ResponseHandler.INSTANCE - .handleResponse(response); - XStream xstream = XStreamInitializer.getInstance(); - xstream.processAnnotations(WxRedpackResult.class); - return (WxRedpackResult) xstream.fromXML(responseContent); - } catch (IOException e) { - String message = MessageFormatter - .format("Exception occured when sending redpack '{}'.", - request.toString()) - .getMessage(); - this.log.error(message, e); - throw new WxErrorException( - WxError.newBuilder().setErrorMsg(message).build()); - } finally { - httpPost.releaseConnection(); - } + String responseContent = this.wxMpService.post(url, request.toString()); + XStream xstream = XStreamInitializer.getInstance(); + xstream.processAnnotations(WxRedpackResult.class); + return (WxRedpackResult) xstream.fromXML(responseContent); } @Override @@ -544,7 +456,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { private void checkParameters(WxUnifiedOrderRequest request) { - List nullFields = com.google.common.collect.Lists.newArrayList(); + List nullFields = Lists.newArrayList(); for (Entry entry : Reflect.on(request).fields() .entrySet()) { Reflect reflect = entry.getValue();