Home > WeChat Applet > WeChat Development > C# develops WeChat portal and applies WeChat fission red envelope

C# develops WeChat portal and applies WeChat fission red envelope

高洛峰
Release: 2017-03-07 09:48:30
Original
1969 people have browsed it

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:

C# develops WeChat portal and applies WeChat fission red envelope


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.

C# develops WeChat portal and applies WeChat fission red envelope

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[恭喜发财]]></wishing>
   <act_name><![CDATA[新年红包]]></act_name> 
   <remark><![CDATA[新年红包]]></remark> 
   <nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str> 
</xml>
Copy after login

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[发放成功.]]></return_msg> 
   <result_code><![CDATA[SUCCESS]]></result_code> 
   <err_code><![CDATA[0]]></err_code> 
   <err_code_des><![CDATA[发放成功.]]></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>
Copy after login

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[系统繁忙,请稍后再试.]]></return_msg>
   <result_code><![CDATA[FAIL]]></result_code>
   <err_code><![CDATA[268458547]]></err_code>
   <err_code_des><![CDATA[系统繁忙,请稍后再试.]]></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>
Copy after login


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.

C# develops WeChat portal and applies WeChat fission red envelope

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.

C# develops WeChat portal and applies WeChat fission red envelope

Based on the above design ideas, the class code we designed is as follows.

/// <summary>
    /// 发送裂变红包的数据信息
    /// </summary>
    public class SendGroupRedPackJson : BaseRedPackJson
    {     
        /// <summary>
        /// 红包金额设置方式
        /// ALL_RAND—全部随机,商户指定总金额和红包发放总人数,由微信支付随机计算出各红包金额
        /// </summary>
        public string amt_type { get; set; }

        public SendGroupRedPackJson()
        {
            this.amt_type = "ALL_RAND";
        }
    }
Copy after login

The code of the result object class is as follows.

/// <summary>
    /// 发送红包的返回结果
    /// </summary>
    public class SendRedPackResult : PayResult
    {
        /// <summary>
        /// 商户订单号
        /// </summary>
        public string mch_billno { get; set; }

        /// <summary>
        /// 商户appid,接口传入的所有appid应该为公众号的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>
        /// 红包发送时间
        /// </summary>
        public string send_time { get; set; }
        /// <summary>
        /// 红包订单的微信单号
        /// </summary>
        public string send_listid { get; set; }
    }
Copy after login


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.

C# develops WeChat portal and applies WeChat fission red envelope

/// <summary>
    /// 微信红包、摇一摇红包的操作API接口
    /// </summary>
    public interface ILotteryApi
    {              
        /// <summary>
        /// 用于企业向微信用户个人发现金红包。需要商户证书
        /// 目前支持向指定微信用户的openid发放指定金额红包。
        /// </summary>
        /// <returns></returns>
        SendRedPackResult SendRedPack(SendRedPackJson json);

        /// <summary>
        /// 用于企业向微信用户个人发裂变红包。需要商户证书
        /// 目前支持向指定微信用户的openid发放指定金额裂变红包。
        /// </summary>
        /// <returns></returns>
        SendRedPackResult SendGroupRedPack(SendGroupRedPackJson json);

        .............
Copy after login

Then the interface code to implement them is as follows.

/// <summary>
    /// 微信红包管理类
    /// </summary>
    public class LotteryApi : ILotteryApi
    {
        #region 裂变红包说明
        //微信支付裂变红包向微信支付商户开发,具体能力如下: 
        //1、商户调用接口时,通过指定发送金额以及指定一位发送对象的方式发放一组裂变红包
        //2、指定发送对象领取到红包后,资金直接进入微信零钱,带给用户微信支付原生的流畅体验
        //3、指定发送对象能够将组合中的剩余红包分享给好友,好友可继续领取,形成传播效应,放大企业品牌价值
        #endregion

        /// <summary>
        /// 用于企业向微信用户个人发裂变红包。需要商户证书
        /// 目前支持向指定微信用户的openid发放指定金额裂变红包。
        /// </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());//随机字符串
            data.SetValue("send_name", AccountInfo.Name);//    红包发送者名称
            
            //商户订单号(每个订单号必须唯一) 组成:mch_id+yyyymmdd+10位一天内不能重复的数字。
            //接口根据商户订单号支持重入,如出现超时可再调用。
            data.SetValue("mch_billno", data.GenerateOutTradeNo(AccountInfo.MchID));

            data.SetValue("re_openid", json.re_openid);//接收红包的种子用户(首个用户)
            data.SetValue("total_amount", json.total_amount);//红包发放总金额,即一组红包金额总和,包括分享者的红包和裂变的红包,单位分
            data.SetValue("total_num", json.total_num);//红包发放总人数,即总共有多少人可以领到该组红包(包括分享者)
            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);
        }

        ...............
Copy after login

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 = "恭喜发财",
                    remark = "企业红包",
                    wishing = "企业红包",
                    total_amount = 600,
                    total_num = 4,
                    re_openid = tosendOpenId, //发送给用户的OpenID
                };

                var groupResult = hbApi.SendGroupRedPack(groupJson);
                message = string.Format("企业发送裂变红包:{0} {1}", groupResult.Success ? "成功" : "失败", groupResult.Message);
                Console.WriteLine(message);
                Console.WriteLine(groupResult.ToJson());
Copy after login


The hbApi object initialization code is as follows

ILotteryApi hbApi = new LotteryApi(accountInfo);
Copy after login

Finally we can see the fission red envelope sent on WeChat.

C# develops WeChat portal and applies WeChat fission red envelope C# develops WeChat portal and applies WeChat fission red envelope C# develops WeChat portal and applies WeChat fission red envelope

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.

C# develops WeChat portal and applies WeChat fission red envelope

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template