I have read the blog about WeChat payment I wrote these days, and there are quite a few people reading it. I don’t know if it’s because the blog is not detailed enough or what is going on. Most of them ask me for source code. I think programmers still need to have such a thinking process, so they didn’t give the source code directly. As the saying goes, “It’s better to teach people how to fish than to fish.” Therefore, I hope to take some time to typify the code yourself while reading the article. OK, let’s not say much nonsense. This time I will share the use of the WeChat cash red envelope interface.
The following is an introduction to cash red envelopes by WeChat development documents:
Cash red envelopes are one of the marketing tools provided by the WeChat payment merchant platform. Since its launch, they have been loved by merchants and users. Merchants can distribute cash red envelopes to WeChat payment users through this platform. After the user receives the red envelope, the funds arrive at the user's WeChat payment change account, which has the same usage export as other funds in the coin wallet; if the user does not receive them, the funds will be returned to the merchant's WeChat payment account after 24 hours.
Product meaning
WeChat payment cash red envelopes have always been favored by users because of the way they carry funds in cash. In recent years, cash red envelopes have played an important role; they have also brought warm responses to merchants' marketing activities in daily operations. In general, cash red envelopes play an important role in scenarios including but not limited to:
To sum up, WeChat cash red envelopes are a marketing tool that can increase user stickiness by following official accounts, registering, etc. This time, I focus on sharing my experiences from the perspective of program development
1. Conditions required to use the WeChat cash red envelope function
1. Have a WeChat merchant platform and complete secret certificates
2. Merchants need to have enough balance to use (if not enough, you can use Tenpay to recharge from the merchant platform)
3. Better development foundation for WeChat payment
The key points and difficulties of the second development
1 WeChat signature algorithm
2 httppclient and the use of certificates
3 Reading of WeChat documents (https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3)
If you have a WeChat h5 payment or scan the QR code to pay, you can read this part of the document, which is a piece of cake. It is effortless to understand. At the same time, as long as you master the knowledge of httpclient, everything will be ready.
Three direct code
public static void sendRedPack(String mch_billno,String openId,String send_name,String total_fee,String total_num,String wishing,String act_name,String remark,String ip) throws Exception{ String non=PayCommonUtil.CreateNoncestr(); SortedMap<Object, Object> p = new TreeMap<Object, Object>(); p.put("nonce_str", non); p.put("mch_billno", mch_billno); p.put("mch_id", ConfigUtil.MCH_ID); p.put("wxappid", ConfigUtil.APPID); p.put("re_openid", openId); p.put("total_amount", total_fee); p.put("total_num", "1"); p.put("client_ip", "127.0.0.1"); p.put("act_name", act_name); p.put("send_name", send_name); p.put("wishing", wishing); p.put("remark",remark); String sign = PayCommonUtil.createSign("UTF-8", p); System.out.println(sign); p.put("sign", sign); String reuqestXml = PayCommonUtil.getRequestXml(p); KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream Instream = new FileInputStream(new File(ConfigUtil.CERT_PATH)); try { keyStore.load(instream, ConfigUtil.MCH_ID.toCharArray()); } finally { enterstream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, ConfigUtil.MCH_ID.toCharArray()).build(); 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");// Refund interface httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); System.out.println("executing request" + httpPost.getRequestLine()); //The requested xml must be transcoded to iso8859-1 encoding, otherwise it is prone to signature errors or text on the red envelope display errors StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(), "ISO8859-1")); // Set type httpPost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(entity.getContent(), "UTF-8")); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } } The following points to note are:
//The requested XML must be transcoded to iso8859-1 encoding, otherwise it is prone to sign errors or text display on the red envelope is incorrect.
StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(), "ISO8859-1"));
This place can be said to have almost crashed me and tried various debugging. It would be OK to add it with a try mentality. This may be because of the difference between httpclient and the native HttpsConnection in data transmission. There is no much research done here.
Calling this method is easier, just like the following
public static void main(String args[]){ try { sendRedPack("12828839012016101420", "Recipient's openid","xxx","100","1","Congratulations on getting rich, every year", "New Year's red envelope", "New Year's red envelope is not grabbing","127.0.0.1"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } The information printed after the red envelope is sent is as follows:
TTP/1.1 200 OK Response content length: 567 <xml> <return_code><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[SUCCESS]]></err_code> <mch_billno><![CDATA[12828839012016101421]]></mch_billno> <mch_id><![CDATA[1282883901]]></mch_id> <wxappid><![CDATA[xxxxx]]></wxappid> <re_openid><![CDATA[xxxx]]></re_openid> <total_amount>100</total_amount> <send_listid><![CDATA[1000041701201610143000090813093]]></send_listid> </xml>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.