mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-18 06:06:23 +08:00
🆕 #1789 微信支付电商收付通增加下载账单的接口
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package com.github.binarywang.wxpay.service;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.*;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.BillTypeEnum;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.TradeTypeEnum;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 电商收付通相关服务类.
|
||||
@@ -360,4 +363,29 @@ public interface EcommerceService {
|
||||
*/
|
||||
SettlementResult querySettlement(String subMchid) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 请求账单API
|
||||
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/bill.shtml
|
||||
* </pre>
|
||||
*
|
||||
* @param billType 账单类型。
|
||||
* @param request 二级商户号。
|
||||
* @return 返回数据 return bill result
|
||||
* @throws WxPayException the wx pay exception
|
||||
*/
|
||||
BillResult applyBill(BillTypeEnum billType, BillRequest request) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 下载账单API
|
||||
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/bill.shtml
|
||||
* </pre>
|
||||
*
|
||||
* @param url 微信返回的账单地址。
|
||||
* @return 返回数据 return inputStream
|
||||
* @throws WxPayException the wx pay exception
|
||||
*/
|
||||
InputStream downloadBill(String url) throws WxPayException;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
@@ -97,6 +98,15 @@ public interface WxPayService {
|
||||
*/
|
||||
String getV3(URI url) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 发送下载 V3请求,得到响应流.
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return 返回请求响应流
|
||||
* @throws WxPayException the wx pay exception
|
||||
*/
|
||||
InputStream downloadV3(URI url) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 获取企业付款服务类.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.binarywang.wxpay.service.impl;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.*;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.BillTypeEnum;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum;
|
||||
import com.github.binarywang.wxpay.bean.ecommerce.enums.TradeTypeEnum;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
@@ -8,15 +9,21 @@ import com.github.binarywang.wxpay.service.EcommerceService;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.github.binarywang.wxpay.v3.util.AesUtils;
|
||||
import com.github.binarywang.wxpay.v3.util.RsaCryptoUtil;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.beanutils.BeanMap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class EcommerceServiceImpl implements EcommerceService {
|
||||
@@ -273,6 +280,18 @@ public class EcommerceServiceImpl implements EcommerceService {
|
||||
return GSON.fromJson(response, SettlementResult.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BillResult applyBill(BillTypeEnum billType, BillRequest request) throws WxPayException {
|
||||
String url = String.format(billType.getUrl(), this.payService.getPayBaseUrl(), this.parseURLPair(request));
|
||||
String response = this.payService.getV3(URI.create(url));
|
||||
return GSON.fromJson(response, BillResult.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downloadBill(String url) throws WxPayException {
|
||||
return this.payService.downloadV3(URI.create(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验通知签名
|
||||
* @param header 通知头信息
|
||||
@@ -287,4 +306,24 @@ public class EcommerceServiceImpl implements EcommerceService {
|
||||
return payService.getConfig().getVerifier().verify(header.getSerialNo(),
|
||||
beforeSign.getBytes(StandardCharsets.UTF_8), header.getSigned());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象拼接到url
|
||||
* @param o 转换对象
|
||||
* @return 拼接好的string
|
||||
*/
|
||||
private String parseURLPair(Object o) {
|
||||
Map<Object, Object> map = new BeanMap(o);
|
||||
Set<Map.Entry<Object, Object>> set = map.entrySet();
|
||||
Iterator<Map.Entry<Object, Object>> it = set.iterator();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Object, Object> e = it.next();
|
||||
if ( !"class".equals(e.getKey()) && e.getValue() != null)
|
||||
sb.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, String.valueOf(e.getKey()))).append("=").append(e.getValue()).append("&");
|
||||
}
|
||||
return sb.deleteCharAt(sb.length() - 1).toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@@ -207,6 +208,31 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downloadV3(URI url) throws WxPayException {
|
||||
CloseableHttpClient httpClient = this.createApiV3HttpClient();
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.addHeader("Accept", ContentType.WILDCARD.getMimeType());
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
//v3已经改为通过状态码判断200 204 成功
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
|
||||
this.log.info("\n【请求地址】:{}\n", url);
|
||||
return response.getEntity().getContent();
|
||||
} else {
|
||||
//有错误提示信息返回
|
||||
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
JsonObject jsonObject = GsonParser.parse(responseString);
|
||||
throw new WxPayException(jsonObject.get("message").getAsString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.log.error("\n【请求地址】:{}\n【异常信息】:{}", url, e.getMessage());
|
||||
throw new WxPayException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
|
||||
CloseableHttpClient apiV3HttpClient = this.getConfig().getApiV3HttpClient();
|
||||
if (null == apiV3HttpClient) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@@ -80,6 +81,11 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream downloadV3(URI url) throws WxPayException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private HttpRequest buildHttpRequest(String url, String requestStr, boolean useKey) throws WxPayException {
|
||||
HttpRequest request = HttpRequest
|
||||
.post(url)
|
||||
|
||||
Reference in New Issue
Block a user