Ejemplo n.º 1
0
        /// <summary>
        /// Places an order on shopster. 
        /// </summary>
        /// <param name="apiContext"></param>
        /// <param name="orderToPlace"></param>
        /// <param name="productMap"></param>
        /// <returns>Null if failed, Shopster NewOrderId otherwise. </returns>
        public int? PlaceOrder(ApiContext apiContext, IOrder orderToPlace, Dictionary<int, int> productMap)
        {
            try
            {
                var call = new PlaceOrderCall(apiContext) {Request = new PlaceOrderRequestType()};
                string messageId = orderToPlace.GetHashCode().ToString(); //This should be unique enough right?
                call.Request.MessageID = messageId;

                call.Request.Cart = ShopsterOrderConverter.ToShopsterOrder(orderToPlace, productMap).orderCart;
                if (call.Request.Cart != null && call.Request.Cart.Items != null && call.Request.Cart.Items.Count > 0)
                {
                    call.Execute();

                    if (call.Response.Status == ResponseStatusType.Failed)
                    {
                        //Todo: Fix this hack, should still return null.
                        //Todo: Implement email/messaging in API to send something to the user.
                        ApiLogger.ErrorFormat(
                            "ShopsterCommunicator::PlaceOrder(): Reponse.Status is 'Failed' for ShopsterAccessToken({0}), ",
                            apiContext.AccessToken);
                        return null;
                    }

                    //TODO : handle warnings
                    if (call.Response.Status == ResponseStatusType.Success ||
                        call.Response.Status == ResponseStatusType.SuccessWithWarnings)
                    {
                        if (call.Response.MessageID != messageId || call.Response.NewOrderId == null)
                        {
//Should never happen in syncronous environment. 
                            ApiLogger.WarnFormat(
                                "ShopsterCommunicator::PlaceOrder(): Strange situation: API Call was assigned messageId({0}), but returned messageId({1}).",
                                messageId, call.Response.MessageID);
                        }

                        try
                        {
                            return Convert.ToInt32(call.Response.NewOrderId);
                        }
                        catch (Exception e)
                        {
                            ApiLogger.ErrorFormat(
                                "ShopsterCommunicator::PlaceOrder(): Couldnt convert call.Response.NewOrderId to int32. Exception is ({0})",
                                e.Message);
                            return null;
                        }
                    }
                }
            }
            catch (ApiException ae)
            {
                string result = ae.Message;


                if (ae is ApiFaultException)
                {
                    var afe = (ApiFaultException) ae;
                    result = string.Format("({0}) {1}: {2}", afe.Error.SeverityCode, result,
                                           afe.Error.Message);
                    //TODO: Logging
                    ApiLogger.ErrorFormat("ShopsterCommunicator::PlaceOrder(): ApiFaultException: ({0}) {1}: {2}",
                                          afe.Error.SeverityCode, result,
                                          afe.Error.Message);
                }
                return null;
            }
            return null;
        }