微信官方給的sdk有點(diǎn)問題,通過百度找到了maven依賴,這個比較靠譜
添加依賴
<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>
重寫config
public class MyConfig implements WXPayConfig {
private byte[] certData;
public MyConfig() throws Exception {
String certPath = "F:\\work\\WxPayAPI_JAVA\\java_sdk_v3.0.9\\src\\main\\resource\\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();
}
public String getAppID() {
return "wx888888888";
}
public String getMchID() {
return "1234567897";
}
public String getKey() {
return "00000000000000000000000";
}
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
public int getHttpConnectTimeoutMs() {
return 8000;
}
public int getHttpReadTimeoutMs() {
return 10000;
}
}
創(chuàng)建測試文件 WXPayExample.java
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, HMACSHA256);
Map<String, String> data = new HashMap<String, String>();
data.put("bill_date", "20181117");
data.put("bill_type", "ALL");
對賬單數(shù)據(jù)
請求下載對賬單的api方法
Map<String, String> resp = wxpay.downloadBill(data);
String s = resp.get("data");
對于數(shù)據(jù)返回的 比較麻煩,所以用bean對其進(jìn)行處理,方便裝換成我們需要的數(shù)據(jù)類型
接收的數(shù)據(jù)bean
public class PaymentPo
//交易時間
private String trade_time;
// 公眾賬號ID
private String appid;
//商戶號
private String mch_id;
//特約商戶號
private String mch_appid;
// 設(shè)備號
private String device_info;
//微信訂單號
private String transaction_id;
// 商戶訂單號
private String out_trade_no;
// 用戶標(biāo)識
private String openid;
// 交易類型
private String trade_type;
//交易狀態(tài)
private String trade_status;
//付款銀行
private String pay_bank;
//貨幣種類
private String money_type;
//應(yīng)結(jié)訂單金額
private String order_pay;
//代金券金額
private String voucher_amount;
//微信退款單號
private String refund_number;
//商戶退款單號
private String out_refund_no;
//退款金額
private String refund_amount;
//充值券退款金額
private String refund_amount_voucher;
//退款類型
private String refunds_type;
//退款狀態(tài)
private String refunds_status;
//商品名稱
private String commodity_name;
//商戶數(shù)據(jù)包
private String data_packet;
//手續(xù)費(fèi)
private String service_charge;
//費(fèi)率
private String rate;
//訂單金額
private String order_amount;
//申請退款金額
private String application_refund_amount;
//費(fèi)率備注
private String rate_notes;
對于返回的 String s = resp.get("data");的結(jié)果處理:
int i = s.indexOf("`");
int j = s.indexOf("總");
String substring = s.substring(i, j - 2);
String[] temp = substring.split(",``");
//String[] payment = temp[0].replace("`", "").split(",");
//System.out.println(payment.length);
ArrayList<PaymentPo> list = new ArrayList<PaymentPo>();
for (int k = 0; k < temp.length; k++) {
String[] payment = temp[k].replace("`", "").split(",");
PaymentPo bean = new PaymentPo();
for (int p = 0; p < payment.length; p++) {
bean.setTrade_time(payment[0]);
bean.setAppid(payment[1]);
bean.setMch_id(payment[2]);
bean.setMch_appid(payment[3]);
bean.setDevice_info(payment[4]);
bean.setTransaction_id(payment[5]);
bean.setOut_trade_no(payment[6]);
bean.setOpenid(payment[7]);
bean.setTrade_type(payment[8]);
bean.setTrade_status(payment[9]);
bean.setPay_bank(payment[10]);
bean.setMoney_type(payment[11]);
bean.setOrder_pay(payment[12]);
bean.setVoucher_amount(payment[13]);
bean.setRefund_number(payment[14]);
bean.setOut_refund_no(payment[15]);
bean.setRefund_amount(payment[16]);
bean.setRefund_amount_voucher(payment[17]);
bean.setRefunds_type(payment[18]);
bean.setRefunds_status(payment[19]);
bean.setCommodity_name(payment[20]);
bean.setData_packet(payment[21]);
bean.setService_charge(payment[22]);
bean.setRate(payment[23]);
bean.setOrder_amount(payment[24]);
bean.setApplication_refund_amount(payment[25]);
}
list.add(bean);
}
Gson gson2 = new Gson();
String str = gson2.toJson(list);
System.out.println(str);
轉(zhuǎn)換成json數(shù)據(jù)輸出
下載資金賬單
這個maven中沒有提供方法,但是微信官方api文檔中寫了有,那就我試試
所以,重寫了maven中的WXPay.java(就是復(fù)制一份到自己想買的目錄中),添加下載資金賬單的方法
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;
}
下載資金賬單是需要證書的,注意用requestWithCert方法,并且 簽名類型 sign_type設(shè)置成HMAC-SHA256
下面就和下載對賬單方法一樣了
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config, HMACSHA256);
Map<String, String> data = new HashMap<String, String>();
data.put("bill_date", "20181117");
data.put("account_type", "Basic");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(data.get("bill_date"));
int year = date.getYear() + 1900 ;
Map<String, String> resp = wxpay.downloadfundflow(data);
String s = resp.get("data");
int i = s.indexOf("`");
int j = s.indexOf("資金流水總筆數(shù)");
String substring = s.substring(i, j);
//方法比較簡單暴力,通過年份(比如2018 + `) 去切割,最后再把年份拼接上去
String[] temp = substring.split("`" +year);
ArrayList<FundFlowBean> list = new ArrayList<FundFlowBean>();
for (int m = 1; m < temp.length; m++) {
String[] payment = temp[m].replace("`", "").split(",");
FundFlowBean bean = new FundFlowBean();
for (int n = 0; n < payment.length; n++) {
bean.setTrade_time(year + payment[0]);
bean.setPayment_number(payment[1]);
bean.setFlow_number(payment[2]);
bean.setBusiness_name(payment[3]);
bean.setBusiness_type(payment[4]);
bean.setInout_type(payment[5]);
bean.setInout_money(payment[6]);
bean.setAccount_balance(payment[7]);
bean.setApplicant(payment[8]);
bean.setRemarks(payment[9]);
if (payment.length==10) {
if (payment[9].equals("system")) {
bean.setVoucher_number("");
}
}else if (payment.length==11){
bean.setVoucher_number(payment[10]);
}
}
list.add(bean);
}
Gson gson2 = new Gson();
String str = gson2.toJson(list);
System.out.println(str);
用于接收資金賬單數(shù)據(jù)的bean類 FundFlowBean .class 同時生成set get方法
public class FundFlowBean
//記賬時間
private String trade_time;
//微信支付業(yè)務(wù)單號
private String payment_number;
//資金流水單號
private String flow_number;
//業(yè)務(wù)名稱
private String business_name;
//業(yè)務(wù)類型
private String business_type;
//收支類型
private String inout_type;
//收支金額(元)
private String inout_money;
//賬戶結(jié)余(元)
private String account_balance;
//資金變更提交申請人
private String applicant;
//備注
private String remarks;
//業(yè)務(wù)憑證號
private String voucher_number;
到此,差不多結(jié)束,關(guān)于config中的各種id,需要去微信后臺上去找和設(shè)置