C#公众号开发之给用户发红包

2019-12-30 19:48:09刘景俊

红包功能简单介绍:

1、商户调用接口时,通过指定发送对象以及发送金额的方式发放红包,这样的方式,允许商户灵活的应用于各种各样丰富的活动场景

2、领取到红包后,用户的资金直接进入微信零钱,避免繁复的领奖流程,带给用户微信支付原生的流畅体验

现金红包官网文档地址

调用现金红包接口需要使用到证书,请前往商户平台下载证书

官网有关详细证书的介绍,点击查看

因为发送现金红包是从商户平台余额扣款,所以商户平台的账户余额必须有充足的余额

下面是调用红包接口详细代码:

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;
  }
}