示例#1
0
文件: WxCard.cs 项目: mahui-cn/FruitU
    public static JsonData ConsumeCodeOnOrderStateChanged(ProductOrder po, ProductOrder.OrderStateEventArgs e)
    {
        if (po == null || e == null)
        {
            throw new ArgumentNullException("sender或事件参数对象不能为null");
        }
        JsonData jRet = new JsonData();

        try
        {
            //只有支付成功才核销微信优惠券
            if ((po.PaymentTerm == PaymentTerm.WECHAT && po.TradeState == TradeState.SUCCESS) ||
                (po.PaymentTerm == PaymentTerm.ALIPAY && (po.TradeState == TradeState.AP_TRADE_SUCCESS || po.TradeState == TradeState.AP_TRADE_FINISHED)) ||
                (po.PaymentTerm == PaymentTerm.CASH && po.TradeState == TradeState.CASHPAID))
            {
                if (po.WxCard != null && !string.IsNullOrEmpty(po.WxCard.Code))
                {
                    if (ConsumeCode(po.WxCard.Code))
                    {
                        jRet["IsConsumed"] = true;
                    }
                    else
                    {
                        jRet["IsConsumed"] = false;
                    }
                }
                else
                {
                    jRet["IsConsumed"] = false;
                }
            }
            else
            {
                jRet["IsConsumed"] = false;
            }
        }
        catch (Exception ex)
        {
            Log.Error("ConsumeCodeOnOrderStateChanged", ex.Message);
            throw ex;
        }

        return(jRet);
    }
示例#2
0
    /// <summary>
    /// 检测订单中所有相关团购活动是否成功,成功则通知管理员和用户,用于订单支付成功后的回调
    /// </summary>
    /// <param name="po"></param>
    /// <param name="ose"></param>
    /// <returns></returns>
    public static JsonData GroupPurchaseEventSuccessHandler(ProductOrder po, ProductOrder.OrderStateEventArgs ose)
    {
        if (po == null || ose == null)
        {
            throw new ArgumentNullException("sender或事件参数对象不能为null");
        }
        JsonData jRet = new JsonData();

        try
        {
            //如果此订单支付成功
            if ((po.PaymentTerm == PaymentTerm.WECHAT && po.TradeState == TradeState.SUCCESS) ||
                (po.PaymentTerm == PaymentTerm.ALIPAY && (po.TradeState == TradeState.AP_TRADE_SUCCESS || po.TradeState == TradeState.AP_TRADE_FINISHED)) ||
                (po.PaymentTerm == PaymentTerm.CASH && po.TradeState == TradeState.CASHPAID))
            {
                po.OrderDetailList.ForEach(od =>
                {
                    if (od.GroupPurchaseEvent != null && !od.GroupPurchaseEvent.IsNotify)
                    {
                        //如果拼团成功,则触发拼团成功事件,通知管理员和所有拼团用户
                        GroupEventStatus eventStatus = CheckGroupPurchaseEventStatus(od.GroupPurchaseEvent);
                        if (eventStatus == GroupEventStatus.EVENT_SUCCESS)
                        {
                            od.GroupPurchaseEvent.GroupPurchaseEventSuccess += WxTmplMsg.GroupPurchaseEventNotify;
                            od.GroupPurchaseEvent.OnGroupPurchaseEventSuccess();

                            //设置此团购活动通知标记为true
                            od.GroupPurchaseEvent.IsNotify = true;
                            UpdateGroupPurchaseEventNotify(od.GroupPurchaseEvent);
                        }
                    }
                });
            }
        }
        catch (Exception ex)
        {
            Log.Error("CheckGroupPurchaseEventStatus", ex.Message);
            throw ex;
        }

        return(jRet);
    }
示例#3
0
    /// <summary>
    /// 发送微信模板消息
    /// 模板消息接收者,1下单通知管理员;2微信支付成功通知管理员;3配送通知用户;4签收不通知;5撤单通知管理员和用户
    /// </summary>
    /// <param name="po"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    public static JsonData SendMsgOnOrderStateChanged(ProductOrder po, ProductOrder.OrderStateEventArgs e)
    {
        if (po == null || e == null)
        {
            throw new ArgumentNullException("sender或事件参数对象不能为null");
        }
        JsonData jRet = new JsonData();

        try
        {
            List <string> listReceiver;

            JsonData jTmplMsg = new JsonData(), jTmplMsgData = new JsonData(), jTmplMsgDataValue;

            //根据不同的订单状态事件,发送不同的微信模板消息
            switch (e.OrderState)
            {
            case OrderState.Submitted:
                //订单下单事件,发送模板消息给管理员
                if (!SendTmplMsgSwitch[OrderState.Submitted])
                {
                    return(false);
                }

                string paymentTerm = string.Empty;
                switch (po.PaymentTerm)
                {
                case PaymentTerm.WECHAT:
                    paymentTerm = "微信支付";
                    break;

                case PaymentTerm.ALIPAY:
                    paymentTerm = "支付宝";
                    break;

                case PaymentTerm.CASH:
                    paymentTerm = "货到付款";
                    break;
                }

                //构造模板消息
                jTmplMsg["touser"]      = string.Empty;
                jTmplMsg["template_id"] = TMPL_ORDER_SUCCESS;
                jTmplMsg["url"]         = string.Empty;
                jTmplMsg["topcolor"]    = MSG_HEAD_COLOR;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("您的订单{0}提交成功", po.OrderID.Substring(18));
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["first"]      = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = po.OrderDate.ToString();
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword1"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("{0}元【{1}】", po.OrderPrice.ToString("C"), paymentTerm);
                jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                jTmplMsgData["keyword2"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = po.OrderDetails;
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword3"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("{0}({1}) {2}", po.DeliverName, po.DeliverPhone, po.DeliverAddress);
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword4"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("订单备注:{0}", po.OrderMemo);
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["remark"]     = jTmplMsgDataValue;

                jTmplMsg["data"] = jTmplMsgData;

                //发送模板消息,通知管理员
                listReceiver    = new List <string>(Config.WxTmplMsgReceiver.ToArray());
                jTmplMsg["url"] = @"http://mahui.me/admin/ManageOrder.aspx";
                jRet            = SendTmplMsg(listReceiver, jTmplMsg);

                //发送模板消息,通知用户
                listReceiver    = new List <string>(new string[] { po.Purchaser.OpenID });
                jTmplMsg["url"] = @"http://mahui.me/MyOrders.aspx";
                jRet.Add(SendTmplMsg(listReceiver, jTmplMsg));

                break;

            case OrderState.Paid:
                //订单支付事件,发送模板消息给管理员
                if (!SendTmplMsgSwitch[OrderState.Paid])
                {
                    return(false);
                }

                //只有支付成功才发送模板消息
                if ((po.PaymentTerm == PaymentTerm.WECHAT && po.TradeState == TradeState.SUCCESS) ||
                    (po.PaymentTerm == PaymentTerm.ALIPAY && (po.TradeState == TradeState.AP_TRADE_SUCCESS || po.TradeState == TradeState.AP_TRADE_FINISHED)) ||
                    (po.PaymentTerm == PaymentTerm.CASH && po.TradeState == TradeState.CASHPAID))
                {
                    //构造模板消息
                    jTmplMsg["touser"]      = string.Empty;
                    jTmplMsg["template_id"] = TMPL_PAY_SUCCESS;
                    jTmplMsg["url"]         = string.Empty;
                    jTmplMsg["topcolor"]    = MSG_HEAD_COLOR;

                    switch (po.PaymentTerm)
                    {
                    case PaymentTerm.WECHAT:
                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = "支付方式:微信支付";
                        jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                        jTmplMsgData["first"]      = jTmplMsgDataValue;

                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = string.Format("微信支付交易号:{0}", po.TransactionID);
                        jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                        jTmplMsgData["remark"]     = jTmplMsgDataValue;

                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = (po.TransactionTime.HasValue ? po.TransactionTime.ToString() : string.Empty);
                        jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                        jTmplMsgData["keyword4"]   = jTmplMsgDataValue;

                        break;

                    case PaymentTerm.ALIPAY:
                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = "支付方式:支付宝";
                        jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                        jTmplMsgData["first"]      = jTmplMsgDataValue;

                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = string.Format("支付宝交易号:{0}", po.AP_TradeNo);
                        jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                        jTmplMsgData["remark"]     = jTmplMsgDataValue;

                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = (po.AP_GMT_Payment.HasValue ? po.AP_GMT_Payment.ToString() : string.Empty);
                        jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                        jTmplMsgData["keyword4"]   = jTmplMsgDataValue;

                        break;

                    case PaymentTerm.CASH:
                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = "支付方式:货到付款";
                        jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                        jTmplMsgData["first"]      = jTmplMsgDataValue;

                        jTmplMsgDataValue          = new JsonData();
                        jTmplMsgDataValue["value"] = (po.PayCashDate.HasValue ? po.PayCashDate.ToString() : string.Empty);
                        jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                        jTmplMsgData["keyword4"]   = jTmplMsgDataValue;

                        break;
                    }
                    jTmplMsgDataValue          = new JsonData();
                    jTmplMsgDataValue["value"] = po.OrderID.Substring(18);
                    jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                    jTmplMsgData["keyword1"]   = jTmplMsgDataValue;

                    jTmplMsgDataValue          = new JsonData();
                    jTmplMsgDataValue["value"] = po.OrderDetails;
                    jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                    jTmplMsgData["keyword2"]   = jTmplMsgDataValue;

                    jTmplMsgDataValue          = new JsonData();
                    jTmplMsgDataValue["value"] = string.Format("{0}元", po.OrderPrice.ToString("C"));
                    jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                    jTmplMsgData["keyword3"]   = jTmplMsgDataValue;

                    jTmplMsg["data"] = jTmplMsgData;

                    //发送模板消息,通知管理员
                    listReceiver    = new List <string>(Config.WxTmplMsgReceiver.ToArray());
                    jTmplMsg["url"] = @"http://mahui.me/admin/ManageOrder.aspx";
                    jRet            = SendTmplMsg(listReceiver, jTmplMsg);

                    //发送模板消息,通知用户
                    listReceiver    = new List <string>(new string[] { po.Purchaser.OpenID });
                    jTmplMsg["url"] = @"http://mahui.me/MyOrders.aspx";
                    jRet.Add(SendTmplMsg(listReceiver, jTmplMsg));
                }

                break;

            case OrderState.Delivered:
                //订单发货事件,发送模板消息给用户
                if (!SendTmplMsgSwitch[OrderState.Delivered])
                {
                    return(false);
                }

                string tradeState = string.Empty;
                switch (po.PaymentTerm)
                {
                case PaymentTerm.WECHAT:
                    if (po.TradeState == TradeState.SUCCESS)
                    {
                        tradeState = "微信支付成功";
                    }
                    else
                    {
                        tradeState = "未支付";
                    }
                    break;

                case PaymentTerm.ALIPAY:
                    if (po.TradeState == TradeState.AP_TRADE_SUCCESS || po.TradeState == TradeState.AP_TRADE_FINISHED)
                    {
                        tradeState = "支付宝支付成功";
                    }
                    else
                    {
                        tradeState = "未支付";
                    }
                    break;

                case PaymentTerm.CASH:
                    tradeState = "货到付款";
                    break;
                }

                //构造模板消息
                jTmplMsg["touser"]      = string.Empty;
                jTmplMsg["template_id"] = TMPL_DELIVER;
                jTmplMsg["url"]         = @"http://mahui.me/MyOrders.aspx";
                jTmplMsg["topcolor"]    = MSG_HEAD_COLOR;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("您的订单{0}已发货,请注意查收。", po.OrderID.Substring(18));
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["first"]      = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = po.OrderDetails;
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword1"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = po.OrderDetailCount;
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword2"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("{0}元【{1}】", po.OrderPrice.ToString("C"), tradeState);
                jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                jTmplMsgData["keyword3"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = "小U";
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword4"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = "13585702012";
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keyword5"]   = jTmplMsgDataValue;

                jTmplMsg["data"] = jTmplMsgData;

                //发货消息只通知用户本人
                listReceiver = new List <string>(new string[] { po.Purchaser.OpenID });
                jRet         = SendTmplMsg(listReceiver, jTmplMsg);

                break;

            case OrderState.Accepted:
                //订单签收事件,发送模板消息给用户
                if (!SendTmplMsgSwitch[OrderState.Accepted])
                {
                    return(false);
                }

                break;

            case OrderState.Cancelled:
                //订单撤单事件,发送模板消息
                if (!SendTmplMsgSwitch[OrderState.Cancelled])
                {
                    return(false);
                }

                jTmplMsg["touser"]      = string.Empty;
                jTmplMsg["template_id"] = TMPL_CANCEL;
                jTmplMsg["url"]         = string.Empty;
                jTmplMsg["topcolor"]    = MSG_HEAD_COLOR;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("您的订单{0}已撤销", po.OrderID.Substring(18));
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["first"]      = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = po.CancelDate.HasValue ? po.CancelDate.ToString() : DateTime.Now.ToString();
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["keynote1"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = string.Format("{0}元", po.OrderPrice.ToString("C"));
                jTmplMsgDataValue["color"] = MSG_HEAD_COLOR;
                jTmplMsgData["keynote2"]   = jTmplMsgDataValue;

                jTmplMsgDataValue          = new JsonData();
                jTmplMsgDataValue["value"] = "查看我的订单";
                jTmplMsgDataValue["color"] = MSG_BODY_COLOR;
                jTmplMsgData["remark"]     = jTmplMsgDataValue;

                jTmplMsg["data"] = jTmplMsgData;

                //发送模板消息,通知管理员
                listReceiver    = new List <string>(Config.WxTmplMsgReceiver.ToArray());
                jTmplMsg["url"] = @"http://mahui.me/admin/ManageOrder.aspx";
                jRet            = SendTmplMsg(listReceiver, jTmplMsg);

                //发送模板消息,通知用户
                listReceiver    = new List <string>(new string[] { po.Purchaser.OpenID });
                jTmplMsg["url"] = @"http://mahui.me/MyOrders.aspx";
                jRet.Add(SendTmplMsg(listReceiver, jTmplMsg));

                break;

            default:
                break;
            }
        }
        catch (Exception ex)
        {
            Log.Error("SendMsgOnOrderStateChanged", ex.Message);
            throw ex;
        }

        return(jRet);
    }