/// <summary> /// 通知cpos /// </summary> /// <param name="pEntity"></param> /// <param name="msg"></param> /// <param name="ht">附加参数</param> /// <param name="isTonyCard"></param> /// <returns></returns> public static bool Notify(AppOrderEntity pEntity, out string msg, Hashtable ht, bool isTonyCard = false) { string content = string.Format("OrderID={0}&OrderStatus={1}&CustomerID={2}&UserID={3}&ChannelID={4}&SerialPay={5}&isTonyCard={6}", pEntity.AppOrderID, pEntity.Status, pEntity.AppClientID, pEntity.AppUserID, pEntity.PayChannelID, pEntity.OrderID, isTonyCard ? 1 : 0); // 附加参数 if (ht.Count > 0) { foreach (DictionaryEntry item in ht) { content += "&" + item.Key + "=" + item.Value; } } var channelbll = new PayChannelBLL(new JIT.Utility.BasicUserInfo()); var channel = channelbll.GetByID(pEntity.PayChannelID); string notifyUrl = channel.NotifyUrl; Loggers.Debug(new DebugLogInfo() { Message = "wx pay Notify 开始调用通知接口:" + pEntity.ToJSON() }); var i = NotifyHandler.Notify(channel.NotifyUrl, content, out msg); string message = i ? " wx pay Notify 调用通知接口成功" : " wx pay Notify 调用通知接口失败"; message += (":" + msg + "::" + notifyUrl + "?" + content); Loggers.Debug(new DebugLogInfo() { Message = message }); return(i); }
/// <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 }); }
private bool Notify(AppOrderEntity pEntity, out string msg, bool isTonyCard = false) { string content = string.Format("OrderID={0}&OrderStatus={1}&CustomerID={2}&UserID={3}&ChannelID={4}&SerialPay={5}&isTonyCard={6}", pEntity.AppOrderID, pEntity.Status, pEntity.AppClientID, pEntity.AppUserID, pEntity.PayChannelID, pEntity.OrderID, isTonyCard ? 1 : 0); var channelbll = new PayChannelBLL(new JIT.Utility.BasicUserInfo()); var channel = channelbll.GetByID(pEntity.PayChannelID); string notifyUrl = channel.NotifyUrl; Loggers.Debug(new DebugLogInfo() { Message = "wx pay Notify 开始调用通知接口:" + pEntity.ToJSON() }); var i = Notify(channel.NotifyUrl, content, out msg); string message = i ? " wx pay Notify 调用通知接口成功" : " wx pay Notify 调用通知接口失败"; message += (":" + msg + "::" + notifyUrl + "?" + content); Loggers.Debug(new DebugLogInfo() { Message = message }); return(i); }
/// <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); }
public void ProcessRequest(HttpContext context) { try { var channelid = Convert.ToInt32(context.Request["ChannelID"]); Loggers.Debug(new DebugLogInfo() { Message = "ChannelID:" + channelid }); var paychannelbll = new PayChannelBLL(new Utility.BasicUserInfo()); var paychannel = paychannelbll.GetByID(channelid); AppOrderEntity entity; AppOrderBLL bll = new AppOrderBLL(new Utility.BasicUserInfo()); if (Process(paychannel, context, out entity)) { Loggers.Debug(new DebugLogInfo() { Message = "entity:" + entity.ToJSON() }); if (!entity.IsNotified.HasValue) { entity.IsNotified = false; } if (!entity.IsNotified.Value) { Loggers.Debug(new DebugLogInfo() { Message = "IsNotified is false" }); //var t = Task.Factory.StartNew(() => // {//起一个新线程通知业务系统处理订单 try { string msg; if (Notify(entity, out msg)) { entity.IsNotified = true; } else { entity.NextNotifyTime = DateTime.Now.AddMinutes(1); } //通知完成,通知次数+1 entity.NotifyCount = (entity.NotifyCount ?? 0) + 1; bll.Update(entity); } catch (Exception ex) { Loggers.Exception(new ExceptionLogInfo(ex)); } //}); //t.Wait(); Loggers.Debug(new DebugLogInfo() { Message = "task is end" }); } else { Loggers.Debug(new DebugLogInfo() { Message = "已处理过的订单" }); }; } else { if (entity != null) { Loggers.Debug(new DebugLogInfo() { Message = "交易失败:" + entity.ErrorMessage }); } } } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = "error:" + ex.Message }); context.Response.Write(ex); } }