微信对账单查询
应用场景
商户可以通过该接口下载历史交易清单。比如掉单、系统错误等导致商户侧和微信侧数据不一致,通过对账单核对后可校正支付状态。
注意:
1、微信侧未成功下单的交易不会出现在对账单中。支付成功后撤销的交易会出现在对账单中,跟原支付单订单号一致;
2、微信在次日9点启动生成前一天的对账单,建议商户10点后再获取;
3、对账单中涉及金额的字段单位为“元”。
4、对账单接口只能下载三个月以内的账单。
5、对账单是以商户号纬度来生成的,如一个商户号与多个appid有绑定关系,则使用其中任何一个appid都可以请求下载对账单。对账单中的appid取自交易时候提交的appid,与请求下载对账单时使用的appid无关。
接口地址
https://api.mch.weixin.qq.com/pay/downloadbill
是否需要证书
不需要。
请求参数
字段名 | 变量名 | 必填 | 类型 | 示例值 | 描述 |
---|---|---|---|---|---|
公众账号ID | appid | 是 | String(32) | wx8888888888888888 | 微信分配的公众账号ID |
商户号 | mch_id | 是 | String(32) | 1900000109 | 微信支付分配的商户号 |
随机字符串 | nonce_str | 是 | String(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | 随机字符串,不长于32位。推荐随机数生成算法 |
签名 | sign | 是 | String(32) | C380BEC2BFD727A4B6845133519F3AD6 | 签名,详见签名生成算法 |
对账单日期 | bill_date | 是 | String(8) | 20140603 | 下载对账单的日期,格式:20140603 |
账单类型 | bill_type | 否 | String(8) | ALL | ALL(默认值),返回当日所有订单信息(不含充值退款订单 SUCCESS,返回当日成功支付的订单(不含充值退款订单) REFUND,返回当日退款订单(不含充值退款订单) RECHARGE_REFUND,返回当日充值退款订单 |
压缩账单 | tar_type | 否 | String | GZIP | 非必传参数,固定值:GZIP,返回格式为.gzip的压缩包账单。不传则默认为数据流形式。 |
具体信息参照官方文档
官方文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6
不多逼逼,上代码!
maven依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
<!--微信支付依赖start-->
<!-- https://mvnrepository.com/artifact/com.github.wxpay/wxpay-sdk -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--微信支付依赖end-->
测试类WxPayTest:
package com.sz;
import com.sz.entity.PtWxTradeDetail;
import com.sz.utils.CommonUtil;
import com.sz.utils.ConfigUtil;
import com.sz.utils.PayCommonUtil;
import org.junit.Test;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* @author 刘志虎
* @date 2019/4/10
*/
public class WxPayTest {
@Test
public void WxPayT1() {
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
parameters.put("appid", ConfigUtil.APP_ID); // APPid
parameters.put("mch_id", ConfigUtil.MCH_ID); // 商户id
// parameters.put("device_info", "");//微信支付分配的终端设备号,填写此字段,只下载该设备号 的对账单
parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
// 下载对账单的日期,格式:20140603
parameters.put("bill_date", "20190321");//
// bill_type:ALL返回当日所有订单信息,默认值SUCCESS返回当日成功支付的订单。REFUND,返回当日退款订单
parameters.put("bill_type", "ALL");
String sign = PayCommonUtil.createSign("utf-8", parameters);
parameters.put("sign", sign);
String reuqestXml = PayCommonUtil.getRequestXml(parameters);
//请求响应信息(微信返回数据格式不太美观,这里处理一下,封装了实体类,有需求的可直接插入数据库)
String result = CommonUtil.httpsRequest(ConfigUtil.DOWNLOAD_BILL_URL, "POST", reuqestXml);
String tradeMsg = result.substring(result.indexOf("`"));
tradeMsg = tradeMsg.replaceAll("%,`0.01,`0.00,`", "%,");
tradeMsg = tradeMsg.replaceAll("%,`0.00,`0.01,`", "%,");
//总汇信息
String sumMag = tradeMsg.substring(tradeMsg.lastIndexOf("%"), tradeMsg.length());
tradeMsg = tradeMsg.substring(0, tradeMsg.lastIndexOf("%") + 1);
tradeMsg = tradeMsg.replace("`", "");
String[] tradeArray = tradeMsg.split("%"); // 根据%来区分
for (String tradeDetailInfo : tradeArray) {
if (tradeDetailInfo.substring(0, 1).equals(",")) {
tradeDetailInfo = tradeDetailInfo.substring(1);
}
String[] tradeDetailArray = tradeDetailInfo.split(",");
PtWxTradeDetail entity = null;
entity = new PtWxTradeDetail();
entity.setId(null); // 自动生成id
entity.setTransDate(tradeDetailArray[0]);// 交易时间
entity.setCommonId(tradeDetailArray[1]);// 公众账号ID
entity.setBusinessNo(tradeDetailArray[2]);// 商户号
entity.setChildBusinessNo(tradeDetailArray[3]);// 子商户号
entity.setEquipmentNo(tradeDetailArray[4]);// 设备号
entity.setWxOrderNo(tradeDetailArray[5]);// 微信订单号
entity.setBusinessOrderNo(tradeDetailArray[6]);// 商户订单号
entity.setUserIdentity(tradeDetailArray[7]);// 用户标识
entity.setTransType(tradeDetailArray[8]);// 交易类型
entity.setTransStatus(tradeDetailArray[9]);// 交易状态
entity.setPaymentBank(tradeDetailArray[10]);// 付款银行
entity.setCurrency(tradeDetailArray[11]);// 货币种类
entity.setTotalAmount(tradeDetailArray[12]);// 总金额
entity.setRedEnvelopesAmount(tradeDetailArray[13]);// 企业红包金额
entity.setWxRefundNo(tradeDetailArray[14]);// 微信退款单号
entity.setBusinessRefundNo(tradeDetailArray[15]);// 商户退款单号
entity.setRefundAmount(tradeDetailArray[16]);// 退款金额
entity.setRedEnvelopesRefundAmount(tradeDetailArray[17]);// 企业红包退款金额
entity.setRefundType(tradeDetailArray[18]);// 退款类型
entity.setRefundStatus(tradeDetailArray[19]);// 退款状态
entity.setBusinessName(tradeDetailArray[20]);// 商品名称
entity.setBusinessData(tradeDetailArray[21]);// 商户数据包
entity.setFee(tradeDetailArray[22]);// 手续费
entity.setRate(tradeDetailArray[23] + "%");// 费率
entity.setCreateDate(new Date());//时间
System.out.println(entity.toString());
}
}
}
使用到的工具类:
CommonUtil
package com.sz.utils;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
/**
* 通用工具类
* @author 刘志虎
* @date 2019/4/10
*/
public class CommonUtil {
/**
* 发送https请求
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return 返回微信服务器响应的信息
*/
public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
System.err.println("连接超时:"+ ce);
} catch (Exception e) {
System.err.println("https请求异常:"+e);
}
return null;
}
}
ConfigUtil
package com.sz.utils;
/**
* 接口地址、商户号信息类
* @author 刘志虎
* @date 2019/4/10
*/
public class ConfigUtil {
/**
* 商户号相关信息
*/
public final static String APP_ID = "";//商户号的应用号
public final static String APP_SECRECT = "";//商户号的应用密码
public final static String TOKEN = "";//商户号的配置token
public final static String MCH_ID = "";//商户号ID
public final static String API_KEY = "rPy4oHnflt0EwXIfs6E8CLhBpouOcysR";//API密钥
public final static String SIGN_TYPE = "MD5";//签名加密方式
public final static String CERT_PATH = "D:/apiclient_cert.p12";//微信支付证书存放路径地址
//微信支付统一接口的回调action
public final static String NOTIFY_URL = "http://14.117.25.80:8016/wxweb/config/pay!paySuccess.action";
//微信支付成功支付后跳转的地址
public final static String SUCCESS_URL = "http://14.117.25.80:8016/wxweb/contents/config/pay_success.jsp";
//oauth2授权时回调action
public final static String REDIRECT_URI = "http://14.117.25.80:8016/GoMyTrip/a.jsp?port=8016";
/**
* 微信基础接口地址
*/
//获取token接口(GET)
public final static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
//oauth2授权接口(GET)
public final static String OAUTH2_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//刷新access_token接口(GET)
public final static String REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
// 菜单创建接口(POST)
public final static String MENU_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
// 菜单查询(GET)
public final static String MENU_GET_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
// 菜单删除(GET)
public final static String MENU_DELETE_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
/**
* 微信支付接口地址
*/
//微信支付统一接口(POST)
public final static String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//微信退款接口(POST)
public final static String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
//订单查询接口(POST)
public final static String CHECK_ORDER_URL = "https://api.mch.weixin.qq.com/pay/orderquery";
//关闭订单接口(POST)
public final static String CLOSE_ORDER_URL = "https://api.mch.weixin.qq.com/pay/closeorder";
//退款查询接口(POST)
public final static String CHECK_REFUND_URL = "https://api.mch.weixin.qq.com/pay/refundquery";
//对账单接口(POST)
public final static String DOWNLOAD_BILL_URL = "https://api.mch.weixin.qq.com/pay/downloadbill";
//短链接转换接口(POST)
public final static String SHORT_URL = "https://api.mch.weixin.qq.com/tools/shorturl";
//接口调用上报接口(POST)
public final static String REPORT_URL = "https://api.mch.weixin.qq.com/payitil/report";
}
MD5Util
package com.sz.utils;
import java.security.MessageDigest;
/**
*MD5加密工具类
* @author 刘志虎
* @date 2019/4/10
*/
public class MD5Util {
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}
MyX509TrustManager
package com.sz.utils;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 信任管理器
* @author 刘志虎
* @date 2019/4/10
*/
public class MyX509TrustManager implements X509TrustManager {
// 检查客户端证书
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 检查服务器端证书
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 返回受信任的X509证书数组
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
PayCommonUtil
package com.sz.utils;
import java.util.*;
/**
* 支付参数工具类
* @author 刘志虎
* @date 2019/4/10
*/
public class PayCommonUtil {
/**
* 生成随机字符串
* @return
*/
public static String CreateNoncestr() {
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String res = "";
for (int i = 0; i < 16; i++) {
Random rd = new Random();
res += chars.charAt(rd.nextInt(chars.length() - 1));
}
return res;
}
/**
* sign签名
* @param characterEncoding 编码格式
* @param parameters 请求参数
* @return
*/
public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + ConfigUtil.API_KEY);
String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
return sign;
}
/**
* 将请求参数转换为xml格式的string
* @param parameters 请求参数
* @return
*/
public static String getRequestXml(SortedMap<Object,Object> parameters){
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
String v = (String)entry.getValue();
if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)||"sign".equalsIgnoreCase(k)) {
sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
}else {
sb.append("<"+k+">"+v+"</"+k+">");
}
}
sb.append("</xml>");
return sb.toString();
}
}
菜鸟一枚,大神勿喷!