Developer forum

Forum » Development » Cancel order using API

Cancel order using API

Lars Larsen
Lars Larsen
Reply

Hi 

I have used the method Dynamicweb.Ecommerce.Services.Orders.Capture() to capture orders using the API. Is it also possible to cancel a not already captured but authorized order using the API?


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

It depends if the checkout handler supports it. Here is a bit of code from the payments web api you can probably use.

var orderService = new OrderService();
                orderService.MarkOrderAsDeleted(order);
                var manager = OrderManager.GetFor(order);
                if (manager.IsOperationSupported(OrderOperations.Cancel))
                {
                    if (order.CaptureInfo.State != OrderCaptureInfo.OrderCaptureState.Success)
                    {
                        TryCancel(result, manager);
                    }
                    else
                    {
                        var availableToReturn = 0.0;
                        if (OrderReturnInfo.CheckIfOrderReturned(order, ref availableToReturn))
                        {
                            TryCancel(result, manager);
                        }
                        else
                        {
                            result.Status = "Success";
                            result.Message = "The order has been captured, you need to process a refund first.";
                        }
                    }
                }
                else
                {
                    result.Status = "Failed";
                    result.Message = "Cancel operation is not supported for specified order";
                }

 

private void TryCancel(PaymentOperationResultModel result, OrderManager manager)
        {
            try
            {
                if (manager.CancelOrder())
                {
                    result.Status = "Success";
                    result.Message = "Order has been canceled successfully.";
                }
                else
                {
                    result.Status = "Failed.";
                    result.Message = "Order has not been canceled.";
                }
            }
            catch (Exception e)
            {
                result.Status = "Failed";
                result.Message = $"Unhandled internal exception: {e.Message}";
            }
        }
Votes for this answer: 1

 

You must be logged in to post in the forum