


In the previous essay "C# Development of WeChat Portal and Application (33)--Encapsulation and Use of WeChat Cash Red Envelopes" introduced the encapsulation and use of ordinary cash red envelopes. This kind of red envelope can only be sent to one person at a time. Users Once you get the red envelope, you are done. If we let the user receive the red envelope, we can continue to send it to multiple users so that they can receive a fixed or random amount. This is called a fission red envelope. This essay continues the above theme and continues to introduce the C# code encapsulation and usage of the fission red envelope.
1. Introduction to fission red envelopes
Users who have received corporate fission red envelopes can continue to help their friends receive red envelopes, and spread corporate red envelopes to more friends in the form of fission, giving marketing more fun and pleasure! Fission red envelopes continuously strengthen corporate brand effects and form fission communication, which is a marketing tool for brand promotion. Enterprises only need to specify the number and total amount of a group of red envelopes, and WeChat Pay will calculate the amount of each red envelope, which is simple and convenient.
WeChat Pay fission red envelopes are developed for WeChat Pay merchants. The specific capabilities are as follows:
1. When the merchant calls the interface, it issues a set of fission red envelopes by specifying the sending amount and specifying a sender.
2. After the designated sender receives the red envelope, the funds will be directly transferred to WeChat change, giving users the smooth experience of WeChat payment.
3. The designated sender can share the remaining red envelopes in the combination Give it to friends, who can continue to receive it, forming a communication effect and amplifying the corporate brand value
The overall processing process and interface of fission red envelopes are not very different from ordinary cash red envelopes. After understanding the cash red envelopes, the encapsulation of fission red envelopes And the process of using it should be a breeze.
Fission red envelope interface, used by enterprises to send fission red envelopes to individual WeChat users
Currently supports issuing fission red envelopes of specified amounts to the openid of designated WeChat users. (To obtain openid, please refer to the WeChat public platform developer documentation: Web page authorization to obtain basic user information)
The corresponding relationship between the interface parameters and the actual effect of user application is as follows:
Interface call request description
Request Url | https://api.mch.weixin.qq.com /mmpaymkttransfers/sendgroupredpack |
---|---|
Do you need a certificate? | Yes (see the merchant certificate for certificate and usage instructions) |
Request method | POST |
Like the cash red envelope introduced earlier, we can also divide the parameters of the fission red envelope into two parts, one is the regular parameter and the other is the business parameter, as shown below.
2. C# code encapsulation of fission red envelope
From the above parameters passed, we can divide it into two parts. When we pass in the following parameters
<xml> <sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign> <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno> <mch_id><![CDATA[1000888888]]></mch_id> <wxappid><![CDATA[wxcbda96de0b165486]]></wxappid> <send_name><![CDATA[send_name]]></send_name> <re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid> <total_amount><![CDATA[600]]></total_amount> <amt_type><![CDATA[ALL_RAND]]></amt_type> <total_num><![CDATA[3]]></total_num> <wishing><![CDATA[恭喜發(fā)財(cái)]]></wishing> <act_name><![CDATA[新年紅包]]></act_name> <remark><![CDATA[新年紅包]]></remark> <nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str> </xml>
After the above incoming parameters are submitted to the address: https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack, the following XML will be returned after success.
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[發(fā)放成功.]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <err_code><![CDATA[0]]></err_code> <err_code_des><![CDATA[發(fā)放成功.]]></err_code_des> <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno> <mch_id>10010404</mch_id> <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid> <re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid> <total_amount>3</total_amount> <send_time><![CDATA[20150227091010]]></send_time> <send_listid><![CDATA[1000000000201502270093647546]]></send_listid> </xml>
If it fails, the returned XML will lack a lot of information, as shown below.
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[系統(tǒng)繁忙,請稍后再試.]]></return_msg> <result_code><![CDATA[FAIL]]></result_code> <err_code><![CDATA[268458547]]></err_code> <err_code_des><![CDATA[系統(tǒng)繁忙,請稍后再試.]]></err_code_des> <mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno> <mch_id>10010404</mch_id> <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid> <re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid> <total_amount>3</total_amount> </xml>
According to these rules, we have encapsulated the interface and implementation code of the fission red envelope for better use.
For example, based on the similar characteristics of the entity classes of ordinary cash red envelopes and fission red envelopes, we designed several classes to store information. The object relationship of the incoming parameters is shown in the figure below.
Similarly, for the returned red envelope sending results, due to the need to consider the information return in error and the successful information return, we designed the relationship between the return result classes as follows Show.
Based on the above design ideas, the class code we designed is as follows.
/// <summary> /// 發(fā)送裂變紅包的數(shù)據(jù)信息 /// </summary> public class SendGroupRedPackJson : BaseRedPackJson { /// <summary> /// 紅包金額設(shè)置方式 /// ALL_RAND—全部隨機(jī),商戶指定總金額和紅包發(fā)放總?cè)藬?shù),由微信支付隨機(jī)計(jì)算出各紅包金額 /// </summary> public string amt_type { get; set; } public SendGroupRedPackJson() { this.amt_type = "ALL_RAND"; } }
The code of the result object class is as follows.
/// <summary> /// 發(fā)送紅包的返回結(jié)果 /// </summary> public class SendRedPackResult : PayResult { /// <summary> /// 商戶訂單號 /// </summary> public string mch_billno { get; set; } /// <summary> /// 商戶appid,接口傳入的所有appid應(yīng)該為公眾號的appid(在mp.weixin.qq.com申請的),不能為APP的appid(在open.weixin.qq.com申請的)。 /// </summary> public string wxappid { get; set; } /// <summary> /// 接受收紅包的用戶 /// 用戶在wxappid下的openid /// </summary> public string re_openid { get; set; } /// <summary> /// 付款金額,單位分 /// </summary> public int total_amount { get; set; } /// <summary> /// 紅包發(fā)送時間 /// </summary> public string send_time { get; set; } /// <summary> /// 紅包訂單的微信單號 /// </summary> public string send_listid { get; set; } }
In this way, with the codes of these objects, we still follow the interface design method introduced in the previous essay according to the interface description to implement the code processing of fission red envelopes.
/// <summary> /// 微信紅包、搖一搖紅包的操作API接口 /// </summary> public interface ILotteryApi { /// <summary> /// 用于企業(yè)向微信用戶個人發(fā)現(xiàn)金紅包。需要商戶證書 /// 目前支持向指定微信用戶的openid發(fā)放指定金額紅包。 /// </summary> /// <returns></returns> SendRedPackResult SendRedPack(SendRedPackJson json); /// <summary> /// 用于企業(yè)向微信用戶個人發(fā)裂變紅包。需要商戶證書 /// 目前支持向指定微信用戶的openid發(fā)放指定金額裂變紅包。 /// </summary> /// <returns></returns> SendRedPackResult SendGroupRedPack(SendGroupRedPackJson json); .............
Then the interface code to implement them is as follows.
/// <summary> /// 微信紅包管理類 /// </summary> public class LotteryApi : ILotteryApi { #region 裂變紅包說明 //微信支付裂變紅包向微信支付商戶開發(fā),具體能力如下: //1、商戶調(diào)用接口時,通過指定發(fā)送金額以及指定一位發(fā)送對象的方式發(fā)放一組裂變紅包 //2、指定發(fā)送對象領(lǐng)取到紅包后,資金直接進(jìn)入微信零錢,帶給用戶微信支付原生的流暢體驗(yàn) //3、指定發(fā)送對象能夠?qū)⒔M合中的剩余紅包分享給好友,好友可繼續(xù)領(lǐng)取,形成傳播效應(yīng),放大企業(yè)品牌價值 #endregion /// <summary> /// 用于企業(yè)向微信用戶個人發(fā)裂變紅包。需要商戶證書 /// 目前支持向指定微信用戶的openid發(fā)放指定金額裂變紅包。 /// </summary> /// <returns></returns> public SendRedPackResult SendGroupRedPack(SendGroupRedPackJson json) { CheckAccount();//檢查AccountInfo的對象屬性值 WxPayData data = new WxPayData(); data.SetValue("wxappid", AccountInfo.UniteAppId);//公眾賬號appid data.SetValue("mch_id", AccountInfo.MchID);//商戶號 data.SetValue("nonce_str", data.GenerateNonceStr());//隨機(jī)字符串 data.SetValue("send_name", AccountInfo.Name);// 紅包發(fā)送者名稱 //商戶訂單號(每個訂單號必須唯一) 組成:mch_id+yyyymmdd+10位一天內(nèi)不能重復(fù)的數(shù)字。 //接口根據(jù)商戶訂單號支持重入,如出現(xiàn)超時可再調(diào)用。 data.SetValue("mch_billno", data.GenerateOutTradeNo(AccountInfo.MchID)); data.SetValue("re_openid", json.re_openid);//接收紅包的種子用戶(首個用戶) data.SetValue("total_amount", json.total_amount);//紅包發(fā)放總金額,即一組紅包金額總和,包括分享者的紅包和裂變的紅包,單位分 data.SetValue("total_num", json.total_num);//紅包發(fā)放總?cè)藬?shù),即總共有多少人可以領(lǐng)到該組紅包(包括分享者) data.SetValue("wishing", json.wishing);//紅包祝福語 data.SetValue("act_name", json.act_name); data.SetValue("remark", json.remark); data.SetValue("amt_type", json.amt_type); data.SetValue("sign", data.MakeSign(AccountInfo.PayAPIKey));//最后生成簽名 var url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack"; return Helper.GetPayResultWithCert<SendRedPackResult>(data, url, AccountInfo.CertPath, AccountInfo.CertPassword); } ...............
3. Calling and effect display of fission red envelope interface
Above we have encapsulated the interface of fission red envelope. If we need to send fission red envelope At this time, you can directly call the above interface to realize the operation of sending red envelopes.
For example, the code for calling the interface is as follows.
//裂變紅包 SendGroupRedPackJson groupJson = new SendGroupRedPackJson() { act_name = "恭喜發(fā)財(cái)", remark = "企業(yè)紅包", wishing = "企業(yè)紅包", total_amount = 600, total_num = 4, re_openid = tosendOpenId, //發(fā)送給用戶的OpenID }; var groupResult = hbApi.SendGroupRedPack(groupJson); message = string.Format("企業(yè)發(fā)送裂變紅包:{0} {1}", groupResult.Success ? "成功" : "失敗", groupResult.Message); Console.WriteLine(message); Console.WriteLine(groupResult.ToJson());
The hbApi object initialization code is as follows
ILotteryApi hbApi = new LotteryApi(accountInfo);
Finally we can see the fission red envelope sent on WeChat.
Since the amount of red envelopes sent needs to be greater than 1 yuan, then if the amount we send is larger, then everyone will split it The amount of red envelopes varies, as shown in the picture below.
The above is about the interface encapsulation and usage process of cash red envelopes and fission red envelopes. I hope it will be helpful to you in using WeChat development. Thank you for your support.
For more articles related to C# development of WeChat portal and application of WeChat fission red envelopes, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)