首頁  >  文章  >  微信小程式  >  微信公眾號開發:商家如何為用戶發紅包實例講解

微信公眾號開發:商家如何為用戶發紅包實例講解

php是最好的语言
php是最好的语言原創
2018-08-01 11:19:086492瀏覽

紅包功能簡單介紹:

1、商家呼叫介面時,透過指定發送物件以及發送金額的方式發放紅包,這樣的方式,允許商家靈活的應用於各種各樣豐富的活動場景

2、領取到紅包後,用戶的資金直接進入微信零錢,避免繁複的領獎流程,帶給用戶微信支付原生的流暢體驗

現金紅包官網文件地址:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1

呼叫現金紅包介面需要使用到證書,請前往商家平台下載證書

官網有關詳細證書的介紹:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3

因為寄現金紅包是從商家平台餘額扣款,所以商家平台的帳戶餘額必須有充足的餘額

以下是呼叫紅包介面詳細程式碼:1、簽署的MD5加密類別:

/// <summary>/// MD5UtilHelper 的摘要说明。/// </summary>public class MD5UtilHelper
{    public MD5UtilHelper()
    {        //
        // TODO: 在此处添加构造函数逻辑
        //
    }    /// <summary>
            /// 获取大写的MD5签名结果
    /// </summary>
    /// <param name="encypStr"></param>
    /// <param name="charset"></param>
    /// <returns></returns>
    public static string GetMD5(string encypStr, string charset)
    {        string retStr;
        MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();        //创建md5对象
        byte[] inputBye;        byte[] outputBye;        //使用GB2312编码方式把字符串转化为字节数组.
        try
        {
            inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
        }        catch (Exception ex)
        {
            inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
        }
        outputBye = m5.ComputeHash(inputBye);

        retStr = System.BitConverter.ToString(outputBye);
        retStr = retStr.Replace("-", "").ToUpper();        return retStr;
    }
}

2 、處理參數的類別:

public class RequestHandler
    {        public RequestHandler(HttpContext httpContext)
        {
            Parameters = new Hashtable();            this.HttpContext = httpContext ?? HttpContext.Current;

        }        /// <summary>
        /// 密钥
        /// </summary>
        private string Key;        protected HttpContext HttpContext;        /// <summary>
        /// 请求的参数
        /// </summary>
        protected Hashtable Parameters;        /// <summary>
        /// debug信息
        /// </summary>
        private string DebugInfo;        /// <summary>
        /// 初始化函数
        /// </summary>
        public virtual void Init()
        {
        }        /// <summary>
        /// 获取debug信息
        /// </summary>
        /// <returns></returns>
        public String GetDebugInfo()
        {            return DebugInfo;
        }        /// <summary>
        /// 获取密钥
        /// </summary>
        /// <returns></returns>
        public string GetKey()
        {            return Key;
        }        /// <summary>
        /// 设置密钥
        /// </summary>
        /// <param name="key"></param>
        public void SetKey(string key)
        {            this.Key = key;
        }        /// <summary>
        /// 设置参数值
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="parameterValue"></param>
        public void SetParameter(string parameter, string parameterValue)
        {            if (parameter != null && parameter != "")
            {                if (Parameters.Contains(parameter))
                {
                    Parameters.Remove(parameter);
                }

                Parameters.Add(parameter, parameterValue);
            }
        }        /// <summary>
        /// 创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名
        /// </summary>
        /// <param name="key">参数名</param>
        /// <param name="value">参数值</param>
        /// key和value通常用于填充最后一组参数
        /// <returns></returns>
        public virtual string CreateMd5Sign(string key, string value)
        {
            StringBuilder sb = new StringBuilder();

            ArrayList akeys = new ArrayList(Parameters.Keys);
            akeys.Sort();            foreach (string k in akeys)
            {                string v = (string)Parameters[k];                if (null != v && "".CompareTo(v) != 0
                    && "sign".CompareTo(k) != 0 && "key".CompareTo(k) != 0)
                {
                    sb.Append(k + "=" + v + "&");
                }
            }

            sb.Append(key + "=" + value);            string sign = MD5UtilHelper.GetMD5(sb.ToString(), GetCharset()).ToUpper();            return sign;
        }        /// <summary>
        /// 输出XML
        /// </summary>
        /// <returns></returns>
        public string ParseXML()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<xml>");            foreach (string k in Parameters.Keys)
            {                string v = (string)Parameters[k];                if (Regex.IsMatch(v, @"^[0-9.]$"))
                {

                    sb.Append("<" + k + ">" + v + "</" + k + ">");
                }                else
                {
                    sb.Append("<" + k + "><![CDATA[" + v + "]]></" + k + ">");
                }

            }
            sb.Append("</xml>");            return sb.ToString();
        }        /// <summary>
        /// 设置debug信息
        /// </summary>
        /// <param name="debugInfo"></param>
        public void SetDebugInfo(String debugInfo)
        {            this.DebugInfo = debugInfo;
        }        public Hashtable GetAllParameters()
        {            return this.Parameters;
        }        protected virtual string GetCharset()
        {            return this.HttpContext.Request.ContentEncoding.BodyName;
        }
    }

3、呼叫現金紅包處理類別:

 /// <summary>
 /// 企业号微信支付接口
 /// </summary>
 public static class TenPay
 {     #region 企业向用户发红包
     /// <summary>
     /// 用于企业向微信用户个人发红包
     /// 目前支持向指定微信用户的openid个人发红包
     /// </summary>
     /// <param name="certPassword">apiclient_cert.p12证书密码即商户号</param>
     /// <param name="data">微信支付需要post的xml数据</param>
     /// <param name="certPath">apiclient_cert.p12的证书物理位置(例如:E:\projects\文档\微信商户平台证书\商户平台API证书</param>
     /// <param name="timeOut"></param>
     /// <returns></returns>
     public static string Sendredpack(string data, string certPassword,string certPath, int timeOut = Config.TIME_OUT)
     {         var urlFormat = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";         string cert = certPath;

         ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
         X509Certificate2 cer = new X509Certificate2(cert, certPassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);         var formDataBytes = data == null ? new byte[0] : Encoding.UTF8.GetBytes(data);
         MemoryStream ms = new MemoryStream();
         ms.Write(formDataBytes, 0, formDataBytes.Length);
         ms.Seek(0, SeekOrigin.Begin);//设置指针读取位置

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlFormat);
         request.ClientCertificates.Add(cer);
         request.Method = "POST";
         request.Timeout = timeOut;

         request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";         #region 输入二进制流
         if (ms != null)
         {
             ms.Position = 0;             //直接写入流
             Stream requestStream = request.GetRequestStream();             byte[] buffer = new byte[1024];             int bytesRead = 0;             while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
             {
                 requestStream.Write(buffer, 0, bytesRead);
             }
             ms.Close();//关闭文件访问
         }         #endregion

         HttpWebResponse response = (HttpWebResponse)request.GetResponse();         using (Stream responseStream = response.GetResponseStream())
         {             using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
             {                 string retString = myStreamReader.ReadToEnd();                 return retString;
             }
         }
     }     private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
     {         if (errors == SslPolicyErrors.None)             return true;         return false;
     }     #endregion

 }

4、呼叫現金紅包介面

#region 发送红包bool fals = false;   //记录发送红包是否成功string xmlResult = null;  //现金红包接口返回的xmlstring certPath = null;  //证书在服务器的物理位置string data = null;  //调用现金红包接口需要的数据try
{
    //创建支付应答对象
    RequestHandler packageReqHandler = new RequestHandler(null);
    //初始化
    packageReqHandler.Init();
    string nonceStr = TenPayV3Util.GetNoncestr();  //时间戳
    //设置package订单参数
    packageReqHandler.SetParameter("nonce_str", nonceStr);    //随机字符串,不长于32位
    packageReqHandler.SetParameter("mch_billno", System.Configuration.ConfigurationManager.AppSettings["TenPayV3_MchId"] + model.JournalNumber);//商户订单号(每个订单号必须唯一)组成:mch_id+yyyymmdd+10位一天内不能重复的数字。接口根据商户订单号支持重入,如出现超时可再调用。
    packageReqHandler.SetParameter("mch_id", System.Configuration.ConfigurationManager.AppSettings["TenPayV3_MchId"]);  //微信支付分配的商户号
    packageReqHandler.SetParameter("wxappid", System.Configuration.ConfigurationManager.AppSettings["TenPayV3_AppId"]);//微信分配的公众账号ID(企业号corpid即为此appId)。接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。 
    packageReqHandler.SetParameter("send_name", "测试");//商户名称
    packageReqHandler.SetParameter("re_openid", model.BankCard);  //用户openid  接受红包的用户用户在wxappid下的openid
    packageReqHandler.SetParameter("total_amount", Convert.ToInt32((decimal)(model.Amount * 100M)).ToString(CultureInfo.InvariantCulture));  //付款金额 单位分
    packageReqHandler.SetParameter("total_num", "1");  //红包发放总人数
    packageReqHandler.SetParameter("wishing", "测试红包");  //红包祝福语
    packageReqHandler.SetParameter("client_ip", HttpContext.Current.Request.UserHostAddress);//Ip地址
    packageReqHandler.SetParameter("act_name", "测试红包");//活动名称
    packageReqHandler.SetParameter("remark", "测试红包");     //备注
    string sign = packageReqHandler.CreateMd5Sign("key", System.Configuration.ConfigurationManager.AppSettings["TenPayV3_Key"]);
    packageReqHandler.SetParameter("sign", sign);                        //签名
    data = packageReqHandler.ParseXML();
    certPath = Server.MapPath("~/") + System.Configuration.ConfigurationManager.AppSettings["certPath"];
    xmlResult = Sendredpack(data, System.Configuration.ConfigurationManager.AppSettings["TenPayV3_MchId"],certPath);
    var res = XDocument.Parse(xmlResult);
    string return_code = res.Element("xml").Element("return_code").Value;
    if ("SUCCESS".Equals(return_code))
    {
        string result_code = res.Element("xml").Element("result_code").Value;
        if ("SUCCESS".Equals(result_code))
        {
            fals = true;
        }
    }
}
catch (Exception exception)
{
}#endregion

注意:憑證所在資料夾權限,IIS必須有權限對該資料夾操作的權限。

相關文章:

PHP微信公眾號自動發送紅包API,php公眾紅包api

微信公眾號紅包介面開發PHP開發 CA憑證出錯,請登陸微信支付商家平台下載憑證

#相關影片:

微信公眾號前段-php微信介面開發實戰專案影片教學聊天機器人

以上是微信公眾號開發:商家如何為用戶發紅包實例講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn