Esempio n. 1
0
        public JsonResult SetWeChatAccount(PropertyAccountModel model)
        {
            JsonModel jm = new JsonModel();

            if (ModelState.IsValid)
            {
                var propertyPlaceId = GetSessionModel().PropertyPlaceId.Value;
                IPropertyAccountBLL propertyAccountBll = BLLFactory <IPropertyAccountBLL> .GetBLL("PropertyAccountBLL");

                var account = propertyAccountBll.GetEntity(u => u.PropertyPlaceId == propertyPlaceId && u.AccountType == ConstantParam.PROPERTY_ACCOUNT_WeChat && u.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);

                //如果微信账户不为空
                if (account != null)
                {
                    account.Number     = model.WeChatNumber;
                    account.MerchantNo = model.WeChatMerchantNo;
                    account.AccountKey = model.WeChatKey;
                    //更新
                    propertyAccountBll.Update(account);
                }
                else
                {
                    T_PropertyAccount account2 = new T_PropertyAccount();
                    account2.PropertyPlaceId = GetSessionModel().PropertyPlaceId.Value;
                    account2.AccountType     = ConstantParam.PROPERTY_ACCOUNT_WeChat;
                    account2.Number          = model.WeChatNumber;
                    account2.MerchantNo      = model.WeChatMerchantNo;
                    account2.AccountKey      = model.WeChatKey;
                    //保存
                    propertyAccountBll.Save(account2);
                }
                //日志记录
                jm.Content = PropertyUtils.ModelToJsonString(model);
            }
            else
            {
                jm.Msg = ConstantParam.JSON_RESULT_MODEL_CHECK_ERROR;
            }
            return(Json(jm, JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public JsonResult SetAlipayAccount(PropertyAccountModel model)
        {
            JsonModel jm = new JsonModel();

            if (ModelState.IsValid)
            {
                var propertyPlaceId = GetSessionModel().PropertyPlaceId.Value;

                //存入文件的路径
                string directory = Server.MapPath(ConstantParam.ALIPAY_KEY);

                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                //私钥
                HttpPostedFileBase privateFile = model.PrivatePath;

                //公钥
                HttpPostedFileBase publicFile = model.PublicPath;

                //获取上传文件名
                string privateFileName = Path.GetFileName(privateFile.FileName);
                string publicFileName  = Path.GetFileName(publicFile.FileName);

                //获取上传文件的扩展名
                string privateFileEx = Path.GetExtension(privateFileName);
                string publicFileEx  = Path.GetExtension(publicFileName);

                //定义上传文件的类型字符串
                string fileType = ".pem";

                //判断文件类型格式是否正确
                if (fileType != privateFileEx || fileType != publicFileEx)
                {
                    jm.Msg = "文件类型只能是pem格式的文件";
                    return(Json(jm, JsonRequestBehavior.AllowGet));
                }

                //存入的文件名
                string PrivateName = Guid.NewGuid() + privateFileEx;
                string PublicName  = Guid.NewGuid() + publicFileEx;

                //组装文件保存路径
                string savePrivatePath = Path.Combine(directory, PrivateName);
                string savePublicPath  = Path.Combine(directory, PublicName);

                //保存数据文件
                privateFile.SaveAs(savePrivatePath);
                publicFile.SaveAs(savePublicPath);

                //读取私钥文件
                string content = PropertyUtils.ReadFile(savePrivatePath);
                if (content == "")
                {
                    jm.Msg = "私钥文件内容为空";
                    return(Json(jm, JsonRequestBehavior.AllowGet));
                }
                string privatekey = content.Replace("\n", "").Replace("\r", "");
                string privateKey = privatekey.Substring(27);
                string PrivateKey = privateKey.Substring(0, privateKey.Length - 25);

                IPropertyAccountBLL propertyAccountBll = BLLFactory <IPropertyAccountBLL> .GetBLL("PropertyAccountBLL");

                var propertyAccount = propertyAccountBll.GetEntity(u => u.PropertyPlaceId == propertyPlaceId && u.AccountType == ConstantParam.PROPERTY_ACCOUNT_Alipay && u.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);

                //如果支付宝不为空
                if (propertyAccount != null)
                {
                    propertyAccount.Number      = model.AlipayNumber;
                    propertyAccount.MerchantNo  = model.AlipayMerchantNo;
                    propertyAccount.AccountKey  = PrivateKey;
                    propertyAccount.PrivatePath = ConstantParam.ALIPAY_KEY + PrivateName;
                    propertyAccount.PublicPath  = ConstantParam.ALIPAY_KEY + PublicName;
                    propertyAccountBll.Update(propertyAccount);
                }
                else
                {
                    T_PropertyAccount propertyaccount = new T_PropertyAccount()
                    {
                        PropertyPlaceId = propertyPlaceId,
                        AccountType     = ConstantParam.PROPERTY_ACCOUNT_Alipay,
                        Number          = model.AlipayNumber,
                        MerchantNo      = model.AlipayMerchantNo,
                        AccountKey      = PrivateKey,
                        PrivatePath     = ConstantParam.ALIPAY_KEY + PrivateName,
                        PublicPath      = ConstantParam.ALIPAY_KEY + PublicName
                    };
                    propertyAccountBll.Save(propertyaccount);
                }

                //日志记录
                //jm.Content = PropertyUtils.ModelToJsonString(model);
            }
            else
            {
                jm.Msg = ConstantParam.JSON_RESULT_MODEL_CHECK_ERROR;
            }
            return(Json(jm, JsonRequestBehavior.AllowGet));
        }
Esempio n. 3
0
        public ActionResult WeixinExpenseNotifyUrl()
        {
            Stream       st       = Request.InputStream;
            StreamReader sr       = new StreamReader(st);
            string       SRstring = sr.ReadToEnd();

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(SRstring);
            sr.Close();

            string return_code = doc.GetElementsByTagName("return_code")[0].InnerText;

            //如果返回成功
            if (return_code == "SUCCESS")
            {
                string result_code = doc.GetElementsByTagName("result_code")[0].InnerText;
                if (result_code == "SUCCESS")
                {
                    string expenseOrderNo = doc.GetElementsByTagName("out_trade_no")[0].InnerText;

                    IHouseUserExpenseDetailsBLL expenseDetailsBLL = BLLFactory <IHouseUserExpenseDetailsBLL> .GetBLL("HouseUserExpenseDetailsBLL");

                    var record = expenseDetailsBLL.GetEntity(e => e.PayTradeNo == expenseOrderNo);

                    if (record != null && record.IsPayed == ConstantParam.PAYED_FALSE)
                    {
                        //获取物业微信账户信息
                        T_PropertyAccount wxAccount = null;
                        if (record.BuildDoorId != null)
                        {
                            wxAccount = record.BuildDoor.BuildUnit.Build.PropertyPlace.PropertyAccounts.Where(
                                a => a.AccountType == ConstantParam.PROPERTY_ACCOUNT_WeChat).FirstOrDefault();
                        }
                        else
                        {
                            wxAccount = record.BuildCompany.PropertyPlace.PropertyAccounts.Where(
                                a => a.AccountType == ConstantParam.PROPERTY_ACCOUNT_WeChat).FirstOrDefault();
                        }
                        if (wxAccount != null)
                        {
                            string WeixinPayKey = wxAccount.AccountKey;
                            //组装签名字符串
                            StringBuilder signStr = new StringBuilder();
                            signStr.Append("appid=" + doc.GetElementsByTagName("appid")[0].InnerText + "&");
                            signStr.Append("bank_type=" + doc.GetElementsByTagName("bank_type")[0].InnerText + "&");
                            signStr.Append("cash_fee=" + doc.GetElementsByTagName("cash_fee")[0].InnerText + "&");
                            signStr.Append("fee_type=" + doc.GetElementsByTagName("fee_type")[0].InnerText + "&");
                            signStr.Append("is_subscribe=" + doc.GetElementsByTagName("is_subscribe")[0].InnerText + "&");
                            signStr.Append("mch_id=" + doc.GetElementsByTagName("mch_id")[0].InnerText + "&");
                            signStr.Append("nonce_str=" + doc.GetElementsByTagName("nonce_str")[0].InnerText + "&");
                            signStr.Append("openid=" + doc.GetElementsByTagName("openid")[0].InnerText + "&");
                            signStr.Append("out_trade_no=" + expenseOrderNo + "&");
                            signStr.Append("result_code=" + result_code + "&");
                            signStr.Append("return_code=" + return_code + "&");
                            signStr.Append("time_end=" + doc.GetElementsByTagName("time_end")[0].InnerText + "&");
                            signStr.Append("total_fee=" + doc.GetElementsByTagName("total_fee")[0].InnerText + "&");
                            signStr.Append("trade_type=" + doc.GetElementsByTagName("trade_type")[0].InnerText + "&");
                            signStr.Append("transaction_id=" + doc.GetElementsByTagName("transaction_id")[0].InnerText + "&");
                            signStr.Append("key=" + WeixinPayKey);
                            string sign = PropertyUtils.GetMD5Str(signStr.ToString()).ToUpper();
                            //签名验证成功
                            if (doc.GetElementsByTagName("sign")[0].InnerText == sign)
                            {
                                record.IsPayed     = ConstantParam.PAYED_TRUE;
                                record.PaymentType = 2;
                                record.PayedDate   = DateTime.Now;
                                expenseDetailsBLL.Update(record);
                            }
                        }
                    }
                    return(Content("success"));
                }
            }
            return(Content("fail"));
        }
        public ApiResultModel CreateWeixinPayTrade(DetailSearchModel model)
        {
            ApiResultModel resultModel = new ApiResultModel();

            try
            {
                //根据用户ID查找业主
                IUserBLL ownerBll = BLLFactory <IUserBLL> .GetBLL("UserBLL");

                T_User owner = ownerBll.GetEntity(u => u.Id == model.UserId && u.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                //如果业主存在
                if (owner != null)
                {
                    //如果验证Token不通过或已过期
                    if (DateTime.Now > owner.TokenInvalidTime || model.Token != owner.Token)
                    {
                        resultModel.Msg = APIMessage.TOKEN_INVALID;
                        return(resultModel);
                    }
                    //更新最近登录时间和Token失效时间
                    owner.LatelyLoginTime  = DateTime.Now;
                    owner.TokenInvalidTime = DateTime.Now.AddDays(Convert.ToInt32(PropertyUtils.GetConfigParamValue("TokenInvalid")));
                    ownerBll.Update(owner);

                    //生成微信支付订单
                    IHouseUserExpenseDetailsBLL expenseDetailsBLL = BLLFactory <IHouseUserExpenseDetailsBLL> .GetBLL("HouseUserExpenseDetailsBLL");

                    var record = expenseDetailsBLL.GetEntity(e => e.Id == model.Id);
                    if (record != null)
                    {
                        //获取物业缴费账户信息
                        T_PropertyAccount wxAccount = null;
                        if (record.BuildDoorId != null)
                        {
                            wxAccount = record.BuildDoor.BuildUnit.Build.PropertyPlace.PropertyAccounts.Where(
                                a => a.AccountType == ConstantParam.PROPERTY_ACCOUNT_WeChat).FirstOrDefault();
                        }
                        else
                        {
                            wxAccount = record.BuildCompany.PropertyPlace.PropertyAccounts.Where(
                                a => a.AccountType == ConstantParam.PROPERTY_ACCOUNT_WeChat).FirstOrDefault();
                        }
                        if (wxAccount == null)
                        {
                            resultModel.Msg = "该用户所属物业未设置账户信息";
                            return(resultModel);
                        }
                        //获取物业账户信息
                        string WeixinAppId  = wxAccount.Number;
                        string WeixinMchId  = wxAccount.MerchantNo;
                        string WeixinPayKey = wxAccount.AccountKey;
                        //生成预订单
                        string result = CreateTradePost(record, WeixinAppId, WeixinMchId, WeixinPayKey);

                        //如果请求失败
                        if (result == null)
                        {
                            resultModel.Msg = APIMessage.WEIXIN_YUORDER_FAIL;
                            return(resultModel);
                        }
                        //解析返回结果
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(result);
                        string return_code = doc.GetElementsByTagName("return_code")[0].InnerText;
                        //如果返回成功
                        if (return_code == "SUCCESS")
                        {
                            string result_code = doc.GetElementsByTagName("result_code")[0].InnerText;
                            if (result_code == "SUCCESS")
                            {
                                //预支付交易会话标识
                                string prepayid = doc.GetElementsByTagName("prepay_id")[0].InnerText;

                                //随机字符串
                                string str          = "1234567890abcdefghijklmnopqrstuvwxyz";
                                Random r            = new Random();
                                string PayRandomStr = "";
                                for (int i = 0; i < 32; i++)
                                {
                                    PayRandomStr += str[r.Next(str.Length)].ToString();
                                }
                                //时间戳
                                var timestamp = Convert.ToInt64(DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds / 10000) * 10;
                                //签名字符串
                                string PaySignStr = "appid=" + WeixinAppId + "&noncestr=" + PayRandomStr + "&package=Sign=WXPay&partnerid="
                                                    + WeixinMchId + "&prepayid=" + prepayid + "&timestamp=" + timestamp + "&key=" + WeixinPayKey;
                                resultModel.result = new
                                {
                                    appid     = WeixinAppId,
                                    partnerid = WeixinMchId,
                                    package   = "Sign=WXPay",
                                    prepayid  = prepayid,
                                    noncestr  = PayRandomStr,
                                    timestamp = timestamp,
                                    sign      = PropertyUtils.GetMD5Str(PaySignStr.ToString()).ToUpper()
                                };
                            }
                            else
                            {
                                string err_code_des = doc.GetElementsByTagName("err_code_des")[0].InnerText;
                                resultModel.Msg = err_code_des;
                            }
                        }
                        else
                        {
                            string return_msg = doc.GetElementsByTagName("return_msg")[0].InnerText;
                            resultModel.Msg = return_msg;
                        }
                    }
                    else
                    {
                        resultModel.Msg = APIMessage.EXPENSE_RECORD_NOEXIST;
                    }
                }
                else
                {
                    resultModel.Msg = APIMessage.NO_USER;
                }
            }
            catch
            {
                resultModel.Msg = APIMessage.REQUEST_EXCEPTION;
            }
            return(resultModel);
        }