using Ecode.WeiXin.Tools;
using Ecode.WeiXin.Tools.WxPayAPI; using Newtonsoft.Json.Linq; using System;
using System.Collections.Generic; using System.Linq;
using System.Security.Cryptography; using System.Text; using System.Web;
using System.Web.Mvc; using System.Xml;
namespace Ecode.WeiXin.WebUI.Controllers {
public class PayWeiXinController : Controller {
//
// GET: /PayWeiXin/ ///
/// 日期转换成unix时间戳 ///
/// ///
private static long DateTimeToUnixTimestamp(DateTime dateTime) {
var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind); return Convert.ToInt64((dateTime - start).TotalSeconds); }
private string ToMD5(string str) {
//MD5加密
var md5 = MD5.Create();
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var sb = new StringBuilder(); foreach (byte b in bs) {
sb.Append(b.ToString(\)); }
//所有字符转为大写
return sb.ToString(); }
public ActionResult Index() {
//获取从其他页面传递过来的订单号和用户ID string orderNumber = Request[\]; string userID = Request[\];
if (String.IsNullOrEmpty(orderNumber) || String.IsNullOrEmpty(userID)) {
return View(); }
//组装state的内容,根据情况来
string state = orderNumber + \ + userID;
//获取code后的回跳页面 string rediretUrl =
HttpUtility.UrlEncode(\);
string urlGetCode =
\ + WxPayConfig.APPID + \ + rediretUrl +
\ + state + \;
return Redirect(urlGetCode); }
public ActionResult Pay() {
string code = Request.QueryString[\]; string state = Request.QueryString[\];
string orderNumber = state.Split('_')[0]; string userID = state.Split('_')[1];
//调用接口,根据订单号和用户编号获取订单信息,避免被篡改页面支付信息
LiuLiuKangBa.BLL.APISDK.Models.ModelSOrder orderInfo = SDKApi.GetOrderInfo(orderNumber, userID); ViewBag.OrderInfo = orderInfo; ViewBag.Oid = orderNumber; ViewBag.Uid = userID;
if (orderInfo == null || orderInfo.AMount == 0) {
//无效的订单
Log.Error(\无效订单\, \ + orderNumber + \ + userID);
return View(); }
else if (orderInfo.Status == 0) {
//可以支付
string body = orderInfo.Name;
string out_trade_no = orderNumber.Trim();
string total_fee = Convert.ToInt32(orderInfo.AMount * 100).ToString();
string url =
\ +
WxPayConfig.APPID + \ + WxPayConfig.APPSECRET + \ + code + \;
string result = Helper.GetResponseData(\, url, Helper.RequestMethod.GET);
Log.Debug(\获取code时的返回值\, result);
JObject jsonObj = JObject.Parse(result);
//拿到openid,微信公众号支付,统一下单时,需要openid string openid = jsonObj[\].ToString();
//组装统一下单需要的参数
Dictionary
dataList.Add(\, WxPayConfig.APPID);
dataList.Add(\, WxPayConfig.MCHID); dataList.Add(\,
DateTime.Now.AddSeconds(Helper.GetRandomNumber(100, 5000)).Ticks.ToString());
dataList.Add(\, body);
dataList.Add(\, out_trade_no); dataList.Add(\, total_fee); dataList.Add(\, Request.UserHostAddress);
dataList.Add(\, WxPayConfig.NOTIFY_URL); dataList.Add(\, \); dataList.Add(\, openid);
//按关键字排序
var orderDataList = dataList.OrderBy(g => g.Key);
StringBuilder strBuilder = new StringBuilder();
foreach (var keyValue in orderDataList) {
strBuilder.Append(keyValue.Key); strBuilder.Append(\);
strBuilder.Append(keyValue.Value);
strBuilder.Append(\); }
//MD5加上key
strBuilder.Append(\ + WxPayConfig.KEY);
Log.Debug(\前\, strBuilder.ToString());
//得到加密的sign string sign =
ToMD5(strBuilder.ToString()).ToUpper();
//组装统一下单需要的xml
StringBuilder xmlBuiler = new StringBuilder(); xmlBuiler.Append(\);
foreach (var keyValue in orderDataList) {
xmlBuiler.Append(\ + keyValue.Key + \ + keyValue.Value + \ + keyValue.Key + \); }
xmlBuiler.Append(\ + sign + \); xmlBuiler.Append(\);
Log.Debug(\提交统一下单的XML\, xmlBuiler.ToString());
string urlUnifiedorder =
\; string resultUnifiedorder =
Helper.GetResponseData(xmlBuiler.ToString(), urlUnifiedorder, Helper.RequestMethod.POST);
Log.Debug(\统一下单返回的XML:\, resultUnifiedorder);
string prepay_id = \; try {
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(resultUnifiedorder);
XmlNode node_return_cod = xmlDoc.SelectSingleNode(\); if
(node_return_cod.InnerText.Equals(\)) {
//通信成功
XmlNode node_result_code = xmlDoc.SelectSingleNode(\); if
(node_result_code.InnerText.Equals(\)) {
//交易成功
//获取到统一下单返回的prepay_id prepay_id =
xmlDoc.SelectSingleNode(\).InnerText; Log.Info(\提交微信支付成功\, \ + orderNumber + \ + userID); } else {
//交易失败
Log.Error(\提交微信支付失败\, \ + orderNumber + \ + userID);