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; using System.Globalization; namespace Mtxfw.VipSite { public partial class pay : System.Web.UI.Page { public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config(""); Mtxfw.DAL.user_Results daoResults = new Mtxfw.DAL.user_Results(); public string IFApp = "0", responseBody = "", signature = "", noncestr = "", timestamp = "", openid = ""; public string timeStamp = "", nonceStr = "", paySign = "", signType = "", reResult = "",rurl=""; 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"; } if (!IsPostBack) { try { ////////////////////////////////////////////请求参数//////////////////////////////////////////// if (!String.IsNullOrEmpty(Request.QueryString["ResultId"])) { Int64 ResultId = Convert.ToInt64(Request.QueryString["ResultId"].ToString()); if (IFApp == "1") { if (Session["payopenid"] != null) { openid = Session["payopenid"].ToString(); } if (openid == "") { if (!String.IsNullOrEmpty(Request.QueryString["code"])) { string paycode = Request.QueryString["code"].ToString(); string weixinresult = Mtxfw.Utility.Common.getPage2("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + config.webappKey + "&secret=" + config.webappSecret + "&code=" + paycode + "&grant_type=authorization_code", "", "utf-8", false, false, ""); Mtxfw.Utility.Common.WriteHtml("/weixin/weixinresult.txt", weixinresult); if (weixinresult.IndexOf("openid") != -1) { Mtxfw.shop.WXaccess_token ac = null; ac = (Mtxfw.shop.WXaccess_token)FromJson2(weixinresult); openid = ac.openid; Session["payopenid"] = openid; } } else { rurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + config.webappKey + "&redirect_uri=" + UrlEncode(config.webUrl + "/pay/pay.aspx?t=1&ResultId=" + ResultId) + "&response_type=code&scope=snsapi_base&#wechat_redirect"; } } noncestr = Mtxfw.Utility.Common.CreatenNonce_str(); timestamp = Mtxfw.Utility.Common.timeStamp(); string jsapi_ticket = Mtxfw.Utility.Common.getjsapi_ticket(Context, config.webappKey, config.webappSecret, config); string string1 = ""; string tmpStr = Mtxfw.Utility.Common.GetSignature(jsapi_ticket, nonceStr, timestamp, config.webUrl + "/mobile/smpay", out string1); signature = tmpStr; } Mtxfw.Model.user_Results ReModel = daoResults.GetModel(ResultId); if (ReModel != null) { if (ReModel.utype == "2" && ReModel.Seef == 0) { if (IFApp == "1") { DateTime dt = DateTime.Now; string sys_id = config.hfpaysys_id; string product_id = config.hfpayproduct_id; string req_date = dt.ToString("yyyyMMdd"); string req_seq_id = ReModel.ListNumber; string huifu_id = config.hfpayMerchantID; string goods_desc = ReModel.conent; string trade_type = "T_JSAPI";//T_JSAPI: 微信公众号 T_MINIAPP: 微信小程序 A_JSAPI: 支付宝 A_NATIVE: 支付宝正扫 U_NATIVE: 银联正扫 U_JSAPI: 银联JS D_NATIVE: 数字人民币正扫 T_H5:微信直连H5支付 T_APP:微信APP支付 T_NATIVE:微信正扫 string wx_data = ""; wx_data = "{\\\"sub_appid\\\":\\\"" + config.webappKey + "\\\",\\\"sub_openid\\\":\\\"" + openid + "\\\"}"; string trans_amt = ReModel.b0.ToString("f2"); string time_expire = dt.AddMinutes(10).ToString("yyyyMMddHHmmss"); string notify_url = config.webUrl + "/pay/hfpay_notify_url.aspx"; SortedDictionary pay_dic = new SortedDictionary(); pay_dic.Add("req_date", req_date); pay_dic.Add("req_seq_id", req_seq_id); pay_dic.Add("huifu_id", huifu_id); pay_dic.Add("goods_desc", goods_desc); pay_dic.Add("trade_type", trade_type); if (wx_data != "") { pay_dic.Add("wx_data", wx_data); } pay_dic.Add("trans_amt", trans_amt); pay_dic.Add("time_expire", time_expire); pay_dic.Add("notify_url", notify_url); string get_PaySign = Mtxfw.Utility.Interface_WxPay.BuildRequest0(pay_dic, config.hfpayPrivateKey); string strq0 = "{"; strq0 += "\"sys_id\":\"" + sys_id + "\","; strq0 += "\"product_id\":\"" + product_id + "\","; strq0 += "\"data\":{"; strq0 += "\"req_date\":\"" + req_date + "\","; strq0 += "\"req_seq_id\":\"" + req_seq_id + "\","; strq0 += "\"huifu_id\":\"" + huifu_id + "\","; strq0 += "\"goods_desc\":\"" + goods_desc + "\","; strq0 += "\"trade_type\":\"" + trade_type + "\","; if (wx_data != "") { strq0 += "\"wx_data\":\"" + wx_data + "\","; } strq0 += "\"trans_amt\":\"" + trans_amt + "\","; strq0 += "\"time_expire\":\"" + time_expire + "\","; strq0 += "\"notify_url\":\"" + notify_url + "\""; strq0 += "}"; strq0 += ",\"sign\":\"" + get_PaySign + "\""; strq0 += "}"; string strResult = Mtxfw.Utility.Common.getPage2("https://api.huifu.com/v2/trade/payment/jspay", "", "utf-8", true, true, strq0); Mtxfw.Utility.Common.WriteHtml("/weixin/apiajaxstrResult.txt", strResult); huifuresult hr = (huifuresult)FromhuifuresultJson(strResult); if (hr.data.resp_code == "00000100") { huifuresult2 hr0 = (huifuresult2)Fromhuifuresult2Json(strResult); string pay_info = hr0.data.pay_info; hjpay_rc_Result hjrc_Result = (hjpay_rc_Result)Fromhjpay_rc_Result(pay_info); reResult = hjrc_Result.package; timeStamp = hjrc_Result.timeStamp; nonceStr = hjrc_Result.nonceStr; paySign = hjrc_Result.paySign; signType = hjrc_Result.signType; } else { responseBody = hr.data.resp_desc; Mtxfw.Utility.Common.WriteHtml("/weixin/apiajaxstrResulterr.txt", strResult + "|" + strq0); } } } } } } catch (Exception err) { Mtxfw.Utility.Common.WriteHtml("/weixin/weixin1.txt", err.ToString()); } } } public object Fromhuifupayresult1Json(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } public object FromhuifuresultJson(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } public object Fromhuifuresult1Json(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } public object Fromhuifuresult2Json(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } public object FromJson2(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } public object Fromhjpay_rc_Result(string json) { JavaScriptSerializer js = new JavaScriptSerializer(); return js.Deserialize(json); } /// /// url编码 /// /// The value to Url encode /// Returns a Url encoded string 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(); } } }