mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 02:28:25 +08:00 
			
		
		
		
	reformat code for PayService
This commit is contained in:
		| @ -30,9 +30,8 @@ public interface WxMpPayService { | ||||
|    * 接口地址:https://api.mch.weixin.qq.com/pay/orderquery | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param transactionId 微信支付分配的商户号 | ||||
|    * @param transactionId 微信订单号 | ||||
|    * @param outTradeNo    商户系统内部的订单号,当没提供transaction_id时需要传这个。 | ||||
|    * @throws WxErrorException | ||||
|    */ | ||||
|   WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException; | ||||
|  | ||||
| @ -49,7 +48,6 @@ public interface WxMpPayService { | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param outTradeNo 商户系统内部的订单号,当没提供transaction_id时需要传这个。 | ||||
|    * @throws WxErrorException | ||||
|    */ | ||||
|   WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorException; | ||||
|  | ||||
| @ -59,7 +57,6 @@ public interface WxMpPayService { | ||||
|    * 接口地址:https://api.mch.weixin.qq.com/pay/unifiedorder | ||||
|    * | ||||
|    * @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置) | ||||
|    * @throws WxErrorException | ||||
|    */ | ||||
|   WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxErrorException; | ||||
|  | ||||
| @ -68,7 +65,6 @@ public interface WxMpPayService { | ||||
|    * 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN | ||||
|    * | ||||
|    * @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置) | ||||
|    * @throws WxErrorException | ||||
|    */ | ||||
|   Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException; | ||||
|  | ||||
| @ -93,7 +89,8 @@ public interface WxMpPayService { | ||||
|    * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5 | ||||
|    * 接口链接:https://api.mch.weixin.qq.com/pay/refundquery | ||||
|    * </pre> | ||||
|    *  以下四个参数四选一 | ||||
|    * 以下四个参数四选一 | ||||
|    * | ||||
|    * @param transactionId 微信订单号 | ||||
|    * @param outTradeNo    商户订单号 | ||||
|    * @param outRefundNo   商户退款单号 | ||||
| @ -112,15 +109,16 @@ public interface WxMpPayService { | ||||
|    * 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) | ||||
|    * | ||||
|    * @param xmlbean Bean需要标记有XML注解,默认使用配置中的PartnerKey进行签名 | ||||
|    * @since 2.5.0 | ||||
|    * @return 签名字符串 | ||||
|    * @see #createSign(Map, String) | ||||
|    * @since 2.5.0 | ||||
|    */ | ||||
|   String createSign(Object xmlbean); | ||||
|  | ||||
|   /** | ||||
|    * 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) | ||||
|    * @param xmlbean  Bean需要标记有XML注解 | ||||
|    * | ||||
|    * @param xmlbean Bean需要标记有XML注解 | ||||
|    * @param signKey 签名Key | ||||
|    * @return 签名字符串 | ||||
|    * @see #createSign(Map, String) | ||||
| @ -129,8 +127,8 @@ public interface WxMpPayService { | ||||
|  | ||||
|   /** | ||||
|    * 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) | ||||
|    * @param prams 参数信息,默认使用配置中的PartnerKey进行签名 | ||||
|    * @param signKey 签名Key | ||||
|    * | ||||
|    * @param prams   参数信息,默认使用配置中的PartnerKey进行签名 | ||||
|    * @return 签名字符串 | ||||
|    * @see #createSign(Map, String) | ||||
|    */ | ||||
| @ -139,16 +137,17 @@ public interface WxMpPayService { | ||||
|  | ||||
|   /** | ||||
|    * 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) | ||||
|    * @param prams  参数信息 | ||||
|    * | ||||
|    * @param prams   参数信息 | ||||
|    * @param signKey 签名Key | ||||
|    * @return 签名字符串 | ||||
|    */ | ||||
|   String createSign(Map<String, String> prams, String signKey); | ||||
|  | ||||
|  | ||||
|    | ||||
|   /** | ||||
|    * 校验签名是否正确,默认使用配置中的PartnerKey进行签名 | ||||
|    * | ||||
|    * @param xmlbean Bean需要标记有XML注解 | ||||
|    * @return true - 签名校验成功,false - 签名校验失败 | ||||
|    * @see #checkSign(Map, String) | ||||
| @ -157,8 +156,9 @@ public interface WxMpPayService { | ||||
|  | ||||
|   /** | ||||
|    * 校验签名是否正确 | ||||
|    * | ||||
|    * @param xmlbean Bean需要标记有XML注解 | ||||
|    * @param signKey  校验的签名Key | ||||
|    * @param signKey 校验的签名Key | ||||
|    * @return true - 签名校验成功,false - 签名校验失败 | ||||
|    * @see #checkSign(Map, String) | ||||
|    */ | ||||
| @ -166,6 +166,7 @@ public interface WxMpPayService { | ||||
|  | ||||
|   /** | ||||
|    * 校验签名是否正确,默认使用配置中的PartnerKey进行签名 | ||||
|    * | ||||
|    * @param prams 需要校验的参数Map | ||||
|    * @return true - 签名校验成功,false - 签名校验失败 | ||||
|    * @see #checkSign(Map, String) | ||||
| @ -174,13 +175,13 @@ public interface WxMpPayService { | ||||
|  | ||||
|   /** | ||||
|    * 校验签名是否正确 | ||||
|    * @param prams 需要校验的参数Map | ||||
|    * @param signKey  校验的签名Key | ||||
|    * | ||||
|    * @param params   需要校验的参数Map | ||||
|    * @param signKey 校验的签名Key | ||||
|    * @return true - 签名校验成功,false - 签名校验失败 | ||||
|    * @see #checkSign(Map, String) | ||||
|    */ | ||||
|   boolean checkSign(Map<String, String> prams, String signKey); | ||||
|    | ||||
|   boolean checkSign(Map<String, String> params, String signKey); | ||||
|  | ||||
|  | ||||
|   /** | ||||
| @ -206,8 +207,9 @@ public interface WxMpPayService { | ||||
|    *   是否需要证书	是(证书及使用说明详见商户证书) | ||||
|    *   请求方式	POST | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param mchBillNo 商户发放红包的商户订单号,比如10000098201411111234567890 | ||||
|    * @param keyFile 证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3) | ||||
|    * @param keyFile   证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3) | ||||
|    */ | ||||
|   WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) throws WxErrorException; | ||||
|  | ||||
|  | ||||
| @ -1,17 +1,16 @@ | ||||
| package me.chanjar.weixin.mp.api.impl; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.FileInputStream; | ||||
| import java.io.IOException; | ||||
| import java.security.KeyStore; | ||||
| import java.util.Arrays; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.SortedMap; | ||||
| import java.util.TreeMap; | ||||
|  | ||||
| import javax.net.ssl.SSLContext; | ||||
|  | ||||
| import com.thoughtworks.xstream.XStream; | ||||
| import me.chanjar.weixin.common.bean.result.WxError; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.BeanUtils; | ||||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | ||||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||||
| import me.chanjar.weixin.mp.api.WxMpPayService; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.pay.WxPayOrderNotifyResultConverter; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.*; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.*; | ||||
| import org.apache.commons.codec.digest.DigestUtils; | ||||
| import org.apache.commons.lang3.ArrayUtils; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| @ -29,36 +28,12 @@ import org.apache.http.util.EntityUtils; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import com.thoughtworks.xstream.XStream; | ||||
|  | ||||
| import me.chanjar.weixin.common.bean.result.WxError; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.BeanUtils; | ||||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | ||||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||||
| import me.chanjar.weixin.mp.api.WxMpPayService; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.bean.pay.WxPayOrderNotifyResultConverter; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxEntPayQueryRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxEntPayRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayOrderCloseRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayOrderQueryRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayRedpackQueryRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayRefundQueryRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayRefundRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPaySendRedpackRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayUnifiedOrderRequest; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxEntPayQueryResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxEntPayResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayBaseResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderCloseResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderNotifyResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderQueryResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayRedpackQueryResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayRefundQueryResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayRefundResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPaySendRedpackResult; | ||||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayUnifiedOrderResult; | ||||
| import javax.net.ssl.SSLContext; | ||||
| import java.io.File; | ||||
| import java.io.FileInputStream; | ||||
| import java.io.IOException; | ||||
| import java.security.KeyStore; | ||||
| import java.util.*; | ||||
|  | ||||
| /** | ||||
|  * Created by Binary Wang on 2016/7/28. | ||||
| @ -71,7 +46,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|   private static final String[] TRADE_TYPES = new String[]{"JSAPI", "NATIVE", "APP"}; | ||||
|   private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS", | ||||
|     "REFUND_SOURCE_UNSETTLED_FUNDS"}; | ||||
|   protected final Logger log = LoggerFactory.getLogger(this.getClass()); | ||||
|   private final Logger log = LoggerFactory.getLogger(this.getClass()); | ||||
|   private WxMpService wxMpService; | ||||
|   private WxMpConfigStorage config = null; | ||||
|  | ||||
| @ -79,8 +54,8 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|     this.wxMpService = wxMpService; | ||||
|   } | ||||
|  | ||||
|   private WxMpConfigStorage getConfig(){ | ||||
|     if(config == null){ | ||||
|   private WxMpConfigStorage getConfig() { | ||||
|     if (config == null) { | ||||
|       this.config = wxMpService.getWxMpConfigStorage(); | ||||
|     } | ||||
|     return this.config; | ||||
| @ -142,21 +117,21 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|  | ||||
|  | ||||
|   private void checkResult(WxPayBaseResult result) throws WxErrorException { | ||||
|   	//校验返回结果签名 | ||||
|   	if(!checkSign(result.toMap())){ | ||||
|   	  log.debug("校验结果签名失败,参数:{}",result.toMap()); | ||||
|   		throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("参数格式校验错误!").build()); | ||||
|   	} | ||||
|     //校验返回结果签名 | ||||
|     if (!checkSign(result.toMap())) { | ||||
|       log.debug("校验结果签名失败,参数:{}", result.toMap()); | ||||
|       throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("参数格式校验错误!").build()); | ||||
|     } | ||||
|  | ||||
|   	//校验结果是否成功 | ||||
|     //校验结果是否成功 | ||||
|     if (!"SUCCESS".equalsIgnoreCase(result.getReturnCode()) | ||||
|       || !"SUCCESS".equalsIgnoreCase(result.getResultCode())) { | ||||
|       WxError error = WxError.newBuilder().setErrorCode(-1) | ||||
|       .setErrorMsg("返回代码: " + result.getReturnCode() + ", 返回信息: " | ||||
|         + result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: " | ||||
|         + result.getErrCode() + ", 错误详情: " + result.getErrCodeDes()) | ||||
|       .build(); | ||||
|       log.debug("结果校验失败,参数:{},详细:{}",result.toMap(),error); | ||||
|         .setErrorMsg("返回代码: " + result.getReturnCode() + ", 返回信息: " | ||||
|           + result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: " | ||||
|           + result.getErrCode() + ", 错误详情: " + result.getErrCodeDes()) | ||||
|         .build(); | ||||
|       log.debug("结果校验失败,参数:{},详细:{}", result.toMap(), error); | ||||
|       throw new WxErrorException(error); | ||||
|     } | ||||
|   } | ||||
| @ -166,7 +141,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|  | ||||
|     if (StringUtils.isNotBlank(request.getRefundAccount())) { | ||||
|       if (!ArrayUtils.contains(REFUND_ACCOUNT, request.getRefundAccount())) { | ||||
|         throw new IllegalArgumentException("refund_account目前必须为" + Arrays.toString(REFUND_ACCOUNT) + "其中之一,实际值:"+ request.getRefundAccount()); | ||||
|         throw new IllegalArgumentException("refund_account目前必须为" + Arrays.toString(REFUND_ACCOUNT) + "其中之一,实际值:" + request.getRefundAccount()); | ||||
|       } | ||||
|     } | ||||
|  | ||||
| @ -181,16 +156,16 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|       log.trace("微信支付回调参数详细:{}", xmlData); | ||||
|       XStream xstream = XStreamInitializer.getInstance(); | ||||
|       xstream.processAnnotations(WxPayOrderNotifyResult.class); | ||||
|   		xstream.registerConverter(new WxPayOrderNotifyResultConverter(xstream.getMapper(),xstream.getReflectionProvider())); | ||||
|   		WxPayOrderNotifyResult result = (WxPayOrderNotifyResult) xstream.fromXML(xmlData); | ||||
|   		log.debug("微信支付回调结果对象:{}",result); | ||||
|   		this.checkResult(result); | ||||
|   		return result; | ||||
|     }catch (WxErrorException e) { | ||||
|     	log.error(e.getMessage(),e); | ||||
|     	throw e; | ||||
| 		}catch (Exception e) { | ||||
|     	log.error(e.getMessage(),e); | ||||
|       xstream.registerConverter(new WxPayOrderNotifyResultConverter(xstream.getMapper(), xstream.getReflectionProvider())); | ||||
|       WxPayOrderNotifyResult result = (WxPayOrderNotifyResult) xstream.fromXML(xmlData); | ||||
|       log.debug("微信支付回调结果对象:{}", result); | ||||
|       this.checkResult(result); | ||||
|       return result; | ||||
|     } catch (WxErrorException 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()); | ||||
|     } | ||||
|   } | ||||
| @ -248,17 +223,17 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|  | ||||
|   @Override | ||||
|   public String createSign(Object xmlBean) { | ||||
|   	return createSign(BeanUtils.xmlBean2Map(xmlBean),getConfig().getPartnerKey()); | ||||
|     return createSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String createSign(Object xmlBean, String signKey) { | ||||
|   	return createSign(BeanUtils.xmlBean2Map(xmlBean), signKey); | ||||
|     return createSign(BeanUtils.xmlBean2Map(xmlBean), signKey); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String createSign(Map<String, String> params) { | ||||
|   	return createSign(params,getConfig().getPartnerKey()); | ||||
|     return createSign(params, getConfig().getPartnerKey()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
| @ -279,22 +254,22 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|  | ||||
|   @Override | ||||
|   public boolean checkSign(Object xmlBean) { | ||||
|     return checkSign(BeanUtils.xmlBean2Map(xmlBean) , getConfig().getPartnerKey()); | ||||
|     return checkSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean checkSign(Object xmlBean, String signKey) { | ||||
|     return checkSign(BeanUtils.xmlBean2Map(xmlBean) , signKey); | ||||
|     return checkSign(BeanUtils.xmlBean2Map(xmlBean), signKey); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean checkSign(Map<String, String> params) { | ||||
|     return checkSign(params , getConfig().getPartnerKey()); | ||||
|     return checkSign(params, getConfig().getPartnerKey()); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean checkSign(Map<String, String> params, String signKey) { | ||||
|   	String sign = this.createSign(params , signKey); | ||||
|     String sign = this.createSign(params, signKey); | ||||
|     return sign.equals(params.get("sign")); | ||||
|   } | ||||
|  | ||||
| @ -371,11 +346,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|  | ||||
|     request.setSign(this.createSign(request)); | ||||
|     String url = PAY_BASE_URL + "/pay/unifiedorder"; | ||||
|     String xmlParam =  xstream.toXML(request); | ||||
|     log.debug("微信统一下单接口,URL:{},参数:{}",url, xmlParam); | ||||
|     String xmlParam = xstream.toXML(request); | ||||
|     log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam); | ||||
|  | ||||
|     String responseContent = this.executeRequest(url, xmlParam); | ||||
|     log.debug("微信统一下单接口,URL:{},结果:{}",url, responseContent); | ||||
|     log.debug("微信统一下单接口,URL:{},结果:{}", url, responseContent); | ||||
|     WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream.fromXML(responseContent); | ||||
|     this.checkResult(result); | ||||
|     return result; | ||||
| @ -385,7 +360,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { | ||||
|     BeanUtils.checkRequiredFields(request); | ||||
|  | ||||
|     if (!ArrayUtils.contains(TRADE_TYPES, request.getTradeType())) { | ||||
|       throw new IllegalArgumentException("trade_type目前必须为" + Arrays.toString(TRADE_TYPES) + "其中之一,实际值:"+request.getTradeType()); | ||||
|       throw new IllegalArgumentException("trade_type目前必须为" + Arrays.toString(TRADE_TYPES) + "其中之一,实际值:" + request.getTradeType()); | ||||
|     } | ||||
|  | ||||
|     if ("JSAPI".equals(request.getTradeType()) && request.getOpenid() == null) { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang