|
@@ -32,8 +32,6 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.BufferedWriter;
|
|
|
-import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -41,7 +39,6 @@ import java.security.PrivateKey;
|
|
|
import java.security.Signature;
|
|
|
import java.util.Base64;
|
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
@@ -274,134 +271,6 @@ public class WxProviderServiceImpl implements WxProviderService {
|
|
|
return resMap;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 申请交易账单
|
|
|
- *
|
|
|
- * @param billDate 格式yyyy-MM-dd 仅支持三个月内的账单下载申请 ,如果传入日期未为当天则会出错
|
|
|
- * @param billType 分为:ALL、SUCCESS、REFUND
|
|
|
- * ALL:返回当日所有订单信息(不含充值退款订单)
|
|
|
- * SUCCESS:返回当日成功支付的订单(不含充值退款订单)
|
|
|
- * REFUND:返回当日退款订单(不含充值退款订单)
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- public String tradeBill(String billDate, String billType) {
|
|
|
- log.info("申请交易账单,billDate:{},billType:{}", billDate, billType);
|
|
|
- String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.TRADE_BILLS.getType())
|
|
|
- .concat("?bill_date=").concat(billDate).concat("&bill_type=").concat(billType);
|
|
|
- // 填则默认返回服务商下的交易或退款数据,下载某个子商户下的交易或退款数据,则该字段必填
|
|
|
- url = url.concat("&sub_mchid=").concat(wechatPayServiceConfig.getSubAppId());
|
|
|
- String res = wechatHttpGet(url);
|
|
|
- log.info("查询退款订单结果:{}", res);
|
|
|
- Map<String, Object> resMap = JSONObject.parseObject(res, new TypeReference<Map<String, Object>>() {
|
|
|
- });
|
|
|
- return resMap.get("download_url").toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param billDate 格式yyyy-MM-dd 仅支持三个月内的账单下载申请,如果传入日期未为当天则会出错
|
|
|
- * @param accountType 分为:BASIC、OPERATION、FEES
|
|
|
- * BASIC:基本账户
|
|
|
- * OPERATION:运营账户
|
|
|
- * FEES:手续费账户
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- public String fundFlowBill(String billDate, String accountType) {
|
|
|
- log.info("申请交易账单,billDate:{},accountType:{}", billDate, accountType);
|
|
|
- String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.FUND_FLOW_BILLS.getType())
|
|
|
- .concat("?bill_date=").concat(billDate).concat("&account_type=").concat(accountType);
|
|
|
- String res = wechatHttpGet(url);
|
|
|
- log.info("查询退款订单结果:{}", res);
|
|
|
- Map<String, Object> resMap = JSONObject.parseObject(res, new TypeReference<Map<String, Object>>() {
|
|
|
- });
|
|
|
- return resMap.get("download_url").toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param billDate 格式yyyy-MM-dd 仅支持三个月内的账单下载申请,如果传入日期未为当天则会出错
|
|
|
- * @param accountType 分为:BASIC、OPERATION、FEES
|
|
|
- * BASIC:基本账户
|
|
|
- * OPERATION:运营账户
|
|
|
- * FEES:手续费账户
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- public String subMerchantFundFlowBill(String billDate, String accountType) {
|
|
|
- log.info("申请单个子商户资金账单,billDate:{},accountType:{}", billDate, accountType);
|
|
|
- String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.FUND_FLOW_BILLS.getType())
|
|
|
- .concat("?bill_date=").concat(billDate).concat("&account_type=").concat(accountType)
|
|
|
- .concat("&sub_mchid=").concat(billDate).concat("&algorithm=").concat("AEAD_AES_256_GCM");
|
|
|
- String res = wechatHttpGet(url);
|
|
|
- log.info("查询退款订单结果:{}", res);
|
|
|
- Map<String, Object> resMap = JSONObject.parseObject(res, new TypeReference<Map<String, Object>>() {
|
|
|
- });
|
|
|
- String downloadBillCount = resMap.get("download_bill_count").toString();
|
|
|
- String downloadBillList = resMap.get("download_bill_list").toString();
|
|
|
- List<Map<String, Object>> billListMap = JSONObject.parseObject(downloadBillList, new TypeReference<List<Map<String, Object>>>() {
|
|
|
- });
|
|
|
- String downloadUrl = billListMap.get(0).get("download_url").toString();
|
|
|
- log.info("downloadBillCount=" + downloadBillCount);
|
|
|
- log.info("downloadUrl=" + downloadUrl);
|
|
|
- return downloadUrl;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载账单
|
|
|
- *
|
|
|
- * @param downloadUrl downloadUrl
|
|
|
- */
|
|
|
- public void downloadBill(String downloadUrl) {
|
|
|
- log.info("下载账单,下载地址:{}", downloadUrl);
|
|
|
- HttpGet httpGet = new HttpGet(downloadUrl);
|
|
|
- httpGet.addHeader("Accept", "application/json");
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- try {
|
|
|
- response = noSignHttpClient().execute(httpGet);
|
|
|
- String body = EntityUtils.toString(response.getEntity());
|
|
|
- int statusCode = response.getStatusLine().getStatusCode();
|
|
|
- if (statusCode == 200 || statusCode == 204) {
|
|
|
- log.info("下载账单,返回结果 = " + body);
|
|
|
- } else {
|
|
|
- throw new RuntimeException("下载账单异常, 响应码 = " + statusCode + ", 下载账单返回结果 = " + body);
|
|
|
- }
|
|
|
- // TODO 将body内容转为excel存入本地或者输出到浏览器,演示存入本地
|
|
|
- writeStringToFile(body);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e.getMessage());
|
|
|
- } finally {
|
|
|
- if (response != null) {
|
|
|
- try {
|
|
|
- response.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 文件写入
|
|
|
- *
|
|
|
- * @param body body
|
|
|
- */
|
|
|
- private void writeStringToFile(String body) {
|
|
|
- FileWriter fw = null;
|
|
|
- try {
|
|
|
- String filePath = "C:\\Users\\lhz12\\Desktop\\wxPay.txt";
|
|
|
- fw = new FileWriter(filePath, true);
|
|
|
- BufferedWriter bw = new BufferedWriter(fw);
|
|
|
- bw.write(body);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (fw != null) {
|
|
|
- fw.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取商户的私钥文件
|
|
|
*
|
|
@@ -450,22 +319,6 @@ public class WxProviderServiceImpl implements WxProviderService {
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取HttpClient,无需进行应答签名验证,跳过验签的流程
|
|
|
- */
|
|
|
- public CloseableHttpClient noSignHttpClient() {
|
|
|
- //获取商户私钥
|
|
|
- PrivateKey privateKey = getPrivateKey(wechatPayServiceConfig.getKeyPemPath());
|
|
|
- //用于构造HttpClient
|
|
|
- WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
|
|
|
- //设置商户信息
|
|
|
- .withMerchant(wechatPayServiceConfig.getSpMchId(), wechatPayServiceConfig.getSerialNo(), privateKey)
|
|
|
- //无需进行签名验证、通过withValidator((response) -> true)实现
|
|
|
- .withValidator((response) -> true);
|
|
|
- // 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 发送get请求
|
|
|
*
|