mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	重构已有支付代码
This commit is contained in:
		| @ -30,20 +30,25 @@ public interface WxMpPayService { | |||||||
|    * @param tradeType  交易类型 JSAPI,NATIVE,APP,WAP |    * @param tradeType  交易类型 JSAPI,NATIVE,APP,WAP | ||||||
|    * @param ip         发起支付的客户端IP |    * @param ip         发起支付的客户端IP | ||||||
|    * @param notifyUrl  通知地址 |    * @param notifyUrl  通知地址 | ||||||
|  |    * @throws WxErrorException  | ||||||
|    * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead |    * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead | ||||||
|    */ |    */ | ||||||
|   @Deprecated |   @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) |    * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | ||||||
|    * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" |    * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | ||||||
|    * |    * | ||||||
|    * @param parameters All required/optional parameters for weixin payment |    * @param parameters All required/optional parameters for weixin payment | ||||||
|  |    * @throws WxErrorException  | ||||||
|    * @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.unifiedOrder(WxUnifiedOrderRequest) instead  |    * @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.unifiedOrder(WxUnifiedOrderRequest) instead  | ||||||
|    */ |    */ | ||||||
|   @Deprecated |   @Deprecated | ||||||
|   WxMpPrepayIdResult getPrepayId(Map<String, String> parameters); |   WxMpPrepayIdResult getPrepayId(Map<String, String> parameters) | ||||||
|  |       throws WxErrorException; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) |    * 统一下单(详见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 |    * 详见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; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 读取支付结果通知 |    * 读取支付结果通知 | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package me.chanjar.weixin.mp.api.impl; | package me.chanjar.weixin.mp.api.impl; | ||||||
|  |  | ||||||
| import java.io.IOException; |  | ||||||
| import java.lang.reflect.Field; | import java.lang.reflect.Field; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @ -10,16 +9,9 @@ import java.util.SortedMap; | |||||||
| import java.util.TreeMap; | import java.util.TreeMap; | ||||||
|  |  | ||||||
| import org.apache.commons.codec.digest.DigestUtils; | 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.joor.Reflect; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| import org.slf4j.helpers.MessageFormatter; |  | ||||||
|  |  | ||||||
| import com.google.common.collect.Lists; | import com.google.common.collect.Lists; | ||||||
| import com.google.common.collect.Maps; | 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.annotation.Required; | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; | 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.common.util.xml.XStreamInitializer; | ||||||
| import me.chanjar.weixin.mp.api.WxMpPayService; | 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.WxMpPayCallback; | ||||||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult; | import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult; | ||||||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayResult; | 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", |   private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", | ||||||
|       "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", |       "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", | ||||||
|       "notify_url", "trade_type" }; |       "notify_url", "trade_type" }; | ||||||
|   private HttpHost httpProxy; |   private WxMpService wxMpService; | ||||||
|   private WxMpServiceImpl wxMpService; |  | ||||||
|  |  | ||||||
|   public WxMpPayServiceImpl(WxMpServiceImpl wxMpService) { |   public WxMpPayServiceImpl(WxMpService wxMpService) { | ||||||
|     this.wxMpService = wxMpService; |     this.wxMpService = wxMpService; | ||||||
|     this.httpProxy = wxMpService.getHttpProxy(); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   @Deprecated |   @Deprecated | ||||||
|   public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, |   public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, | ||||||
|       double amt, String body, String tradeType, String ip, |       double amt, String body, String tradeType, String ip, | ||||||
|       String callbackUrl) { |       String callbackUrl) throws WxErrorException { | ||||||
|     Map<String, String> packageParams = new HashMap<>(); |     Map<String, String> packageParams = new HashMap<>(); | ||||||
|     packageParams.put("appid", |     packageParams.put("appid", | ||||||
|         this.wxMpService.getWxMpConfigStorage().getAppId()); |         this.wxMpService.getWxMpConfigStorage().getAppId()); | ||||||
| @ -85,7 +75,8 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   @Deprecated |   @Deprecated | ||||||
|   public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) { |   public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) | ||||||
|  |       throws WxErrorException { | ||||||
|     final SortedMap<String, String> packageParams = new TreeMap<>(parameters); |     final SortedMap<String, String> packageParams = new TreeMap<>(parameters); | ||||||
|     packageParams.put("appid", |     packageParams.put("appid", | ||||||
|         this.wxMpService.getWxMpConfigStorage().getAppId()); |         this.wxMpService.getWxMpConfigStorage().getAppId()); | ||||||
| @ -106,29 +97,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|  |  | ||||||
|     request.append("</xml>"); |     request.append("</xml>"); | ||||||
|  |  | ||||||
|     HttpPost httpPost = new HttpPost( |     String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; | ||||||
|         "https://api.mch.weixin.qq.com/pay/unifiedorder"); |     String responseContent = this.wxMpService.post(url, request.toString()); | ||||||
|     if (this.httpProxy != null) { |     XStream xstream = XStreamInitializer.getInstance(); | ||||||
|       RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) |     xstream.alias("xml", WxMpPrepayIdResult.class); | ||||||
|           .build(); |     return (WxMpPrepayIdResult) xstream.fromXML(responseContent); | ||||||
|       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(); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private void checkParameters(Map<String, String> parameters) { |   private void checkParameters(Map<String, String> parameters) { | ||||||
| @ -238,7 +211,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpPayResult getJSSDKPayResult(String transactionId, |   public WxMpPayResult getJSSDKPayResult(String transactionId, | ||||||
|       String outTradeNo) { |       String outTradeNo) throws WxErrorException { | ||||||
|     String nonce_str = System.currentTimeMillis() + ""; |     String nonce_str = System.currentTimeMillis() + ""; | ||||||
|  |  | ||||||
|     SortedMap<String, String> packageParams = new TreeMap<>(); |     SortedMap<String, String> packageParams = new TreeMap<>(); | ||||||
| @ -267,27 +240,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|     } |     } | ||||||
|     request.append("</xml>"); |     request.append("</xml>"); | ||||||
|  |  | ||||||
|     HttpPost httpPost = new HttpPost( |     String url = "https://api.mch.weixin.qq.com/pay/orderquery"; | ||||||
|         "https://api.mch.weixin.qq.com/pay/orderquery"); |     String responseContent = this.wxMpService.post(url, request.toString()); | ||||||
|     if (this.httpProxy != null) { |     XStream xstream = XStreamInitializer.getInstance(); | ||||||
|       RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) |     xstream.alias("xml", WxMpPayResult.class); | ||||||
|           .build(); |     return (WxMpPayResult) xstream.fromXML(responseContent); | ||||||
|       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); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -325,49 +282,26 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|     } |     } | ||||||
|     request.append("</xml>"); |     request.append("</xml>"); | ||||||
|  |  | ||||||
|     HttpPost httpPost = new HttpPost( |     String url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; | ||||||
|         "https://api.mch.weixin.qq.com/secapi/pay/refund"); |     String responseContent = this.wxMpService.post(url, request.toString()); | ||||||
|     if (this.httpProxy != null) { |     XStream xstream = XStreamInitializer.getInstance(); | ||||||
|       RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) |     xstream.processAnnotations(WxMpPayRefundResult.class); | ||||||
|           .build(); |     WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream | ||||||
|       httpPost.setConfig(config); |         .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); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); |     return wxMpPayRefundResult; | ||||||
|     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); |  | ||||||
|       } |  | ||||||
|  |  | ||||||
|       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(); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -400,34 +334,12 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|  |  | ||||||
|     request.append("</xml>"); |     request.append("</xml>"); | ||||||
|  |  | ||||||
|     HttpPost httpPost = new HttpPost( |     String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; | ||||||
|         "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); |  | ||||||
|     if (this.httpProxy != null) { |  | ||||||
|       RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy) |  | ||||||
|           .build(); |  | ||||||
|       httpPost.setConfig(config); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); |     String responseContent = this.wxMpService.post(url, request.toString()); | ||||||
|     httpPost.setEntity(entity); |     XStream xstream = XStreamInitializer.getInstance(); | ||||||
|     try (CloseableHttpResponse response = this.wxMpService.getHttpclient() |     xstream.processAnnotations(WxRedpackResult.class); | ||||||
|         .execute(httpPost)) { |     return (WxRedpackResult) xstream.fromXML(responseContent); | ||||||
|       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(); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -544,7 +456,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||||||
|  |  | ||||||
|   private void checkParameters(WxUnifiedOrderRequest request) { |   private void checkParameters(WxUnifiedOrderRequest request) { | ||||||
|  |  | ||||||
|     List<String> nullFields = com.google.common.collect.Lists.newArrayList(); |     List<String> nullFields = Lists.newArrayList(); | ||||||
|     for (Entry<String, Reflect> entry : Reflect.on(request).fields() |     for (Entry<String, Reflect> entry : Reflect.on(request).fields() | ||||||
|         .entrySet()) { |         .entrySet()) { | ||||||
|       Reflect reflect = entry.getValue(); |       Reflect reflect = entry.getValue(); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 BinaryWang
					BinaryWang