|
|
|
|
@ -71,7 +71,7 @@ public class WxPayHelper { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】初始化加载证书-异常:", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -141,7 +141,7 @@ public class WxPayHelper { |
|
|
|
|
if (e instanceof ServiceException) { |
|
|
|
|
throw e; |
|
|
|
|
} |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】支付通知异常:", e); |
|
|
|
|
throw new ServiceException(String.format("【微信】支付通知异常:[%s]", e.getMessage())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -160,20 +160,23 @@ public class WxPayHelper { |
|
|
|
|
* @param msgOpenId 用于推送的openId |
|
|
|
|
* @return 成功返回 json, 否则返回null |
|
|
|
|
*/ |
|
|
|
|
public static JSONObject createOrder(MerchantEnum merchantEnum, String title, String totalFee, String outTradeNo, String notifyType, String ip, String payOpenId, String msgOpenId) { |
|
|
|
|
public static JSONObject createOrder(MerchantEnum merchantEnum, String title, BigDecimal totalFee, String outTradeNo, String notifyType, String ip, String payOpenId, String msgOpenId) { |
|
|
|
|
try { |
|
|
|
|
if (ObjectUtils.isEmpty(payOpenId) || ObjectUtils.isEmpty(outTradeNo) |
|
|
|
|
|| merchantEnum == null || totalFee == null || notifyType == null) { |
|
|
|
|
log.info("【微信】下单参数缺失"); |
|
|
|
|
log.error("【微信】创建预支付交易订单-失败:参数缺失"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String cents; |
|
|
|
|
int cents; |
|
|
|
|
try { |
|
|
|
|
cents = new BigDecimal(totalFee).movePointRight(2).toString(); // 金额转换=>单位分
|
|
|
|
|
cents = totalFee.movePointRight(2).intValueExact(); // 金额转换=>单位分
|
|
|
|
|
if (cents == 0) { |
|
|
|
|
log.error("【微信】创建预支付交易订单-金额转换异常或金额为0"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.info("【微信】统一下单金额转换异常"); |
|
|
|
|
log.error("【微信】创建预支付交易订单-失败:金额转换异常:", e); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -190,20 +193,20 @@ public class WxPayHelper { |
|
|
|
|
unifiedorder.setOut_trade_no(outTradeNo); |
|
|
|
|
unifiedorder.setBody(title); |
|
|
|
|
|
|
|
|
|
unifiedorder.setTotal_fee(cents); // 金额分
|
|
|
|
|
unifiedorder.setTotal_fee(Integer.toString(cents)); // 金额分
|
|
|
|
|
unifiedorder.setSpbill_create_ip(ip); |
|
|
|
|
unifiedorder.setNotify_url(WeChatConfig.getBaseURL() + merchantEnum.NOTIFY_URL); |
|
|
|
|
unifiedorder.setTrade_type("JSAPI"); |
|
|
|
|
// 额外参数
|
|
|
|
|
unifiedorder.setAttach(notifyType); |
|
|
|
|
|
|
|
|
|
log.info("【微信】统一下单 {},{},{},{},{},{},{},{}", appId, mchId, nonce, title, outTradeNo, totalFee, ip, merchantEnum.NOTIFY_URL); |
|
|
|
|
log.info("【微信】创建预支付交易订单[{}] {},{},{},{},{},{},{}", outTradeNo, appId, mchId, nonce, title, totalFee, ip, merchantEnum.NOTIFY_URL); |
|
|
|
|
|
|
|
|
|
// 微信统一下单
|
|
|
|
|
UnifiedorderResult unifiedorderResult = PayMchAPI.payUnifiedorder(unifiedorder, mchKey); |
|
|
|
|
log.info("【微信】统一下单返回:{}", JsonHelper.toJsonString(unifiedorderResult)); |
|
|
|
|
log.info("【微信】创建预支付交易订单[{}]-响应数据:{}", outTradeNo, JsonHelper.toJsonString(unifiedorderResult)); |
|
|
|
|
if (unifiedorderResult == null) { |
|
|
|
|
log.info("【微信】统一下单失败"); |
|
|
|
|
log.error("【微信】创建预支付交易订单[{}]-失败:响应数据为空", outTradeNo); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -211,7 +214,7 @@ public class WxPayHelper { |
|
|
|
|
// 下单是否成功
|
|
|
|
|
Boolean sign_status = unifiedorderResult.getSign_status(); |
|
|
|
|
if (!SUCCESS.equals(result_code) || sign_status == null || !sign_status) { |
|
|
|
|
log.info("【微信】统一下单返回异常信息:sign_status={}, err_code=[ {} ], err_code_des=[ {} ]", sign_status, unifiedorderResult.getErr_code(), unifiedorderResult.getErr_code_des()); |
|
|
|
|
log.error("【微信】创建预支付交易订单[{}]-失败-返回错误信息:sign_status={}, err_code=[ {} ], err_code_des=[ {} ]", outTradeNo, sign_status, unifiedorderResult.getErr_code(), unifiedorderResult.getErr_code_des()); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
String json = weixin.popular.util.PayUtil.generateMchPayJsRequestJson(unifiedorderResult.getPrepay_id(), appId, mchKey); |
|
|
|
|
@ -222,7 +225,7 @@ public class WxPayHelper { |
|
|
|
|
return respJson; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("【微信】统一下单异常信息:{} - ", e.getMessage(), e); |
|
|
|
|
log.error("【微信】创建预支付交易订单[{}]-失败-异常信息:{} - ", outTradeNo, e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
@ -243,19 +246,23 @@ public class WxPayHelper { |
|
|
|
|
try { |
|
|
|
|
log.info("【微信】退款开始 outTradeNo={}, refundMoney={}, totalFee={}", outTradeNo, refundMoney, totalFee); |
|
|
|
|
|
|
|
|
|
int feeCents = refundMoney.movePointRight(2).intValue(); // 单位分
|
|
|
|
|
int totalFeeCents = totalFee.movePointRight(2).intValue(); |
|
|
|
|
|
|
|
|
|
if (feeCents == 0 || totalFeeCents == 0) { |
|
|
|
|
order.setErrorMsg(ResultEnum.REFUND_MONEY_IS_ZERO.message); |
|
|
|
|
return order; |
|
|
|
|
int refundFeeCents, totalFeeCents; |
|
|
|
|
try { |
|
|
|
|
refundFeeCents = refundMoney.movePointRight(2).intValueExact(); // 单位分
|
|
|
|
|
totalFeeCents = totalFee.movePointRight(2).intValueExact(); |
|
|
|
|
if (refundFeeCents == 0 || totalFeeCents == 0) { |
|
|
|
|
order.setErrorMsg(ResultEnum.REFUND_MONEY_IS_ZERO.message); |
|
|
|
|
return order; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("【微信】退款-失败:金额转换异常:", e); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (merchantEnum == null) { |
|
|
|
|
merchantEnum = MerchantEnum.WX; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SecapiPayRefund refund = new SecapiPayRefund(); |
|
|
|
|
refund.setAppid(merchantEnum.APP_ID); |
|
|
|
|
refund.setMch_id(WeChatConfig.MCH_ID); |
|
|
|
|
@ -263,7 +270,7 @@ public class WxPayHelper { |
|
|
|
|
refund.setSign_type("MD5"); |
|
|
|
|
refund.setOut_trade_no(outTradeNo); |
|
|
|
|
refund.setOut_refund_no(outRefundNo); |
|
|
|
|
refund.setRefund_fee(feeCents); |
|
|
|
|
refund.setRefund_fee(refundFeeCents); |
|
|
|
|
refund.setTotal_fee(totalFeeCents); |
|
|
|
|
refund.setRefund_fee_type("CNY"); |
|
|
|
|
refund.setRefund_account("REFUND_SOURCE_UNSETTLED_FUNDS"); |
|
|
|
|
@ -312,7 +319,7 @@ public class WxPayHelper { |
|
|
|
|
order.setSuccess(true); |
|
|
|
|
return order; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】退款异常:", e); |
|
|
|
|
order.setErrorMsg(ResultEnum.INTERFACE_WX_INVOKE_ERROR.message); |
|
|
|
|
String refundResult = order.getRefundResult(); |
|
|
|
|
order.setRefundResult(ObjectUtils.isEmpty(refundResult) ? order.getErrorMsg() : refundResult); |
|
|
|
|
@ -339,9 +346,18 @@ public class WxPayHelper { |
|
|
|
|
return order; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int totalFeeCents; |
|
|
|
|
try { |
|
|
|
|
totalFeeCents = totalFee.movePointRight(2).intValue(); |
|
|
|
|
int totalFeeCents; |
|
|
|
|
try { |
|
|
|
|
totalFeeCents = totalFee.movePointRight(2).intValueExact(); |
|
|
|
|
if (totalFeeCents == 0) { |
|
|
|
|
log.error("【微信】[盒子支付]-金额转换异常或金额为0"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("【微信】[盒子支付]-失败:金额转换异常:", e); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String appId = WeChatConfig.APP_ID; |
|
|
|
|
String mchId = WeChatConfig.MCH_ID; |
|
|
|
|
@ -432,9 +448,8 @@ public class WxPayHelper { |
|
|
|
|
log.info("【微信】[扫码盒子]免密支付-耗时:[{}] [{}]", ((System.currentTimeMillis() - begTime) + "ms"), outTradeNo); |
|
|
|
|
return wxOrder; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.info("【微信】[盒子支付]异常 {}", e.getMessage()); |
|
|
|
|
log.error("【微信】[盒子支付]异常:", e); |
|
|
|
|
order.setErrorMsg("网络异常或当前不支持微信支付"); |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
} |
|
|
|
|
return order; |
|
|
|
|
} |
|
|
|
|
@ -488,7 +503,7 @@ public class WxPayHelper { |
|
|
|
|
return wxOrder; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】[盒子支付]等待扫码支付异常:", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
@ -544,7 +559,7 @@ public class WxPayHelper { |
|
|
|
|
order.setSuccess(true); // 订单支付成功(判断条件)
|
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】撤销订单异常:", e); |
|
|
|
|
order.setErrorMsg("异常信息:" + e.getMessage()); |
|
|
|
|
} |
|
|
|
|
return order; |
|
|
|
|
@ -650,7 +665,7 @@ public class WxPayHelper { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<RefundqueryResultItem> refundOrderList = wxResult.getRefundqueryResultItems(); |
|
|
|
|
if (refundOrderList == null || refundOrderList.size() == 0) { |
|
|
|
|
if (refundOrderList == null || refundOrderList.isEmpty()) { |
|
|
|
|
return billList; |
|
|
|
|
} |
|
|
|
|
BigDecimal totalFee = new BigDecimal(wxResult.getTotal_fee()).movePointLeft(2); |
|
|
|
|
@ -763,7 +778,7 @@ public class WxPayHelper { |
|
|
|
|
order.setTotalFee(new BigDecimal(totalFee).movePointLeft(2)); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】查询订单-异常:", e); |
|
|
|
|
order.setErrorMsg("异常信息:" + e.getMessage()); |
|
|
|
|
} |
|
|
|
|
return order; |
|
|
|
|
@ -831,7 +846,7 @@ public class WxPayHelper { |
|
|
|
|
order.setTotalFee(new BigDecimal(totalFee).movePointLeft(2)); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】查询订单-异常:", e); |
|
|
|
|
order.setErrorMsg("异常信息:" + e.getMessage()); |
|
|
|
|
} |
|
|
|
|
return order; |
|
|
|
|
@ -1210,17 +1225,20 @@ public class WxPayHelper { |
|
|
|
|
public static WxNativePayResult createNativeOrder(String notifyUrl, String title, BigDecimal totalFee, String outTradeNo, String notifyType, String ip) { |
|
|
|
|
try { |
|
|
|
|
if (ObjectUtils.isEmpty(outTradeNo) || ObjectUtils.isEmpty(notifyUrl) || totalFee == null || notifyType == null) { |
|
|
|
|
log.info("Native下单参数缺失"); |
|
|
|
|
log.error("【微信】[Native下单]参数缺失"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 金额转换
|
|
|
|
|
String cents; |
|
|
|
|
int cents; |
|
|
|
|
try { |
|
|
|
|
cents = totalFee.movePointRight(2).toString(); // 单位分
|
|
|
|
|
cents = totalFee.movePointRight(2).intValueExact(); // 单位分
|
|
|
|
|
if (cents == 0) { |
|
|
|
|
log.error("【微信】[Native下单]-金额转换异常或金额为0"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.info("【微信】Native统一下单金额转换异常"); |
|
|
|
|
log.error("【微信】[Native下单]金额转换异常:", e); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1236,20 +1254,19 @@ public class WxPayHelper { |
|
|
|
|
unifiedorder.setOut_trade_no(outTradeNo); |
|
|
|
|
unifiedorder.setBody(title); |
|
|
|
|
|
|
|
|
|
unifiedorder.setTotal_fee(cents); // 金额分
|
|
|
|
|
unifiedorder.setTotal_fee(Integer.toString(cents)); // 金额分
|
|
|
|
|
unifiedorder.setSpbill_create_ip(ip); |
|
|
|
|
unifiedorder.setNotify_url(WeChatConfig.getBaseURL() + notifyUrl); |
|
|
|
|
unifiedorder.setTrade_type("NATIVE"); |
|
|
|
|
// 额外参数
|
|
|
|
|
unifiedorder.setAttach(notifyType); |
|
|
|
|
|
|
|
|
|
log.info("【微信】Native下单 {},{},{},{},{},{},{},{}", appId, mchId, nonce, title, outTradeNo, totalFee, ip, notifyUrl); |
|
|
|
|
log.info("【微信】[Native下单] {},{},{},{},{},{},{},{}", appId, mchId, nonce, title, outTradeNo, totalFee, ip, notifyUrl); |
|
|
|
|
|
|
|
|
|
// 微信统一下单
|
|
|
|
|
UnifiedorderResult unifiedorderResult = PayMchAPI.payUnifiedorder(unifiedorder, mchKey); |
|
|
|
|
log.info("【微信】Native统一下单返回:{}", JsonHelper.toJsonString(unifiedorderResult)); |
|
|
|
|
log.info("【微信】[Native下单]返回:{}", JsonHelper.toJsonString(unifiedorderResult)); |
|
|
|
|
if (unifiedorderResult == null) { |
|
|
|
|
log.info("【微信】Native统一下单失败"); |
|
|
|
|
log.error("【微信】[Native下单]失败"); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1257,13 +1274,14 @@ public class WxPayHelper { |
|
|
|
|
// 下单是否成功
|
|
|
|
|
Boolean sign_status = unifiedorderResult.getSign_status(); |
|
|
|
|
if (!SUCCESS.equals(result_code) || sign_status == null || !sign_status) { |
|
|
|
|
log.info("【微信】Native统一下单返回异常信息:sign_status={}, err_code=[ {} ], err_code_des=[ {} ]", sign_status, unifiedorderResult.getErr_code(), unifiedorderResult.getErr_code_des()); |
|
|
|
|
log.error("【微信】[Native下单]返回异常信息:sign_status={}, err_code=[ {} ], err_code_des=[ {} ]", sign_status, unifiedorderResult.getErr_code(), unifiedorderResult.getErr_code_des()); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
String codeUrl = unifiedorderResult.getCode_url(); |
|
|
|
|
String prepayId = unifiedorderResult.getPrepay_id(); |
|
|
|
|
log.info("微信】Native统一下单 返回 codeUrl={}, prepayId={}", codeUrl, prepayId); |
|
|
|
|
log.info("微信】[Native下单] codeUrl={}, prepayId={}", codeUrl, prepayId); |
|
|
|
|
if (ObjectUtils.isEmpty(codeUrl) || ObjectUtils.isEmpty(prepayId)) { |
|
|
|
|
log.error("微信】[Native下单]-失败:响应数据缺失 codeUrl={}, prepayId={}", codeUrl, prepayId); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
WxNativePayResult wxNativePayResult = new WxNativePayResult(); |
|
|
|
|
@ -1271,8 +1289,7 @@ public class WxPayHelper { |
|
|
|
|
wxNativePayResult.setPrepayId(prepayId); |
|
|
|
|
return wxNativePayResult; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("【微信】统一下单异常信息:{}", e.getMessage()); |
|
|
|
|
ErrorHelper.println(e); |
|
|
|
|
log.error("【微信】统一下单异常信息:", e); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|