1.在pom文件里面添加依赖
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
2.重写config---关于config中的各种id、参数等,需要自行去微信后台上去找和设置
import com.github.wxpay.sdk.WXPayConfig;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class MyConfig implements WXPayConfig {
private byte[] certData;
public MyConfig() throws Exception {
//证书
String certPath = "D:\\svn\\quickpassroot\\quick-pass-service\\quick-pass-core\\src\\main\\resources\\file\\apiclient_cert.p12";
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
//appid
public String getAppID() {
return "*******";
}
//商户号
public String getMchID() {
return "******";
}
//key
public String getKey() {
return "*******";
}
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
public int getHttpConnectTimeoutMs() {
return 8000;
}
public int getHttpReadTimeoutMs() {
return 10000;
}
}
3.DO
(1)对账单
public class WeChatBillDO implements Serializable {
private Integer id;
/**
* 交易时间
*/
private String tradeTime;
/**
* 公众账号ID
*/
private String appId;
/**
* 商户号
*/
private String mchId;
/**
* 子商户号
*/
private String mchAppId;
/**
* 设备号
*/
private String deviceInfo;
/**
* 微信订单号
*/
private String transactionId;
/**
* 商户订单号
*/
private String outTradeNo;
/**
* 用户标识
*/
private String openId;
/**
* 交易类型
*/
private String tradeType;
/**
* 交易状态
*/
private String tradeStatus;
/**
* 付款银行
*/
private String payBank;
/**
* 货币种类
*/
private String moneyType;
/**
* 应结订单金额
*/
private String orderPay;
/**
* 代金劵金额
*/
private String voucherAmount;
/**
* 微信退款单号
*/
private String refundNumber;
/**
* 商户退款单号
*/
private String outRefundNo;
/**
* 退款金额
*/
private String refundAmount;
/**
* 充值劵退款金额
*/
private String refundAmountVoucher;
/**
* 退款类型
*/
private String refundsType;
/**
* 退款状态
*/
private String refundsStatus;
/**
* 商品名称
*/
private String commodityName;
/**
* 商户数据包
*/
private String dataPacket;
/**
* 手续费
*/
private String serviceCharge;
/**
* 费率
*/
private String rate;
/**
* 订单金额
*/
private String orderAmount;
/**
* 申请退款金额
*/
private String applicationRefundAmount;
/**
* 费率备注
*/
private String rateNotes;
(2)资金账单
public class WeChatCapitalBillDO implements Serializable {
private Integer id;
/**
* 记账时间
*/
private String tradeTime;
/**
* 微信支付业务单号
*/
private String paymentNumber;
/**
* 资金流水单号
*/
private String flowNumber;
/**
* 业务名称
*/
private String businessName;
/**
* 业务类型
*/
private String businessType;
/**
* 收支类型
*/
private String inoutType;
/**
* 收支金额(元)
*/
private String inoutMoney;
/**
* 帐户结余(元)
*/
private String accountBalance;
/**
* 资金变更提交申请人
*/
private String applicant;
/**
* 备注
*/
private String remarks;
/**
* 业务凭证号
*/
private String voucherNumber;
4.下载资金账单,这个maven中没有提供方法,但是微信官方api文档中写了有,所以,重写了maven中的WXPay.java(就是复制一份到自己想买的目录中),添加下载资金账单的方法
public Map<String, String> downloadfundflow(Map<String, String> reqData) throws Exception {
return this.downloadfundflow(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
}
public Map<String, String> downloadfundflow(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
String url;
if (this.useSandbox) {
url = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadfundflow";
} else {
url = "https://api.mch.weixin.qq.com/pay/downloadfundflow";
}
String respStr = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs).trim();
Object ret;
if (respStr.indexOf("<") == 0) {
ret = WXPayUtil.xmlToMap(respStr);
} else {
ret = new HashMap();
((Map)ret).put("return_code", "SUCCESS");
((Map)ret).put("return_msg", "ok");
((Map)ret).put("data", respStr);
}
return (Map)ret;
}
5.下载对账单及资金账单
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.weein.quickpass.base.entity.WeChatBillDO;
import com.weein.quickpass.base.entity.WeChatCapitalBillDO;
import com.weein.quickpass.core.config.MyConfig;
import com.weein.wcommon.utils.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.github.wxpay.sdk.WXPayConstants.SignType.HMACSHA256;
/**
* @author hzt
* @describe 下载微信账单
*/
public class DownloadWeChatBill {
//main方法,用来测试
public static void main(String[] args) throws Exception {
//List<WeChatBillDO> list = downloadBill();
List<WeChatCapitalBillDO> list = downloadCapitalBill();
System.out.println(list);
}
/**
* 下载微信对账单
*
* @return
* @throws Exception
*/
public static List<WeChatBillDO> downloadBill() throws Exception {
List<WeChatBillDO> list = Lists.newArrayList();
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, HMACSHA256);
Map<String, String> data = new HashMap<String, String>();
//下载对账单的日期,格式:20140603,当前日期前一天。
String billDate = DateUtils.format(DateUtils.addDay(new Date(), -1), DateUtils.FORMAT_DATE_02);
data.put("bill_date", billDate);
//data.put("bill_date", "20190329");
data.put("bill_type", "ALL");
Map<String, String> resp = wxpay.downloadBill(data);
String result = resp.get("data");
//查询当日没有账单时
if (Strings.isNullOrEmpty(result)) {
return list;
}
int i = result.indexOf("`");
int j = result.indexOf("总");
String substring = result.substring(i, j - 2);
String[] temp = substring.split(",``");
for (int k = 0; k < temp.length; k++) {
String[] payment = temp[k].replace("`", "").split(",");
WeChatBillDO weChatBillDO = new WeChatBillDO();
weChatBillDO.setTradeTime(payment[0]);
weChatBillDO.setAppId(payment[1]);
weChatBillDO.setMchId(payment[2]);
weChatBillDO.setMchAppId(payment[3]);
weChatBillDO.setDeviceInfo(payment[4]);
weChatBillDO.setTransactionId(payment[5]);
weChatBillDO.setOutTradeNo(payment[6]);
weChatBillDO.setOpenId(payment[7]);
weChatBillDO.setTradeType(payment[8]);
weChatBillDO.setTradeStatus(payment[9]);
weChatBillDO.setPayBank(payment[10]);
weChatBillDO.setMoneyType(payment[11]);
weChatBillDO.setOrderPay(payment[12]);
weChatBillDO.setVoucherAmount(payment[13]);
weChatBillDO.setRefundNumber(payment[14]);
weChatBillDO.setOutRefundNo(payment[15]);
weChatBillDO.setRefundAmount(payment[16]);
weChatBillDO.setRefundAmountVoucher(payment[17]);
weChatBillDO.setRefundsType(payment[18]);
weChatBillDO.setRefundsStatus(payment[19]);
weChatBillDO.setCommodityName(payment[20]);
weChatBillDO.setDataPacket(payment[21]);
weChatBillDO.setServiceCharge(payment[22]);
weChatBillDO.setRate(payment[23]);
weChatBillDO.setOrderAmount(payment[24]);
weChatBillDO.setApplicationRefundAmount(payment[25]);
list.add(weChatBillDO);
}
return list;
}
/**
* 下载微信支付资金账单
*
* @return
* @throws Exception
*/
public static List<WeChatCapitalBillDO> downloadCapitalBill() throws Exception {
List<WeChatCapitalBillDO> list = Lists.newArrayList();
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, HMACSHA256);
Map<String, String> data = new HashMap<String, String>();
//下载账单的日期,格式:20140603,当前日期前一天。
//String billDate = DateUtils.format(DateUtils.addDay(new Date(), -1), DateUtils.FORMAT_DATE_02);
//data.put("bill_date", billDate);
data.put("bill_date", "20190329");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(data.get("bill_date"));
int year = date.getYear() + 1900;
//账单的资金来源账户account_type:Basic 基本账户/Operation 运营账户/Fees 手续费账户
data.put("account_type", "Basic");
Map<String, String> resp = wxpay.downloadfundflow(data);
String result = resp.get("data");
//查询当日没有账单时
if (Strings.isNullOrEmpty(result)) {
return list;
}
int i = result.indexOf("`");
int j = result.indexOf("资金流水总笔数");
String substring = result.substring(i, j);
//方法比较简单暴力,通过年份(比如2018 + `) 去切割,最后再把年份拼接上去
String[] temp = substring.split("`" + year);
for (int m = 1; m < temp.length; m++) {
String[] payment = temp[m].replace("`", "").split(",");
WeChatCapitalBillDO weChatCapitalBillDO = new WeChatCapitalBillDO();
weChatCapitalBillDO.setTradeTime(year + payment[0]);
weChatCapitalBillDO.setPaymentNumber(payment[1]);
weChatCapitalBillDO.setFlowNumber(payment[2]);
weChatCapitalBillDO.setBusinessName(payment[3]);
weChatCapitalBillDO.setBusinessType(payment[4]);
weChatCapitalBillDO.setInoutType(payment[5]);
weChatCapitalBillDO.setInoutMoney(payment[6]);
weChatCapitalBillDO.setAccountBalance(payment[7]);
weChatCapitalBillDO.setApplicant(payment[8]);
weChatCapitalBillDO.setRemarks(payment[9]);
if (payment.length == 10) {
if (payment[9].equals("system")) {
weChatCapitalBillDO.setVoucherNumber("");
}
} else if (payment.length == 11) {
weChatCapitalBillDO.setVoucherNumber(payment[10]);
}
list.add(weChatCapitalBillDO);
}
return list;
}
}
5.将得到的数据定时存入数据库中
/**
* 生成下载微信对账单并存入数据库任务
*/
@Scheduled(cron = "0/10 * * * * ?")
public void downloadWeChatBill() {
weChatBillService.saveWeChatBill();
}
/**
* 生成下载微信资金账单并存入数据库任务
*/
@Scheduled(cron = "0/10 * * * * ?")
public void downloadFundFlow() {
weChatCapitalBillService.saveFundFlow();
}
6.service
public interface IWeChatBillService {
int saveWeChatBill();
}
public interface IWeChatCapitalBillService {
int saveFundFlow();
}
7.实现,两个账单存入数据库的实现一样,就放一个
@Service(version = DubboConstant.DUBBO_SERVICE_VERSION)
@Component
public class WeChatCapitalBillServiceImpl implements IWeChatCapitalBillService {
@Resource
IWeChatCapitalBillMapper weChatCapitalBillMapper;
@Override
public int saveFundFlow() {
int x = 0;
List<WeChatCapitalBillDO> list = null;
try {
list = DownloadWeChatBill.downloadCapitalBill();
} catch (Exception e) {
e.printStackTrace();
}
if (CollectionUtils.isNotEmpty(list)) {
for (WeChatCapitalBillDO weChatCapitalBillDO : list) {
weChatCapitalBillMapper.insertSelective(weChatCapitalBillDO);
x++;
}
}
return x;
}
}