Пример #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder strLog = new StringBuilder();

        strLog.AppendFormat("【网银充值,DateTime:{0}】\r\n", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
        if (!IsPostBack)
        {
            try
            {
                //客户ID,账户号,订单号
                String CustID, BestPayAccount, OrderSeq;
                //充值金额,账户余额
                long RechargeBalance, Balance;

                #region 获取支付平台post过来的一些变量

                //获取支付平台post过来的一些变量
                String uptranSeq       = Request.Form["UPTRANSEQ"];             //支付平台交易流水号
                String tranDate        = Request.Form["TRANDATE"];              //支付平台交易日期
                String retnCode        = Request.Form["RETNCODE"];              //处理结果码
                String retnInfo        = Request.Form["RETNINFO"];              //处理结果解释码
                String orderReqtranSeq = Request.Form["ORDERREQTRANSEQ"];       //订单请求交易流水号
                String orderSeq        = Request.Form["ORDERSEQ"];              //订单号
                String orderAmount     = Request.Form["ORDERAMOUNT"];           //订单总金额
                String productAmount   = Request.Form["PRODUCTAMOUNT"];         //产品金额
                String attachAmount    = Request.Form["ATTACHAMOUNT"];          //附加金额
                String curType         = Request.Form["CURTYPE"];               //币种
                String encodeType      = Request.Form["ENCODETYPE"];            //加密方式
                String attach          = Request.Form["ATTACH"];                //SP附加信息
                String sign            = Request.Form["SIGN"];                  //数字签名

                strLog.AppendFormat("[网银返回参数]:uptranSeq:{0},tranDate:{1},retnCode:{2},retnInfo:{3},orderReqtranSeq:{4},orderSeq:{5},orderAmount:{6},productAmount:{7},attachAmount:{8},curType:{9},attach:{10},sign:{11},",
                                    uptranSeq, tranDate, retnCode, retnInfo, orderReqtranSeq, orderSeq, orderAmount, productAmount, attachAmount, curType, attach, sign);

                if (retnCode != "0000")
                {
                    return;
                }

                //验证签名:16进制转换(MD5加密)
                String newSign = String.Format("UPTRANSEQ={0}&MERCHANTID={1}&ORDERID={2}&PAYMENT={3}&RETNCODE={4}&RETNINFO={5}&PAYDATE={6}&KEY={7}",
                                               uptranSeq, BesttoneAccountConstDefinition.DefaultInstance.MERCHANTID, orderSeq, orderAmount, retnCode, retnInfo, tranDate, BesttoneAccountConstDefinition.DefaultInstance.MERCHANTID_KEY);
                String md5EncodingSign = BesttoneAccountHelper.MACSign(newSign);
                strLog.AppendFormat("newSign:{0},md5EncodingSign:{1}\r\n", newSign, md5EncodingSign);
                if (!md5EncodingSign.Equals(sign))
                {
                    strLog.Append("[签名验证]:签名验证有误");
                    return;
                }

                String responseXml = String.Format("UPTRANSEQ_{0}", uptranSeq);
                strLog.AppendFormat("[返回给网银参数]:ResponseXml:{0}\r\n", responseXml);

                #endregion

                #region 更新银行扣款订单状态

                //查询订单,检查订单状态
                RechargeOrder      _recharge_order      = _rechargeOrder_dao.QueryByOrderSeq(orderSeq);
                BankRechargeRecord _bankRecharge_record = _bankRechargeRecord_dao.QueryByOrderTransacntionID(orderReqtranSeq);
                if (_recharge_order == null)
                {
                    return;
                }
                if (_bankRecharge_record == null)
                {
                    return;
                }

                CustID          = _recharge_order.CustID;
                BestPayAccount  = _recharge_order.TargetAccount;
                OrderSeq        = _recharge_order.OrderSeq;
                RechargeBalance = _recharge_order.OrderAmount;

                strLog.AppendFormat("[订单信息]:CustID:{0},BestPayAccount:{1},OrderSeq:{2},RechargeBalance:{3},订单状态:{4}\r\n",
                                    CustID, BestPayAccount, OrderSeq, RechargeBalance, _recharge_order.Status);

                //检查订单状态    6 扣款异常  主表
                if (_recharge_order.Status == 6)
                {
                    return;
                }
                else if (_recharge_order.Status == 2 || _recharge_order.Status == 3 || _recharge_order.Status == 5 || _recharge_order.Status == 9)
                {
                    Response.Write(responseXml.ToString());    // 2 扣款成功    3 已充成功    5  充值失败   9 已对账
                }
                // Response.Write 后,不会再往下执行
                // 0 ,1, 4, 7 往下做  先把主表状态改为2,然后将扣款子表状态改为1 意味着扣款成功
                Boolean result = false;
                //修改订单状态
                _recharge_order.Status    = 2;
                _recharge_order.PayTime   = DateTime.Now;
                _recharge_order.UptranSeq = uptranSeq;
                result = _rechargeOrder_dao.Update(_recharge_order);
                strLog.AppendFormat("[修改订单结果]:{0}\r\n", result);
                if (!result)
                {
                    return;
                }

                //修改扣款记录状态
                _bankRecharge_record.UptranSeq  = uptranSeq;
                _bankRecharge_record.Sign       = sign;
                _bankRecharge_record.TranDate   = tranDate;
                _bankRecharge_record.ReturnCode = retnCode;
                _bankRecharge_record.ReturnDesc = retnInfo;
                _bankRecharge_record.PayTime    = DateTime.Now;
                _bankRecharge_record.Status     = 1;
                result = _bankRechargeRecord_dao.Update(_bankRecharge_record);
                if (!result)
                {
                    return;
                }

                #endregion

                #region 向账户进行充值

                //生成一条充值流水号记录
                DateTime rechargeTime  = DateTime.Now;
                String   transactionid = BesttoneAccountHelper.CreateTransactionID();

                AccountRechargeRecord rechargeRecord_entity = new AccountRechargeRecord();
                rechargeRecord_entity.RechargeTransactionID = transactionid;
                rechargeRecord_entity.RechargeDate          = rechargeTime.ToString("yyyyMMdd");
                rechargeRecord_entity.OrderSeq     = OrderSeq;
                rechargeRecord_entity.OrderAmount  = RechargeBalance;
                rechargeRecord_entity.RechargeType = "0";
                rechargeRecord_entity.OrderDesc    = "网银充值";
                rechargeRecord_entity.ReqTime      = rechargeTime;
                rechargeRecord_entity.CompleteTime = new DateTime(1900, 1, 1);
                rechargeRecord_entity.Status       = 0;
                rechargeRecord_entity.ReturnCode   = "";
                rechargeRecord_entity.ReturnDesc   = "";
                strLog.AppendFormat("[开始给账户充值]:transactionid:{0},", transactionid);
                try
                {
                    //开始调用接口充值
                    Result = BesttoneAccountHelper.AccountRecharge(transactionid, BestPayAccount, RechargeBalance, rechargeTime, out Balance, out ErrMsg);
                    if (Result == 0)
                    {
                        strLog.Append("充值状态:充值成功\r\n");
                        //充值成功,则更新订单状态
                        _recharge_order = _rechargeOrder_dao.QueryByOrderSeq(OrderSeq);
                        _recharge_order.CompleteTime  = DateTime.Now;
                        _recharge_order.Status        = 3;
                        _recharge_order.RechargeCount = _recharge_order.RechargeCount + 1;
                        _recharge_order.ReturnCode    = "0000";
                        _recharge_order.ReturnDesc    = "充值成功";
                        _rechargeOrder_dao.Update(_recharge_order);
                        //如果需要开发票 -- 插入发票表  2013-04-14 add-start
                        System.Text.StringBuilder datalog = new System.Text.StringBuilder();
                        _rechargeOrder_dao.UpdateInvoice(orderSeq, "0", out datalog);  //将发票状态改为申请中.
                        strLog.Append("更新发票状态:" + datalog.ToString());
                        //2013-04-14 add-end
                        //更新流水号状态
                        rechargeRecord_entity.Status       = 1;
                        rechargeRecord_entity.CompleteTime = DateTime.Now;
                        rechargeRecord_entity.ReturnCode   = "0000";
                        rechargeRecord_entity.ReturnDesc   = "充值成功";
                    }
                    else
                    {
                        strLog.Append("充值状态:充值失败\r\n");
                        //充值失败,则更新订单状态
                        _recharge_order = _rechargeOrder_dao.QueryByOrderSeq(OrderSeq);
                        _recharge_order.RechargeCount = _recharge_order.RechargeCount + 1;
                        _recharge_order.ReturnCode    = Result.ToString();
                        _recharge_order.ReturnDesc    = ErrMsg;
                        _rechargeOrder_dao.Update(_recharge_order);

                        //更新流水号状态
                        rechargeRecord_entity.Status       = 2;
                        rechargeRecord_entity.CompleteTime = DateTime.Now;
                        rechargeRecord_entity.ReturnCode   = Result.ToString();
                        rechargeRecord_entity.ReturnDesc   = ErrMsg;
                    }
                }
                catch (Exception ex)
                {
                    strLog.AppendFormat("充值异常1:{0}\r\n", ex.Message);
                }
                finally
                {
                    //插入充值流水记录
                    _accountRechargeRecord_dao.Insert(rechargeRecord_entity);
                    Response.Write(responseXml.ToString());
                }

                #endregion
            }
            catch (Exception ex)
            {
                strLog.AppendFormat("异常2:{0}\r\n", ex.Message);
            }
            finally
            {
                log(strLog.ToString());
            }
        }
    }
Пример #2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                String custid         = Request["hiddenCustID"];
                String spid           = Request["hiddenSPID"];
                String InvoiceType    = Request["InvoiceType"];
                String InvoiceTitle   = Request["InvoiceTitle"];
                String InvoiceContent = Request["InvoiceContent"];
                String ContactPerson  = Request["ContactPerson"];
                String ContactPhone   = Request["ContactPhone"];
                String Address        = Request["Address"];
                String Zip            = Request["Zip"];
                String Mem            = Request["Mem"];
                String NeedInvoice    = Request["NeedInvoice"];


                long     balance = Convert.ToInt64(Convert.ToDouble(Request["TranAmount"]) * 100);
                DateTime reqTime = DateTime.Now;

                //查询账户信息
                BesttoneAccountDAO _besttoneAccount_dao = new BesttoneAccountDAO();
                BesttoneAccount    account_entity       = _besttoneAccount_dao.QueryByCustID(custid);
                if (account_entity == null)
                {
                    CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "账户信息异常", this.Context);
                    return;
                }

                #region 账户充值金额上限校验

                long OnceRechargeLimit   = BesttoneAccountConstDefinition.DefaultInstance.AccountRechargeLimitedOne;            //单笔充值金额上限
                long RechargeAmountLimit = BesttoneAccountConstDefinition.DefaultInstance.AccountRechargeLimitedDay;            //账户单日充值额度上限
                long CurrentAmountLimit  = BesttoneAccountConstDefinition.DefaultInstance.AccountBalanceLimited;                //账户余额上限
                if (OnceRechargeLimit > 0)
                {
                    //检测用户单笔充值金额是否超限(10000元)
                    if (balance > OnceRechargeLimit)
                    {
                        result = 100003;
                        ErrMsg = String.Format("单笔充值金额最多不能超过{0}元", BesttoneAccountHelper.ConvertAmountToYuan(OnceRechargeLimit));
                        CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "充值金额超过上限", this.Context);
                        return;
                    }
                }
                if (RechargeAmountLimit > 0)
                {
                    //检测用户当日充值是否超限(当日充值金额不能超过50000元)
                    long hadRechargeAmount = _rechargeOrder_dao.QueryCurrentRechargeAmount(account_entity.BestPayAccount);
                    if ((hadRechargeAmount + balance) > RechargeAmountLimit)
                    {
                        result = 100001;
                        ErrMsg = String.Format("您今日累计充值金额:{0}元,本次充值将超过您的当日累计充值限额:{1}元,请改日再进行充值操作!",
                                               BesttoneAccountHelper.ConvertAmountToYuan(hadRechargeAmount), BesttoneAccountHelper.ConvertAmountToYuan(RechargeAmountLimit));
                        CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "充值金额超过上限", this.Context);
                        return;
                    }
                }

                if (CurrentAmountLimit > 0)
                {
                    long accountBalance = 0;
                    //检测用户帐户余额(个人账户余额不能超过100000元)
                    result = BesttoneAccountHelper.QueryAccountBalance(account_entity.BestPayAccount, out accountBalance, out ErrMsg);
                    if (result != 0)
                    {
                        CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "账户信息异常", this.Context);
                        return;
                    }

                    if ((accountBalance + balance) > CurrentAmountLimit)
                    {
                        result = 100002;
                        ErrMsg = String.Format("您的账户余额为:{0}元,本次充值将超过您的账户余额上限:{1}元,请消费后再进行充值操作!",
                                               BesttoneAccountHelper.ConvertAmountToYuan(accountBalance), BesttoneAccountHelper.ConvertAmountToYuan(CurrentAmountLimit));
                        CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "充值金额超过上限", this.Context);
                        return;
                    }
                }

                #endregion

                bool Result = false;

                #region 生成订单信息

                /***************************************************************************/
                String orderSeq      = BesttoneAccountHelper.CreateOrderSeq();              //订单号
                String transactionID = BesttoneAccountHelper.CreateTransactionID();         //流水号
                // 在发起网银扣款请求,主表状态为1,扣款子表状态为0
                //初始化充值订单
                //2013-04-13 add -start
                if (String.IsNullOrEmpty(NeedInvoice))
                {
                    NeedInvoice = "0";
                }
                //2013-04-13 add -end
                RechargeOrder _recharge_order = new RechargeOrder(orderSeq, transactionID, reqTime.ToString("yyyyMMdd"), "RMB",
                                                                  balance, balance, 0, "网银充值", custid, account_entity.BestPayAccount, "0", spid, reqTime, new DateTime(1900, 1, 1),
                                                                  new DateTime(1900, 1, 1), 1, 0, "", "", "", NeedInvoice); //2013-04-13 add NeedInvoice 字段

                //初始化网银扣款流水记录
                BankRechargeRecord _bankRecharge_record = new BankRechargeRecord(orderSeq, transactionID, reqTime.ToString("yyyyMMdd"), "RMB",
                                                                                 balance, balance, 0, "网银充值", account_entity.BestPayAccount, 0, reqTime, new DateTime(1900, 1, 1), "", "", "", "", "");

                Result = _rechargeOrder_dao.Insert(_recharge_order);
                if (!Result)
                {
                    CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "网银充值失败1", this.Context);
                    return;
                }

                //如果需要开发票 -- 插入发票表  2013-04-14 add-start
                if ("1".Equals(NeedInvoice))
                {
                    System.Text.StringBuilder datalog = new System.Text.StringBuilder();
                    //Result = _rechargeOrder_dao.InsertInvoice(orderSeq, InvoiceType, "", InvoiceTitle, ContactPerson, ContactPhone, Address, Zip, Mem, out datalog);
                    if ("0".Equals("InvoiceType"))
                    {
                        InvoiceTitle = "个人";
                    }
                    if (String.IsNullOrEmpty(InvoiceTitle))
                    {
                        InvoiceTitle = "个人";
                    }
                    if (String.IsNullOrEmpty(InvoiceContent))
                    {
                        InvoiceContent = "日用品";
                    }
                    Result = _rechargeOrder_dao.InsertInvoice(orderSeq, InvoiceType, InvoiceContent, InvoiceTitle, ContactPerson, ContactPhone, Address, Zip, Mem, "9", out datalog);  // "9" 代表需要开票
                    if (!Result)
                    {
                        CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "网银充值失败-发票生成失败", this.Context);
                        return;
                    }
                }
                //2013-04-14 add-end

                Result = _bankRechargeRecord_dao.Insert(_bankRecharge_record);
                if (!Result)
                {
                    CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "网银充值失败2", this.Context);
                    return;
                }
                /***************************************************************************/

                #endregion

                this._ORDERSEQ        = orderSeq;
                this._ORDERREQTRANSEQ = transactionID;
                this._ORDERDATE       = reqTime.ToString("yyyyMMddHHmmss");
                this._ORDERAMOUNT     = balance.ToString();
                this._PRODUCTAMOUNT   = balance.ToString();

                //MAC签名
                String mac = String.Format("MERCHANTID={0}&ORDERSEQ={1}&ORDERDATE={2}&ORDERAMOUNT={3}", this.MERCHANTID, this._ORDERSEQ, this._ORDERDATE, this._ORDERAMOUNT);
                mac = BesttoneAccountHelper.MACSign(mac);

                this._MAC = mac;
            }
            catch (Exception ex)
            {
                CommonBizRules.ErrorHappenedRedircet(result, ErrMsg, "网银充值失败", this.Context);
                return;
            }
        }
    }