配置参数
<!--支付微信appid-->
<add key="AppId" value="" />
<!--微信secret-->
<add key="secret" value="" />
<!--接收微信异步回调接口-->
<add key="notify_url" value="" />
<!--商户号-->
<add key="MchId" value="" />
<!--商户号KEY 用户登录商户平台自行设置的(32位)-->
<add key="key" value="" />
1.公众号 appid
2.微信支付平台 商户号 商户秘钥Key
3.配置目录 微信支付平台上面
配置好以上参数 就可以开始开发了
不多说 直接上代码 完整 看不懂的慢慢看 因为我做的是一码 多用 所以 前台页面会不一样 前台页面自行设置
using Aop.Api;
using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using Tea.Utils;
namespace YouXingTuShuJu_V2.Controllers
{
/// <summary>
/// 微信支付
/// </summary>
public class WeChatAPIController : ApiController
{
static string _pre_order_url = "https://api.mch.weixin.qq.com/";//请求域名
static string _appid = AliPayConfig.AppId;//公众账号ID
static string _mch_id = AliPayConfig.MchId;//商户号
static string _partnerKey = AliPayConfig.key;//商家私钥
/// <summary>
///
/// </summary>
/// <param name="dizhi"></param>
/// <returns></returns>
/// <summary>
///
/// </summary>
/// <param name="dizhi"></param>
/// <returns></returns>
public DataTable WeChatIndex(int dizhi)
{
int FId = dizhi;
string signType = "";
string nonce_str = "";
string timeStamp = "";
string package = "";
string appId = "";
string paySign = "";
DataTable aa = new DataTable();//可以给表创建一个名字,tb
//根据FId查询订单信息
Maticsoft.BLL.T_CreateOrderData createOrderDataBll = new Maticsoft.BLL.T_CreateOrderData();
DataTable dt = createOrderDataBll.GetcreateOrderDataInfo(FId);
if (dt.Rows.Count > 0)
{
//附加数据
var _attach = "支付测试";
//商品描述
var _body = "账号激活";
//随机字符串
var _nonce_str = CreateNonce_str();
//回调地址
var _notify_url = "http://www.shangtuikeji.com/api/WeChatAPI/Notify_url";
//商户订单号
var _out_trade_no = Convert.ToString(dt.Rows[0]["FNumber"]);
//终端IP
var _spbill_create_ip = GetWebClientIp();
//订单金额
var _total_fee = Convert.ToDouble(dt.Rows[0]["FMoney"]);//元为单位
//交易类型
var _trade_type = "JSAPI";
//设备号
var _scene_info = $@"{{""h5_info"": {{""type"":""Wap"",""wap_url"": ""{_notify_url}"",""wap_name"": ""{_body}""}}}}";
//交易起始时间
var _time_start = DateTime.Now.ToString("yyyyMMddHHmmss");
//交易结束时间
var _time_expire = DateTime.Now.AddHours(1).ToString("yyyyMMddHHmmss");
var openid = dt.Rows[0]["FMyUserOpenid"].ToString();
var sign_type = "MD5";
//下单
var pre_order_httpResult = UnifiedOrder(_appid, _attach, _body, _mch_id, _nonce_str, _notify_url, _out_trade_no, _spbill_create_ip, _total_fee,
_trade_type, _scene_info, _time_start, _time_expire, openid, sign_type);
var pre_order_resultStr = XElement.Parse(pre_order_httpResult);
var pre_order_result_code = pre_order_resultStr.Element("return_code").Value;
var pre_order_result_msg = pre_order_resultStr.Element("return_msg").Value;
aa.Columns.Add("signType", typeof(System.String));//类型是可以变换的,比如System.Int32,System.Double..
aa.Columns.Add("nonce_str", typeof(System.String));
aa.Columns.Add("timeStamp", typeof(System.String));
aa.Columns.Add("package", typeof(System.String));
aa.Columns.Add("appId", typeof(System.String));
aa.Columns.Add("paySign", typeof(System.String));
if (pre_order_result_code == "SUCCESS")
{
signType = "MD5";
nonce_str = pre_order_resultStr.Element("nonce_str").Value;
///秒
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string ts = Convert.ToInt64(t.TotalSeconds).ToString();
timeStamp = ts;
package = pre_order_resultStr.Element("prepay_id").Value;
appId = pre_order_resultStr.Element("appid").Value;
var stringADict = new Dictionary<string, string>();
stringADict.Add("signType", signType);
stringADict.Add("nonceStr", nonce_str);
stringADict.Add("timeStamp", timeStamp);
stringADict.Add("package", "prepay_id=" + package);
stringADict.Add("appId", appId);
paySign = Sign(stringADict, _partnerKey);
DataRow row = aa.NewRow();
row["signType"] = signType;
row["nonce_str"] = nonce_str;
row["timeStamp"] = timeStamp;
row["package"] = package;
row["appId"] = appId;
row["paySign"] = paySign;
aa.Rows.Add(row);
}
}
return aa;
}
/// <summary>
/// 下单
/// </summary>
/// <param name="appid">公众账号ID</param>
/// <param name="attach">附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据</param>
/// <param name="body">String(32) 商品描述 商品或支付单简要描</param>
/// <param name="mch_id">商户号</param>
/// <param name="nonce_str">随机字符串</param>
/// <param name="notify_url">接收微信支付异步通知回调地址,不可带参,与下面的Notify对应,开发者可自定义其他url地址 </param>
/// <param name="out_trade_no">商户系统内部的订单号,32个字符内、可包含字母</param>
/// <param name="spbill_create_ip">终端ip</param>
/// <param name="total_fee">收钱总额 分为单位 前台传过来后需要处理成分</param>
/// <param name="trade_type">交易类型H5支付的交易类型为MWEB</param>
/// <param name="scene_info">场景信息 WAP网站应用{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}</param>
/// <param name="time_start">交易起始时间</param>
/// <param name="time_expire">交易结束时间</param>
/// <returns></returns>
static string UnifiedOrder(string appid, string attach, string body, string mch_id, string nonce_str, string notify_url, string out_trade_no,
string spbill_create_ip, double total_fee, string trade_type, string scene_info, string time_start, string time_expire, string openid, string sign_type)
{
var stringADict = new Dictionary<string, string>();
stringADict.Add("appid", appid);
stringADict.Add("attach", attach);
stringADict.Add("body", body);
stringADict.Add("mch_id", mch_id);
stringADict.Add("nonce_str", nonce_str);
stringADict.Add("notify_url", notify_url);
stringADict.Add("out_trade_no", out_trade_no);
stringADict.Add("openid", openid);
stringADict.Add("spbill_create_ip", spbill_create_ip);
stringADict.Add("total_fee", Math.Round(Convert.ToDecimal(total_fee) * 100, 0).ToString());//元转分
stringADict.Add("trade_type", trade_type);
stringADict.Add("scene_info", scene_info);
stringADict.Add("time_start", time_start);
stringADict.Add("time_expire", time_expire);
stringADict.Add("sign_type", sign_type);
var sign = Sign(stringADict, _partnerKey);//生成签名字符串
//组合xml内容
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("<xml>");
strBuilder.Append($"<appid>{appid}</appid>");//公众号id
strBuilder.Append($"<attach>{attach}</attach>");//附加数据
strBuilder.Append($"<body>{body}</body>");//商品描述
strBuilder.Append($"<mch_id>{mch_id}</mch_id>");//商户号
strBuilder.Append($"<nonce_str>{nonce_str}</nonce_str>");//随机字符串
strBuilder.Append($"<notify_url>{notify_url}</notify_url>");//接收微信支付异步通知回调地址,不可带参,与下面的Notify对应,开发者可自定义其他url地址
strBuilder.Append($"<out_trade_no>{out_trade_no}</out_trade_no>");//商户系统内部的订单号,32个字符内、可包含字母
strBuilder.Append($"<openid>{openid}</openid>");
strBuilder.Append($"<spbill_create_ip>{spbill_create_ip}</spbill_create_ip>");//终端ip
strBuilder.Append($"<total_fee>{Math.Round(Convert.ToDecimal(total_fee) * 100, 0).ToString()}</total_fee>");//收钱总额 分为单位 前台传过来后需要处理成分
strBuilder.Append($"<trade_type>{trade_type}</trade_type>");//交易类型H5支付的交易类型为MWEB
strBuilder.Append($"<scene_info>{scene_info}</scene_info>");
strBuilder.Append($"<time_start>{time_start}</time_start>");//交易起始时间
strBuilder.Append($"<time_expire>{time_expire}</time_expire>");//交易结束时间
strBuilder.Append($"<sign>{sign}</sign>");
strBuilder.Append($"<sign_type>{sign_type}</sign_type>");
strBuilder.Append("</xml>");
//var url = _pre_order_url + "/sandboxnew/pay/unifiedorder";//沙箱
var url = _pre_order_url + "pay/unifiedorder";
var pre_order_httpResult = HttpPostRequestXml(url, strBuilder);
return pre_order_httpResult;
}
/// <summary>
/// 查询订单
/// </summary>
/// <param name="appid">公众账号ID</param>
/// <param name="mch_id">商户号</param>
/// <param name="out_trade_no">商户系统内部的订单号或微信的订单号</param>
/// <param name="nonce_str">随机字符串</param>
/// <returns></returns>
static string Orderquery(string appid, string mch_id, string out_trade_no, string nonce_str)
{
var stringADict = new Dictionary<string, string>();
stringADict.Add("appid", appid);
stringADict.Add("mch_id", mch_id);
stringADict.Add("out_trade_no", out_trade_no);
stringADict.Add("nonce_str", nonce_str);
var sign = Sign(stringADict, _partnerKey);//生成签名字符串
//组合xml内容
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("<xml>");
strBuilder.Append($"<appid>{appid}</appid>");
strBuilder.Append($"<mch_id>{mch_id}</mch_id>");
strBuilder.Append($"<out_trade_no>{out_trade_no}</out_trade_no>");
strBuilder.Append($"<nonce_str>{nonce_str}</nonce_str>");
strBuilder.Append($"<sign>{sign}</sign>");
strBuilder.Append("</xml>");
//接口地址
var url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
var pre_order_httpResult = HttpPostRequestXml(url, strBuilder);
return pre_order_httpResult;
}
/// <summary>
/// 发送post xml文件请求
/// </summary>
/// <param name="Url"></param>
/// <param name="strBuilder"></param>
/// <returns></returns>
static string HttpPostRequestXml(string Url, StringBuilder strBuilder)
{
string result = string.Empty;
string data = strBuilder.ToString();
//进行utf-8编码
var encoding = Encoding.GetEncoding("utf-8");
byte[] buffer = encoding.GetBytes(data);
//根据webURL创建HttpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "post";
//request.Headers.Add("charset:utf-8");
request.ContentLength = buffer.Length;
request.ContentType = "text/xml";
StreamWriter myWriter = null;
try
{
myWriter = new StreamWriter(request.GetRequestStream());
myWriter.Write(data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
myWriter.Close();
}
//读取服务器返回的信息
HttpWebResponse objResponse = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
return result;
}
private static string[] strs = new string[]
{
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
};
/// <summary>
/// 创建随机字符串
/// </summary>
/// <returns></returns>
public static string CreateNonce_str()
{
Random r = new Random();
var sb = new StringBuilder();
var length = strs.Length;
for (int i = 0; i < 15; i++)
{
sb.Append(strs[r.Next(length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 获取终端IP地址
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{
string userIP = "";
try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
return "";
string CustomerIP = "";
//CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (CustomerIP == null)
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.Compare(CustomerIP, "unknown", true) == 0)
return System.Web.HttpContext.Current.Request.UserHostAddress;
return CustomerIP;
}
catch { }
return userIP;
}
/// <summary>
/// 生成签名
/// 签名在线验证工具:
/// http://mch.weixin.qq.com/wiki/tools/signverify/
/// </summary>
/// <param name="stringADict">参与签名生成的参数列表</param>
/// <param name="partnerKey">商家私钥</param>
/// <returns></returns>
public static string Sign(IDictionary<string, string> stringADict, string partnerKey)
{
var sb = new StringBuilder();
foreach (var sA in stringADict.OrderBy(x => x.Key))//参数名ASCII码从小到大排序(字典序);
{
if (string.IsNullOrEmpty(sA.Value)) continue;//参数的值为空不参与签名;
if (string.Compare(sA.Key, "sign", true) == 0) continue; // 参数中为签名的项,不参加计算
sb.Append(sA.Key).Append("=").Append(sA.Value).Append("&");
}
var string1 = sb.ToString();
string1 = string1.Remove(string1.Length - 1, 1);
sb.Append("key=").Append(partnerKey);//在stringA最后拼接上key=(API密钥的值)得到stringSignTemp字符串
var stringSignTemp = sb.ToString();
// var sign = MD5Encrypt(stringSignTemp, "UTF-8").ToUpper();//对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。
var sign = MD5Encrypt(stringSignTemp, "UTF-8").ToUpper();//对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。
return sign;
}
/// <summary>
/// Md5加密
/// </summary>
/// <param name="myString"></param>
/// <returns></returns>
public string GetMD52(string myString)
{
//return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(myString))).Replace("-", "");
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(myString));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/// <summary>
/// 用MD5加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <returns></returns>
public static string MD5Encrypt(string password, string encoding)
{
//MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
//byte[] hashedDataBytes;
//hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding(encoding).GetBytes(password));
//StringBuilder tmp = new StringBuilder();
//foreach (byte i in hashedDataBytes)
//{
// tmp.Append(i.ToString("x2"));
//}
//return tmp.ToString();
//return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(myString))).Replace("-", "");
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(password));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
public static IAopClient GetAlipayClient()
{
string appid = AliPayConfig.AppId;
string secret = AliPayConfig.secret;
string notify_url = AliPayConfig.notify_url;
string MchId = AliPayConfig.MchId;
string key = AliPayConfig.key;
IAopClient client = new DefaultAopClient(appid, secret, notify_url, MchId, key); ;
return client;
}
public class AliPayConfig
{
//微信secret
public static string secret = WebConfigurationManager.AppSettings["secret"].ToString();
//微信appid
public static string AppId = WebConfigurationManager.AppSettings["AppId"].ToString();
//商户号
public static string MchId = WebConfigurationManager.AppSettings["MchId"].ToString();
//商户号KEY 用户登录商户平台自行设置的(32位)
public static string key = WebConfigurationManager.AppSettings["key"].ToString();
//微信异步回调地址
public static string notify_url = WebConfigurationManager.AppSettings["notify_url"].ToString();
// 日志记录
// public static string LogPath = WebConfigurationManager.AppSettings["AliLog"].ToString();
}
/// <summary>
/// 付款返回的数据
/// </summary>
/// <returns></returns>
public string Notify_url()
{
string xmlData = getPostStr();
PayResult(xmlData);
return "SUCCESS";
}
//获得Post过来的数据
public string getPostStr()
{
Int32 intLen = Convert.ToInt32(System.Web.HttpContext.Current.Request.InputStream.Length);
byte[] b = new byte[intLen];
System.Web.HttpContext.Current.Request.InputStream.Read(b, 0, intLen);
return System.Text.Encoding.UTF8.GetString(b);
}
/// <summary>
/// 付款结果处理
/// </summary>
public HttpResponseMessage PayResult(string ResultMsg)
{
HttpResponseMessage resp = new HttpResponseMessage();
try
{
if (!string.IsNullOrEmpty(ResultMsg))
{
var xml = new XmlDocument();
xml.LoadXml(ResultMsg);
//处理返回的值
DataSet ds = new DataSet();
StringReader stram = new StringReader(ResultMsg);
XmlTextReader reader = new XmlTextReader(stram);
ds.ReadXml(reader);
string return_code = ds.Tables[0].Rows[0]["return_code"].ToString();
if (return_code.ToUpper() == "SUCCESS")
{
//通信成功
string result_code = ds.Tables[0].Rows[0]["result_code"].ToString();//业务结果
if (result_code.ToUpper() == "SUCCESS")
{
//微信appid
string appid = ds.Tables[0].Rows[0]["appid"].ToString();
//商家数据包
string attach = ds.Tables[0].Rows[0]["attach"].ToString();
//商家号
string mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString();
//用户openid
string openid = ds.Tables[0].Rows[0]["openid"].ToString();
//订单金额
Int32 total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString());
//微信支付订单号
string transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString();
string out_trade_no = ds.Tables[0].Rows[0]["out_trade_no"].ToString();
//更新业务数据 并执行业务逻辑 out_trade_no
Maticsoft.BLL.T_CreateOrderData t_CreateOrderDataBll = new Maticsoft.BLL.T_CreateOrderData();
string Token = "";
if (t_CreateOrderDataBll.PayOrderSucceed(out_trade_no, out Token) > 0)
{
//rides修改验证
var Redis = new RedisHelper();
if (!string.IsNullOrEmpty(Token))
{
Redis.SetHashValue(Token, "RegisterPay", "1");
}
resp.Content = new StringContent(JsonConvert.SerializeObject("success"), System.Text.Encoding.UTF8, "text/plian");
return resp;
}
else
{
resp.Content = new StringContent(JsonConvert.SerializeObject("支付失败"), System.Text.Encoding.UTF8, "text/plian");
return resp;
}
}
else
{
resp.Content = new StringContent(JsonConvert.SerializeObject("支付失败"), System.Text.Encoding.UTF8, "text/plian");
return resp; ;
}
}
else
{
//验证失败
resp.Content = new StringContent(JsonConvert.SerializeObject("支付失败"), System.Text.Encoding.UTF8, "text/plian");
return resp;
}
}
}
catch (Exception ex)
{
LoggerHelper.Info("服务器微信异步通知页面", ex);
resp.Content = new StringContent(JsonConvert.SerializeObject("支付失败"), System.Text.Encoding.UTF8, "text/plian");
return resp;
}
return resp;
}
}
}