/// <summary> /// /// </summary> /// <param name="pmtid"></param> /// <param name="ids"></param> /// <returns></returns> public JsonResult Pay(string pmtid, string ids) { if (string.IsNullOrWhiteSpace(ids)) { Log.Error("payment/pay 参数异常:IDS为空"); return(ErrorResult <dynamic>("参数异常")); } if (string.IsNullOrWhiteSpace(pmtid)) { Log.Error("payment/pay 参数异常:pmtid为空"); return(ErrorResult <dynamic>("参数异常")); } #if DEBUG Log.Debug("Pay ids:" + ids); #endif var orderIdArr = ids.Split(',').Select(item => long.Parse(item)); //获取待支付的所有订单 var orders = _iOrderService.GetOrders(orderIdArr).Where(item => item.OrderStatus == Entities.OrderInfo.OrderOperateStatus.WaitPay && item.UserId == CurrentUser.Id).ToList(); if (orders == null || orders.Count == 0) //订单状态不正确 { Log.Error("payment/pay 未找到可支付的订单"); return(ErrorResult <dynamic>("未找到可支付的订单")); } decimal total = orders.Sum(a => a.OrderTotalAmount - a.CapitalAmount); if (total == 0) { Log.Error("payment/pay 支付金额不能为0"); return(ErrorResult <dynamic>("支付金额不能为0")); } foreach (var item in orders) { if (item.OrderType == Entities.OrderInfo.OrderTypes.FightGroup) { if (!_iFightGroupService.OrderCanPay(item.Id)) { Log.Error("payment/pay 有拼团订单为不可付款状态"); return(ErrorResult <dynamic>("有拼团订单为不可付款状态")); //throw new MallException("有拼团订单为不可付款状态"); } } } //获取所有订单中的商品名称 var productInfos = GetProductNameDescriptionFromOrders(orders); string webRoot = Request.Scheme + "://" + Request.Host.ToString(); string urlPre = webRoot + "/m-" + PlatformType + "/Payment/"; //获取同步返回地址 string returnUrl = webRoot + "/Pay/Return/{0}"; //获取异步通知地址 string payNotify = webRoot + "/Pay/Notify/{0}"; string notifyPre = urlPre + "Notify/", returnPre = webRoot + "/m-" + PlatformType + "/Member/PaymentToOrders?ids=" + ids; if (pmtid.ToLower().Contains("weixin")) {//微信里跳转到分享页面 //支付成功后晒单地址(source=pay用来区分从哪个页面跳转过去的) returnPre = webRoot + "/m-" + PlatformType + "/Order/OrderShare?source=pay&orderids=" + ids; } var payment = Core.PluginsManagement.GetPlugins <IPaymentPlugin>(true).FirstOrDefault(d => d.PluginInfo.PluginId == pmtid); if (payment == null) { throw new MallException("错误的支付方式"); } string openId = Core.Helper.WebHelper.GetCookie(CookieKeysCollection.Mall_USER_OpenID); if (!string.IsNullOrWhiteSpace(openId)) { openId = Core.Helper.SecureHelper.AESDecrypt(openId, "Mobile"); } else { var openIds = _iMemberService.GetOpenIdByUser(CurrentUser.Id); var openUserInfo = openIds.FirstOrDefault(item => item.AppIdType == Entities.MemberOpenIdInfo.AppIdTypeEnum.Payment); if (openUserInfo != null) { openId = openUserInfo.OpenId; } } #region 支付流水获取 var orderPayModel = orders.Select(item => new Entities.OrderPayInfo { PayId = 0, OrderId = item.Id }); //保存支付订单 long payid = _iOrderService.SaveOrderPayInfo(orderPayModel, PlatformType); #endregion #if DEBUG Log.Debug("Pay payid:" + payid); #endif //组织返回Model Mall.Web.Models.PayJumpPageModel model = new Mall.Web.Models.PayJumpPageModel(); model.PaymentId = pmtid; model.OrderIds = ids; model.TotalPrice = total; model.UrlType = payment.Biz.RequestUrlType;; model.PayId = payid; try { #if DEBUG Core.Log.Info("其他详情 : returnPre = " + returnPre + " notifyPre = " + notifyPre + payment.PluginInfo.PluginId.Replace(".", "-") + " ids = " + ids + " totalAmount=" + total + " productInfos=" + productInfos + " openId=" + openId); #endif model.RequestUrl = payment.Biz.GetRequestUrl(returnPre, notifyPre + payment.PluginInfo.PluginId.Replace(".", "-") + "/", payid.ToString(), total, productInfos, openId); Log.Debug(string.Format("openId:{0} Url:{1}", openId, model.RequestUrl)); } catch (Exception ex) { Core.Log.Error("支付页面加载支付插件出错:", ex); throw new MallException("错误的支付方式"); } #if DEBUG Core.Log.Info("支付方式详情 : id = " + payment.PluginInfo.PluginId + " name = " + payment.PluginInfo.DisplayName + " url = " + model.RequestUrl); #endif if (string.IsNullOrWhiteSpace(model.RequestUrl)) { throw new MallException("错误的支付方式,获取支付地址为空"); } switch (model.UrlType) { case UrlType.Page: return(SuccessResult <dynamic>(data: new { jumpUrl = model.RequestUrl })); //return Json(new { success = true, msg = "", jumpUrl = model.RequestUrl }); //return Redirect(model.RequestUrl); case UrlType.QRCode: return(SuccessResult <dynamic>(data: new { jumpUrl = "/Pay/QRPay/?id=" + pmtid + "&url=" + model.RequestUrl + "&orderIds=" + ids })); //return Json(new { success = true, msg = "", jumpUrl = "/Pay/QRPay/?id=" + pmtid + "&url=" + model.RequestUrl + "&orderIds=" + ids }); //return Redirect("/Pay/QRPay/?id=" + pmtid + "&url=" + model.RequestUrl + "&orderIds=" + ids); } return(ErrorResult <dynamic>("调用支付方式异常")); }
/// <summary> /// 支付跳转页 /// <para>完成支付前参数整理工作,并跳转到支付页面</para> /// </summary> /// <param name="pmtid"></param> /// <param name="ids"></param> /// <returns></returns> public ActionResult Index(string pmtid, string ids) { if (string.IsNullOrWhiteSpace(ids)) { return(RedirectToAction("index", "userCenter", new { url = "/userOrder", tar = "userOrder" })); } if (string.IsNullOrWhiteSpace(pmtid)) { return(RedirectToAction("Pay", "Order", new { orderIds = ids })); } var orderIdArr = ids.Split(',').Select(item => long.Parse(item)); //获取待支付的所有订单 var orders = _iOrderService.GetOrders(orderIdArr).Where(item => item.OrderStatus == Entities.OrderInfo.OrderOperateStatus.WaitPay && item.UserId == CurrentUser.Id).ToList(); if (orders == null || orders.Count == 0) //订单状态不正确 { return(RedirectToAction("index", "userCenter", new { url = "/userOrder", tar = "userOrder" })); } decimal total = orders.Sum(a => a.OrderTotalAmount - a.CapitalAmount); if (total == 0) { return(RedirectToAction("Pay", "Order", new { orderIds = ids })); } foreach (var item in orders) { if (item.OrderType == Entities.OrderInfo.OrderTypes.FightGroup) { if (!_iFightGroupService.OrderCanPay(item.Id)) { throw new MallException("有拼团订单为不可付款状态"); } } } //获取所有订单中的商品名称 var productInfos = GetProductNameDescriptionFromOrders(orders); string webRoot = CurrentUrlHelper.CurrentUrlNoPort(); //获取同步返回地址 string returnUrl = webRoot + "/Pay/Return/{0}"; //获取异步通知地址 string payNotify = webRoot + "/Pay/Notify/{0}/"; var payment = Core.PluginsManagement.GetPlugins <IPaymentPlugin>(true).FirstOrDefault(d => d.PluginInfo.PluginId == pmtid); if (payment == null) { throw new MallException("错误的支付方式"); } #region 支付流水获取 var orderPayModel = orders.Select(item => new Entities.OrderPayInfo { PayId = 0, OrderId = item.Id }); //保存支付订单 long payid = _iOrderService.SaveOrderPayInfo(orderPayModel, PlatformType.PC); #endregion //组织返回Model Mall.Web.Models.PayJumpPageModel model = new Mall.Web.Models.PayJumpPageModel(); model.PaymentId = pmtid; model.OrderIds = ids; model.TotalPrice = total; model.UrlType = payment.Biz.RequestUrlType;; model.PayId = payid; try { model.RequestUrl = payment.Biz.GetRequestUrl(string.Format(returnUrl, EncodePaymentId(payment.PluginInfo.PluginId)), string.Format(payNotify, EncodePaymentId(payment.PluginInfo.PluginId)), payid.ToString(), total, productInfos); } catch (Exception ex) { Core.Log.Error("支付页面加载支付插件出错", ex); model.IsErro = true; model.ErroMsg = ex.Message + ",请检查平台支付配置"; } if (string.IsNullOrWhiteSpace(model.RequestUrl)) { model.IsErro = true; model.ErroMsg = "获取支付地址为空,请检查平台支付配置"; } switch (model.UrlType) { case UrlType.Page: return(Redirect(model.RequestUrl)); case UrlType.QRCode: return(Redirect("/Pay/QRPay/?id=" + pmtid + "&url=" + HttpUtility.UrlEncode(model.RequestUrl) + "&orderIds=" + ids)); } ViewBag.Keyword = SiteSettings.Keyword; //form提交在页面组织参数并自动提交 return(View(model)); }