public ActionResult CreateOrUpdateReturnRequest(RefundOrderModel model)
 {
     if (!_permissionService.Authorize(StandardPermissionProvider.ManageReturnRequests))
     {
         return(AccessDeniedView());
     }
     if (ModelState.IsValid)
     {
         if (model.RefundOrderId > 0)
         {
             var refundOrder = _refundOrderService.GetRefundOrderById(model.RefundOrderId);
             refundOrder.ReasonForRefund = model.ReasonForRefund;
             refundOrder.RefundAmount    = model.RefundAmount;
             refundOrder.OperatorId      = _workContext.CurrentCustomer.Id;
             refundOrder.OperatorEmail   = _workContext.CurrentCustomer.Email;
             refundOrder.OperateTime     = DateTime.Now;
             _refundOrderService.UpdateRefundOrder(refundOrder);
             string[] orderItemIds     = model.ChoseOrderItemIds.TrimEnd(',').Split(',');
             var      refundOrderItems = _refundOrderItemService.GetAllRefundOrderItems(model.OrderId);
             for (int i = 0; i < refundOrderItems.Count; i++)
             {
                 _refundOrderItemService.DeleteRefundOrderItem(refundOrderItems[i]);
             }
             foreach (var item in orderItemIds)
             {
                 RefundOrderItem refundorderItem = new RefundOrderItem();
                 refundorderItem.OrderId     = model.OrderId;
                 refundorderItem.OrderItemId = Convert.ToInt32(item);
                 _refundOrderItemService.InsertRefundOrderItem(refundorderItem);
                 SuccessNotification("修改退货请求成功");
             }
         }
         else
         {
             RefundOrder refundOrder = new RefundOrder();
             refundOrder.OrderId         = model.OrderId;
             refundOrder.ReasonForRefund = model.ReasonForRefund;
             refundOrder.RefundAmount    = model.RefundAmount;
             refundOrder.OperatorId      = _workContext.CurrentCustomer.Id;
             refundOrder.OperatorEmail   = _workContext.CurrentCustomer.Email;
             refundOrder.OperateTime     = DateTime.Now;
             _refundOrderService.InsertRefundOrder(refundOrder);
             string[] orderItemIds = model.ChoseOrderItemIds.TrimEnd(',').Split(',');
             foreach (var item in orderItemIds)
             {
                 RefundOrderItem refundorderItem = new RefundOrderItem();
                 refundorderItem.OrderId     = model.OrderId;
                 refundorderItem.OrderItemId = Convert.ToInt32(item);
                 _refundOrderItemService.InsertRefundOrderItem(refundorderItem);
             }
             var order = _orderService.GetOrderById(model.OrderId);
             order.OrderStatus = OrderStatus.Complete;
             _orderService.UpdateOrder(order);
             SuccessNotification("添加退货请求成功");
         }
         return(RedirectToAction("List"));
     }
     else
     {
         var orderItems = _orderService.GetAllOrderItems(model.OrderId, null, null, null, null, null, null);
         if (orderItems == null)
         {
             return(new HttpUnauthorizedResult());
         }
         foreach (var item in orderItems)
         {
             SelectListItem selectItem = new SelectListItem();
             selectItem.Selected = true;
             selectItem.Value    = item.Id.ToString();
             selectItem.Text     = item.Product.Name;
             model.SelectList.Add(selectItem);
         }
         model.RefundAmount = 0;
     }
     return(View(model));
 }
Example #2
0
        /// <summary>
        /// 退款支付
        /// </summary>
        /// <param name="refundPaymentRequest">请求</param>
        /// <returns>Result</returns>
        public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
        {
            var result = new RefundPaymentResult();
            //获取供运商ID
            ICollection <OrderItem> list         = refundPaymentRequest.Order.OrderItems;
            ConfigurationModel      wenxinConfig = new ConfigurationModel();
            var modelList = refundPaymentRequest.Order.OrderItems as List <OrderItem>;
            // var totalFee = postProcessPaymentRequest.Order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture);
            var vendorId = modelList[0].Product.VendorId;
            var wenxin   = _vendorService.GetVendorById(vendorId);

            wenxinConfig.APPID     = wenxin.APPID.Trim();
            wenxinConfig.APPSECRET = wenxin.APPSECRET.Trim();
            wenxinConfig.KEY       = wenxin.KEY.Trim();
            wenxinConfig.MCHID     = wenxin.MCHID.Trim();
            wenxinConfig.IP        = refundPaymentRequest.Order.CustomerIp;
            //wenxinConfig.SSLCERT_PATH = _webHelper.GetStoreLocation(false) + wenxin.SSLCERT_PATH.Trim().Substring(1, wenxin.SSLCERT_PATH.Trim().Length - 1);
            wenxinConfig.SSLCERT_PATH     = wenxin.SSLCERT_PATH.Trim();
            wenxinConfig.SSLCERT_PASSWORD = wenxin.MCHID.Trim();
            OrderDetails orderdetails = new OrderDetails
            {
                Attach    = wenxin.Name,
                OrderId   = refundPaymentRequest.Order.Id,
                Body      = modelList[0].Product.Name,
                Detail    = modelList[0].Product.ShortDescription,
                ProductId = modelList[0].Product.Id.ToString(),
                Total_fee = (Convert.ToDouble(refundPaymentRequest.Order.OrderTotal) * 100).ToString()
            };

            wenxinConfig.orderDetails = orderdetails;
            //退款
            string amountToRefund = Convert.ToInt32(refundPaymentRequest.AmountToRefund * 100).ToString();
            Refund refund         = new Refund(wenxinConfig);

            //调用订单退款接口,如果内部出现异常则在页面上显示异常原因
            if (refundPaymentRequest.Order.WxTransactionId == null)
            {
                result.AddError("没有查询到微信支付订单");
                return(result);
            }
            try
            {
                string    returnMess  = refund.Run(refundPaymentRequest.Order.WxTransactionId, refundPaymentRequest.Order.Id.ToString(), orderdetails.Total_fee, amountToRefund);
                string[]  cpTypeArray = returnMess.Replace("<br>", ",").Split(',');
                WxPayData res         = new WxPayData();
                foreach (var item in cpTypeArray)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        res.SetValue(item.Split('=')[0], item.Split('=')[1]);
                    }
                }
                if (res.GetValue("return_code").ToString() != "SUCCESS" || res.GetValue("result_code").ToString() != "SUCCESS")
                {
                    //退款提示
                    result.AddError(res.GetValue("err_code_des").ToString());
                    return(result);
                }
                else
                {
                    //添加退款记录表
                    RefundOrderItem item = new RefundOrderItem();
                    item.OrderId         = refundPaymentRequest.Order.Id;
                    item.CustomNumber    = refundPaymentRequest.Order.Customer.Id;
                    item.VendorId        = vendorId;
                    item.WxTransactionId = res.GetValue("transaction_id").ToString();
                    item.WxRefunId       = res.GetValue("refund_id").ToString();
                    item.WxOutRefunNo    = res.GetValue("out_refund_no").ToString();
                    item.OrderTotal      = refundPaymentRequest.Order.OrderTotal;
                    item.RefundedAmount  = refundPaymentRequest.AmountToRefund;
                    item.CreatedOnUtc    = System.DateTime.Now;
                    item.Deleted         = false;
                    _refundOrderItemService.InsertRefundOrderItem(item);
                }
            }
            catch (WxPayException ex)
            {
                result.AddError(ex.ToString());
            }
            catch (Exception ex)
            {
                result.AddError(ex.ToString());
            }
            return(result);
        }