Exemplo n.º 1
0
        ///// <summary>
        ///// 交易信息全部退款
        ///// </summary>
        ///// <param name="alipayId"></param>
        ///// <param name="sceneryDomain"></param>
        ///// <returns>0 成功 1系统已经存在信息 2退款异常</returns>
        //public int Refund(int alipayId, UserDomain CurrentUser)
        //{
        //    LogHelper.WeChatLog(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.WeChatLog(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.WeChatLog(string.Format("退款失败:{0},body:{1}", refundResponse.Code, result));
        //                return 2;
        //            }
        //        }
        //        else
        //        {
        //            return 1;
        //        }
        //    }
        //    return 0;
        //}

        //private AlipayTradeRefundResponse DoRefund(string biz_content, AlipayDomain alipay)
        //{
        //    AlipayTradeRefundRequest payRequst = new AlipayTradeRefundRequest();
        //    payRequst.BizContent = biz_content;
        //    payRequst.SetNotifyUrl("");
        //    Dictionary<string, string> paramsDict = (Dictionary<string, string>)payRequst.GetParameters();
        //    IAopClient client = new DefaultAopClient(serverUrl, alipay.APP_ID, alipay.APP_PRIVATE_KEY, "", version,
        //        sign_type, alipay.ALIPAY_PUBLIC_KEY, charset);

        //    AlipayTradeRefundResponse payResponse = client.Execute(payRequst);

        //    return payResponse;

        //}


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

            var sceneryOrderRefund = new SceneryOrderRefundDao();

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

            var weChatDetailDao = new WeChatDetailDao();

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


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

                        //微信交易号
                        string Transaction_id = model.Transaction_id;
                        //退款金额
                        string refund_amount = Convert.ToInt32(sceneryOrder.Total * 100).ToString();

                        var query  = QueryCondition.Instance.AddEqual("APPID", model.AppId).AddEqual("RowState", "1");
                        var wechat = new WxPayConfigDao().GetAllDomain(query);
                        if (wechat.Count != 1)
                        {
                            return("没有找到微信配置信息");
                        }


                        try
                        {
                            var result = VisualSmart.BizService.Implements.WeChat.Api.Refund.Run(
                                model.Transaction_id, model.Out_trade_no, model.Total_fee.ToString(), refund_amount, wechat[0]);

                            if (result.GetValue("return_code").ToString() == "SUCCESS" &&
                                result.GetValue("result_code").ToString() == "SUCCESS") //退款申请成功
                            {
                                var out_refund_no = result.GetValue("out_refund_no");   //商户退款单号
                                var refund_id     = result.GetValue("refund_id");       //微信退款单号
                                var refund_fee    = result.GetValue("refund_fee");      //金额 分

                                LogHelper.WeChatLog(string.Format("退款成功-》微信退款单号:{0}", refund_id));

                                model.Out_refund_no  = out_refund_no.ToString();
                                model.Refund_id      = refund_id.ToString();
                                model.Total_fee      = -Convert.ToInt32(refund_fee);
                                model.WeChatPlayDate = sceneryOrder.PlayDate;
                                model.SerialId       = sceneryOrder.SerialId;
                                model.Creater        = CurrentUser.Name;
                                model.Updater        = CurrentUser.Name;
                                model.CreateTime     = DateTime.Now;
                                model.UpdateTime     = DateTime.Now;
                                if (weChatDetailDao.Add(model))
                                {
                                    //更新订单申请表状态
                                    sceneryOrderRefund.ComfirmRefund(sceneryOrder.Id, CurrentUser);
                                }
                                return("");
                            }
                            else
                            {
                                LogHelper.WriteLog(string.Format("退款失败:{0},业务代码:{1}", result.GetValue("return_msg"), result.GetValue("err_code_des")));
                                return("微信退款失败,网关返回码:" + result.GetValue("return_msg") + " ,业务返回码:" + result.GetValue("err_code") + ",业务返回码描述:" + result.GetValue("err_code_des"));
                            }
                        }
                        catch (WxPayException ex)
                        {
                            return(ex.Message);
                        }
                        catch (Exception ex)
                        {
                            return(ex.Message);
                        }
                    }
                    return("交易信息中没有找到原始交易记录");
                }
                return("交易表中已经存在退款信息");
            }
            return("退款订单信息不存在");
        }
Exemplo n.º 2
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("退款订单信息不存在");
        }