企业付款相关接口抽取为一个独立的接口实现类

This commit is contained in:
Binary Wang
2017-12-19 23:07:26 +08:00
parent 42d5396e10
commit a361487507
21 changed files with 827 additions and 449 deletions

View File

@@ -0,0 +1,45 @@
package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.request.WxEntPayRequest;
import com.github.binarywang.wxpay.bean.result.WxEntPayQueryResult;
import com.github.binarywang.wxpay.bean.result.WxEntPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
/**
* <pre>
* 企业付款相关服务类
* Created by BinaryWang on 2017/12/19.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface EntPaySerivce {
/**
* <pre>
* 企业付款业务是基于微信支付商户平台的资金管理能力为了协助商户方便地实现企业向个人付款针对部分有开发能力的商户提供通过API完成企业付款的功能。
* 比如目前的保险行业向客户退保、给付、理赔。
* 企业付款将使用商户的可用余额需确保可用余额充足。查看可用余额、充值、提现请登录商户平台“资金管理”https://pay.weixin.qq.com/进行操作。
* 注意:与商户微信支付收款资金并非同一账户,需要单独充值。
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
* 接口链接https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
* </pre>
*
* @param request 请求对象
*/
EntPayResult entPay(EntPayRequest request) throws WxPayException;
/**
* <pre>
* 查询企业付款API
* 用于商户的企业付款操作进行结果查询,返回付款操作详细结果。
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
* 接口链接https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo
* </pre>
*
* @param partnerTradeNo 商户订单号
*/
EntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException;
}

View File

@@ -2,6 +2,9 @@ package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.request.*;
@@ -15,7 +18,7 @@ import java.util.Map;
/**
* <pre>
* 微信支付相关接口
* 微信支付相关接口.
* Created by Binary Wang on 2016/7/28.
* </pre>
*
@@ -23,6 +26,41 @@ import java.util.Map;
*/
public interface WxPayService {
/**
* 获取微信支付请求url前缀沙箱环境可能不一样
*/
String getPayBaseUrl();
/**
* 发送post请求得到响应字节数组
*
* @param url 请求地址
* @param requestStr 请求信息
* @param useKey 是否使用证书
* @return 返回请求结果字节数组
*/
byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException;
/**
* 发送post请求得到响应字符串
*
* @param url 请求地址
* @param requestStr 请求信息
* @param useKey 是否使用证书
* @return 返回请求结果字符串
*/
String post(String url, String requestStr, boolean useKey) throws WxPayException;
/**
* 获取企业付款服务类
*/
EntPaySerivce getEntPaySerivce();
/**
* 设置企业付款服务类,允许开发者自定义实现类
*/
void setEntPaySerivce(EntPaySerivce entPaySerivce);
/**
* <pre>
* 查询订单(详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2)
@@ -173,29 +211,15 @@ public interface WxPayService {
WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException;
/**
* <pre>
* 企业付款业务是基于微信支付商户平台的资金管理能力为了协助商户方便地实现企业向个人付款针对部分有开发能力的商户提供通过API完成企业付款的功能。
* 比如目前的保险行业向客户退保、给付、理赔。
* 企业付款将使用商户的可用余额需确保可用余额充足。查看可用余额、充值、提现请登录商户平台“资金管理”https://pay.weixin.qq.com/进行操作。
* 注意:与商户微信支付收款资金并非同一账户,需要单独充值。
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
* 接口链接https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers
* </pre>
*
* @param request 请求对象
* 请使用this.getEntPayService().entPay()方法{@link EntPaySerivce#entPay(EntPayRequest)}
*/
@Deprecated
WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException;
/**
* <pre>
* 查询企业付款API
* 用于商户的企业付款操作进行结果查询,返回付款操作详细结果。
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3
* 接口链接https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo
* </pre>
*
* @param partnerTradeNo 商户订单号
* 请使用this.getEntPayService().queryEntPay()方法 {@link EntPaySerivce#queryEntPay(String)}
*/
@Deprecated
WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException;
/**

View File

@@ -3,6 +3,8 @@ package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
@@ -15,6 +17,7 @@ import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPaySerivce;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.SignUtils;
import com.google.common.base.Joiner;
@@ -49,8 +52,20 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected static ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
private EntPaySerivce entPaySerivce = new EntPayServiceImpl(this);
protected WxPayConfig config;
@Override
public EntPaySerivce getEntPaySerivce() {
return entPaySerivce;
}
@Override
public void setEntPaySerivce(EntPaySerivce entPaySerivce) {
this.entPaySerivce = entPaySerivce;
}
@Override
public WxPayConfig getConfig() {
return this.config;
@@ -61,7 +76,8 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
this.config = config;
}
private String getPayBaseUrl() {
@Override
public String getPayBaseUrl() {
if (this.getConfig().useSandbox()) {
return PAY_BASE_URL + "/sandboxnew";
}
@@ -69,26 +85,6 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return PAY_BASE_URL;
}
/**
* 发送post请求得到响应字节数组
*
* @param url 请求地址
* @param requestStr 请求信息
* @param useKey 是否使用证书
* @return 返回请求结果字节数组
*/
protected abstract byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException;
/**
* 发送post请求得到响应字符串
*
* @param url 请求地址
* @param requestStr 请求信息
* @param useKey 是否使用证书
* @return 返回请求结果字符串
*/
protected abstract String post(String url, String requestStr, boolean useKey) throws WxPayException;
@Override
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException {
request.checkAndSign(this.getConfig(), false);
@@ -352,27 +348,15 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
}
@Override
@Deprecated
public WxEntPayResult entPay(WxEntPayRequest request) throws WxPayException {
request.checkAndSign(this.getConfig(), false);
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
String responseContent = this.post(url, request.toXML(), true);
WxEntPayResult result = BaseWxPayResult.fromXML(responseContent, WxEntPayResult.class);
result.checkResult(this, request.getSignType(), true);
return result;
return WxEntPayResult.createFrom(this.getEntPaySerivce().entPay(request));
}
@Override
@Deprecated
public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException {
WxEntPayQueryRequest request = new WxEntPayQueryRequest();
request.setPartnerTradeNo(partnerTradeNo);
request.checkAndSign(this.getConfig(), false);
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
String responseContent = this.post(url, request.toXML(), true);
WxEntPayQueryResult result = BaseWxPayResult.fromXML(responseContent, WxEntPayQueryResult.class);
result.checkResult(this, request.getSignType(), true);
return result;
return WxEntPayQueryResult.createFrom(this.getEntPaySerivce().queryEntPay(partnerTradeNo));
}
@Override

View File

@@ -0,0 +1,50 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPaySerivce;
import com.github.binarywang.wxpay.service.WxPayService;
/**
* <pre>
* Created by BinaryWang on 2017/12/19.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class EntPayServiceImpl implements EntPaySerivce {
private WxPayService payService;
public EntPayServiceImpl(WxPayService payService) {
this.payService = payService;
}
@Override
public EntPayResult entPay(EntPayRequest request) throws WxPayException {
request.checkAndSign(this.payService.getConfig(), false);
String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
String responseContent = this.payService.post(url, request.toXML(), true);
EntPayResult result = BaseWxPayResult.fromXML(responseContent, EntPayResult.class);
result.checkResult(this.payService, request.getSignType(), true);
return result;
}
@Override
public EntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException {
EntPayQueryRequest request = new EntPayQueryRequest();
request.setPartnerTradeNo(partnerTradeNo);
request.checkAndSign(this.payService.getConfig(), false);
String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
String responseContent = this.payService.post(url, request.toXML(), true);
EntPayQueryResult result = BaseWxPayResult.fromXML(responseContent, EntPayQueryResult.class);
result.checkResult(this.payService, request.getSignType(), true);
return result;
}
}

View File

@@ -32,9 +32,8 @@ import java.nio.charset.StandardCharsets;
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
@Override
protected byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
try {
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr);
@@ -57,7 +56,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
}
@Override
protected String post(String url, String requestStr, boolean useKey) throws WxPayException {
public String post(String url, String requestStr, boolean useKey) throws WxPayException {
try {
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr);

View File

@@ -22,9 +22,8 @@ import java.nio.charset.StandardCharsets;
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
@Override
protected byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
public byte[] postForBytes(String url, String requestStr, boolean useKey) throws WxPayException {
try {
HttpRequest request = this.buildHttpRequest(url, requestStr, useKey);
byte[] responseBytes = request.send().bodyBytes();
@@ -40,7 +39,7 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
}
@Override
protected String post(String url, String requestStr, boolean useKey) throws WxPayException {
public String post(String url, String requestStr, boolean useKey) throws WxPayException {
try {
HttpRequest request = this.buildHttpRequest(url, requestStr, useKey);
String responseString = this.getResponseString(request.send());