111,092
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Security;
using System.Net;
using System.Xml;
using System.IO;
public partial class SaoMa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random rnd = new Random();
string n = rnd.Next(1000, 9999).ToString();
this.Label1.Text = DateTime.Now.ToString("yyyyMMddHHmmss")+n; //订单号
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
WxPayHelper wxPayHelper = new WxPayHelper();
//先设置基本信息
string appid = "wx208736198e224c75";
string mch_id = "1419898802";
wxPayHelper.SetAppId("wx208736198e224c75");
wxPayHelper.SetAppKey("ddewxvrwaz561063793xzkwangka6841");
wxPayHelper.SetPartnerKey("ddewxvrwaz561063793xzkwangka6841");//商户key
wxPayHelper.SetMatchID("1419898802");
wxPayHelper.SetSignType("sha1");
string orderid = this.Label1.Text; //订单号
// string purl = wxPayHelper.CreateNativeUrl(orderid);
//生成与支付订单
//根据订单号获取订单信息数据 金额 等
string device_info = "1234"; //设备号
string nonce_str = CommonUtil.CreateNoncestr();
string body = "微网咖扫码支付"; //商品描述
string detail = "现场扫码付款";//商品详情
string attach = "1";//附加数据,原样返回;
string out_trade_no = orderid;//订单号
string total_fee = this.TextBox1.Text.Trim();
// string total_fee = (decimal.Parse(this.TextBox1.Text.Trim()) * 100).ToString(); //订单金额 以分为单位 1 即表示 0.01元
string fee_type = "1";//1 人民币
string notify_url = "http://xzk.abcd.com/Sm/nurl.aspx";//支付成功后返回执行订单操作的路径
string spbill_create_ip = Request.UserHostAddress.ToString();
string time_start = DateTime.Now.ToString("yyyyMMddHHmmss"); //交易时间
string time_expire = DateTime.Now.AddHours(12).ToString("yyyyMMddHHmmss");//交易结束时间
string goods_tag = "扫码"; //商品标记
string trade_type = "NATIVE"; //交易类型
string product_id = orderid;
Dictionary<string, string> dicArray = new Dictionary<string, string>();
dicArray.Add("appid", appid);
dicArray.Add("body", body);
dicArray.Add("attach", attach);
dicArray.Add("device_info", device_info);
dicArray.Add("out_trade_no", out_trade_no);
dicArray.Add("total_fee", total_fee);
dicArray.Add("notify_url", notify_url);
dicArray.Add("spbill_create_ip", spbill_create_ip);
dicArray.Add("time_start", time_start);
dicArray.Add("time_expire", time_expire);
dicArray.Add("nonce_str", CommonUtil.CreateNoncestr());
dicArray.Add("goods_tag", goods_tag);
dicArray.Add("product_id", product_id);
dicArray.Add("mch_id", mch_id);
dicArray.Add("trade_type", trade_type);
string bizString = CommonUtil.FormatBizQueryParaMap(dicArray, false);
string nwesign = wxPayHelper.Sign(bizString);
dicArray.Add("sign", nwesign);
string package = wxPayHelper.Getpostorderxml(dicArray);
string postorder = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//Response.ContentType = "xml";
//Response.Write(package);
//Response.End();
string rexml = RequestUrl(postorder, package, "post", "xml", "utf-8");
XmlDocument xd = new XmlDocument();
xd.LoadXml(rexml);
XmlElement xe = xd.DocumentElement;
string return_code = "";
string return_msg = "";
string appid1 = "";
string mch_id1 = "";
string nonce_str1 = "";
string sign1 = "";
string result_code1 = "";
string prepay_id = "";
string trade_type1 = "";
string code_url = "";
foreach (XmlNode item in xe.ChildNodes)
{
if (item.LocalName == "return_code") return_code = item.InnerText;
if (item.LocalName == "return_msg") return_msg = item.InnerText;
if (item.LocalName == "appid") appid1 = item.InnerText;
if (item.LocalName == "mch_id") mch_id1 = item.InnerText;
if (item.LocalName == "nonce_str") nonce_str1 = item.InnerText;
if (item.LocalName == "sign") sign1 = item.InnerText;
if (item.LocalName == "result_code") result_code1 = item.InnerText;
if (item.LocalName == "prepay_id") prepay_id = item.InnerText;
if (item.LocalName == "trade_type") trade_type1 = item.InnerText;
if (item.LocalName == "trade_type") trade_type1 = item.InnerText;
if (item.LocalName == "code_url") code_url = item.InnerText;
}
/*第三方接口生成二维码*/
byte[] buffer = Encoding.Default.GetBytes(code_url);
string text = Encoding.GetEncoding("UTF-8").GetString(buffer);
Image1.ImageUrl = "http://qr.liantu.com/api.php?text=" + text.Replace("&", "%26");
}
protected string RequestUrl(string url, string data, string method, string contentType, string charset)
{
var request = WebRequest.Create(url);
request.Method = method;
request.ContentType = contentType;
request.Headers.Add("charset:" + charset);
var encoding = Encoding.GetEncoding(charset);
if (data != null)
{
byte[] buffer = encoding.GetBytes(data);
request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length);
}
else { request.ContentLength = 0; }
using (HttpWebResponse wr = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))
{ return reader.ReadToEnd(); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Xml;
using System.IO;
public partial class nurl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StreamReader reader = new StreamReader(Request.InputStream);
string xmlData = reader.ReadToEnd();
if (xmlData != "")
{
string appid = ""; //公众账号id
string attach = "";//上架数据包
string device_info = "";//设备号
string bank_type = ""; //付款银行
string fee_type = ""; //货币类型
string is_subscribe = "";//是否关注公众号
string mch_id = ""; //商户号
string nonce_str = ""; //随机码
string openid = ""; //用户标识
string out_trade_no = ""; //订单号
string result_code = ""; //业务结果
string return_msg = "";//返回信息
string return_code = "";//返回状态吗
string sign = ""; //签名
string time_end = "";//支付完成时间
string cash_fee = ""; //现金支付金额
string cash_fee_type = "";//现金支付货币类型
string total_fee = ""; //总金额
string trade_type = ""; //交易类型
string transaction_id = "";
string err_code = ""; //错误代码
string err_code_des = ""; //错误代码描述
XmlDocument xd = new XmlDocument();
xd.LoadXml(xmlData);
XmlElement xe = xd.DocumentElement;
foreach (XmlNode item in xe.ChildNodes)
{
if (item.LocalName == "appid") appid = item.InnerText; //公众账号id
if (item.LocalName == "attach") attach = item.InnerText;//上架数据包
if (item.LocalName == "device_info") device_info = item.InnerText;//设备号
if (item.LocalName == "bank_type") bank_type = item.InnerText; //付款银行
if (item.LocalName == "fee_type") fee_type = item.InnerText; //货币类型
if (item.LocalName == "is_subscribe") is_subscribe = item.InnerText;//是否关注公众号
if (item.LocalName == "mch_id") mch_id = item.InnerText; //商户号
if (item.LocalName == "nonce_str") nonce_str = item.InnerText; //随机码
if (item.LocalName == "openid") openid = item.InnerText; //用户标识
if (item.LocalName == "out_trade_no") out_trade_no = item.InnerText; //订单号
if (item.LocalName == "result_code") result_code = item.InnerText; //业务结果
if (item.LocalName == "return_msg") return_msg = item.InnerText;//返回信息
if (item.LocalName == "return_code") return_code = item.InnerText;//返回状态吗
if (item.LocalName == "sign") sign = item.InnerText; //签名
if (item.LocalName == "time_end") time_end = item.InnerText;//支付完成时间
if (item.LocalName == "cash_fee") cash_fee = item.InnerText; //现金支付金额
if (item.LocalName == "cash_fee_type") cash_fee_type = item.InnerText;//现金支付货币类型
if (item.LocalName == "total_fee") total_fee = item.InnerText; //总金额
if (item.LocalName == "trade_type") trade_type = item.InnerText; //交易类型
if (item.LocalName == "transaction_id") transaction_id = item.InnerText;
if (item.LocalName == "err_code") err_code = item.InnerText; //错误代码
if (item.LocalName == "err_code_des") err_code_des = item.InnerText; //错误代码描述
}
if (return_code == "SUCCESS") //
{
WxPayHelper wxPayHelper = new WxPayHelper();
string PartnerKey = "ddewxvrwaz561063793xzkwangka6841";
wxPayHelper.SetAppId("wx208736198e224c75");
wxPayHelper.SetAppKey("ddewxvrwaz561063793xzkwangka6841"); //支付key
wxPayHelper.SetPartnerKey(PartnerKey);//商户key
//验证签名
Dictionary<string, string> dicAr = new Dictionary<string, string>();
dicAr.Add("device_info", return_msg);
dicAr.Add("appid", appid);
dicAr.Add("mch_id", mch_id);
dicAr.Add("nonce_str", nonce_str);
dicAr.Add("result_code", result_code);
string endsign = wxPayHelper.Sign(CommonUtil.FormatBizQueryParaMap(dicAr, false));
dicAr.Add("sign", endsign);
//验证签名 和 结果信息
if (result_code == "SUCCESS" && endsign.ToUpper() == sign.ToUpper())
{
//执行订单处理
//处理订单成功后 返回success 失败 返回其他字符fail 以便 微信平台间断性向商户服务器请求
Response.Write(@"<xml> <return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>");
string flag = "SUCCESS";
string usertel = "00000000000";
string addtype = "成功";
string sqlfield = "[UserTel],[AddMoney],[AddType],[Addtime],[Flag]";
string sql = "insert into Recharge_Record(" + sqlfield + ")values('" + usertel + "','" + cash_fee + "','" + addtype + "','" +
time_end + "','" + flag + "')";
Class1.ExecSql(sql);
}
}
}
}
}
}