第三方app抽奖发送微信红包
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抽奖发送微信红包的更多相关文章
- 第三方app抽奖发送微信红包实现
1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...
- 基于微信红包插件的原理实现android任何APP自动发送评论(已开源)
背景 地址:https://github.com/huijimuhe/postman 核心就是android的AccessibilityService,回复功能api需要23以上版本才行. 其实很像在 ...
- 微信授权登陆接入第三方App(步骤总结)Android
微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录
- (实用篇)php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中.内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <?php class ...
- PHP实现微信随机红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- Python微信红包算法
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- 小程序发微信红包后端Nodejs实现
前提条件 1.有一个微信开放平台 https://open.weixin.qq.com/ 2.有一个微信公众平台 https://mp.weixin.qq.com 并且开通微信支付 3.有一个微信小 ...
- PHP实现微信红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- [转载]Android平台第三方应用分享到微信开发
一.申请APPID 微信公共平台和微博分享一样,也需要申请一个ID,来作为调起微信.分享到微信的唯一标识. 申请微信APPID可以到微信平台http://open.weixin.qq.com/app/ ...
随机推荐
- 倒计时demo
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong,nonatom ...
- 一篇非常适合git入门的文章
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579 ...
- 安卓常用 widget
验证码 public class SpinnerImg extends ImageView { /** * 完成选择后启动另外一个spinner */ private ItemListener ite ...
- 【转】dmidecode命令详解
Dmidecode 这款软件允许你在 Linux 系统下获取有关硬件方面的信息.Dmidecode 遵循 SMBIOS/DMI 标准,其输出的信息包括 BIOS.系统.主板.处理器.内存.缓存等等. ...
- 青蛙的约会<数论,extgcd>
青蛙的约会 题意: 在一个圆上有一个零刻度点,公青蛙和母青蛙分别在A点和B点<不同的位 置>,他们每秒行走的距离分别是m和n,圆的周长是L.问题是这两个青 蛙能不能相遇,若能在什么时候相遇 ...
- Android自定义XML属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-style ...
- 如何在Eclipse中添加Servlet-api.jar的方法
方法一: 点击窗口->首选项->java->构建路径->类路径变量->新建:将你的tomcat目录下的common/lib/servlet.jar加进来.如果你建立了一个 ...
- If only it could be all the same like we first me
为什么 你当时对我好 Why? You nice to me at that time. 又为什么 现在变得冷淡了 Why? Now you give a cold shoulder to me. 我 ...
- 编程实现prim算法和Dijkstra算法。
网址链接:http://blog.csdn.net/anialy/article/details/7603170
- Linux 安装 Redis 服务
下载地址 http://download.redis.io/releases/redis-3.2.0.tar.gz 官网下载地址 http://redis.io/download 1.下载安装包 cd ...