zoukankan      html  css  js  c++  java
  • 电子发票插入微信卡包之PDF上传

    项目中需要将开具的电子发票插入到微信的卡包中,采用的是微信自建平台模式。

    因为是自己的开的票,所以调用插卡接口前,需要上传PDF文件,代码如下:


    /**
    * 将电子发票PDF文件上传至微信 * @param accessToken 微信的access_token * @param file 本地的pdf文件 * @return<br> * @author xxx, 2019年9月23日.<br> */ public String uploadPdfFile(String accessToken, File file){ HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token="+accessToken); CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); httpPost.setConfig(requestConfig); //2.3 设置请求实体,封装了请求参数 HttpEntity requestEntity = MultipartEntityBuilder.create().addPart("pdf", new FileBody(file, ContentType.create("multipart/form-data", Consts.UTF_8), file.getName())).build(); httpPost.setEntity(requestEntity); try { response = httpClient.execute(httpPost, new BasicHttpContext()); log.info(" 上传PDF文件至微信返回参数:{}",JSON.toJSONString(response)); if (response.getStatusLine().getStatusCode() != 200) { throw new InsertWxInvoiceCardException(2013,"上传PDF文件至微信,请求失败"); } HttpEntity entity = response.getEntity(); if (entity != null) { String resultStr = EntityUtils.toString(entity, "utf-8"); log.info(" 上传PDF文件至微信返回参数:{}",resultStr); JSONObject result = JSON.parseObject(resultStr); int errcode = (Integer)result.get("errcode"); if(errcode != 0){ throw new InsertWxInvoiceCardException(2013,"上传PDF文件至微信失败"); } return result.getString("s_media_id"); } } catch (IOException e) { e.printStackTrace(); } finally { if (response != null) try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }

    返回的是上传pdf成功后的s_media_id,这个参数可以在本地存储,因为在后续的插卡接口中需要使用。
  • 相关阅读:
    头部布局,搜索验证和AJAX自动搜索提示,并封装成组件,提高代码复用性
    yii2 使用指定数据库执行createCommand
    yii2在控制器中调用另一个控制器方法
    yii2 命令行执行php命令 commands(命令)
    ArrayDataProvider数据分页
    Yii2 数据查询
    Yii2的Gridview应用技巧补充
    yii2 or查询
    SQL---mysql新增字段
    Yii2.0 安装使用报错:yiiwebRequest::cookieValidationKey must be configured with a secret key.
  • 原文地址:https://www.cnblogs.com/mirakel/p/11572641.html
Copyright © 2011-2022 走看看