Ejemplo n.º 1
0
        /// <summary>
        ///打款
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string Transfers(HttpContext context)
        {
            int    successCount = 0;
            string orderIds     = context.Request["order_ids"];//订单

            orderIds = orderIds.TrimEnd(',');
            ZentCloud.BLLJIMP.Model.PayConfig payConfig = bllPay.GetPayConfig();
            #region 团购-要先给团员退款
            if (orderIds.Split(',').Count() == 1)
            {
                var order = bllMall.GetOrderInfo(orderIds.Split(',')[0]);
                if (order.OrderType == 2 && (string.IsNullOrEmpty(order.GroupBuyParentOrderId)))
                {
                    //团长订单不能先退款,先退完团员才能退团长
                    if (bllMall.GetCount <WXMallOrderInfo>(string.Format(" GroupBuyParentOrderId='{0}' And PaymentStatus=1 And (Ex1 IS NULL Or Ex1 ='') And TotalAmount>0", order.OrderID)) > 0)
                    {
                        resp.errcode = 1;
                        resp.errmsg  = "请先给团员订单退款,然后给团长订单退款";
                        return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
                    }
                }
            }
            #endregion

            foreach (string orderId in orderIds.Split(','))
            {
                var orderInfo = bllMall.GetOrderInfo(orderId);
                if (orderInfo.WebsiteOwner != bllMall.WebsiteOwner)
                {
                    continue;
                }

                if (orderInfo.TotalAmount > 0)
                {
                    string msg = "";
                    #region 微信退款
                    if (orderInfo.PaymentType == 2)
                    {
                        string weixinRefundId = "";
                        if (bllPay.WeixinRefund(orderInfo.OrderID, orderInfo.OrderID, orderInfo.TotalAmount, orderInfo.TotalAmount, payConfig.WXAppId, payConfig.WXMCH_ID, payConfig.WXPartnerKey, out msg, out weixinRefundId, bllMall.WebsiteOwner))
                        {
                            successCount++;
                            orderInfo.Ex1 = weixinRefundId;
                            orderInfo.Ex2 = msg;
                            bllMall.Update(orderInfo);
                        }
                        else
                        {
                            if ((!string.IsNullOrEmpty(msg)) && (msg.Contains("请使用可用余额退款")))//尝试使用余额退款
                            {
                                if (bllPay.WeixinRefundYuEr(orderInfo.OrderID, orderInfo.OrderID, orderInfo.TotalAmount, orderInfo.TotalAmount, payConfig.WXAppId, payConfig.WXMCH_ID, payConfig.WXPartnerKey, out msg, out weixinRefundId, bllMall.WebsiteOwner))
                                {
                                    successCount++;
                                    orderInfo.Ex1 = weixinRefundId;
                                    orderInfo.Ex2 = msg;
                                    bllMall.Update(orderInfo);
                                }
                                else
                                {
                                    resp.errcode = 1;
                                    resp.errmsg  = msg;
                                    return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
                                }
                            }
                            else
                            {
                                resp.errcode = 1;
                                resp.errmsg  = msg;
                                return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
                            }
                        }
                    }
                    #endregion

                    #region 支付宝退款
                    else if (orderInfo.PaymentType == 1)
                    {
                        string batchNo   = DateTime.Now.ToString("yyyyMMdd") + ((int)(bllMall.GetTimeStamp(DateTime.Now) / 1000)).ToString();
                        string notifyUrl = string.Format("http://{0}/Alipay/NotifyRefund.aspx", context.Request.Url.Host);
                        string remark    = string.Format("订单号{0}", orderInfo.OrderID);
                        //支付宝支付的退款
                        bool isSuccess = bllPay.AlipayRefund(orderInfo.PayTranNo, batchNo, orderInfo.TotalAmount, notifyUrl, out msg, remark);
                        if (!isSuccess)
                        {
                            resp.errcode = 1;
                            resp.errmsg  = msg;
                            return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
                        }
                    }
                    #endregion

                    else if (orderInfo.PaymentType == 3)
                    {
                        if (!bllPay.JDPayRefund(orderInfo.OrderID, orderInfo.OrderID, orderInfo.TotalAmount, "", "", out msg))
                        {
                            resp.errcode = 1;
                            resp.errmsg  = msg;
                            return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
                        }
                    }
                }
            }
            return(ZentCloud.Common.JSONHelper.ObjectToJson(resp));
        }