修复checkstyle检查出来的部分代码问题

This commit is contained in:
Binary Wang
2017-11-07 15:29:10 +08:00
parent 97d6f90ac4
commit 67abc9216f
21 changed files with 217 additions and 138 deletions

View File

@@ -19,16 +19,17 @@ import java.util.TreeMap;
/**
* <pre>
* 签名相关工具类
* 签名相关工具类.
* Created by Binary Wang on 2017-3-23.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
*/
public class SignUtils {
private static final Logger log = LoggerFactory.getLogger(SignUtils.class);
/**
* 请参考并使用 {@link #createSign(Object, String, String, boolean)}
* 请参考并使用 {@link #createSign(Object, String, String, boolean)}.
*/
@Deprecated
public static String createSign(Object xmlBean, String signKey) {
@@ -36,7 +37,7 @@ public class SignUtils {
}
/**
* 请参考并使用 {@link #createSign(Map, String, String, boolean)}
* 请参考并使用 {@link #createSign(Map, String, String, boolean)}.
*/
@Deprecated
public static String createSign(Map<String, String> params, String signKey) {
@@ -44,7 +45,7 @@ public class SignUtils {
}
/**
* 微信支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
* 微信支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3).
*
* @param xmlBean Bean里的属性如果存在XML注解则使用其作为key否则使用变量名
* @param signType 签名类型如果为空则默认为MD5
@@ -57,27 +58,25 @@ public class SignUtils {
}
/**
* 微信支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
* 微信支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3).
*
* @param params 参数信息
* @param signType 签名类型如果为空则默认为MD5
* @param signKey 签名Key
* @param isIgnoreSignType 签名时是否忽略signType
* @param params 参数信息
* @param signType 签名类型如果为空则默认为MD5
* @param signKey 签名Key
* @param ignoreSignType 签名时是否忽略signType
* @return 签名字符串
*/
public static String createSign(Map<String, String> params, String signType, String signKey, boolean isIgnoreSignType) {
public static String createSign(Map<String, String> params, String signType, String signKey, boolean ignoreSignType) {
SortedMap<String, String> sortedMap = new TreeMap<>(params);
StringBuilder toSign = new StringBuilder();
for (String key : sortedMap.keySet()) {
String value = params.get(key);
boolean shouldSign = false;
if (isIgnoreSignType && "sign_type".equals(key)) {
if (ignoreSignType && "sign_type".equals(key)) {
shouldSign = false;
} else if (StringUtils.isNotEmpty(value)
&& !Lists.newArrayList(
"sign", "key", "xmlString", "xmlDoc", "couponList").contains(key)
) {
&& !Lists.newArrayList("sign", "key", "xmlString", "xmlDoc", "couponList").contains(key)) {
shouldSign = true;
}
@@ -88,18 +87,18 @@ public class SignUtils {
toSign.append("key=").append(signKey);
if (SignType.HMAC_SHA256.equals(signType)) {
return createHMACSha256Sign(toSign.toString(), signKey);
return createHmacSha256Sign(toSign.toString(), signKey);
} else {
return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
}
}
private static String createHMACSha256Sign(String message, String key) {
private static String createHmacSha256Sign(String message, String key) {
try {
Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
Mac sha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "HmacSHA256");
hmacSHA256.init(secretKeySpec);
byte[] bytes = hmacSHA256.doFinal(message.getBytes());
sha256.init(secretKeySpec);
byte[] bytes = sha256.doFinal(message.getBytes());
return Hex.encodeHexString(bytes).toUpperCase();
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error(e.getMessage(), e);
@@ -109,7 +108,7 @@ public class SignUtils {
}
/**
* 校验签名是否正确
* 校验签名是否正确.
*
* @param xmlBean Bean需要标记有XML注解
* @param signType 签名类型如果为空则默认为MD5
@@ -121,7 +120,7 @@ public class SignUtils {
}
/**
* 校验签名是否正确
* 校验签名是否正确.
*
* @param params 需要校验的参数Map
* @param signType 签名类型如果为空则默认为MD5

View File

@@ -2,6 +2,7 @@ package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
@@ -398,9 +399,43 @@ public class WxPayServiceAbstractImplTest {
this.logger.info(result);
}
/**
* @see {@link com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest}
* @throws Exception
*/
@Test
public void testParseOrderNotifyResult() throws Exception {
// 请参考com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest里的单元测试
// 请参考com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResultTest 里的单元测试
String xmlString = "<xml>\n" +
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
" <attach><![CDATA[支付测试]]></attach>\n" +
" <bank_type><![CDATA[CFT]]></bank_type>\n" +
" <fee_type><![CDATA[CNY]]></fee_type>\n" +
" <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
" <mch_id><![CDATA[10000100]]></mch_id>\n" +
" <nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>\n" +
" <openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>\n" +
" <out_trade_no><![CDATA[1409811653]]></out_trade_no>\n" +
" <result_code><![CDATA[SUCCESS]]></result_code>\n" +
" <return_code><![CDATA[SUCCESS]]></return_code>\n" +
" <sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>\n" +
" <sub_mch_id><![CDATA[10000100]]></sub_mch_id>\n" +
" <time_end><![CDATA[20140903131540]]></time_end>\n" +
" <total_fee>1</total_fee>\n" +
" <trade_type><![CDATA[JSAPI]]></trade_type>\n" +
" <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" +
" <coupon_count>2</coupon_count>\n" +
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
" <coupon_id_0>10000</coupon_id_0>\n" +
" <coupon_fee_0>100</coupon_fee_0>\n" +
" <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" +
" <coupon_id_1>10001</coupon_id_1>\n" +
" <coupon_fee_1>200</coupon_fee_1>\n" +
"</xml>";
WxPayOrderNotifyResult result = this.payService.parseOrderNotifyResult(xmlString);
System.out.println(result);
}
@Test