1.通过code获取到用户的openid.
地址: "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxid&secret=secret&code="
+ code + "&grant_type=authorization_code";
公众号需获取网页授权获取用户基本信息权限。
引导用户到指定的授权页面
例如:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=redirect_uri&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
参考文章:http://www.cnblogs.com/ansiboy/p/3755158.html
2.通过openid及用户相关信息向用户发放红包。
地址:"https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"
需要发放商户号用户openid等相关信息,需要注意的是签名信息与随机字符串
代码如下:
1. public static String createSendRedPackOrderSign(WeixinRedPacket redPack){ StringBuffer sign = new StringBuffer(); sign.append("act_name=").append(redPack.getAct_name()); sign.append("&client_ip=").append(redPack.getClient_ip()); sign.append("&mch_billno=").append(redPack.getMch_billno()); sign.append("&mch_id=").append(redPack.getMch_id()); sign.append("&nonce_str=").append(redPack.getNonce_str()); sign.append("&re_openid=").append(redPack.getRe_openid()); sign.append("&remark=").append(redPack.getRemark()); sign.append("&send_name=").append(redPack.getSend_name()); sign.append("&total_amount=").append(redPack.getTotal_amount()); sign.append("&total_num=").append(redPack.getTotal_num()); sign.append("&wishing=").append(redPack.getWishing()); sign.append("&wxappid=").append(redPack.getWxappid()); sign.append("&key=").append("abcdefg123456789abcdefg123456789"); return DigestUtils.md5Hex(getContentBytes(sign.toString(), "utf-8")).toUpperCase(); } private static String create_nonce_str() { return UUID.randomUUID().toString(); }
2.需要注意上传证书及将所有信息发放给微信,这里还需要使用微信给出的开发包,在微信上下载即可。
KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:\\certs\\apiclient_cert.p12")); try { keyStore.load(instream, "123".toCharArray());//商户号 } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, "123".toCharArray()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(sslsf) .build(); try { HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); StringEntity s = new StringEntity(requestJson,"UTF-8"); s.setContentEncoding("UTF-8"); s.setContentType("text/xml");//发送json数据需要设置contentType httppost.setEntity(s); CloseableHttpResponse resp = httpclient.execute(httppost); try { HttpEntity entity = resp.getEntity(); if (entity != null) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"utf-8")); String text; String xmlString = ""; while ((text = bufferedReader.readLine()) != null) { xmlString += text; } //return_code与result_code都为success代表发放红包成功 maps = XmlParseUtil.parse(xmlString); } EntityUtils.consume(entity); } finally { resp.close(); } } finally { httpclient.close(); }
微信官方文档地址:https://pay.weixin.qq.com/helper/cashredopenapi_V2.pdf
时间: 2024-10-24 22:57:05