Exemplo n.º 1
0
        /// <summary>
        /// Order Placed Event
        /// </summary>
        /// <param name="eventMessage"></param>
        public void HandleEvent(OrderPlacedEvent eventMessage)
        {
            var storeScope         = _storeContext.ActiveStoreScopeConfiguration;
            var ShipRocketSettings = _settingService.LoadSetting <ShipRocketSetting>(storeScope);

            if (ShipRocketSettings.Enable)
            {
                var order = eventMessage.Order;

                if (order.PaymentMethodSystemName == "Payments.CashOnDelivery")
                {
                    var oldorder = _ShipRocketService.GetShiprocketOrderByOrderId(order.Id);

                    if (oldorder == null)
                    {
                        NopShiprocketOrder SO = new NopShiprocketOrder()
                        {
                            OrderId           = order.Id,
                            ShiprocketStatues = false
                        };
                        _ShipRocketService.InsertShiprocketOrder(SO);
                    }
                }
            }
        }
        /// <summary>
        /// ScheduleTask Execute Method
        /// </summary>
        public void Execute()
        {
            var storeScope         = _storeContext.ActiveStoreScopeConfiguration;
            var ShipRocketSettings = _settingService.LoadSetting <ShipRocketSetting>(storeScope);

            if (ShipRocketSettings.Enable)
            {
                var ShiprocketUserName = _ShipRocketSeting.UserEmail;
                var ShipRocketBaseUrl  = _ShipRocketSeting.BaseURL;
                var ShipRocketPassword = _ShipRocketSeting.Password;

                if (ShiprocketUserName != null && ShipRocketBaseUrl != null && ShipRocketPassword != null)
                {
                    try
                    {
                        var allOrder = _ShipRocketService.GetAllShiprocketOrder();

                        if (allOrder.Count() > 0)
                        {
                            ShipRocketApiConfiguration rocket = new ShipRocketApiConfiguration();

                            var token = rocket.GetTocket(ShipRocketBaseUrl, ShiprocketUserName, ShipRocketPassword);

                            if (token.Contains("token"))
                            {
                                ShipRocketTokenResponse Responses = JsonConvert.DeserializeObject <ShipRocketTokenResponse>(token);

                                if (Responses != null)
                                {
                                    if (!string.IsNullOrEmpty(Responses.token))
                                    {
                                        foreach (var a in allOrder)
                                        {
                                            var order = _OrderService.GetOrderById(a.OrderId);

                                            if (!order.Deleted)
                                            {
                                                if (order.PaymentMethodSystemName == "Payments.CashOnDelivery")
                                                {
                                                    ProccedShiprocket(order, Responses.token.Trim(), ShipRocketBaseUrl, a, true);
                                                }
                                                else
                                                {
                                                    ProccedShiprocket(order, Responses.token.Trim(), ShipRocketBaseUrl, a);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _LoggerService.InsertLog(LogLevel.Error, "There was a problem for getting token" + token);
                            }
                        }
                    }
                    catch (Exception Ex)
                    {
                        _LoggerService.InsertLog(LogLevel.Error, "Getting Exception in shiprocket scheduletask" + Ex.Message, Ex.InnerException.ToString());
                    }
                }
                else
                {
                    _LoggerService.InsertLog(LogLevel.Error, "Can not get Ship rocket credential please enter this credential in all setting", "ShipRocketSeting.UserName, ShipRocketSeting.UserName,ShipRocketSeting.Password");
                }

                List <int> ssid = new List <int>();
                ssid.Add(30);
                var pendingOrder = _OrderService.SearchOrders(ssIds: ssid);
                ShipRocketApiConfiguration rocket1 = new ShipRocketApiConfiguration();
                var token1 = rocket1.GetTocket(ShipRocketBaseUrl, ShiprocketUserName, ShipRocketPassword);
                if (token1.Contains("token"))
                {
                    ShipRocketTokenResponse Responses = JsonConvert.DeserializeObject <ShipRocketTokenResponse>(token1);

                    foreach (var p in pendingOrder)
                    {
                        var orderShiprocketMap = _ShipRocketService.GetShiprocketOrderByOrderId(p.Id);
                        if (orderShiprocketMap != null)
                        {
                            var orderStatus = rocket1.GetShiprocketOrderStatus(Responses.token.Trim(), ShipRocketBaseUrl, Convert.ToInt32(orderShiprocketMap.ShiprocketOrderId));

                            if (!string.IsNullOrEmpty(orderStatus))
                            {
                                if (orderStatus == "DELIVERED")
                                {
                                    var Shipments = _shipmentService.GetShipmentsByOrderId(p.Id);
                                    foreach (var shipment in Shipments)
                                    {
                                        try
                                        {
                                            if (!shipment.DeliveryDateUtc.HasValue)
                                            {
                                                _orderProcessingService.Deliver(Shipments.FirstOrDefault(), true);

                                                _customerActivityService.InsertActivity("EditOrder",
                                                                                        string.Format(_localizationService.GetResource("ActivityLog.EditOrder"), p.CustomOrderNumber), p);
                                            }
                                        }
                                        catch (Exception exc)
                                        {
                                            var customer = workContext.CurrentCustomer;
                                            _logger.Error(exc.Message, exc, customer);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }