1、控制器方法:

 private string SendRedPackge(string OpenId, int Amount, string LuckyCode)
{
Models.PayWeiXinModel pModel = new Models.PayWeiXinModel();
Html_Tools.Common.PayForWeiXinHelp PayHelp = new Html_Tools.Common.PayForWeiXinHelp();
string result = string.Empty; //接叐收红包的用户 用户在wxappid下的openid
pModel.re_openid = OpenId;//"oFIYdszuDXVqVCtwZ-yIcbIS262k";
//付款金额,单位分
pModel.total_amount = Amount;
//最小红包金额,单位分
pModel.min_value = Amount;
//最大红包金额,单位分
pModel.max_value = Amount;
//商户订单号
pModel.mch_billno = LuckyCode;
//调用方法
string postData = PayHelp.DoDataForPayWeiXin(pModel);
//发送
result = PayHelp.PayForWeiXin(postData);
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
var rmodel=new Models.WeiXinRedPackgeResultModel();
Html_Tools.Common.WeiXinMPHelper.FillEntityFromXml(rmodel, doc);
if (rmodel.result_code.Equals("SUCCESS"))
{
return "红包已成功发放,请及时领取";
}
else
{
Html_Tools.Common.ToolsHelper.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 发送红包异常信息:" + result + "\r\n");
          pBll.DrawPrize(OpenId, LuckyCode, false);
return "不好意思,红包发送出错啦。";
}
}

2、工具类PayForWeiXinHelp方法:

 public class PayForWeiXinHelp
{ /// <summary>
/// 调用微信支付接口前处理数据,包括sign验证等
/// </summary>
/// <param name="payForWeiXin"></param>
/// <returns></returns>
public string DoDataForPayWeiXin(Models.PayWeiXinModel payForWeiXin)
{
#region 处理nonce_str随机字符串,不长于 32 位(本程序生成长度为16位的)
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
payForWeiXin.nonce_str = RandomStr(str, );
#endregion #region 商户信息从config文件中读取
//商户支付密钥key
string key = WeiXinConfig.key;
//商户号
payForWeiXin.mch_id = WeiXinConfig.mch_id;
//商户 appid
payForWeiXin.wxappid = WeiXinConfig.wxappid;
//提供方名称
payForWeiXin.nick_name = WeiXinConfig.nick_name;
//红包収送者名称
payForWeiXin.send_name = WeiXinConfig.send_name;
//红包収放总人数
payForWeiXin.total_num = int.Parse(WeiXinConfig.total_num);
//红包祝福诧
payForWeiXin.wishing = WeiXinConfig.wishing;
//活劢名称
payForWeiXin.act_name = WeiXinConfig.act_name;
payForWeiXin.act_id = "act_id";
//备注信息
payForWeiXin.remark = WeiXinConfig.remark;
//商户logo的url
payForWeiXin.logo_imgurl = WeiXinConfig.logo_imgurl;
//分享文案
payForWeiXin.share_content = WeiXinConfig.share_content;
//分享链接
payForWeiXin.share_url = WeiXinConfig.share_url;
//分享的图片url
payForWeiXin.share_imgurl = WeiXinConfig.share_imgurl;
//调用接口的机器 Ip 地址
payForWeiXin.client_ip = WeiXinConfig.client_ip;
#endregion #region 订单信息
//生成订单号组成: mch_id+yyyymmdd+10 位一天内不能重复的数字
//生成10位不重复的数字
payForWeiXin.mch_billno = payForWeiXin.mch_billno+System.DateTime.Now.ToString("yyyyMMddHHmmss");
#endregion string postData = @"<xml>
<mch_billno><![CDATA[{0}]]></mch_billno>
<mch_id><![CDATA[{1}]]></mch_id>
<wxappid><![CDATA[{2}]]></wxappid>
<send_name><![CDATA[{3}]]></send_name>
<re_openid><![CDATA[{4}]]></re_openid>
<total_amount><![CDATA[{5}]]></total_amount>
<total_num><![CDATA[{6}]]></total_num>
<wishing><![CDATA[{7}]]></wishing>
<client_ip><![CDATA[{8}]]></client_ip>
<act_name><![CDATA[{9}]]></act_name>
<remark><![CDATA[{10}]]></remark>
<nonce_str><![CDATA[{11}]]></nonce_str>";
postData = string.Format(postData,
payForWeiXin.mch_billno,
payForWeiXin.mch_id,
payForWeiXin.wxappid,
payForWeiXin.send_name,
payForWeiXin.re_openid,
payForWeiXin.total_amount,
payForWeiXin.total_num,
payForWeiXin.wishing,
payForWeiXin.client_ip,
payForWeiXin.act_name,
payForWeiXin.remark,
payForWeiXin.nonce_str
); //原始传入参数
string[] signTemp = { "mch_billno=" + payForWeiXin.mch_billno, "mch_id=" + payForWeiXin.mch_id, "wxappid=" + payForWeiXin.wxappid, "send_name=" + payForWeiXin.send_name, "re_openid=" + payForWeiXin.re_openid, "total_amount=" + payForWeiXin.total_amount, "total_num=" + payForWeiXin.total_num, "wishing=" + payForWeiXin.wishing, "client_ip=" + payForWeiXin.client_ip, "act_name=" + payForWeiXin.act_name, "remark=" + payForWeiXin.remark, "nonce_str=" + payForWeiXin.nonce_str }; List<string> signList = signTemp.ToList(); #region 处理支付签名
//对signList按照ASCII码从小到大的顺序排序
signList.Sort(); string signOld = string.Empty;
//string payForWeiXinOld = string.Empty;
int i = ;
foreach (string temp in signList)
{
signOld += temp + "&";
i++;
}
//signOld = signOld.Substring(0, signOld.Length - 1);
//拼接Key
signOld += "key=" + key;
//处理支付签名
payForWeiXin.sign = GetMD5Hash(signOld).ToUpper();
#endregion
postData += "<sign><![CDATA[{0}]]></sign></xml>";
postData = string.Format(postData, payForWeiXin.sign);
return postData;
} /// <summary>
/// 调用微信支付接口
/// </summary>
/// <param name="payForWeiXin"></param>
/// <returns></returns>
public string PayForWeiXin(string postData)
{
string result = string.Empty;
try
{
result = PostPage("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", postData);
}
catch (Exception ex)
{ }
return result;
} /// <summary>
/// post微信请求
/// </summary>
/// <param name="posturl"></param>
/// <param name="postData"></param>
/// <returns></returns>
public string PostPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
//CerPath证书路径
string certPath = WeiXinConfig.certPath;
//证书密码
string password = WeiXinConfig.password;
X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(certPath, password, X509KeyStorageFlags.MachineKeySet); // 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = data.Length;
request.ClientCertificates.Add(cert);
outstream = request.GetRequestStream();
outstream.Write(data, , data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content; }
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
} public string RandomStr(string str, int Length)
{
string result = string.Empty;
Random rd = new Random();
for (int i = ; i < Length; i++)
{
result += str[rd.Next(str.Length)];
}
return result;
} /// <summary>
/// Md5加密
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static String Encrypt(String s)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = ; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], ).PadLeft(, '');
}
return ret.PadLeft(, '');
} public static string GetMD5Hash(string value)
{
var md5 = MD5.Create();
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
var sb = new StringBuilder();
foreach (byte b in bs)
{
sb.Append(b.ToString("x2"));
}
//所有字符转为大写
return sb.ToString();
} public static void FillEntityFromXml<T>(this T entity, System.Xml.XmlDocument doc)
{
Dictionary<string, string> Dic = new Dictionary<string, string>();
System.Xml.XmlNodeList nodelist = doc.DocumentElement.ChildNodes;
if (nodelist.Count > )
{
Type t = entity.GetType();
foreach (System.Xml.XmlElement el in nodelist)//读元素值
{
System.Reflection.PropertyInfo proInfo = t.GetProperty(el.Name);//访问Obj的类型的属性Value的属性信息(propertyInfo)
if (proInfo != null)
{
string str2 = proInfo.PropertyType.Name;
if (str2 == null)
{
goto Label_018A;
}
if (!(str2 == "DateTime"))
{
if (str2 == "Boolean")
{
goto Label_00E9;
}
if (str2 == "Int64")
{
goto Label_0134;
}
goto Label_018A;
}
proInfo.SetValue(entity, new DateTime(long.Parse(el.InnerText)), null);
}
continue;
Label_00E9:
proInfo.SetValue(entity, el.InnerText == "", null);
continue;
Label_0134:
proInfo.SetValue(entity, long.Parse(el.InnerText), null);
continue;
Label_018A:
proInfo.SetValue(entity, el.InnerText, null);
}
}
} }

第三方app抽奖发送微信红包的更多相关文章

  1. 第三方app抽奖发送微信红包实现

    1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...

  2. 基于微信红包插件的原理实现android任何APP自动发送评论(已开源)

    背景 地址:https://github.com/huijimuhe/postman 核心就是android的AccessibilityService,回复功能api需要23以上版本才行. 其实很像在 ...

  3. 微信授权登陆接入第三方App(步骤总结)Android

    微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录

  4. (实用篇)php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)

    微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中.内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <?php class ...

  5. PHP实现微信随机红包算法和微信红包的架构设计简介

    微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...

  6. Python微信红包算法

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  7. 小程序发微信红包后端Nodejs实现

    前提条件 1.有一个微信开放平台 https://open.weixin.qq.com/ 2.有一个微信公众平台 https://mp.weixin.qq.com  并且开通微信支付 3.有一个微信小 ...

  8. PHP实现微信红包算法和微信红包的架构设计简介

    微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...

  9. [转载]Android平台第三方应用分享到微信开发

    一.申请APPID 微信公共平台和微博分享一样,也需要申请一个ID,来作为调起微信.分享到微信的唯一标识. 申请微信APPID可以到微信平台http://open.weixin.qq.com/app/ ...

随机推荐

  1. 用For Each语句对Session.Contents树组进行遍历

    <%@ LANGUAGE=VBScript codepage ="936" %> <% Option Explicit %> 您的sessionID号是:& ...

  2. C#后台绑定ComboBox

    C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  3. 【威佐夫博奕】 betty定理 poj 1067

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  4. 【iOS 】UIView 中有一个autoresizingMask的属性

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum  ...

  5. tab奇偶行颜色交替+插件

    (function($){ $.fn.tableUI=function(options){ var defaults={ evenRowclass:"evenRow", oddro ...

  6. Git本地项目上传 & SourceTree & GitHub 简单使用

    Git(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理 ...

  7. Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)

    Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...

  8. 转载 Deep learning:四(logistic regression练习)

    前言: 本节来练习下logistic regression相关内容,参考的资料为网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage ...

  9. lucene-SpanNotQuery和SpanOrQuery交迭与全局跨度

    1.在匹配结果中排除相互交迭的跨度 SpanNotQuery构造函数的第一个参数表示要包含的跨度对象,第二个参数表示要排除的跨度对象. 1) SpanNearQuery quick_fox=new S ...

  10. CC_CALLBACK之间的区别

    #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_A ...