/// <summary> /// 验证订单是否支付 /// </summary> /// <param name="pRequest"></param> /// <returns></returns> internal static bool IsOrderPaid(TradeRequest pRequest) { var para = pRequest.GetParameter <QueryOrderByAppInfoParameters>(); var bll = new AppOrderBLL(pRequest.GetUserInfo()); var entity = bll.GetByAppInfo(pRequest.AppID.Value, para.AppOrderID, pRequest.ClientID); return(entity.Status == 2); }
/// <summary> /// 更新微信反馈 /// </summary> /// <param name="request"></param> /// <returns></returns> internal static object WXGetUpdateFeedBackUrl(TradeRequest request) { WXGetUpdateFeedBackReqPara para = request.GetParameter <WXGetUpdateFeedBackReqPara>(); var channelBll = new PayChannelBLL(new Utility.BasicUserInfo()); var channel = channelBll.GetByID(para.PayChannelID); var WXChannel = channel.ChannelParameters.DeserializeJSONTo <WeiXinPayChannel>(); string url = WeiXinPayGateway.GetUpdateFeedBackUrl(WXChannel, para.FeedBackID, para.OpenID); return(new { Url = url }); }
/// <summary> /// 创建微信支付NativePay连接 /// </summary> /// <param name="request"></param> /// <returns></returns> internal static object CreateWXNativePayUrl(TradeRequest request) { CreateWXNativePayUrlReqPara para = request.GetParameter <CreateWXNativePayUrlReqPara>(); var channelBll = new PayChannelBLL(new Utility.BasicUserInfo()); var channel = channelBll.GetByID(para.PayChannelID); var WXChannel = channel.ChannelParameters.DeserializeJSONTo <WeiXinPayChannel>(); NativePayHelper req = new NativePayHelper(WXChannel); return(new { NativePayUrl = req.GenerateNativeUrl(para.ProductID) }); }
/// <summary> /// 获取微信签名信息 /// </summary> /// <param name="request"></param> /// <returns></returns> internal static object WXGetSign(TradeRequest request) { WXGetSignReqPara para = request.GetParameter <WXGetSignReqPara>(); var channelBll = new PayChannelBLL(new Utility.BasicUserInfo()); var channel = channelBll.GetByID(para.PayChannelID); if (channel.PayType != 5 || channel.PayType != 6) { throw new Exception("非微信支付通道,不能调用些接口"); } var WXChannel = channel.ChannelParameters.DeserializeJSONTo <WeiXinPayChannel>(); var newSign = CommonUtil.CreateSign(para.NoSignDic, WXChannel); return(new { Sign = newSign }); }
/// <summary> /// 查询订单信息 /// </summary> /// <param name="pRequest"></param> /// <returns></returns> public static object QueryOrder(TradeRequest pRequest) { QueryOrderResponse response = new QueryOrderResponse(); var user = pRequest.GetUserInfo(); AppOrderBLL bll = new AppOrderBLL(user); var para = pRequest.GetParameter <QueryOrderParameters>(); var entity = bll.GetByID(para.OrderID); if (entity != null) { response.Status = entity.Status; } else { response.Message = "无此订单信息"; } return(response); }
/// <summary> /// 使用汇付储值卡支付订单 /// </summary> /// <param name="request"></param> /// <returns></returns> internal static PrePaidCardPayRD PrePaidCardPay(TradeRequest request) { PrePaidCardPayRD rd = new PrePaidCardPayRD(); var rp = request.GetParameter <PrePaidCardPayRP>(); var userInfo = request.GetUserInfo(); if (rp == null || string.IsNullOrEmpty(rp.OrderId) || string.IsNullOrEmpty(rp.CardNo) || string.IsNullOrEmpty(rp.Password)) { rd.errcode = 40000; rd.errmsg = "参数错误"; return(rd); } try { var appBll = new AppOrderBLL(userInfo); var appOrder = appBll.GetAppOrderByAppOrderId(rp.OrderId); if (appOrder == null) { rd.errcode = 40001; rd.errmsg = "未找到支付订单,请重试!"; return(rd); } if (CacheOrder.Contains(rp.OrderId)) { rd.errcode = 40003; rd.errmsg = "订单支付中,请稍后重试"; return(rd); } CacheOrder.Add(rp.OrderId); // 限制重复提交支付,只针对单服务器程序,多服务请走第三方缓存 if (appOrder.Status == 2) { rd.errcode = 0; rd.errmsg = "该订单已支付"; return(rd); } string msg = string.Empty; var result = Consumption(rp, appOrder, out msg); if (result.rspCd == "0000") { rd.errcode = 200; rd.errmsg = "success"; AppOrderBLL bll = new AppOrderBLL(new Utility.BasicUserInfo()); if (!(appOrder.IsNotified ?? false)) { Task.Factory.StartNew(() => {//起一个新线程通知业务系统处理订单 try { string errmsg; if (NotifyHandler.Notify(appOrder, out errmsg)) { appOrder.IsNotified = true; } else { appOrder.NextNotifyTime = DateTime.Now.AddMinutes(1); } //通知完成,通知次数+1 appOrder.NotifyCount = (appOrder.NotifyCount ?? 0) + 1; bll.Update(appOrder); } catch (Exception ex) { Loggers.Exception(new ExceptionLogInfo(ex)); } }); } } else { rd.errcode = 40002; rd.errmsg = result.rspDesc; } } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = ex.Message }); } finally { CacheOrder.Remove(rp.OrderId); } return(rd); }
/// <summary> /// 设置支付渠道 /// </summary> /// <param name="request"></param> /// <returns></returns> internal static object SetPayChannel(TradeRequest request) { var paras = request.GetParameter <SetPayChannel>(); var payChannelResponsList = new PayChannelResponsList(); var payChannelList = new PayChannelResponse(); payChannelResponsList.PayChannelIdList = new List <PayChannelResponse>(); foreach (var para in paras.AddPayChannelData) { var payType = para.PayType; var notifyUrl = para.NotifyUrl; var payChannelBll = new PayChannelBLL(new Utility.BasicUserInfo()); var payChannelEntity = new PayChannelEntity(); var channelId = para.ChannelId; if (payType == 0) { throw new Exception("支付类型不能为空"); } #region 银联支付 if (payType == 1 || payType == 2) { var unionPayData = para.UnionPayData; if (unionPayData.MerchantID == null || string.IsNullOrEmpty(unionPayData.MerchantID)) { throw new Exception("缺少参数【MerchantID】"); } if (unionPayData.DecryptCertificateFilePath == null || string.IsNullOrEmpty(unionPayData.DecryptCertificateFilePath)) { throw new Exception("缺少参数【DecryptCertificateFilePath】"); } if (unionPayData.PacketEncryptKey == null || string.IsNullOrEmpty(unionPayData.PacketEncryptKey)) { throw new Exception("缺少参数【PacketEncryptKey】"); } if (unionPayData.CertificateFilePath == null || string.IsNullOrEmpty(unionPayData.CertificateFilePath)) { throw new Exception("缺少参数【CertificateFilePath】"); } if (unionPayData.CertificateFilePassword == null || string.IsNullOrEmpty(unionPayData.CertificateFilePassword)) { throw new Exception("缺少参数【CertificateFilePassword】"); } var unionpay = new UnionPayInfo(); #region 解密 var decFilePath = unionPayData.DecryptCertificateFilePath; var decFileName = decFilePath.Substring(decFilePath.LastIndexOf('/') + 1, decFilePath.Length - decFilePath.LastIndexOf('/') - 1); var uploadFilePath = HttpContext.Current.Server.MapPath("/PayCenter/"); FileSystem.CreateDirectoryIfNotExists(uploadFilePath); SaveFiles(decFilePath, uploadFilePath + decFileName); #endregion #region 加密 var creFilePath = unionPayData.CertificateFilePath; var creFileName = creFilePath.Substring(creFilePath.LastIndexOf('/') + 1, creFilePath.Length - creFilePath.LastIndexOf('/') - 1); FileSystem.CreateDirectoryIfNotExists(uploadFilePath); SaveFiles(creFilePath, uploadFilePath + creFileName); #endregion unionpay.MerchantID = unionPayData.MerchantID; unionpay.PacketEncryptKey = unionPayData.PacketEncryptKey; unionpay.DecryptCertificateFilePath = uploadFilePath + decFileName; unionpay.CertificateFilePath = uploadFilePath + creFileName; unionpay.CertificateFilePassword = unionPayData.CertificateFilePassword; payChannelEntity.ChannelParameters = unionpay.ToJSON(); } #endregion #region 支付宝 if (payType == 3) { var wapData = para.WapData; if (wapData.Partner == null || string.IsNullOrEmpty(wapData.Partner)) { throw new Exception("缺少参数【Partner】"); } if (wapData.SellerAccountName == null || string.IsNullOrEmpty(wapData.SellerAccountName)) { throw new Exception("缺少参数【SellerAccountName】"); } //if (wapData.MD5Key == null || string.IsNullOrEmpty(wapData.MD5Key)) //{ // throw new Exception("缺少参数【MD5Key】"); //} if (wapData.RSA_PublicKey == null || string.IsNullOrEmpty(wapData.RSA_PublicKey)) { throw new Exception("缺少参数【RSA_PublicKey】"); } if (wapData.RSA_PrivateKey == null || string.IsNullOrEmpty(wapData.RSA_PrivateKey)) { throw new Exception("缺少参数【RSA_PrivateKey】"); } payChannelEntity.ChannelParameters = wapData.ToJSON(); } if (payType == 4) { var wapData = para.WapData; if (wapData.Partner == null || string.IsNullOrEmpty(wapData.Partner)) { throw new Exception("缺少参数【Partner】"); } if (wapData.MD5Key == null || string.IsNullOrEmpty(wapData.MD5Key)) { throw new Exception("缺少参数【MD5Key】"); } wapData.AgentID = "8582928j2"; payChannelEntity.ChannelParameters = wapData.ToJSON(); } #endregion #region 微信 if (payType == 5 || payType == 6) { var wxPayData = para.WxPayData; if (wxPayData.AppID == null || string.IsNullOrEmpty(wxPayData.AppID)) { throw new Exception("缺少参数【AppID】"); } //if (wxPayData.AppSecret == null || string.IsNullOrEmpty(wxPayData.AppSecret)) //{ // throw new Exception("缺少参数【AppSecret】"); //} //if (wxPayData.ParnterID == null || string.IsNullOrEmpty(wxPayData.ParnterID)) //{ // throw new Exception("缺少参数【ParnterID】"); //} //if (wxPayData.ParnterKey == null || string.IsNullOrEmpty(wxPayData.ParnterKey)) //{ // throw new Exception("缺少参数【ParnterKey】"); //} //if (wxPayData.PaySignKey == null || string.IsNullOrEmpty(wxPayData.PaySignKey)) //{ // throw new Exception("缺少参数【PaySignKey】"); //} if (wxPayData.Mch_ID == null || string.IsNullOrEmpty(wxPayData.Mch_ID)) { throw new Exception("缺少参数【Mch_ID】"); } if (wxPayData.SignKey == null || string.IsNullOrEmpty(wxPayData.SignKey)) { throw new Exception("缺少参数【SignKey】"); } if (wxPayData.Trade_Type == null || string.IsNullOrEmpty(wxPayData.Trade_Type)) { throw new Exception("缺少参数【Trade_Type】"); } payChannelEntity.ChannelParameters = wxPayData.ToJSON(); } #endregion channelId = payChannelBll.GetMaxChannelId(); payChannelEntity.ChannelID = channelId; payChannelEntity.IsTest = false; payChannelEntity.NotifyUrl = notifyUrl; payChannelEntity.PayType = payType; payChannelBll.Create(payChannelEntity); payChannelList.ChannelId = channelId; payChannelResponsList.PayChannelIdList.Add(payChannelList); } return(payChannelResponsList); }
/// <summary> /// 创建交易中心支付订单AppOrder /// </summary> /// <param name="pRequest"></param> /// <returns></returns> public static CreateOrderResponse CreateOrder(TradeRequest pRequest) { var userInfo = pRequest.GetUserInfo(); CreateOrderResponse response = new CreateOrderResponse(); response.ResultCode = 0; CreateOrderParameters para = pRequest.GetParameter <CreateOrderParameters>(); Loggers.Debug(new DebugLogInfo() { Message = "业务参数:" + para.ToJSON() }); if (para == null) { throw new Exception("Parameters参数不正确"); } AppOrderBLL bll = new AppOrderBLL(userInfo); AppOrderEntity entity; #region 支付等待5秒后可再次支付 var appOrderEntity = bll.QueryByEntity(new AppOrderEntity() { ClientIP = pRequest.ClientID, AppOrderID = para.AppOrderID }, null).FirstOrDefault(); if (appOrderEntity != null) { DateTime dtNow = DateTime.Now; TimeSpan ts = dtNow - appOrderEntity.CreateTime.Value; if (ts.TotalSeconds < 5) { throw new Exception("支付已启动,请稍后再试"); } } #endregion #region 在支付中心创建订单 var tran = bll.CreateTran(); using (tran.Connection) { try { #region 除已存在的订单 bll.DeleteByAppInfo(pRequest.ClientID, para.AppOrderID, pRequest.AppID.Value, tran); #endregion #region 创建订单 entity = new AppOrderEntity() { Status = 0, MobileNO = para.MobileNO, AppClientID = pRequest.ClientID, AppUserID = pRequest.UserID, AppID = pRequest.AppID, AppOrderAmount = Convert.ToInt32(para.AppOrderAmount), AppOrderDesc = para.AppOrderDesc, AppOrderID = para.AppOrderID, AppOrderTime = para.GetDateTime(), Currency = 1, CreateBy = pRequest.UserID, PayChannelID = para.PayChannelID, LastUpdateBy = pRequest.UserID, OpenId = para.OpenId, ClientIP = para.ClientIP }; bll.Create(entity, tran);//并且生成了一个自动增长的订单标识orderid Loggers.Debug(new DebugLogInfo() { Message = "创建支付中心订单并保存数据库:" + entity.ToJSON() }); #endregion tran.Commit(); } catch { tran.Rollback(); throw; } } #endregion #region 获取Channel PayChannelBLL channelBll = new PayChannelBLL(userInfo); var channel = channelBll.GetByID(para.PayChannelID);//PayChannelID是不同商户的支付方式的标识 if (channel == null) { throw new Exception("无此ChannelID的Channel信息"); } #endregion #region 测试Channel订单价格设置为1分钱*** entity.AppOrderAmount = channel.IsTest.Value ? 1 : entity.AppOrderAmount; #endregion string url = string.Empty; if (para.AppOrderDesc != "ScanWxPayOrder") { if (para.PaymentMode == 0) { channel.PayType = 9; //新版支付宝扫码支付 } url = CreatePayRecord(response, para, bll, entity, channel); } response.PayUrl = url; response.OrderID = entity.OrderID; return(response); }