给个人发红包,总是出现
< xml >< return_code > <![CDATA[SUCCESS]]> </ return_code >< return_msg > <![CDATA[参数错误:send_name字段必填,并且少于32字符]]> </ return_msg >< result_code > <![CDATA[FAIL]]> </ result_code >< err_code > <![CDATA[PARAM_ERROR]]> </ err_code >< err_code_des > <![CDATA[参数错误:send_name字段必填,并且少于32字符]]> </ err_code_des >< mch_billno > <![CDATA[150820463120190425947306011]]> </ mch_billno >< mch_id > <![CDATA[XXXX82XX631]]> </ mch_id >< wxappid > <![CDATA[XXXXXXXXXXXXX]]> </ wxappid >< re_openid > <![CDATA[XXXXXXXXXXX]]> </ re_openid >< total_amount >1</ total_amount ></ xml > |
同时验签是通过的
Java 代码块
/** * 需要证书的请求 * @param strUrl String * @param reqData 向wxpay post的请求数据 Map * @param connectTimeoutMs 超时时间,单位是毫秒 * @param readTimeoutMs 超时时间,单位是毫秒 * @return API返回数据 * @throws Exception */ public String requestWithCert(String strUrl, Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception { String UTF8 = "UTF-8" ; String reqBody = WXPayUtil.mapToXml(reqData); System.out.println(reqBody); URL httpUrl = new URL(strUrl); char [] password = config.getMchID().toCharArray(); InputStream certStream = config.getCertStream(); KeyStore ks = KeyStore.getInstance( "PKCS12" ); ks.load(certStream, password); // 实例化密钥库 & 初始化密钥工厂 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, password); // 创建SSLContext SSLContext sslContext = SSLContext.getInstance( "TLS" ); sslContext.init(kmf.getKeyManagers(), null , new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection(); httpURLConnection.setDoOutput( true ); httpURLConnection.setRequestMethod( "POST" ); httpURLConnection.setConnectTimeout(connectTimeoutMs); httpURLConnection.setReadTimeout(readTimeoutMs); httpURLConnection.connect(); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(reqBody.getBytes(UTF8)); // if (httpURLConnection.getResponseCode()!= 200) { // throw new Exception(String.format("HTTP response code is %d, not 200", httpURLConnection.getResponseCode())); // } //获取内容 InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream, UTF8)); final StringBuffer stringBuffer = new StringBuffer(); String line = null ; while ((line = bufferedReader.readLine()) != null ) { stringBuffer.append(line); } String resp = stringBuffer.toString(); if (stringBuffer!= null ) { try { bufferedReader.close(); } catch (IOException e) { // e.printStackTrace(); } } if (inputStream!= null ) { try { inputStream.close(); } catch (IOException e) { // e.printStackTrace(); } } if (outputStream!= null ) { try { outputStream.close(); } catch (IOException e) { // e.printStackTrace(); } } if (certStream!= null ) { try { certStream.close(); } catch (IOException e) { // e.printStackTrace(); } } // if (httpURLConnection!=null) { // httpURLConnection.disconnect(); // } return resp; } |
请求发红包的URL为:https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb
p12的证书已导入,商户API-Key也是正确的
参考的项目为:
< dependency > < groupId >com.objcoding</ groupId > < artifactId >WXPay-SDK-Java</ artifactId > < version >0.0.5</ version > </ dependency > |
github网址:https://github.com/objcoding/WXPay-SDK-Java
参考编写的官方文档:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2
需要技术支持,谢谢。
同时 错误提示的“参数错误:send_name字段必填,并且少于32字符” 我已经给了参数send_name,根据官方的文档提示,无法定位到问题,需要官方处理
问题一:文档中表意不清,send_name一直认为是商户号名称,其实是appid所指定的公众号名称或小程序名称
问题二:文档中,notify_way 参数必填,可是填写便不能成功,提示:未开通该功能,注释掉该参数便正常
问题三:文档中,total_amount 并没有说明最小值为100,最低只能发一块钱进行
以下代码块是可以实现的
WXPayUtil.java 请参考 https://github.com/objcoding/WXPay-SDK-Java
本来轻轻松松搞定的事,花了一天半时间,感觉微信的人项目管理方面还是太弱,太多的坑了
检查一下send_name的字段是不是过长,需要小于32字符,一个汉字是3个字符