Error Calling Web API (backend) OrderProcessing/PlaceOrder

1 month ago
I am working on an integration for a website using version 4.6 and the Web API backend.

I am able to create a new customer, add billing and shipping addresses, and place items into the shopping cart.  With the shopping cart I create a new order and then call the OrderProcessing/PlaceOrder method.

Calling the PlaceOrder method generates the following error:
  "message": "Value cannot be null. (Parameter 'entity')",
  "inner_exception_message": null,
  "stack_trace": "System.ArgumentNullException: Value cannot be null. (Parameter 'entity')
at Nop.Plugin.Misc.WebApi.Framework.Infrastructure.Mapper.Extensions.MappingExtensions.ToDto[TBaseJsonDto](Object entity)
at Nop.Plugin.Misc.WebApi.Backend.Controllers.Orders.OrderProcessingController.PlaceOrder(ProcessPaymentRequestDto model)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Nop.Services.Authentication.AuthenticationMiddleware.InvokeAsync(HttpContext context) in D:\\nopCommerce-release-4.60.5-WAS\\Libraries\\Nop.Services\\Authentication\\AuthenticationMiddleware.cs:line 95
at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\\projects\\dotnet\\src\\MiniProfiler.AspNetCore\\MiniProfilerMiddleware.cs:line 121
at WebMarkupMin.AspNetCore7.WebMarkupMinMiddleware.InvokeCore(HttpContext context, Boolean useMinification, Boolean useCompression)
at WebMarkupMin.AspNetCore7.WebMarkupMinMiddleware.InvokeCore(HttpContext context, Boolean useMinification, Boolean useCompression)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Nop.Services.Installation.InstallUrlMiddleware.InvokeAsync(HttpContext context, IWebHelper webHelper) in D:\\nopCommerce-release-4.60.5-WAS\\Libraries\\Nop.Services\\Installation\\InstallUrlMiddleware.cs:line 53
at Nop.Services.Common.KeepAliveMiddleware.InvokeAsync(HttpContext context, IWebHelper webHelper) in D:\\nopCommerce-release-4.60.5-WAS\\Libraries\\Nop.Services\\Common\\KeepAliveMiddleware.cs:line 50
at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.InvokeCore(HttpContext context)
at Nop.Plugin.Misc.WebApi.Framework.Middleware.JwtMiddleware.InvokeAsync(HttpContext context, IWorkContext workContext, ICustomerService customerService)
at Nop.Plugin.Misc.WebApi.Framework.Middleware.ErrorHandlerMiddleware.InvokeAsync(HttpContext context)"

I get the same error using my integration code and the Swagger "Try It" interface.  This is the JSON object for the Swagger try it

{
  "store_id": 1,
  "customer_id": 4464219,
  "order_guid": "4aac9892-6bd4-4412-adcb-cf996b217ad3",
  "order_guid_generated_on_utc": "2024-03-13T22:52:09.2886257+00:00",
  "order_total": 15.0,
  "payment_method_system_name": "Payments.Sage",
  "credit_card_type": "Visa",
  "credit_card_name": "Joe Blow",
  "credit_card_number": "4111111111111111",
  "credit_card_expire_year": 2025,
  "credit_card_expire_month": 1,
  "credit_card_cvv2": "123",
  "initial_order": {
    "order_guid": "4aac9892-6bd4-4412-adcb-cf996b217ad3",
    "store_id": 1,
    "customer_id": 4464219,
    "billing_address_id": 132327,
    "shipping_address_id": 0,
    "pickup_address_id": 0,
    "pickup_in_store": false,
    "order_status_id": 10,
    "shipping_status_id": 10,
    "payment_status_id": 10,
    "payment_method_system_name": "Payments.Sage",
    "customer_currency_code": "USD",
    "currency_rate": 1.0,
    "customer_tax_display_type_id": 10,
    "vat_number": "12345",
    "order_subtotal_incl_tax": 15.0,
    "order_subtotal_excl_tax": 15.0,
    "order_sub_total_discount_incl_tax": 0.0,
    "order_sub_total_discount_excl_tax": 0.0,
    "order_shipping_incl_tax": 0.0,
    "order_shipping_excl_tax": 0.0,
    "payment_method_additional_fee_incl_tax": 0.0,
    "payment_method_additional_fee_excl_tax": 0.0,
    "tax_rates": "0:0;",
    "order_tax": 0.0,
    "order_discount": 0.0,
    "order_total": 15.0,
    "refunded_amount": 0.0,
    "checkout_attribute_description": "",
    "checkout_attributes_xml": "",
    "customer_language_id": 1,
    "affiliate_id": 0,
    "customer_ip": "192.168.1.1",
    "allow_storing_credit_card_number": false,
    "card_type": "",
    "card_name": "",
    "card_number": "",
    "masked_credit_card_number": "",
    "card_cvv2": "",
    "card_expiration_month": "",
    "card_expiration_year": "",
    "authorization_transaction_id": "",
    "authorization_transaction_code": "",
    "authorization_transaction_result": "",
    "capture_transaction_id": "",
    "capture_transaction_result": "",
    "subscription_transaction_id": "",
    "paid_date_utc": "2024-03-13T22:57:17.399Z",
    "shipping_method": "",
    "shipping_rate_computation_method_system_name": "",
    "custom_values_xml": "",
    "deleted": false,
    "created_on_utc": "2024-03-13T22:57:17.399Z",
    "custom_order_number": "114876",
    "redeemed_reward_points_entry_id": 0,
    "id": 114876
  },
  "recurring_cycle_length": 0,
  "recurring_cycle_period": 0,
  "recurring_total_cycles": 0,
  "custom_values": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  }
}

Any clue as to what I am doing wrong?

George
1 month ago
Hi. I found a problematic part of the code and we will fix the error in the next release. The essence of the problem is that, in all likelihood, some kind of error occurs during the order placement process, due to which the PlacedOrder field remains null and an extension method that does not allow such a situation is applied to it. The OrderProcessingController.PlaceOrder method should look like this so that the error does not occur again:


public virtual async Task<IActionResult> PlaceOrder([FromBody] ProcessPaymentRequestDto model)
{
    var result = await _orderProcessingService.PlaceOrderAsync(model.FromDto<ProcessPaymentRequest>());

    var resultDto = new PlaceOrderResultDto
    {
        Errors = result.Errors, Success = result.Success, PlacedOrder = result.PlacedOrder?.ToDto<OrderDto>()
    };

    return Ok(resultDto);
}
1 month ago
OK,  Enabling null for the PlacedOrder paramenter in the referenced method gets rid of the error I initially submitted.  

Now I get 2 errors on the return of the Task<PlaceOrderResultDto>

first error shows up in the system log:

Log level
Error
Short message
Cart is empty
Full message
Nop.Core.NopException: Cart is empty
   at Nop.Services.Orders.OrderProcessingService.PrepareAndValidateShoppingCartAndCheckoutAttributesAsync(PlaceOrderContainer details, ProcessPaymentRequest processPaymentRequest, Currency currentCurrency) in D:\nopCommerce-release-4.60.5-WAS-Base\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 681
   at Nop.Services.Orders.OrderProcessingService.PreparePlaceOrderDetailsAsync(ProcessPaymentRequest processPaymentRequest) in D:\nopCommerce-release-4.60.5-WAS-Base\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 472
   at Nop.Services.Orders.OrderProcessingService.PlaceOrderAsync(ProcessPaymentRequest processPaymentRequest) in D:\nopCommerce-release-4.60.5-WAS-Base\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 1732

the second error that I get trying to parse the result of the PlaceOrder method is

JsonSerializationException: Required property 'placed_order' expects a non-null value. Path '', line 1, position 64.

Am i not doing something that is required in the process or has this method never been used by anyone and hence not fully debugged?

Sure could use some insight.
George
1 month ago
In this case, the problem is not in the method itself, but in how and why you use it. Essentially, all controllers from WebApi.Backend are wrappers over services from Nop.Services. These are not methods from the administration area, you can look at the implementation of the PlaceOrderAsync method from the OrderProcessingService service to understand what and how it does and what data it requires to work
1 month ago
Sergey,

Thanks for pointing me in the proper direction.  I have it all figured out now and have it working in my integration project.

George