Ejemplo n.º 1
0
        public void HandleEvent(ShipmentSentEvent eventMessage)
        {
            _log.Information("Shipment Sent: " + eventMessage.Shipment.TrackingNumber + " Order Id: " + eventMessage.Shipment.Order.Id);

            var weixinRecord =
                eventMessage.Shipment.Order.Customer.ExternalAuthenticationRecords.FirstOrDefault(
                    q => q.ProviderSystemName == "ExternalAuth.WeiXin");

            if (weixinRecord != null)
            {
                var openId = weixinRecord.ExternalIdentifier;

                var postUrl = @"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + GetAccessToken();

                var data = new
                {
                    touser      = openId,
                    template_id = _shippedTemplateId,
                    url         = Path.Combine(_webHelper.GetStoreLocation(), "orderdetails/shipment/" + eventMessage.Shipment.Id),
                    data        = new
                    {
                        first = new
                        {
                            value = "亲,宝贝已经启程向您飞来,好想快点来到你身边!",
                            color = "#173177"
                        },
                        keyword1 = new
                        {
                            value = eventMessage.Shipment.Order.Id.ToString(),
                            color = "#173177"
                        },
                        keyword2 = new
                        {
                            value = eventMessage.Shipment.CourierName,
                            color = "#173177"
                        },
                        keyword3 = new
                        {
                            value = eventMessage.Shipment.TrackingNumber,
                            color = "#173177"
                        },
                        remark = new
                        {
                            value = "点击查看完整的物流信息 。如有问题请致电 +61 0487 538 888 或直接在微信里留言,我们将在第一时间为您服务!",
                            color = "#173177"
                        },
                    }
                };

                var result = HttpUtil.Post(JsonMapper.ToJson(data), postUrl, 100, "application/json");
                _log.Information("Weixin template message result: " + result);
            }
        }
        public void HandleEvent(ShipmentSentEvent eventMessage)
        {
            if (!_smsSettings.Enabled)
            {
                return;
            }
            if (!_smsSettings.EnableShippingShipped)
            {
                return;
            }

            if (_smsSettings.IgnnoreUserIDs.Split(',').Contains(eventMessage.Shipment.Order.CustomerId.ToString()))
            {
                eventMessage.Shipment.Order.OrderNotes.Add(new OrderNote
                {
                    Note = "SMS alert not send. user:"******" in Ignore Lit",
                    DisplayToCustomer = false,
                    CreatedOnUtc      = DateTime.UtcNow
                });
                _orderService.UpdateOrder(eventMessage.Shipment.Order);
                return;
            }

            string template = "";

            if (eventMessage.Shipment.Order.CustomerLanguageId == _smsSettings.SecondLanguageID)
            {
                // second language
                template = _smsSettings.TemplateShippingShipped2;
            }
            else
            {
                if (eventMessage.Shipment.Order.CustomerLanguageId == _smsSettings.ThirdLanguageID)
                {
                    // Third language
                    template = _smsSettings.TemplateShippingShipped3;
                }
                else
                {
                    // default
                    template = _smsSettings.TemplateShippingShipped;
                }
            }
            if (string.IsNullOrEmpty(template))
            {
                template = _smsSettings.TemplateShippingShipped;
            }

            try
            {
                template = string.Format(template, eventMessage.Shipment.Order.Id, eventMessage.Shipment.Order.OrderTotal);
            }
            catch { }
            var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName("SMS.Notifications");

            if (pluginDescriptor == null)
            {
                return;
            }
            if (!_pluginFinder.AuthenticateStore(pluginDescriptor, _storeContext.CurrentStore.Id))
            {
                return;
            }

            var plugin = pluginDescriptor.Instance() as SMSNotificationsProvider;

            if (plugin == null)
            {
                return;
            }
            string phonenum = string.IsNullOrEmpty(eventMessage.Shipment.Order.Customer.BillingAddress.PhoneNumber) ?
                              eventMessage.Shipment.Order.Customer.ShippingAddress.PhoneNumber :
                              eventMessage.Shipment.Order.Customer.BillingAddress.PhoneNumber;

            phonenum = ParcePhoneNumbe(phonenum);
            string resText = "";

            if (plugin.SendSms(phonenum, template, out resText))
            {
                eventMessage.Shipment.Order.OrderNotes.Add(new OrderNote
                {
                    Note = "\"Order shipped\" SMS alert has been sent to phone:" + phonenum,
                    DisplayToCustomer = false,
                    CreatedOnUtc      = DateTime.UtcNow
                });
                _orderService.UpdateOrder(eventMessage.Shipment.Order);
            }
            else
            {
                eventMessage.Shipment.Order.OrderNotes.Add(new OrderNote
                {
                    Note = "\"Order shipped\" SMS alert can't send to phone:" + phonenum + ". Error " + resText,
                    DisplayToCustomer = false,
                    CreatedOnUtc      = DateTime.UtcNow
                });
                _orderService.UpdateOrder(eventMessage.Shipment.Order);
            }
        }