4.40 Custom Plugin Help

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
I'm having an issue with a custom plugin that I updated from 4.10.  I'm getting an access violation when this is called.  I've looked and I'm not seeing anything that sticks out to where I'm referencing a null object.

The plugin is triggered when an item has been paid.

Any insight is greatly appreciated.

Lance

"Object reference not set to an instance of an object.
Full message
System.NullReferenceException: Object reference not set to an instance of an object.
   at Nop.Plugin.CDE.TempReg.Services.EventConsumer.ProcessApprovedPurchase(Order order)
   at Nop.Plugin.CDE.TempReg.Services.EventConsumer.HandleEventAsync(OrderPaidEvent eventMessage)
   at Nop.Services.Events.EventPublisher.PublishAsync[TEvent](TEvent event) in C:\2019CODE\CDENOP4.40.4\Libraries\Nop.Services\Events\EventPublisher.cs:line 33"



using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Logging;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments;
using Nop.Core.Events;
using Nop.Core.Http;
using Nop.Services.Catalog;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Directory;
using Nop.Services.Events;
using Nop.Services.Logging;
using Nop.Services.Orders;
using Nop.Services.Stores;


using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Messages;
using Nop.Services.Customers;
using Nop.Services.Messages;
using Nop.Web.Framework.Events;
using Nop.Web.Framework.Models;

//add by nova
//to be solved
using Nop.Services.OrderTextService;
//using Nop.Core.Data;
//using Nop.Core.Infrastructure;
using static Nop.Services.OrderTextService.CDETMPREGServiceSoapClient;
//end by nova



namespace Nop.Plugin.CDE.TempReg.Services
{
    //public async Task HandleEventAsync(OrderPaidEvent eventMessage)
    //IConsumer<OrderCancelledEvent>, IConsumer<OrderPaidEvent>, IConsumer<EntityDeletedEvent<Order>>
    public class EventConsumer :  IConsumer<OrderPaidEvent>
    {
        private readonly IAddressService _addressService;
        private readonly ICustomerService _customerService;
        private readonly ICountryService _countryService;
        private readonly IOrderService _orderService;
        private readonly IProductService _productService;
        private readonly IStateProvinceService _stateProvinceService;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly ILogger _logger;
        private readonly IWebHelper _webHelper;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ISettingService _settingService;
        private readonly IWidgetPluginManager _widgetPluginManager;


        public EventConsumer(IAddressService addressService,
         ICustomerService customerService,
         ICountryService countryService,
         IOrderService orderService,
         IProductService productService,
         IStateProvinceService stateProvinceService,
         IStoreContext storeContext,
         IStoreService storeService,
         IGenericAttributeService genericAttributeService,
         ILogger logger,
         IWebHelper webHelper,
         IHttpClientFactory httpClientFactory,
         ISettingService settingService,
         IWidgetPluginManager widgetPluginManager)
        {
            this._addressService = addressService;
            this._customerService = customerService;
            this._countryService = countryService;
            this._orderService = orderService;
            this._stateProvinceService = stateProvinceService;
            this._storeContext = storeContext;
            this._storeService = storeService;
            this._genericAttributeService = genericAttributeService;
            this._logger = logger;
            this._webHelper = webHelper;
            this._httpClientFactory = httpClientFactory;
            this._settingService = settingService;
            this._widgetPluginManager = widgetPluginManager;
        }

        private string FixIllegalJavaScriptChars(string text)
        {
            if (string.IsNullOrEmpty(text))
                return text;

            //replace ' with \' (http://stackoverflow.com/questions/4292761/need-to-url-encode-labels-when-tracking-events-with-google-analytics)
            text = text.Replace("'", "\\'");
            return text;
        }

        //to be solved
        //add by nova
        private async Task ProcessApprovedPurchase(Order order)
        {
            if (order != null)
            {
                var shippingAddress = await _addressService.GetAddressByIdAsync(order.ShippingAddressId.Value);
                var country = await _countryService.GetCountryByAddressAsync(shippingAddress);
                var stateProvince = await _stateProvinceService.GetStateProvinceByAddressAsync(shippingAddress);
                var customer = await _customerService.GetCustomerByIdAsync(order.CustomerId);

                var service = new CDETMPREGServiceSoapClient(EndpointConfiguration.CDETMPREGServiceSoap);
                var detail = new CDetails()
                {
                    iAddr1 = shippingAddress.Address1,
                    iCity = shippingAddress.City,
                    iCoName = shippingAddress.Company,
                    iCountry = country.Name,
                    iEmail = shippingAddress.Email,
                    iFName = shippingAddress.FirstName,
                    iLName = shippingAddress.LastName,
                    iState = stateProvince.Name,
                    iPostalCode = shippingAddress.ZipPostalCode,
                    iLgName = await _customerService.GetCustomerFullNameAsync(customer),
                    iOrderID = order.Id.ToString()
                };

                var orderItems = await _orderService.GetOrderItemsAsync(order.Id);

                if (orderItems != null)
                {

                    var ch = "XYZ";
                    var textFormat = "\r\n{0}\r\n";
                    //var orderItemRepository = EngineContext.Current.Resolve<IRepository<OrderItem>>();
                    foreach (var item in orderItems)
                    {
                        var product = await _productService.GetProductByIdAsync(item.ProductId);
                        detail.iPID = product.PrivateInteger;
                        if (product.PrivateInteger > 0)
                        {
                            var outDetails = service.Generate(detail, 1, ch);
                            if (outDetails != null)
                            {
                                if (outDetails.rescode == 0)
                                {
                                    item.OrderText = string.Format(textFormat, outDetails.oText2);
                                    //orderItemRepository.Update(item);
                                    await _orderService.UpdateOrderItemAsync(item);
                                }
                            }
                        }
                    }
                }
            }
        }
        //end by nova        

        /// <summary>
        /// Handles the event.
        /// </summary>
        /// <param name="eventMessage">The event message.</param>
        ///

        public async Task HandleEventAsync(OrderPaidEvent eventMessage)
        {
            if (eventMessage != null)
            {
                var order = eventMessage.Order;

                /*
                            order.OrderNotes.Add(new OrderNote
                            {
                                Note = "\"Order called smscomp\" Tmp Code Made",
                                DisplayToCustomer = false,
                                CreatedOnUtc = DateTime.UtcNow
                            });

                */

                if (order.PaymentStatus == PaymentStatus.Paid || order.PaymentStatus == PaymentStatus.Authorized)
                {
                    await ProcessApprovedPurchase(order);
                    /*
                                  order.OrderNotes.Add(new OrderNote
                                  {
                                      Note = "\"Order placed\" Tmp Code Made",
                                      DisplayToCustomer = false,
                                      CreatedOnUtc = DateTime.UtcNow
                                  });
                                _orderService.UpdateOrder(order);
                  */
                }
            }
        }
    }
}
1 ano atrás
CDESoftware wrote:

                var shippingAddress = await _addressService.GetAddressByIdAsync(order.ShippingAddressId.Value);

Maybe it is not the case for you but not all orders have a Shipping Address if the items do not require shipping
So check check the  ShippingAddressId  is not null or has a value
    if (order.ShippingAddressId.HasValue)

Otherwise generally check for null values
1 ano atrás
Thank you.  While I did refactor the code to verify nulls, it didn't help.

However...  I found the issue.

I was dumb and was not assigning productService to _productService early in the code.

        private readonly IAddressService _addressService;
        private readonly ICustomerService _customerService;
        private readonly ICountryService _countryService;
        private readonly IOrderService _orderService;
        private readonly IProductService _productService;
        private readonly IStateProvinceService _stateProvinceService;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly ILogger _logger;
        private readonly IWebHelper _webHelper;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ISettingService _settingService;
        private readonly IWidgetPluginManager _widgetPluginManager;
        private readonly IWorkContext _workContext;


        public EventConsumer(IAddressService addressService,
         ICustomerService customerService,
         ICountryService countryService,
         IOrderService orderService,
         IProductService productService,
         IStateProvinceService stateProvinceService,
         IStoreContext storeContext,
         IStoreService storeService,
         IGenericAttributeService genericAttributeService,
         ILogger logger,
         IWebHelper webHelper,
         IHttpClientFactory httpClientFactory,
         ISettingService settingService,
         IWidgetPluginManager widgetPluginManager,
         IWorkContext workContext)
        {
            _addressService = addressService;
            _customerService = customerService;
            _countryService = countryService;
            _orderService = orderService;
            _productService = productService; // WOOPS - Forgot that one.
            _stateProvinceService = stateProvinceService;
            _storeContext = storeContext;
            _storeService = storeService;
            _genericAttributeService = genericAttributeService;
            _logger = logger;
            _webHelper = webHelper;
            _httpClientFactory = httpClientFactory;
            _settingService = settingService;
            _widgetPluginManager = widgetPluginManager;
            _workContext = workContext;
        }
1 ano atrás
Thank you.  While I did refactor the code to verify nulls, it didn't help.

However...  I found the issue.

I was dumb and was not assigning productService to _productService early in the code.

        private readonly IAddressService _addressService;
        private readonly ICustomerService _customerService;
        private readonly ICountryService _countryService;
        private readonly IOrderService _orderService;
        private readonly IProductService _productService;
        private readonly IStateProvinceService _stateProvinceService;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly ILogger _logger;
        private readonly IWebHelper _webHelper;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ISettingService _settingService;
        private readonly IWidgetPluginManager _widgetPluginManager;
        private readonly IWorkContext _workContext;


        public EventConsumer(IAddressService addressService,
         ICustomerService customerService,
         ICountryService countryService,
         IOrderService orderService,
         IProductService productService,
         IStateProvinceService stateProvinceService,
         IStoreContext storeContext,
         IStoreService storeService,
         IGenericAttributeService genericAttributeService,
         ILogger logger,
         IWebHelper webHelper,
         IHttpClientFactory httpClientFactory,
         ISettingService settingService,
         IWidgetPluginManager widgetPluginManager,
         IWorkContext workContext)
        {
            _addressService = addressService;
            _customerService = customerService;
            _countryService = countryService;
            _orderService = orderService;
            _productService = productService; // WOOPS - Forgot that one.
            _stateProvinceService = stateProvinceService;
            _storeContext = storeContext;
            _storeService = storeService;
            _genericAttributeService = genericAttributeService;
            _logger = logger;
            _webHelper = webHelper;
            _httpClientFactory = httpClientFactory;
            _settingService = settingService;
            _widgetPluginManager = widgetPluginManager;
            _workContext = workContext;
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.