diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index dbe23df1..d13d2737 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -1,40 +1,19 @@ package com.github.binarywang.wxpay.service; -import java.io.File; -import java.util.Date; -import java.util.Map; - import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryResult; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendResult; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult; +import com.github.binarywang.wxpay.bean.coupon.*; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult; -import com.github.binarywang.wxpay.bean.request.WxPayAuthcode2OpenidRequest; -import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest; -import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest; -import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; -import com.github.binarywang.wxpay.bean.request.WxPayReportRequest; -import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest; -import com.github.binarywang.wxpay.bean.request.WxPayShorturlRequest; -import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; -import com.github.binarywang.wxpay.bean.result.WxPayBillResult; -import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderReverseResult; -import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; -import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult; -import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; +import com.github.binarywang.wxpay.bean.request.*; +import com.github.binarywang.wxpay.bean.result.*; import com.github.binarywang.wxpay.config.WxPayConfig; import com.github.binarywang.wxpay.exception.WxPayException; +import java.io.File; +import java.util.Date; +import java.util.Map; + /** *
* 微信支付相关接口. @@ -98,6 +77,23 @@ public interface WxPayService { */ WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxPayException; + /** + *+ * 查询订单(适合于需要自定义子商户号和子商户appid的情形). + * 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 + * 该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。 + * 需要调用查询接口的情况: + * ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知; + * ◆ 调用支付接口后,返回系统错误或未知交易状态情况; + * ◆ 调用被扫支付API,返回USERPAYING的状态; + * ◆ 调用关单或撤销接口API之前,需确认支付状态; + * 接口地址:https://api.mch.weixin.qq.com/pay/orderquery + *+ * + * @param request 查询订单请求对象 + */ + WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException; + /** ** 关闭订单. @@ -114,6 +110,22 @@ public interface WxPayService { */ WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException; + /** + *+ * 关闭订单(适合于需要自定义子商户号和子商户appid的情形). + * 应用场景 + * 以下情况需要调用关单接口: + * 1. 商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付; + * 2. 系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。 + * 注意:订单生成后不能马上调用关单接口,最短调用时间间隔为5分钟。 + * 接口地址:https://api.mch.weixin.qq.com/pay/closeorder + * 是否需要证书: 不需要。 + *+ * + * @param request 关闭订单请求对象 + */ + WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException; + /** * 调用统一下单接口,并组装生成支付所需参数对象. * @@ -185,11 +197,19 @@ public interface WxPayService { throws WxPayException; /** - * @see WxPayService#parseOrderNotifyResult(String). - * @deprecated use {@link WxPayService#parseOrderNotifyResult(String)} instead + *+ * 微信支付-查询退款(适合于需要自定义子商户号和子商户appid的情形). + * 应用场景: + * 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,用零钱支付的退款20分钟内到账, + * 银行卡支付的退款3个工作日后重新查询退款状态。 + * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5 + * 接口链接:https://api.mch.weixin.qq.com/pay/refundquery + *+ * + * @param request 微信退款单号 + * @return 退款信息 */ - @Deprecated - WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException; + WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) throws WxPayException; /** * 解析支付结果通知. @@ -315,6 +335,24 @@ public interface WxPayService { */ WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxPayException; + /** + *+ * 下载对账单(适合于需要自定义子商户号和子商户appid的情形). + * 商户可以通过该接口下载历史交易清单。比如掉单、系统错误等导致商户侧和微信侧数据不一致,通过对账单核对后可校正支付状态。 + * 注意: + * 1、微信侧未成功下单的交易不会出现在对账单中。支付成功后撤销的交易会出现在对账单中,跟原支付单订单号一致,bill_type为REVOKED; + * 2、微信在次日9点启动生成前一天的对账单,建议商户10点后再获取; + * 3、对账单中涉及金额的字段单位为“元”。 + * 4、对账单接口只能下载三个月以内的账单。 + * 接口链接:https://api.mch.weixin.qq.com/pay/downloadbill + * 详情请见: 下载对账单 + *+ * + * @param request 下载对账单请求 + * @return 保存到本地的临时文件 + */ + WxPayBillResult downloadBill(WxPayDownloadBillRequest request) throws WxPayException; + /** ** 提交刷卡支付. diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index ef81f31b..5f0e8d79 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -1,69 +1,19 @@ package com.github.binarywang.wxpay.service.impl; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.zip.ZipException; - -import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.github.binarywang.utils.qrcode.QrcodeUtils; import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryResult; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendResult; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest; -import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult; +import com.github.binarywang.wxpay.bean.coupon.*; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult; import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult; import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; +import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult; import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult; -import com.github.binarywang.wxpay.bean.request.WxPayAuthcode2OpenidRequest; -import com.github.binarywang.wxpay.bean.request.WxPayDefaultRequest; -import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest; -import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest; -import com.github.binarywang.wxpay.bean.request.WxPayOrderCloseRequest; -import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest; -import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest; -import com.github.binarywang.wxpay.bean.request.WxPayQueryCommentRequest; -import com.github.binarywang.wxpay.bean.request.WxPayRedpackQueryRequest; -import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest; -import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; -import com.github.binarywang.wxpay.bean.request.WxPayReportRequest; -import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest; -import com.github.binarywang.wxpay.bean.request.WxPayShorturlRequest; -import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; -import com.github.binarywang.wxpay.bean.result.BaseWxPayResult; -import com.github.binarywang.wxpay.bean.result.WxPayAuthcode2OpenidResult; -import com.github.binarywang.wxpay.bean.result.WxPayBillBaseResult; -import com.github.binarywang.wxpay.bean.result.WxPayBillResult; -import com.github.binarywang.wxpay.bean.result.WxPayCommonResult; -import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayOrderReverseResult; -import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult; -import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; -import com.github.binarywang.wxpay.bean.result.WxPaySandboxSignKeyResult; -import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult; -import com.github.binarywang.wxpay.bean.result.WxPayShorturlResult; -import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; +import com.github.binarywang.wxpay.bean.request.*; +import com.github.binarywang.wxpay.bean.result.*; import com.github.binarywang.wxpay.config.WxPayConfig; +import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.constant.WxPayConstants.BillType; import com.github.binarywang.wxpay.constant.WxPayConstants.SignType; import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType; @@ -74,6 +24,18 @@ import com.github.binarywang.wxpay.util.SignUtils; import com.google.common.base.Joiner; import com.google.common.collect.Maps; import jodd.io.ZipUtil; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.zip.ZipException; import static com.github.binarywang.wxpay.constant.WxPayConstants.QUERY_COMMENT_DATE_FORMAT; import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType; @@ -145,6 +107,11 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { request.setOutRefundNo(StringUtils.trimToNull(outRefundNo)); request.setRefundId(StringUtils.trimToNull(refundId)); + return this.refundQuery(request); + } + + @Override + public WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/refundquery"; @@ -155,12 +122,6 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { return result; } - @Override - @Deprecated - public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException { - return this.parseOrderNotifyResult(xmlData); - } - @Override public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException { try { @@ -195,7 +156,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { public WxScanPayNotifyResult parseScanPayNotifyResult(String xmlData) throws WxPayException { try { log.debug("扫码支付回调通知请求参数:{}", xmlData); - WxScanPayNotifyResult result = BaseWxPayResult.fromXML(xmlData,WxScanPayNotifyResult.class); + WxScanPayNotifyResult result = BaseWxPayResult.fromXML(xmlData, WxScanPayNotifyResult.class); log.debug("扫码支付回调通知解析后的对象:{}", result); result.checkResult(this, this.getConfig().getSignType(), false); return result; @@ -243,6 +204,12 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { WxPayOrderQueryRequest request = new WxPayOrderQueryRequest(); request.setOutTradeNo(StringUtils.trimToNull(outTradeNo)); request.setTransactionId(StringUtils.trimToNull(transactionId)); + + return this.queryOrder(request); + } + + @Override + public WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/orderquery"; @@ -265,6 +232,12 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { WxPayOrderCloseRequest request = new WxPayOrderCloseRequest(); request.setOutTradeNo(StringUtils.trimToNull(outTradeNo)); + + return this.closeOrder(request); + } + + @Override + public WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/closeorder"; @@ -425,14 +398,15 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { params.put("appid", this.getConfig().getAppId()); params.put("mch_id", this.getConfig().getMchId()); params.put("product_id", productId); - params.put("time_stamp", String.valueOf(System.currentTimeMillis() / 1000));//这里需要秒,10位数字 + //这里需要秒,10位数字 + params.put("time_stamp", String.valueOf(System.currentTimeMillis() / 1000)); params.put("nonce_str", String.valueOf(System.currentTimeMillis())); String sign = SignUtils.createSign(params, null, this.getConfig().getMchKey(), false); params.put("sign", sign); for (String key : params.keySet()) { - codeUrl.append(key + "=" + params.get(key) + "&"); + codeUrl.append(key).append("=").append(params.get(key)).append("&"); } String content = codeUrl.toString().substring(0, codeUrl.length() - 1); @@ -475,12 +449,17 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { request.setTarType(tarType); request.setDeviceInfo(deviceInfo); + return this.downloadBill(request); + } + + @Override + public WxPayBillResult downloadBill(WxPayDownloadBillRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/downloadbill"; String responseContent; - if (TarType.GZIP.equals(tarType)) { + if (TarType.GZIP.equals(request.getTarType())) { responseContent = this.handleGzipBill(url, request.toXML()); } else { responseContent = this.post(url, request.toXML(), false); @@ -489,7 +468,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { } } - return this.handleBill(billType, responseContent); + return this.handleBill(request.getBillType(), responseContent); } private WxPayBillResult handleBill(String billType, String responseContent) { @@ -517,9 +496,10 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { this.log.error("解压zip文件出错", e); } } - } catch (IOException e) { - e.printStackTrace(); + } catch (Exception e) { + this.log.error("解析对账单文件时出错",e); } + return null; }