亚洲熟妇无码久久精品,最近中文字幕在线看免费完整版,亚洲AV中文无码字幕色三,一级a性色生活片久久毛片,一本大道香蕉综合视频在线观看

關(guān)于ASP.NET微信公眾號用于給指定OpenId用戶發(fā)送紅包

發(fā)布于: 2019-01-26    瀏覽: 5259    作者:Wu

本人是長沙軟件開發(fā)公司簡界程序員小伍,下面給大家分享一個功能(關(guān)于ASP.NET微信公眾號用于給指定OpenId用戶發(fā)送紅包)。

微信公眾號要實現(xiàn)對指定用戶發(fā)送紅包,必須指定一個存放兵發(fā)放金額的商戶號,在微信商戶平臺里面申請商戶號并獲取相關(guān)參數(shù)例如發(fā)送紅包所要用到的安全證書apiclient_cert.p12。下面分幾步驟講解:

紅包功能簡單介紹:

1、商戶調(diào)用接口時,通過指定發(fā)送對象以及發(fā)送金額的方式發(fā)放紅包,這樣的方式,允許商戶靈活的應(yīng)用于各種各樣豐富的活動場景

2、領(lǐng)取到紅包后,用戶的資金直接進入微信零錢,避免繁復(fù)的領(lǐng)獎流程,帶給用戶微信支付原生的流暢體驗

現(xiàn)金紅包官網(wǎng)文檔地址:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1

調(diào)用現(xiàn)金紅包接口需要使用到證書,請前往商戶平臺下載證書

官網(wǎng)有關(guān)詳細(xì)證書的介紹:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3

因為發(fā)送現(xiàn)金紅包是從商戶平臺余額扣款,所以商戶平臺的賬戶余額必須有充足的余額

一、獲取關(guān)注微信公眾號用戶OpenId

在微信商戶平臺獲取以下參數(shù):MchId【微信支付商戶號】、AppId【應(yīng)用ID】、AppSecret【應(yīng)用密鑰】、AppKey【API密鑰】

private void GetUserOpenId()
{
string code = Request.QueryString["code"];

string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", PayConfig.AppId, PayConfig.AppSecret, code);

string returnStr = HttpUtil.Send("", url);

var obj = JsonConvert.DeserializeObject<OpenModel>(returnStr);

url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", PayConfig.AppId, obj.refresh_token);
returnStr = HttpUtil.Send("", url);
obj = JsonConvert.DeserializeObject<OpenModel>(returnStr);

url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", obj.access_token, obj.openid);
returnStr = HttpUtil.Send("", url);
acc = obj.access_token;
Session["acc"] = acc;
OpenId = obj.openid;

}

二、構(gòu)造發(fā)送紅包金額數(shù)及實體類參數(shù)

/// <summary>
/// 構(gòu)造參數(shù)
/// </summary>
/// <returns></returns>
public string GetJsApiParameters()
{
#region 紅包金額隨機數(shù)

Random rd1 = new Random();
int a = rd1.Next(30,100);//生成0.3-1元之間的隨機數(shù)(金額單位/分)
if (a > 0)
{
//float f = (float)(a * 0.01);
//Num = f.ToString();
Num = a.ToString();
}
else if (a <30||a>100)
{
Num = "30";//若紅包金額出現(xiàn)異常,設(shè)置固定金額為0.3元
}

#endregion 紅包金額隨機數(shù)

int iMin = 1000;
int iMax = 9999;
Random rd = new Random();//構(gòu)造隨機數(shù)
string strMch_billno = PayConfig.MchId + DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(iMin, iMax).ToString();//商戶訂單號
//隨機字符串 
NonceStr = TenpayUtil.getNoncestr();

//創(chuàng)建支付應(yīng)答對象
var packageReqHandler = new RequestHandler(Context);
//初始化
packageReqHandler.init();
packageReqHandler.setParameter("act_name", "活動名稱:紅包抽獎");
packageReqHandler.setParameter("client_ip", PayConfig.IP);//調(diào)用的域名IP
packageReqHandler.setParameter("mch_billno", strMch_billno);//商戶訂單號
packageReqHandler.setParameter("mch_id", PayConfig.MchId);//商戶號
packageReqHandler.setParameter("nonce_str", NonceStr);//隨機數(shù)
packageReqHandler.setParameter("remark", "備注信息");//這里需要填寫繳費用戶的信息(編號、名稱)
packageReqHandler.setParameter("re_openid", OpenId);//發(fā)送的指定的OpenId
packageReqHandler.setParameter("send_name", "發(fā)送方名稱");//發(fā)送方
packageReqHandler.setParameter("total_amount", Num);//發(fā)送金額
packageReqHandler.setParameter("total_num", "1");//發(fā)送紅包個數(shù)
packageReqHandler.setParameter("wishing", "感謝您參加紅包抽獎活動,祝您生活愉快!");//備注
packageReqHandler.setParameter("wxappid", PayConfig.AppId);//appId應(yīng)用ID
//packageReqHandler.setParameter("scene_id", PRODUCT_2);
packageReqHandler.setParameter("sign", packageReqHandler.CreateMd5Sign("key", PayConfig.AppKey));//生成簽名

string parameters = packageReqHandler.parseXML();//構(gòu)造XML
return parameters;
LogUtil.WriteLog("返回構(gòu)造參數(shù)XML========" + parameters);
}

三、定義紅包接口URL

string strUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";//發(fā)送紅包的API接口

四、定義微信認(rèn)證證書接口及要解決的返回Json數(shù)據(jù)

/// <summary>
/// 微信需要證書接口
/// </summary>
/// <param name="url"></param>
/// <param name="postdata"></param>
/// <returns></returns>
public string Postxmltourl(string url, 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);
// 準(zhǔn)備請求... 
try
{
//CerPath證書路徑,這里是本機的路徑,實際應(yīng)用中,按照實際情況來填寫
string certPath = @"D:\NOTDELETE\DEMO\apiclient_cert.p12";
//證書密碼
string password = "證書密碼為微信商戶支付號";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

// X509Certificate2 cert = new X509Certificate2(certPath, password, X509KeyStorageFlags.MachineKeySet);
X509Certificate2 cert = new X509Certificate2(certPath, password);
// 設(shè)置參數(shù) 
request = WebRequest.Create(url) 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, 0, data.Length);
outstream.Close();
//發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù) 
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求 
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回結(jié)果網(wǎng)頁(html)代碼 
string content = sr.ReadToEnd();
string err = string.Empty;
return content;

}
catch (Exception ex)//獲取異常錯誤信息
{
string err = ex.Message;
return string.Empty;
}

五、定義結(jié)果接受證書請求

string strResult = Postxmltourl(strUrl, strData);

在線客服

售前咨詢

售后服務(wù)

投訴/建議

服務(wù)熱線
0731-83091505
18874148081
亚洲熟妇无码久久精品,最近中文字幕在线看免费完整版,亚洲AV中文无码字幕色三,一级a性色生活片久久毛片,一本大道香蕉综合视频在线观看