微信官方给的sdk有点问题,通过百度找到了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;
}
}
创建测试文件 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");
对账单数据
请求下载对账单的api方法
Map<String, String> resp = wxpay.downloadBill(data);
String s = resp.get("data");
对于数据返回的 比较麻烦,所以用bean对其进行处理,方便装换成我们需要的数据类型
接收的数据bean
public class PaymentPo
//交易时间
private String trade_time;
// 公众账号ID
private String appid;
//商户号
private String mch_id;
//特约商户号
private String mch_appid;
// 设备号
private String device_info;
//微信订单号
private String transaction_id;
// 商户订单号
private String out_trade_no;
// 用户标识
private String openid;
// 交易类型
private String trade_type;
//交易状态
private String trade_status;
//付款银行
private String pay_bank;
//货币种类
private String money_type;
//应结订单金额
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;
//退款状态
private String refunds_status;
//商品名称
private String commodity_name;
//商户数据包
private String data_packet;
//手续费
private String service_charge;
//费率
private String rate;
//订单金额
private String order_amount;
//申请退款金额
private String application_refund_amount;
//费率备注
private String rate_notes;
对于返回的 String s = resp.get("data");的结果处理:
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);
转换成json数据输出
下载资金账单
这个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;
}
下载资金账单是需要证书的,注意用requestWithCert方法,并且 签名类型 sign_type设置成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("资金流水总笔数");
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);
用于接收资金账单数据的bean类 FundFlowBean .class 同时生成set get方法
public class FundFlowBean
//记账时间
private String trade_time;
//微信支付业务单号
private String payment_number;
//资金流水单号
private String flow_number;
//业务名称
private String business_name;
//业务类型
private String business_type;
//收支类型
private String inout_type;
//收支金额(元)
private String inout_money;
//账户结余(元)
private String account_balance;
//资金变更提交申请人
private String applicant;
//备注
private String remarks;
//业务凭证号
private String voucher_number;
到此,差不多结束,关于config中的各种id,需要去微信后台上去找和设置
网友评论