예제 #1
0
        /// <summary>
        /// 支付购电
        /// </summary>
        /// <param name="co_id">房间ID号</param>
        /// <param name="payAmt">充值金额</param>
        /// <returns></returns>
        public APIRst ForPay(int co_id, decimal payAmt)
        {
            APIRst rst = new APIRst();

            if (co_id == 0)
            {
                rst.rst = false;
                rst.err = new APIErr()
                {
                    code = 100, msg = "设备编号不能为空"
                };
                return(rst);
            }
            if (payAmt <= 0)
            {
                rst.rst = false;
                rst.err = new APIErr()
                {
                    code = 100, msg = "充值金额不能为零"
                };
                return(rst);
            }
            //App支付预处理
            try
            {
                v4_pay_logVModel pay = new v4_pay_logVModel();
                #region 登记付款log记录
                DataTable dtSource = bll.GetUserInfo();
                int       nRows    = dtSource.Rows.Count;
                if (nRows == 0)
                {
                    rst.rst      = false;
                    rst.err.code = (int)ResultCodeDefine.Error;
                    rst.err.msg  = "没有此用户,请重新登陆";
                    return(rst);
                }
                string  uName      = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["CrmName"]);
                string  code       = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["Contract"]);
                string  roomName   = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["CoFullName"]);
                string  moduleName = CommFunc.ConvertDBNullToString(dtSource.Rows[0]["ModuleName"]);
                decimal rdAmt      = CommFunc.ConvertDBNullToDecimal(dtSource.Rows[0]["RdAmt"]);

                pay.Co_id        = co_id;
                pay.Module_id    = 0;
                pay.ModuleAddr   = ""; // CommFunc.ConvertDBNullToString(Request["ModuleAddr"]);
                pay.Fun_id       = 0;
                pay.PayAmt       = payAmt;
                pay.PayVal       = payAmt; //Math.Round(pay.PayAmt / pay.Price, 2, MidpointRounding.AwayFromZero);
                pay.PayType      = 1;
                pay.IsWrong      = 0;
                pay.IsPay        = 10;
                pay.PayStartTime = DateTime.Now;
                pay.PayEndTime   = DateTime.Now;
                if (pay.Co_id == 0)
                {
                    rst.rst = false;
                    rst.err = new APIErr()
                    {
                        code = 100, msg = "充值信息缺失"
                    };
                    return(rst);
                }
                if (pay.PayAmt <= 0 || pay.PayVal <= 0)
                {
                    rst.rst = false;
                    rst.err = new APIErr()
                    {
                        code = 100, msg = "充值金额" + pay.PayAmt + "单价:" + pay.Price + "转换电量:" + pay.PayVal + "不能为零"
                    };
                    return(rst);
                }
                if (pay.PayStartTime.Year == 1900 || pay.PayEndTime.Year == 1900 || pay.PayEndTime < pay.PayStartTime)
                {
                    rst.rst = false;
                    rst.err = new APIErr()
                    {
                        code = 100, msg = "充值日期错误"
                    };
                    return(rst);
                }
                #endregion

                #region 发起微信支付
                string strBody     = string.Format("【{0}{1}】充值", roomName, pay.ModuleAddr); //商品描述
                string out_tran_no = "";
                new WxPayAPI.WxPay();                                                       /*初始下微信支付环境*/
                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid    = "";
                jsApiPay.total_fee = (int)(payAmt * 100);
                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(strBody, "附件信息:智能用电", "APP", out out_tran_no);
                //FileLog.WriteLog("步骤一:", "单号:" + out_tran_no);
                WxPayData wxJsApiParam = jsApiPay.GetAppApiParameters();//获取H5调起JS API参数
                object    order        = new
                {
                    appid     = wxJsApiParam.GetValue("appid").ToString(),
                    partnerid = wxJsApiParam.GetValue("partnerid").ToString(),
                    prepayid  = wxJsApiParam.GetValue("prepayid").ToString(),
                    package   = "Sign=WXPay",
                    noncestr  = wxJsApiParam.GetValue("noncestr").ToString(),
                    timestamp = CommFunc.ConvertDBNullToInt32(wxJsApiParam.GetValue("timestamp")),
                    sign      = wxJsApiParam.GetValue("sign").ToString(),
                };
                pay.Trade_no = out_tran_no;
                #endregion

                if (string.IsNullOrEmpty(pay.Trade_no))
                {
                    rst.rst = false;
                    rst.err = new APIErr()
                    {
                        code = 100, msg = "微信支付产生单号为空"
                    };
                    return(rst);
                }
                bll.AddPayLog(pay, rdAmt);

                rst.rst  = true;
                rst.data = order;
            }
            catch (Exception ex)
            {
                rst.rst      = false;
                rst.err.code = (int)ResultCodeDefine.Error;
                rst.err.msg  = ex.Message;
                FileLog.WriteLog("支付失败:", ex.Message + ex.StackTrace);
            }
            return(rst);
        }