.net mvc 微信支付

 一、微信第三方登录
通过微信打开链接:http://www.hzm.com/Entry/Login
微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:
  . 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;   . 通过code参数加上AppID和AppSecret等,通过API换取access_token;   . 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
.控制器中代码
public ActionResult Login()
{
//若已经登录,直接跳到首页
if (HttpContext.User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Entry");
}
//微信登录
//1.获取code
if (string.IsNullOrEmpty(Request.QueryString["code"]))
{
//从定向到微信,微信再重定向到当前函数(进入第1步获取code)
return RedirectToWeixin();
}
//2.获取openID
//通过code获取openID
string openID = "";
try
{
openID = GetOpenID(Request.QueryString["code"]);
}
catch(Exception e)
{
Log.Debug("GetOpenID Exception", e.StackTrace);
return Redirect("/Entry/Login");
}
//3.查找或存储用户信息
User user = new User();
user = db.Users.SingleOrDefault(p => p.OpenID == openID);
if (user == null)
{
//添加用户
user = new User();
user.OpenID = openID;
db.Users.Add(user);
db.SaveChanges();
}
//登录成功
FormsAuthentication.SetAuthCookie(user.ID.ToString(), false);//记录登录凭证
return RedirectToAction("Index", "Entry");
}
1.1.获取code
private ActionResult RedirectToWeixin()
{
JsApiPay jsApiPay = new JsApiPay(this);
var redirect_url = jsApiPay.GetRedirectToWeixinUrlForCode();//将回调stata参数值设为awardUserID
return Redirect(redirect_url);//调回:/Entry/Login
}
1.2.通过code获取openID和access_token
private string GetOpenID(string code)
{
JsApiPay jsApiPay = new JsApiPay(this);
//如果code有值
if (!string.IsNullOrEmpty(code))
{
Log.Debug(" GetOpenID code", code);
//去获取OpenID
jsApiPay.GetOpenidAndAccessTokenFromCode(code);
}
Log.Debug("GetOpenidAndAccessTokenFromCode openid", jsApiPay.openid.ToString());
return jsApiPay.openid.ToString();
}
二、微信支付
2.1.生成业务订单和微信交易号Out_trade_no
public ActionResult CreateOrder(int entryID, PayMode payMode)
{
//一个报名只应对应一个订单
//判断订单是否已经存在,则直接进入详情页
if (db.Orders.Any(p => p.EntryID == entryID))
{
return Redirect("/Entry/Detail?out_trade_no=" + db.Orders.SingleOrDefault(p => p.EntryID == entryID).Out_trade_no);
}
Entry entry = db.Entrys.Include("Event").SingleOrDefault(p => p.ID == entryID);
Order order = new Order();
order.EntryID = entry.ID;
order.Amount = GetDiscount(entry.Event);
order.PayMode = payMode;
order.State = OrderSatet.Unpaid;
order.SubmitTime = DateTime.Now;
order.Body = entry.Event.Theme;
order.Out_trade_no = WxPayApi.GenerateOutTradeNo();
db.Orders.Add(order);
db.SaveChanges();
if (payMode == PayMode.Remittance)
{
return RedirectToAction("Detail", new { out_trade_no = order.Out_trade_no });
}
return RedirectToAction("Pay", new { orderID = order.ID });
}
2.2.微信支付
public ActionResult Pay(int orderID)
{
//获取订单
Order order = db.Orders.SingleOrDefault(p => p.ID == orderID); //修改支付方式
order.PayMode = PayMode.Weixin;
db.SaveChanges(); //微信openID
Entry entry = db.Entrys.Include("Event").SingleOrDefault(p => p.ID == order.EntryID);
User user = db.Users.SingleOrDefault(p => p.ID == entry.UserID);
Log.Debug("openID", user.OpenID);
Log.Debug("entry.UserID", entry.UserID.ToString());
Log.Debug("HttpContext.User.Identity.Name", HttpContext.User.Identity.Name);
//当前订单的商户系统的订单号
ViewData["out_trade_no"] = order.Out_trade_no;
//支付费用
decimal totalFee = GetDiscount(entry.Event);
Log.Debug("totalFee", "100.00");
JsApiPay jsApiPay = new JsApiPay(this);
try
{
//若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数
jsApiPay.openid = user.OpenID; jsApiPay.total_fee = Convert.ToInt32(totalFee * );//支持两位小数,如0.01
Log.Debug(this.GetType().ToString(), "参数赋值成功1");
//JSAPI支付预处理
Log.Debug(this.GetType().ToString(), "参数赋值成功2");
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(entry.Name + "的" + entry.Event.Theme, "报名", order.Out_trade_no); Log.Debug(this.GetType().ToString(), "预支付订单生成成功");
//获取H5调起JS API参数
var wxJsApiParam = jsApiPay.GetJsApiParameters();
ViewData["wxJsApiParam"] = wxJsApiParam; Log.Debug(this.GetType().ToString(), "wxJsApiParam参数获取成功");
//预支付订单号
var prepay_id = jsApiPay.unifiedOrderResult.GetValue("prepay_id").ToString();
ViewData["prepay_id"] = prepay_id;
Log.Debug(this.GetType().ToString(), "prepay_id参数获取成功");
/*###################################*/
Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
}
catch (Exception ex)
{
Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试" + "</span>");
Log.Debug("Award-ConfirmPayPage", ex.Message + ex.StackTrace);
Log.Debug("Award-ConfirmPayPage", "预支付订单生成或保存异常");
} return View(); }
2.3.页面中调起微信支付接口(Pay.html)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>支付页面</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script type="text/javascript">
var wxJsApiParam=@Html.Raw(ViewData["wxJsApiParam"].ToString());
//调用微信JS api 支付 function jsApiCall() {
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
wxJsApiParam,
function (res)
{
WeixinJSBridge.log(res.err_msg); //alert(res.err_code + res.err_desc + res.err_msg); if(res.err_msg=="get_brand_wcpay_request:ok"){
location.href ="/Entry/Detail?out_trade_no=@ViewData["out_trade_no"]";
}else
{
location.href ="/Entry/Detail?out_trade_no=@ViewData["out_trade_no"]";
} //alert(res.err_msg);
}
);
} function callpay()
{
if (typeof WeixinJSBridge == "undefined")
{
if (document.addEventListener)
{
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}
else if (document.attachEvent)
{
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}
else
{
jsApiCall();
}
}
callpay();
</script>
</head>
<body>
</body>
</html>
2.4.支付成功后微信发送支付成功异步通知(post方式,通知地址在Config.cs中配置的NOTIFY_URL,在这里添加订单处理业务逻辑)
public ActionResult Notify()
{
ResultNotify notify = new ResultNotify(this);
WxPayData data = notify.GetNotifyData();
Log.Debug("return_code", data.GetValue("return_code").ToString());
Log.Debug("return_msg", data.GetValue("return_code").ToString());
if (data.GetValue("return_code").ToString() == "SUCCESS")
{
Log.Debug("out_trade_no", data.GetValue("out_trade_no").ToString());
var out_trade_no = data.GetValue("out_trade_no").ToString(); Order order = db.Orders.SingleOrDefault(p => p.Out_trade_no == out_trade_no);
Log.Debug("orderID", order.ID.ToString()); order.State = OrderSatet.Paid;
var time_end = data.GetValue("time_end").ToString();
order.PayTime = DateTime.ParseExact(time_end, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
order.TransactionID = data.GetValue("transaction_id").ToString();
order.OpenID = data.GetValue("openid").ToString();
db.SaveChanges(); }
notify.ProcessNotify(); return View();
}
2.5.支付成功后跳转到(Pay.html页面设置的地址)
如:location.href ="/Entry/Detail?out_trade_no=@ViewData["out_trade_no"]";
微信支付官方文档地址:
1.App微信支付:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3
上一篇:Divide two numbers,两数相除求商,不能用乘法,除法,取模运算


下一篇:JS轮播图(网易云轮播图)