I was wondering if anyone has encountered an issue where the purchase order from OrderRepository is NULL after calling SaveAsPurchaseOrder saving a cart about once or twice a day(we have about 200 orders per day).
It seems that every part of the process before the exception is working fine. We're not getting any exceptions or indication anything went wrong before the exception. For some bizzare reason the purchase order we get back is null after the cart is successfully saved.
Here is the code that does the order processing from the cart. The problem line is below the comment "Null reference exception is thrown here because purchase order is null"
public OrderReference SaveCurrentShoppingCartAsPurchaseOrder()
{
ICart cart = this._shoppingCartManager.GetCart();
//Assign order number before processing for payment
string orderNumber = this._orderNumberManager.TryIssueNewOrderNumberOrFallback(cart.MarketId, false);
cart.Properties[PurchaseOrderConstants.OrderNumberFieldName] = orderNumber;
using (var scope = new TransactionScope())
{
this._inventoryProcessor.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment(), OrderStatus.InProgress, (item, issue) => { });
try
{
//Process the payment
cart.ProcessPayments();
}
catch (PaymentException e)
{
this._inventoryProcessor.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment(), OrderStatus.Cancelled, (item, issue) => { });
this._logManager.WriteError("Payment processing failed", e);
throw new PaymentCouldNotBeProccessedOrIncorrectAmountWereAuthorizedException("Payment processing failed");
}
//Find the amount authorized / processed
decimal totalProcessedPayment = cart.GetFirstForm().Payments.Where(p => p.Status.Equals(PaymentStatus.Processed.ToString())).Sum(p => p.Amount);
Money cartTotal = cart.GetTotal();
//If the processed amount were different than the total
if (cartTotal.Amount != totalProcessedPayment)
{
this._inventoryProcessor.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment(), OrderStatus.Cancelled, (item, issue) => { });
throw new PaymentCouldNotBeProccessedOrIncorrectAmountWereAuthorizedException("Wrong amount");
}
cart.GetFirstShipment().OrderShipmentStatus = OrderShipmentStatus.InventoryAssigned;
try
{
this._inventoryProcessor.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment(), OrderStatus.Completed, (item, issue) => {
var message = string.Format("Issue adjusting inventory for Item: {0} with issue {1} ", item.Code, issue);
_logManager.WriteInfo(message);
});
}
catch (Exception e)
{
_logManager.WriteError("Error adjusting inventory", e);
}
//Save as purchase order
OrderReference reference = this._orderRepository.SaveAsPurchaseOrder(cart);
//Mark order as OnHold to indicate it hasn't been queued for processing yet
IPurchaseOrder purchaseOrder = this._orderRepository.Load<IPurchaseOrder>(orderReference.OrderGroupId);
// Null reference exception is thrown here becuase purchaseOrder is NULL
purchaseOrder.OrderStatus = OrderStatus.OnHold;
purchaseOrder.AcceptChanges();
this._orderRepository.Delete(cart.OrderLink);
scope.Complete();
return reference;
}
}
So far I can only get this to happen in our preproduction or production environment so I wonder if there is some synchronization issues between the Episerver API and database in DXC.
Or maybe we're doing something not very efficient here? Also we're on Commerce 12.17.2 but it appears that we had the same issue before when we were on Commerce 11.
Thanks!