Authored by LUOXC

FIXBUG

... ... @@ -70,12 +70,12 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
public boolean notifyVerify(Map<String, String> paramsMap) {
//验证业务结果
String resultCode = paramsMap.get(WeixinPayConfig.ApiConstants.RETURN_RESULT_CODE);
if(!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(resultCode)){
if (!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(resultCode)) {
log.error("[{}] trade failed, resultCode: {}", paramsMap.get("out_trade_no"), resultCode);
return false;
}
if(!verifySign(paramsMap)) {
if (!verifySign(paramsMap)) {
log.error("[{}] sign verify failed", paramsMap.get("out_trade_no"));
return false;
}
... ... @@ -84,8 +84,6 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
}
public PayQueryBo payQuery(String tradeNo, int orderCreateTime) {
Map<String, String> queryParams = buildPayQureyParams(tradeNo);
String requestXml = WXUtils.createWXPayXml(queryParams);
... ... @@ -96,18 +94,18 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
private PayQueryBo queryConvert(String respXml) {
PayQueryBo queryBo = new PayQueryBo();
if(StringUtils.isEmpty(respXml)) {
if (StringUtils.isEmpty(respXml)) {
return queryBo;
}
Map<String, String> queryMap = WXUtils.parseWXPayXml(respXml);
if(queryMap == null || queryMap.isEmpty()) {
if (queryMap == null || queryMap.isEmpty()) {
return queryBo;
}
String returnCode = queryMap.get(WeixinPayConfig.ApiConstants.RETURN_CODE);
String resultCode = queryMap.get(WeixinPayConfig.ApiConstants.RETURN_RESULT_CODE);
if(!"SUCCESS".equals(returnCode) || !"SUCCESS".equals(resultCode)) {
String resultCode = queryMap.get(WeixinPayConfig.ApiConstants.RETURN_RESULT_CODE);
if (!"SUCCESS".equals(returnCode) || !"SUCCESS".equals(resultCode)) {
return queryBo;
}
... ... @@ -116,7 +114,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
queryBo.setPayStatus(payStatus);
queryBo.setRefundStatus(refundStatus);
if( payStatus || refundStatus) {
if (payStatus || refundStatus) {
String out_trade_no = queryMap.get("out_trade_no");
int index = out_trade_no.indexOf(WeixinPayConfig.WECHAT_TRADE_NO_PREFIX);
if (index >= 0) {
... ... @@ -138,9 +136,9 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
}
/**
* 支付查询请求
*
* @param tradeNo
* @param requestXml
* @return
... ... @@ -162,6 +160,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 支付查询请求参数
*
* @param tradeNo
* @return
*/
... ... @@ -188,14 +187,14 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
return sign;
}
protected Map<String, String> withHmacSha256Sign(Map<String, String> signParams){
protected Map<String, String> withHmacSha256Sign(Map<String, String> signParams) {
signParams.put(WeixinPayConfig.ApiConstants.SIGN_TYPE, "HMAC-SHA256");
String sign = hmacSha256(signParams);
signParams.put(WeixinPayConfig.ApiConstants.SIGN, sign);
return signParams;
}
protected Map<String, String> withHmacSha256SignWithoutSignType(Map<String, String> signParams){
protected Map<String, String> withHmacSha256SignWithoutSignType(Map<String, String> signParams) {
signParams.put(WeixinPayConfig.ApiConstants.SIGN_TYPE, "HMAC-SHA256");
String sign = hmacSha256(signParams);
signParams.put(WeixinPayConfig.ApiConstants.SIGN, sign);
... ... @@ -251,7 +250,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
}
public PayRefundBo refundOpenApi(PayRefundBo refundBo){
public PayRefundBo refundOpenApi(PayRefundBo refundBo) {
Map<String, String> queryParams = buildRefundParams(refundBo);
String requestXml = WXUtils.createWXPayXml(queryParams);
String respXml = sendRefundRequest(String.valueOf(refundBo.getPayOrderCode()), requestXml);
... ... @@ -267,13 +266,13 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
return bo;
}
Map<String, String> reponseMap = WXUtils.parseWXPayXml(responseXml);
if(!"SUCCESS".equals(reponseMap.get("return_code"))) {
Map<String, String> reponseMap = WXUtils.parseWXPayXml(responseXml);
if (!"SUCCESS".equals(reponseMap.get("return_code"))) {
bo.setRefundStatus(RefundContant.PAYMENT_REFUND_RESULTCODE_REQERR);
bo.setRefundMsg(reponseMap.get("return_msg"));
return bo;
}
if(!"SUCCESS".equals(reponseMap.get("result_code"))) {
if (!"SUCCESS".equals(reponseMap.get("result_code"))) {
bo.setRefundStatus(RefundContant.PAYMENT_REFUND_RESULTCODE_FAIL);
bo.setRefundMsg(reponseMap.get("err_code") + ": " + reponseMap.get("err_code_des"));
return bo;
... ... @@ -289,6 +288,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 退款请求
*
* @param requestXml
* @return
*/
... ... @@ -297,7 +297,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
String respXml = "";
HttpClient httpClient = getSslHttpClient();
if(httpClient == null) {
if (httpClient == null) {
log.error("init weixin ssl httpclient faild, unable refund weixinpay");
return respXml;
}
... ... @@ -315,16 +315,17 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
public JSONObject prepayRequest(OrderInfo orderInfo) {
log.info("[{}] prepayRequest begin", orderInfo.getOrderCode());
Map<String, String> requestParams = buildPrepayParams( orderInfo);
Map<String, String> requestParams = buildPrepayParams(orderInfo);
String requestXml = WXUtils.createWXPayXml(requestParams);
String respXml = sendPrepayRequest(orderInfo.getOrderCode(), requestXml);
if(StringUtils.isEmpty(respXml)) {
if (StringUtils.isEmpty(respXml)) {
log.error("[{}] prepayRequest failed", orderInfo.getOrderCode());
return null;
}
Map<String, String> reponseMap = WXUtils.parseWXPayXml(respXml);
if(!checkPrepayResponse(orderInfo.getOrderCode(), reponseMap)) {
String requestSignType = requestParams.get(WeixinPayConfig.ApiConstants.SIGN_TYPE);
if (!checkPrepayResponse(orderInfo.getOrderCode(), reponseMap,requestSignType)) {
log.error("[{}] valid prepay response failed", orderInfo.getOrderCode());
return null;
}
... ... @@ -344,8 +345,8 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
paramMap.put("out_refund_no", refundBo.getRefundOrderCode());
//String totalFee = String.valueOf((int)YHMath.mul(amount, 100)); //微信金额必须以分为单位,且不能包含小数点
paramMap.put("total_fee", String.valueOf((int)YHMath.mul(refundBo.getOrderTotalFee(), 100)));
paramMap.put("refund_fee", String.valueOf((int)YHMath.mul(refundBo.getAmount(), 100)));
paramMap.put("total_fee", String.valueOf((int) YHMath.mul(refundBo.getOrderTotalFee(), 100)));
paramMap.put("refund_fee", String.valueOf((int) YHMath.mul(refundBo.getAmount(), 100)));
paramMap.put("op_user_id", getMchId());
//签名
... ... @@ -353,22 +354,20 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
}
/**
* 获取预支付的返回数据
*
* @param requestParams
* @param responseParams
* @return
*/
protected JSONObject getPrepayData(Map<String, String> requestParams, Map<String, String> responseParams) {
JSONObject prepayJson = null;
if(WeixinPayConfig.TRADE_TYPE_NATIVE.equals(getTradeType())) {
if (WeixinPayConfig.TRADE_TYPE_NATIVE.equals(getTradeType())) {
prepayJson = getNativePrepayData(requestParams, responseParams);
}
else if(WeixinPayConfig.TRADE_TYPE_JSAPI.equals(getTradeType())) {
} else if (WeixinPayConfig.TRADE_TYPE_JSAPI.equals(getTradeType())) {
prepayJson = getJSAPIPrepayData(requestParams, responseParams);
}
else {
} else {
prepayJson = getAppPrepayData(requestParams, responseParams);
}
return prepayJson;
... ... @@ -377,13 +376,14 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 返回扫码预支付
*
* @param requestParams
* @param responseParams
* @return
*/
private JSONObject getNativePrepayData(Map<String, String> requestParams, Map<String, String> responseParams) {
String codeUrl = responseParams.get(WeixinPayConfig.ApiConstants.RETURN_CODE_URL);
if(StringUtils.isEmpty(codeUrl)) {
if (StringUtils.isEmpty(codeUrl)) {
log.error("[{}] obtain codeUrl failed", requestParams.get(WeixinPayConfig.ApiConstants.OUT_TRADE_NO));
return null;
}
... ... @@ -399,7 +399,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
*/
private JSONObject getJSAPIPrepayData(Map<String, String> requestParams, Map<String, String> responseParams) {
String prepayId = responseParams.get(WeixinPayConfig.ApiConstants.RETURN_PREPAY_ID);
if(StringUtils.isEmpty(prepayId)) {
if (StringUtils.isEmpty(prepayId)) {
log.error("[{}] obtain prepayId failed", requestParams.get(WeixinPayConfig.ApiConstants.OUT_TRADE_NO));
return null;
}
... ... @@ -426,13 +426,12 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
}
/**
* 组织预支付返回给APP的数据
*/
private JSONObject getAppPrepayData(Map<String, String> requestParams, Map<String, String> responseParams) {
String prepayId = responseParams.get(WeixinPayConfig.ApiConstants.RETURN_PREPAY_ID);
if(StringUtils.isEmpty(prepayId)) {
if (StringUtils.isEmpty(prepayId)) {
log.error("[{}] obtain prepayId failed", requestParams.get(WeixinPayConfig.ApiConstants.OUT_TRADE_NO));
return null;
}
... ... @@ -450,7 +449,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
withHmacSha256SignWithoutSignType(prePayData);
JSONObject sendData = new JSONObject();
sendData.put("prePayUrl", weixinPayConfig.prepayUrl()); //实际上没用,为兼容APP,暂且保留
sendData.put("prePayUrl", weixinPayConfig.prepayUrl()); //实际上没用,为兼容APP,暂且保留
sendData.put("token", "xxxxx"); //实际上没用,为兼容APP,暂且保留
sendData.put("prePayData", prePayData);
... ... @@ -461,30 +460,31 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 获取并校验预支付返回参数
*
* @param orderCode
* @param reponseMap
* @return
*/
private boolean checkPrepayResponse(long orderCode, Map<String, String> reponseMap) {
if(reponseMap == null || reponseMap.size() == 0) {
private boolean checkPrepayResponse(long orderCode, Map<String, String> reponseMap, String signType) {
if (reponseMap == null || reponseMap.size() == 0) {
log.error("[{}] Prepay response parse failed", orderCode);
return false;
}
String returnCode = reponseMap.get(WeixinPayConfig.ApiConstants.RETURN_CODE);
String resultCode = reponseMap.get(WeixinPayConfig.ApiConstants.RETURN_RESULT_CODE);
if(!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(returnCode)) {
String resultCode = reponseMap.get(WeixinPayConfig.ApiConstants.RETURN_RESULT_CODE);
if (!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(returnCode)) {
log.error("[{}] prepay returnCode error: {}", orderCode, returnCode);
return false;
}
//验证签名
if(!verifySign(reponseMap)){
if (!verifySign(reponseMap, signType)) {
log.error("[{}] sign verify failed", orderCode);
return false;
}
//return_code和result_code都为SUCCESS时,返回prepayId
if(!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(resultCode)){
if (!WeixinPayConfig.ApiConstants.PREPAY_RESULT_SUCCESS.equals(resultCode)) {
log.error("[{}] prepay resultCode error: {}", orderCode, resultCode);
return false;
}
... ... @@ -494,10 +494,13 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
public boolean verifySign(Map<String, String> paramsMap) {
return verifySign(paramsMap, paramsMap.get(WeixinPayConfig.ApiConstants.SIGN_TYPE));
}
public boolean verifySign(Map<String, String> paramsMap, String signType) {
if (paramsMap == null) {
return false;
}
String signType = paramsMap.get(WeixinPayConfig.ApiConstants.SIGN_TYPE);
if (Objects.equals(signType, "HMAC-SHA256")) {
return hmacSha256(paramsMap).equals(paramsMap.get(WeixinPayConfig.ApiConstants.SIGN));
} else {
... ... @@ -507,6 +510,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 预支付请求
*
* @param orderCode
* @param requestXml
* @return
... ... @@ -528,6 +532,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
/**
* 生成预支付请求参数
*
* @param orderInfo
* @return
*/
... ... @@ -548,7 +553,7 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
parameters.put(WeixinPayConfig.ApiConstants.NOTIFY_URL, notifyURL);
//parameters.put(WeixinPayConfig.ApiConstants.TRADE_TYPE, "APP");
parameters.put(WeixinPayConfig.ApiConstants.TRADE_TYPE, getTradeType());
if(WeixinPayConfig.TRADE_TYPE_JSAPI.equals(getTradeType())) {
if (WeixinPayConfig.TRADE_TYPE_JSAPI.equals(getTradeType())) {
parameters.put(WeixinPayConfig.ApiConstants.OPEN_ID, orderInfo.getOpenid());
}
parameters.put(WeixinPayConfig.ApiConstants.TIME_EXPIRE, getPayExpireTimeStr(orderInfo));
... ... @@ -561,9 +566,9 @@ public abstract class AbstractWeixinPayService extends AbstractPayService {
return withHmacSha256Sign(parameters);
}
private String getPayExpireTimeStr(OrderInfo orderInfo){
private String getPayExpireTimeStr(OrderInfo orderInfo) {
int payExpireTime = DateUtil.getCurrentTimeSecond() + orderInfo.getPayExpireTime()*60;
int payExpireTime = DateUtil.getCurrentTimeSecond() + orderInfo.getPayExpireTime() * 60;
// 超时时间秒
return DateUtil.formatYYMMddHHmmss(payExpireTime);
... ...