using System;
using System.Text;
using System.Reflection;
using System.Security.Cryptography;
using System.Net;
public partial class wxpay : System.Web.UI.Page
{
DAL dal = new DAL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string type = Request.Form["type"];
string userId = Request.Form["userId"];
//一.系统本身自有的业务处理
//1.必要信息的初始化
//用户主键
string orderId = DateTime.Now.ToFileTimeUtc().ToString(); //生成订单号
decimal totalFee = 980;//设置默认商品费用为【1分】
string nonceStr = DateTime.Now.ToFileTimeUtc().ToString(); //获取 随机字符串
dal.caozuo("insert into orders values('" + orderId + "','" + userId + "','" + totalFee + "','" + DateTime.Now + "'," + 0 + ")");
// dal.caozuo("insert into orders values('1','1','1','1',0)");
//二.微信系统下单
//1.创建支付应答对象并初始化
PaySign sign = GetPaySign(orderId, totalFee, nonceStr, Request.UserHostAddress);
string signs = ModelToUriParam(sign) + "&key=";
string md5Signs = MD5(signs).ToUpper();//MD5签名
string body = GenerateBodyXml(sign, md5Signs);
byte[] postData = Encoding.UTF8.GetBytes(body);//编码,尤其是汉字,事先要看下抓取网页的编码方式
string url = "";//地址
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/json");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
string srcString = Encoding.UTF8.GetString(responseData);//解码
Response.Write(srcString);
}
}
private static string GenerateBodyXml(object obj, string md5Signs)
{
PropertyInfo[] propertis = obj.GetType().GetProperties();
StringBuilder sb = new StringBuilder();
sb.Append("");
foreach (var p in propertis)
{
var v = p.GetValue(obj, null);
if (v == null)
continue;
sb.AppendFormat("<{0}>{1}{0}>", p.Name, v.ToString());
}
sb.AppendFormat("{0}", md5Signs);
sb.Append("");
return sb.ToString();
}
protected static string MD5(string inputText)
{
System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText);
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null;
for (int i = 0; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x2");
}
return byte2String;
}
protected static string ModelToUriParam(object obj)
{
PropertyInfo[] propertis = obj.GetType().GetProperties();
StringBuilder sb = new StringBuilder();
foreach (var p in propertis)
{
var v = p.GetValue(obj, null);
if (v == null)
continue;
sb.Append(p.Name);
sb.Append("=");
sb.Append(v.ToString());
//sb.Append(HttpUtility.UrlEncode(v.ToString()));
sb.Append("&");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
private static PaySign GetPaySign(string orderid,decimal merFee, string nonceStr,string clientId)
{
PaySign paySign = new PaySign
{
appid = "",
body = "111",
mch_id = "",
nonce_str = nonceStr,
notify_url = "",
out_trade_no = orderid,
spbill_create_ip = clientId,
total_fee = (Math.Round(merFee * 100, 0)).ToString(),//转化为单位:分,且只能为整型
trade_type = "MWEB"
};
return paySign;
}
///
/// 签名类A(请注意,此类的属性字段顺序不可调整)
/// 微信预支付前面规则,是按参数ASCII码依次排列的,以下属性已人为排列
///
public class PaySign
{
public string appid { get; set; }
///
/// 附加数据(描述)
///
public string attach { get; set; }
///
/// 商品描述
///
public string body { get; set; }
///
/// 商户号
///
public string mch_id { get; set; }
///
/// 小于32位的随机数
///
public string nonce_str { get; set; }
///
/// 通知地址
///
public string notify_url { get; set; }
///
/// 微信用户openid
///
public string openid { get; set; }
///
/// 商户订单号
///
public string out_trade_no { get; set; }
///
/// 客户端ip
///
public string spbill_create_ip { get; set; }
///
/// 订单金额
///
public object total_fee { get; set; }
///
/// 支付类型
///
public string trade_type { get; set; }
}
///
/// 返回前端的签名类B(请注意,此类的属性字段顺序不可调整)
///
public class ResSign
{
public string appId { get; set; }
///
/// 小于32位的随机数
///
public string nonceStr { get; set; }
///
/// package
///
public string package { get; set; }
///
/// signType
///
public string signType { get; set; }
///
/// timeStamp
///
public string timeStamp { get; set; }
///
/// key
///
public string key { get; set; }
}
}