微信批次核销明细下载


微信相关文档:
核销明细文件地址获取: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter9_1_10.shtml
微信签名生成规则: https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml
账单文件数据下载: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_8.shtml

一、微信批次核销明细下载

1、生成微信签名


//组装Authorization 以及签名
    //获取时间戳
    long timestamp = System.currentTimeMillis() / 1000L;
   	//获取随机串
    public static String generateNonceStr() {
    	String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(new SecureRandom().nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
    //构造请求签名串
    String nonce = generateNonceStr();
    String method = "GET";
    String url = "v3/marketing/favor/stocks/{stock_id}/use-flow".replace("{stock_id}","批次号");
    String body = "";
    String preSign = method + "\n"
                + url + "\n"
                + timestamp + "\n"
                + nonce + "\n"
                + body + "\n";
        //对签名串进行RSA2加签
String sign(byte[] message) {
    Signature sign = Signature.getInstance("SHA256withRSA");
    sign.initSign("微信支付私钥");
    sign.update(message);

    return Base64.getEncoder().encodeToString(sign.sign());
}

String signature = sign(preSign.getBytes("utf-8"));
String auth = "mchid=\"" + yourMerchantId + "\","
    + "nonce_str=\"" + nonceStr + "\","
    + "timestamp=\"" + timestamp + "\","
    + "serial_no=\"" + yourCertificateSerialNo + "\","
    + "signature=\"" + signature + "\"";

2、获取批次核销明细地址

代码如下(示例):

//设置HTTP Authorization头
String hostUrl = "https://api.mch.weixin.qq.com";
String method = "v3/marketing/favor/stocks/{stock_id}/use-flow";
//GET请求(官网)
  HttpGet httpGet = new HttpGet(hostUrl + method);
  httpGet.addHeader("Accept", "application/json");
  httpGet.addHeader("Authorization", auth);
  response = httpClient.execute(httpGet);
//POST请求
  HttpPost httpPost = new HttpPost(hostUrl + method);
  StringEntity reqEntity = new StringEntity(
  requestJson, ContentType.create("application/json", "utf-8"));
  httpPost.setEntity(reqEntity);
  httpPost.addHeader("Accept", "application/json");
  httpPost.addHeader("Authorization", auth);
  response = httpClient.execute(httpPost);

HttpEntity resEntity = response.getEntity();
String retMsg = EntityUtils.toString(resEntity, "UTF-8");// 微信返回的参数

返回参数示例:url是储存在微信服务器的文件地址

{ 
  "hash_type": "SHA1",
  "hash_value": "8ae0eb442c408d2e90d669d6f4ad6b7e6e049d6f", 
  "url": "https://api.mch.weixin.qq.com/v3/billdownload/file?token=ja7q-s1yy1ZbROASakz0Jx4BjW3qdnympjfcB4v4yLftXXXXXXXXXXXX"
} 

微信支付验签

Header sign = response.getFirstHeader("Wechatpay-Signature");
Header serialNoHeader = response.getFirstHeader("Wechatpay-Serial");
Header timestampHead = response.getFirstHeader("Wechatpay-TimeStamp");
Header nonceHead = response.getFirstHeader("Wechatpay-Nonce");

二、通过文件地址获取数据

1.读入数据

代码如下(示例):

String method = "/v3/billdownload/file?token=xxx"; // token换成微信返回的url中的token
  HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx");
  httpGet.addHeader("Accept", "application/json");
  httpGet.addHeader("Authorization", auth);
  response = httpClient.execute(httpGet);
  int statusCode = response.getStatusLine().getStatusCode();
  if( statusCode == 200){
  	//成功
  }
  String retMsg = EntityUtils.toString(response.getEntity(), "UTF-8");

总结

主要学习微信支付的签名规则,下载微信支付代金券批次的账单

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值