mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	Merge pull request #214 from videome/develop
Improve prepay API to support all parameters.
This commit is contained in:
		| @ -682,9 +682,31 @@ public interface WxMpService { | |||||||
|    * @param ip 发起支付的客户端IP |    * @param ip 发起支付的客户端IP | ||||||
|    * @param notifyUrl 通知地址 |    * @param notifyUrl 通知地址 | ||||||
|    * @return |    * @return | ||||||
|  |    * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) 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); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | ||||||
|  |    * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | ||||||
|  |    *  | ||||||
|  |    * @param parameters | ||||||
|  |    *            All required/optional parameters for weixin payment | ||||||
|  |    * @return | ||||||
|  |    * @throws IllegalArgumentException | ||||||
|  |    */ | ||||||
|  |   WxMpPrepayIdResult getPrepayId(Map<String, String> parameters); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 | ||||||
|  |    * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | ||||||
|  |    * @param parameters | ||||||
|  |    *            the required or optional parameters | ||||||
|  |    * @return | ||||||
|  |    */ | ||||||
|  |   Map<String, String> getJSSDKPayInfo(Map<String, String> parameters);  	 | ||||||
|  |   	 | ||||||
|   /** |   /** | ||||||
|    * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 |    * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 | ||||||
|    * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 |    * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | ||||||
| @ -696,7 +718,9 @@ public interface WxMpService { | |||||||
|    * @param ip 发起支付的客户端IP |    * @param ip 发起支付的客户端IP | ||||||
|    * @param notifyUrl 通知地址 |    * @param notifyUrl 通知地址 | ||||||
|    * @return |    * @return | ||||||
|  |    * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getJSSDKPayInfo(Map<String, String>) instead | ||||||
|    */ |    */ | ||||||
|  |   @Deprecated | ||||||
|   Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl); |   Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl); | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -11,6 +11,7 @@ import java.util.Date; | |||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  | import java.util.Map.Entry; | ||||||
| import java.util.SortedMap; | import java.util.SortedMap; | ||||||
| import java.util.TreeMap; | import java.util.TreeMap; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| @ -777,35 +778,37 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { |   public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { | ||||||
|     String nonce_str = System.currentTimeMillis() + ""; |     Map<String, String> packageParams = new HashMap<String, String>(); | ||||||
|  |  | ||||||
|     SortedMap<String, String> packageParams = new TreeMap<String, String>(); |  | ||||||
|     packageParams.put("appid", wxMpConfigStorage.getAppId()); |     packageParams.put("appid", wxMpConfigStorage.getAppId()); | ||||||
|     packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); |     packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | ||||||
|     packageParams.put("nonce_str", nonce_str); |  | ||||||
|     packageParams.put("body", body); |     packageParams.put("body", body); | ||||||
|     packageParams.put("out_trade_no", outTradeNo); |     packageParams.put("out_trade_no", outTradeNo); | ||||||
|  |  | ||||||
|     packageParams.put("total_fee", (int) (amt * 100) + ""); |     packageParams.put("total_fee", (int) (amt * 100) + ""); | ||||||
|     packageParams.put("spbill_create_ip", ip); |     packageParams.put("spbill_create_ip", ip); | ||||||
|     packageParams.put("notify_url", callbackUrl); |     packageParams.put("notify_url", callbackUrl); | ||||||
|     packageParams.put("trade_type", tradeType); |     packageParams.put("trade_type", tradeType); | ||||||
|     packageParams.put("openid", openId); |     packageParams.put("openid", openId); | ||||||
|  |  | ||||||
|  |     return getPrepayId(packageParams); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) { | ||||||
|  |     String nonce_str = System.currentTimeMillis() + ""; | ||||||
|  |  | ||||||
|  |     final SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters); | ||||||
|  |     packageParams.put("appid", wxMpConfigStorage.getAppId()); | ||||||
|  |     packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | ||||||
|  |     packageParams.put("nonce_str", nonce_str); | ||||||
|  |     checkParameters(packageParams); | ||||||
|  |  | ||||||
|     String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()); |     String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()); | ||||||
|     String xml = "<xml>" + |     packageParams.put("sign", sign); | ||||||
|         "<appid>" + wxMpConfigStorage.getAppId() + "</appid>" + |  | ||||||
|         "<mch_id>" + wxMpConfigStorage.getPartnerId() + "</mch_id>" + |     StringBuilder request = new StringBuilder("<xml>"); | ||||||
|         "<nonce_str>" + nonce_str + "</nonce_str>" + |     for (Entry<String, String> para : packageParams.entrySet()) { | ||||||
|         "<sign>" + sign + "</sign>" + |       request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey())); | ||||||
|         "<body><![CDATA[" + body + "]]></body>" + |     } | ||||||
|         "<out_trade_no>" + outTradeNo + "</out_trade_no>" + |     request.append("</xml>"); | ||||||
|         "<total_fee>" + packageParams.get("total_fee") + "</total_fee>" + |  | ||||||
|         "<spbill_create_ip>" + ip + "</spbill_create_ip>" + |  | ||||||
|         "<notify_url>" + callbackUrl + "</notify_url>" + |  | ||||||
|         "<trade_type>" + tradeType + "</trade_type>" + |  | ||||||
|         "<openid>" + openId + "</openid>" + |  | ||||||
|         "</xml>"; |  | ||||||
|  |  | ||||||
|     HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); |     HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); | ||||||
|     if (httpProxy != null) { |     if (httpProxy != null) { | ||||||
| @ -813,7 +816,7 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|       httpPost.setConfig(config); |       httpPost.setConfig(config); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     StringEntity entity = new StringEntity(xml, Consts.UTF_8); |     StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | ||||||
|     httpPost.setEntity(entity); |     httpPost.setEntity(entity); | ||||||
|     try { |     try { | ||||||
|       CloseableHttpResponse response = getHttpclient().execute(httpPost); |       CloseableHttpResponse response = getHttpclient().execute(httpPost); | ||||||
| @ -828,9 +831,39 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     return new WxMpPrepayIdResult(); |     return new WxMpPrepayIdResult(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid", "mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip", "notify_url", | ||||||
|  |       "trade_type", }; | ||||||
|  |  | ||||||
|  |   private void checkParameters(Map<String, String> parameters) { | ||||||
|  |     for (String para : REQUIRED_ORDER_PARAMETERS) { | ||||||
|  |       if (!parameters.containsKey(para)) | ||||||
|  |         throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing."); | ||||||
|  |     } | ||||||
|  |     if ("JSAPI".equals(parameters.get("trade_type")) && !parameters.containsKey("openid")) | ||||||
|  |       throw new IllegalArgumentException("Reqiured argument 'openid' is missing when trade_type is 'JSAPI'."); | ||||||
|  |     if ("NATIVE".equals(parameters.get("trade_type")) && !parameters.containsKey("product_id")) | ||||||
|  |       throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'."); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { |   public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { | ||||||
|     WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(openId, outTradeNo, amt, body, tradeType, ip, callbackUrl); |     Map<String, String> packageParams = new HashMap<String, String>(); | ||||||
|  |     packageParams.put("appid", wxMpConfigStorage.getAppId()); | ||||||
|  |     packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | ||||||
|  |     packageParams.put("body", body); | ||||||
|  |     packageParams.put("out_trade_no", outTradeNo); | ||||||
|  |     packageParams.put("total_fee", (int) (amt * 100) + ""); | ||||||
|  |     packageParams.put("spbill_create_ip", ip); | ||||||
|  |     packageParams.put("notify_url", callbackUrl); | ||||||
|  |     packageParams.put("trade_type", tradeType); | ||||||
|  |     packageParams.put("openid", openId); | ||||||
|  |  | ||||||
|  |     return getJSSDKPayInfo(packageParams); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) { | ||||||
|  |     WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters); | ||||||
|     String prepayId = wxMpPrepayIdResult.getPrepay_id(); |     String prepayId = wxMpPrepayIdResult.getPrepay_id(); | ||||||
|     if (prepayId == null || prepayId.equals("")) { |     if (prepayId == null || prepayId.equals("")) { | ||||||
|       throw new RuntimeException("get prepayid error"); |       throw new RuntimeException("get prepayid error"); | ||||||
|  | |||||||
| @ -22,6 +22,9 @@ public class WxMpPrepayIdResult implements Serializable { | |||||||
|     private String result_code; |     private String result_code; | ||||||
|     private String prepay_id; |     private String prepay_id; | ||||||
|     private String trade_type; |     private String trade_type; | ||||||
|  |     private String err_code; | ||||||
|  |     private String err_code_des; | ||||||
|  |     private String code_url; | ||||||
|  |  | ||||||
|     public String getReturn_code() { |     public String getReturn_code() { | ||||||
|         return return_code; |         return return_code; | ||||||
| @ -94,4 +97,28 @@ public class WxMpPrepayIdResult implements Serializable { | |||||||
|     public void setTrade_type(String trade_type) { |     public void setTrade_type(String trade_type) { | ||||||
|         this.trade_type = trade_type; |         this.trade_type = trade_type; | ||||||
|     } |     } | ||||||
|  |      | ||||||
|  |     public String getErr_code() { | ||||||
|  |       return err_code; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void setErr_code(String err_code) { | ||||||
|  |       this.err_code = err_code; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public String getErr_code_des() { | ||||||
|  |       return err_code_des; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void setErr_code_des(String err_code_des) { | ||||||
|  |       this.err_code_des = err_code_des; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public String getCode_url() { | ||||||
|  |       return code_url; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void setCode_url(String code_url) { | ||||||
|  |       this.code_url = code_url; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian