260 lines
11 KiB
C#
260 lines
11 KiB
C#
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();
|
|
public string appKey = "", IFApp = "0", strBillNo="";
|
|
private string appSecret = "";
|
|
|
|
Mtxfw.DAL.user_Results daoResults = new Mtxfw.DAL.user_Results();
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
string str_u = Request.ServerVariables["HTTP_USER_AGENT"].Trim().ToLower();
|
|
if (str_u.IndexOf("micromessenger") != -1)
|
|
{
|
|
IFApp = "1";
|
|
}
|
|
string hostname = Request.Url.Host;
|
|
|
|
if (!IsPostBack)
|
|
{
|
|
|
|
try
|
|
{
|
|
string strWeixin_OpenID = "";
|
|
|
|
|
|
if (!String.IsNullOrEmpty(Request.QueryString["PayID"]))
|
|
{
|
|
strBillNo = Request.QueryString["PayID"].ToString();
|
|
|
|
Mtxfw.Model.user_Results ReModel = daoResults.GetModel2(2, strBillNo);
|
|
if (ReModel != null)
|
|
{
|
|
paybill.Text = ReModel.Totalmoney.ToString("f2");
|
|
if (ReModel.Seef == 0)
|
|
{
|
|
if (IFApp == "1")
|
|
{
|
|
appKey = config.webappKey;
|
|
appSecret = config.webappSecret;
|
|
|
|
|
|
if (Session["gzptaccess_token"] == null)
|
|
{
|
|
if (Request.QueryString["code"] != null)
|
|
{
|
|
|
|
string code = Request.QueryString["code"].ToString();
|
|
WXaccess_token ac = getgzptaccess_token(code, appKey, appSecret);
|
|
strWeixin_OpenID = ac.openid;
|
|
|
|
}
|
|
else
|
|
{
|
|
//Response.Redirect(Mtxfw.Utility.Interface_WxPay.OAuth2_GetUrl_Pay(Request.RawUrl.ToString(), 1));
|
|
Getcode(config.webUrl + Request.RawUrl.ToString(), appKey);
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
WXaccess_token ac = new WXaccess_token();
|
|
ac.access_token = atoken[0];
|
|
ac.openid = atoken[1];
|
|
strWeixin_OpenID = ac.openid;
|
|
}
|
|
else
|
|
{
|
|
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
|
|
{
|
|
Getcode(config.webUrl + Request.RawUrl.ToString(), appKey);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
string _Pay_Package = Mtxfw.Utility.Interface_WxPay.Get_RequestHtml(config, HttpContext.Current, strWeixin_OpenID, strBillNo, Convert.ToDecimal(ReModel.Totalmoney), ReModel.conent, "Card_Store");
|
|
|
|
Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", _Pay_Package + "|OpenID=" + strWeixin_OpenID);
|
|
//微信jspai支付
|
|
if (_Pay_Package.Length > 0)
|
|
{
|
|
wx_packageValue = _Pay_Package;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string PageUrl = Mtxfw.Utility.Interface_WxPay.Get_RequestHtml5(config, HttpContext.Current, strBillNo, Convert.ToDecimal(ReModel.Totalmoney), ReModel.conent, "Card_Store");
|
|
Response.Redirect(PageUrl);
|
|
;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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("/?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("/?Error=openid" + str);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Response.Redirect("/?Error=atoken" + atoken.Length);
|
|
}
|
|
}
|
|
return ac;
|
|
}
|
|
}
|
|
} |