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庶近、參數(shù)等,需要自行去微信后臺上去找和設置
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;
/**
* 交易狀態(tài)
*/
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;
/**
* 退款狀態(tài)
*/
private String refundsStatus;
/**
* 商品名稱
*/
private String commodityName;
/**
* 商戶數(shù)據(jù)包
*/
private String dataPacket;
/**
* 手續(xù)費
*/
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;
/**
* 微信支付業(yè)務單號
*/
private String paymentNumber;
/**
* 資金流水單號
*/
private String flowNumber;
/**
* 業(yè)務名稱
*/
private String businessName;
/**
* 業(yè)務類型
*/
private String businessType;
/**
* 收支類型
*/
private String inoutType;
/**
* 收支金額(元)
*/
private String inoutMoney;
/**
* 帳戶結余(元)
*/
private String accountBalance;
/**
* 資金變更提交申請人
*/
private String applicant;
/**
* 備注
*/
private String remarks;
/**
* 業(yè)務憑證號
*/
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 手續(xù)費賬戶
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("資金流水總筆數(shù)");
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.將得到的數(shù)據(jù)定時存入數(shù)據(jù)庫中
/**
* 生成下載微信對賬單并存入數(shù)據(jù)庫任務
*/
@Scheduled(cron = "0/10 * * * * ?")
public void downloadWeChatBill() {
weChatBillService.saveWeChatBill();
}
/**
* 生成下載微信資金賬單并存入數(shù)據(jù)庫任務
*/
@Scheduled(cron = "0/10 * * * * ?")
public void downloadFundFlow() {
weChatCapitalBillService.saveFundFlow();
}
6.service
public interface IWeChatBillService {
int saveWeChatBill();
}
public interface IWeChatCapitalBillService {
int saveFundFlow();
}
7.實現(xiàn),兩個賬單存入數(shù)據(jù)庫的實現(xiàn)一樣肤京,就放一個
@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;
}
}