Files
g.hnyhua.cn/Mtxfw.shop/pay/wxpay.aspx.cs

192 lines
7.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Text;
namespace Mtxfw.shop
{
public partial class wxpay : System.Web.UI.Page
{
protected string wx_packageValue = "";
public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config();
public Mtxfw.DAL.user_info daoUser = new Mtxfw.DAL.user_info();
private string appKey = "";
private string appSecret = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
appKey = config.webappKey;
appSecret = config.webappSecret;
string strBillNo = Mtxfw.Utility.Interface_WxPay.getTimestamp();
string strWeixin_OpenID = "";
if (Request.QueryString["code"] != null)
{
string code = Request.QueryString["code"].ToString();
WXaccess_token ac = getgzptaccess_token(code, appKey, appSecret);
strWeixin_OpenID = ac.openid;
string strWeixin_Token = ac.access_token;
}
else
{
//Response.Redirect(Mtxfw.Utility.Interface_WxPay.OAuth2_GetUrl_Pay(Request.RawUrl.ToString(), 1));
Getcode("http://shop.q-b.cc" + Request.RawUrl.ToString(), appKey);
}
if (!String.IsNullOrEmpty(Request["je"]) && !String.IsNullOrEmpty(Request["t"]) && !String.IsNullOrEmpty(Request["billbo"]) && strWeixin_OpenID != "")
{
string strbody = "在线充值";
strBillNo = Request["billbo"].ToString();
paybill.Text = Request["je"].ToString();
if (Request["t"].ToString() == "1")
{
strbody = "在线支付";
}
hidBillID.Value = strBillNo;
string _Pay_Package = Mtxfw.Utility.Interface_WxPay.Get_RequestHtml(HttpContext.Current, strWeixin_OpenID, strBillNo, Convert.ToDecimal(Request["je"].ToString()), strbody, "Card_Store");
//Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", _Pay_Package);
//微信jspai支付
if (_Pay_Package.Length > 0)
{
wx_packageValue = _Pay_Package;
}
}
}
catch (Exception err)
{
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin0.txt", err.ToString());
}
}
}
/// <summary>
/// 获取未授权的临时令牌
/// </summary>
private void Getcode(string strurl, string appID)
{
string url = "https://open.weixin.qq.com/connect/oauth2/authorize";
string[] parameters ={
"appid="+appID,
"redirect_uri=" + UrlEncode(strurl),
"response_type=code",
"scope=snsapi_userinfo",
"#wechat_redirect"
};
string tempParameters = string.Empty;
for (int i = 0; i < parameters.Length; i++)
{
if (i != parameters.Length - 1)
{
tempParameters += parameters[i] + "&";
}
else
{
tempParameters += parameters[i] + "";
}
}
url = url + "?" + tempParameters + "";
Response.Redirect(url);
//ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>top.location='" + url + "';</script>");
}
/// <summary>
/// url编码
/// </summary>
/// <param name="value">The value to Url encode</param>
/// <returns>Returns a Url encoded string</returns>
private string UrlEncode(string value)
{
string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
StringBuilder result = new StringBuilder();
foreach (char symbol in value)
{
if (unreservedChars.IndexOf(symbol) != -1)
{
result.Append(symbol);
}
else
{
result.Append('%' + String.Format("{0:X2}", (int)symbol));
}
}
return result.ToString();
}
public object FromJson2(string json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<WXaccess_token>(json);
}
public WXaccess_token getgzptaccess_token(string code, string appID, string appSecret)
{
WXaccess_token ac = null;
string[] Aaccess = new string[2];
string straccess_token = "";
if (Session["gzptaccess_token"] == null)
{
string str = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code", "", "utf-8");
if (str.IndexOf("openid") != -1)
{
ac = (WXaccess_token)FromJson2(str);
Session["gzptaccess_token"] = ac.access_token + "|" + ac.openid + "|" + DateTime.Now;
}
else
{
Response.Redirect("/login.aspx?Error=appid|" + appID + "|secret" + appSecret + "|code" + code + "");
}
}
else
{
string[] atoken = Session["gzptaccess_token"].ToString().Split('|');
if (atoken.Length > 2)
{
bool b = true;
if (atoken[2] != "")
{
DateTime dt = Convert.ToDateTime(atoken[2]);
DateTime dt1 = DateTime.Now;
DateTime dt2 = dt.AddSeconds(7000);
if (DateTime.Compare(dt2, dt1) < 0)
{
b = false;
}
}
if (b)
{
straccess_token = atoken[0];
ac = new WXaccess_token();
ac.access_token = atoken[0];
ac.openid = atoken[1];
}
else
{
string str = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code", "", "utf-8");
if (str.IndexOf("openid") != -1)
{
ac = (WXaccess_token)FromJson2(str);
Session["gzptaccess_token"] = ac.access_token + "|" + ac.openid + "|" + DateTime.Now;
}
else
{
Response.Redirect("/login.aspx?Error=openid" + str);
}
}
}
else
{
Response.Redirect("/login.aspx?Error=atoken" + atoken.Length);
}
}
return ac;
}
}
}