Пример #1
0
        /// <summary>
        /// 分销系统订单申请退款
        /// </summary>
        /// <param name="alipayId"></param>
        /// <param name="sceneryDomain"></param>
        /// <returns>0 成功 1系统已经存在信息 2退款异常</returns>
        public string SceneryOrderRefund(int alipayId, UserDomain CurrentUser)
        {
            LogHelper.AlipayLog(string.Format("退款开始-》操作人:{0},退款ID:{1}", CurrentUser.Name, alipayId));

            var sceneryOrderRefund = new SceneryOrderRefundDao();

            var models = sceneryOrderRefund.GetAllDomain(QueryCondition.Instance.AddEqual("Id", alipayId.ToString()).AddEqual("ApprovalStatus", "0").AddEqual("RowState", "1"));

            var alipayDetailDao = new AlipayDetailDao();

            if (models.Count == 1)
            {
                //订单实体
                var sceneryOrder = models[0];
                //在交易表中查询是否已经退款过
                if (alipayDetailDao.IsExistsSceneryOrderRefund(sceneryOrder.BatchNumber, sceneryOrder.SerialId, -sceneryOrder.Total) == false)
                {
                    //查询交易表中支付时的记录
                    var list = alipayDetailDao.GetAllDomain(QueryCondition.Instance.AddEqual("SceneryName", sceneryOrder.SceneryName)
                                                            .AddEqual("BatchNumber", sceneryOrder.BatchNumber).AddEqualLarger("Total_amount", "0").AddEqual("SerialId", ""));
                    if (list.Count == 1)
                    {
                        //退款实体
                        var model = list[0];
                        // var tempTotal = -sceneryOrder.Total;


                        ////////////////////////////////////////////请求参数////////////////////////////////////////////

                        //支付宝交易号
                        string trade_no = model.Trade_no;
                        //退款金额
                        string refund_amount = sceneryOrder.Total.ToString();
                        //退款金额不能大于订单金额

                        //商户网站订单系统中唯一订单号,必填
                        StringBuilder request = new StringBuilder();
                        request.Append("{\"out_request_no\":\"" + sceneryOrder.SerialId + "\","); //本次退款请求流水号,部分退款时必传
                        request.Append("\"trade_no\":\"" + trade_no + "\",");                     //支付时返回的支付宝交易号,与out_trade_no必填一个
                        request.Append("\"refund_amount\":\"" + refund_amount + "\"}");

                        var query = QueryCondition.Instance.AddEqual("APP_ID", model.AppId).AddEqual("RowState", "1");


                        var alipay = new AlipayDao().GetAllDomain(query);
                        if (alipay.Count != 1)
                        {
                            return("没有找到支付宝配置信息");
                        }

                        LogHelper.AlipayLog(string.Format("退款JSON:{0},APP_ID:{1}", request.ToString(), alipay[0].APP_ID));
                        AlipayTradeRefundResponse refundResponse = DoRefund(request.ToString(), alipay[0]);

                        //请在这里加上商户的业务逻辑程序代码
                        //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
                        string result = refundResponse.Body;

                        if (refundResponse.Code == "10000")//退款成功
                        {
                            Alipay_trade_refund_response alipayRefund = JsonConvert.DeserializeObject <Alipay_trade_refund_response>(refundResponse.Body);
                            model.Code           = alipayRefund.alipay_trade_refund_response.code;
                            model.Trade_no       = alipayRefund.alipay_trade_refund_response.trade_no;
                            model.Out_trade_no   = alipayRefund.alipay_trade_refund_response.out_trade_no;
                            model.Total_amount   = alipayRefund.alipay_trade_refund_response.refund_fee;
                            model.Send_pay_date  = alipayRefund.alipay_trade_refund_response.gmt_refund_pay;
                            model.Msg            = alipayRefund.alipay_trade_refund_response.msg;
                            model.SerialId       = sceneryOrder.SerialId;
                            model.Creater        = CurrentUser.Name;
                            model.Updater        = CurrentUser.Name;
                            model.CreateTime     = DateTime.Now;
                            model.UpdateTime     = DateTime.Now;
                            model.Total_amount   = -sceneryOrder.Total;
                            model.AlipayPlayDate = sceneryOrder.PlayDate;
                            if (alipayDetailDao.Add(model))
                            {
                                //更新订单申请表状态
                                sceneryOrderRefund.ComfirmRefund(sceneryOrder.Id, CurrentUser);
                            }

                            return("");
                        }
                        else
                        {
                            LogHelper.AlipayLog(string.Format("退款失败:{0},body:{1}", refundResponse.Code, result));
                            return("支付宝退款失败,网关返回码:" + refundResponse.Code + " ,业务返回码:" + refundResponse.SubCode + ",业务返回码描述:" + refundResponse.SubMsg);
                        }
                    }
                    return("交易信息中没有找到原始交易记录");
                }
                return("交易表中已经存在退款信息");
            }



            return("退款订单信息不存在");
        }
Пример #2
0
        /// <summary>
        /// 交易信息全部退款
        /// </summary>
        /// <param name="alipayId"></param>
        /// <param name="sceneryDomain"></param>
        /// <returns>0 成功 1系统已经存在信息 2退款异常</returns>
        public int Refund(int alipayId, UserDomain CurrentUser)
        {
            LogHelper.AlipayLog(string.Format("退款开始-》操作人:{0},退款ID:{1}", CurrentUser.Name, alipayId));
            var alipayDetailDao = new AlipayDetailDao();
            var list            = alipayDetailDao.GetAllDomain(QueryCondition.Instance.AddEqual("Id", alipayId.ToString()));

            if (list.Count == 1)
            {
                var model     = list[0];
                var tempTotal = -model.Total_amount;

                if (!alipayDetailDao.IsExistsOut_trade_no(model.Out_trade_no, tempTotal, model.AppId))
                {
                    ////////////////////////////////////////////请求参数////////////////////////////////////////////

                    //支付宝交易号
                    string trade_no = model.Trade_no;
                    //支付宝交易号与商户网站订单号不能同时为空
                    string out_request_no = model.Out_trade_no;
                    //退款金额
                    string refund_amount = model.Total_amount.ToString();
                    //退款金额不能大于订单金额

                    //商户网站订单系统中唯一订单号,必填
                    StringBuilder request = new StringBuilder();
                    request.Append("{\"out_request_no\":\"" + out_request_no + "\",");
                    request.Append("\"trade_no\":\"" + trade_no + "\",");
                    request.Append("\"refund_amount\":\"" + refund_amount + "\"}");

                    var query = QueryCondition.Instance.AddEqual("APP_ID", model.AppId).AddEqual("RowState", "1");

                    var alipay = new AlipayDao().GetAllDomain(query);
                    if (alipay.Count != 1)
                    {
                        return(2);
                    }
                    LogHelper.AlipayLog(string.Format("退款JSON:{0},APP_ID:{1}", request.ToString(), alipay[0].APP_ID));
                    AlipayTradeRefundResponse refundResponse = DoRefund(request.ToString(), alipay[0]);

                    //请在这里加上商户的业务逻辑程序代码
                    //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
                    string result = refundResponse.Body;

                    if (refundResponse.Code == "10000")//退款成功
                    {
                        Alipay_trade_refund_response alipayRefund = JsonConvert.DeserializeObject <Alipay_trade_refund_response>(refundResponse.Body);
                        model.Code          = alipayRefund.alipay_trade_refund_response.code;
                        model.Trade_no      = alipayRefund.alipay_trade_refund_response.trade_no;
                        model.Out_trade_no  = alipayRefund.alipay_trade_refund_response.out_trade_no;
                        model.Total_amount  = alipayRefund.alipay_trade_refund_response.refund_fee;
                        model.Send_pay_date = alipayRefund.alipay_trade_refund_response.gmt_refund_pay;
                        model.Msg           = alipayRefund.alipay_trade_refund_response.msg;

                        model.Creater    = CurrentUser.Name;
                        model.Updater    = CurrentUser.Name;
                        model.CreateTime = DateTime.Now;
                        model.UpdateTime = DateTime.Now;

                        model.Total_amount = -model.Total_amount;
                        //新增一个负数的支付单信息。
                        alipayDetailDao.Add(model);
                        //新增一个退款申请的信息。
                        //判断退款信息是否存在。
                    }
                    else
                    {
                        LogHelper.AlipayLog(string.Format("退款失败:{0},body:{1}", refundResponse.Code, result));
                        return(2);
                    }
                }
                else
                {
                    return(1);
                }
            }
            return(0);
        }