Order Placement

Place orders into our fulfilment system.

Request Parameters

Each request to the web sevice requires a number of parameters, all of which are listed below. All requests require a key which will be sent to you by your account manager.

Name Type Default Description
ApiKey* String The key used to authenticate with the service.
PromoCode* String The promotion for us to store the order under.
OrderRecord* String The order data.
Back to Top

Responses

The response from the web service is a table with the specification below. If an error occurs, the response will not follow this format. Instead the response will be passed back in a single JSON string object with the parameter name: Error

Name Type Default Description
{"ORDERNO": "0001234"} String This is the unique order number assigned by the fulfilment system padded to eight characters with leading zeros.
Back to Top

Errors

Below are the errors which are specific to this web service. If an error occurs the JSON returned will have with a single property called "Error". All error responses will be contained within this property.

Error Message Cause Fix
ApiKey is missing! The ApiKey parameter is missing. Pass the ApiKey in the querystring or POST form-data.
Order record is missing! The OrderRecord parameter is missing. Pass the OrderRecord in the querystring or POST form-data.
Promotion code is missing! The PromoCode parameter is missing. Pass the PromoCode in the querystring or POST form-data.
Client record missing for client code [AccountName] The account setup has not been completed by MRM. Report the issue to us so we can advise on setup progress.
Unknown promotion code [PromoCode] The promotion for this code has not been setup by MRM. Report the issue to us so we can advise on setup progress.
No data returned from order process. An error occured when processing the order. Report the issue to us so we can investigate.
Back to Top

Available endpoints

Example requests

https://system-name.mrm.co.uk/webservices/orderplacement?ApiKey=AA11-AA11-AA11-AA11&PromoCode=[PromoCode]&OrderRecord=[OrderRecord]

Example response

{"ORDERNO": "0001234"}
Back to Top

Sample Code

public class PushFulfilmentOrders : IHttpHandler
{
    public class Order
    {
        public Int32 OrderStatusId { get; set; }

        public String MRMOrderID { get; set; }
        public DateTime? PushedToFulfilmentDate { get; set; }

        public string DeliveryAddressLine1 { get; set; }
        public string DeliveryAddressLine2 { get; set; }
        public string DeliveryAddressLine3 { get; set; }
        public string DeliveryTown { get; set; }
        public string DeliveryCounty { get; set; }
        public string DeliveryPostcode { get; set; }
        public string DeliveryCountry { get; set; }

        public String BillingTitle { get; set; }
        public String BillingFirstName { get; set; }
        public String BillingLastName { get; set; }
        public string BillingAddressLine1 { get; set; }
        public string BillingAddressLine2 { get; set; }
        public string BillingAddressLine3 { get; set; }
        public string BillingTown { get; set; }
        public string BillingCounty { get; set; }
        public string BillingPostcode { get; set; }
        public string BillingCountry { get; set; }

        [JsonIgnore]
        public String FulfilmentSystemErrorMessage { get; set; }
        [NotMapped]
        public ICollection<OrderLineDTO> OrderLinesDTO { get; set; }
    }

    public class OrderLineDTO
    {
        public String ProductCode { get; set; }
        public Decimal UnitPrice { get; set; }
        public Int32 Quantity { get; set; }
        
        public OrderLineDTO(){
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        int intCountSuccessfulPlacedIntoFulfillment = 0;
        int intCountErroredWhenPlacingIntoFulfillment = 0;
        int intCountConnectionErroredWhenPlacingIntoFulfillment = 0;
        StringBuilder sbConnectionErrors = new StringBuilder();

        using (DatabaseContext _db = new Database.Models.DatabaseContext())
        {
		//get all orders that are status 'ReadyForFulfilment'
		List&lt;Order&gt; PaidOrders = _db.Orders.Where(w => w.OrderStatusId == (int)OrderStatus.ReadyForFulfilment && w.MRMOrderID == null).ToList&lt;Order&gt;();
		foreach (Order order in PaidOrders)
		{
    		String MRMOrderNo = "0";
    		String strPromoCode = WebConfigurationManager.AppSettings["MRM.Promotion"];

    		//check the town field of the delivery details
    		String strTown = order.DeliveryTown;
    		if (String.IsNullOrWhiteSpace(strTown))
    		{
        		strTown = order.DeliveryCounty;
        		if (String.IsNullOrWhiteSpace(strTown))
        		{
					strTown = order.DeliveryAddressLine3;
					if (String.IsNullOrWhiteSpace(strTown))
					{
						strTown = order.DeliveryAddressLine2;
						if (String.IsNullOrWhiteSpace(strTown))
    					{
        					strTown = order.DeliveryAddressLine1;
    					}
					}
				}
    		}
    		order.DeliveryTown = strTown;

    		//if no billing details are captured, pass through the delivery details
    		order.BillingTitle = order.DeliveryTitle;
    		order.BillingFirstName = order.DeliveryFirstName;
    		order.BillingLastName = order.DeliveryLastName;
    		order.BillingAddressLine1 = order.DeliveryAddressLine1;
    		order.BillingAddressLine2 = order.DeliveryAddressLine2;
    		order.BillingAddressLine3 = order.DeliveryAddressLine3;
    		order.BillingTown = order.DeliveryTown;
    		order.BillingCounty = order.DeliveryCounty;
    		order.BillingPostcode = order.DeliveryPostcode;
    		order.BillingCountry = order.DeliveryCountry;

    		String strJSONResponse = String.Empty;
    		Boolean blnErrorOccured = false;

    		foreach (OrderLine _orderLine in order.OrderLines)
    		{
        		OrderLineDTO _orderLineDTO = new OrderLineDTO();
        		if (_orderLine.ProductVariantOptionId.HasValue)
        		{
					//variant product
					_orderLineDTO.ProductCode =  _orderLine.ProductVariantOption.FulfilmentCatalogueCode;
					_orderLineDTO.UnitPrice = _orderLine.ProductVariantOption.Price;
        		}
        		else
        		{
					//non variant product
					_orderLineDTO.ProductCode = _orderLine.Product.FulfilmentCatalogueCode;
					_orderLineDTO.UnitPrice = _orderLine.Product.Price;
        		}
        		_orderLineDTO.Quantity = _orderLine.Quantity;
        		order.OrderLinesDTO.Add(_orderLineDTO);
    		}
        
    		//upload order
    		using (WebClient client = new WebClient())
    		{
        		try
        		{
					byte[] response = client.UploadValues(WebConfigurationManager.AppSettings["Newton.UploadOrder.URL"], new NameValueCollection()
					{
    					{ "ApiKey", WebConfigurationManager.AppSettings["Newton.API.Key"] },
    					{ "OrderRecord", JsonConvert.SerializeObject(order) },
    					{ "PromoCode", strPromoCode }
					});
					strJSONResponse = Encoding.UTF8.GetString(response, 0, response.Length);
        		}
        		catch (Exception ex)
        		{
					strJSONResponse = ex.Message;
					blnErrorOccured = true;
        		}
    		}
        
    		if (strJSONResponse.Contains("{\"Error"))
				blnErrorOccured = true;
			
    		if (String.IsNullOrWhiteSpace(strJSONResponse))
				blnErrorOccured = true;

    		if (!blnErrorOccured)
    		{
        		JObject response = JObject.Parse(strJSONResponse);

        		double parsedOrderNo;
        		if (Double.TryParse((string)response["ORDERNO"], out parsedOrderNo))
        		{
					intCountSuccessfulPlacedIntoFulfillment += 1;

					order.MRMOrderID = (string)response["ORDERNO"];
					order.OrderStatusId = (int)OrderStatus.SentForFulfilment;
					order.PushedToFulfilmentDate = DateTime.UtcNow;
					_db.SaveChanges();
        		}
        		else
        		{
					// failed to push to fulfilment system for some reason
					intCountErroredWhenPlacingIntoFulfillment += 1;

					order.OrderStatusId = (int)OrderStatus.FailedToPushToFulfilment;

					if (!String.IsNullOrWhiteSpace((string)response["Error"]))
						order.FulfilmentSystemErrorMessage = (string)response["Error"];
					else
						order.FulfilmentSystemErrorMessage = "Blank response from Newton.";
				
					_db.SaveChanges();
        		}
    		}
    		else 
			{
				// failed to push to fulfilment system for some reason
				intCountErroredWhenPlacingIntoFulfillment += 1;

				order.OrderStatusId = (int)OrderStatus.FailedToPushToFulfilment;
				order.FulfilmentSystemErrorMessage = strJSONResponse;
				_db.SaveChanges();
    		}        
        }

        context.Response.ContentType = "text/plain";
        if (intCountSuccessfulPlacedIntoFulfillment &gt; 0)
			context.Response.Write(String.Format("Successfully placed: {0} orders into fulfilment system. ", intCountSuccessfulPlacedIntoFulfillment));
        
        if (intCountErroredWhenPlacingIntoFulfillment &gt; 0 || intCountConnectionErroredWhenPlacingIntoFulfillment &gt; 0)
			context.Response.Write(String.Format("{0} order(s) errored when placing them into fulfilment, their order status ID has been changed to 5 = (FailedToPushToFulfilment).", intCountErroredWhenPlacingIntoFulfillment));

        if (intCountSuccessfulPlacedIntoFulfillment == 0 && intCountErroredWhenPlacingIntoFulfillment == 0)
			context.Response.Write("No orders to process.");
    }
}
Back to Top