public RESTNancyModule()
            : base(ListenerConfig.Instance.POSRESTModuleBase)
        {
            Get["/status/{kiosk?}"] = parameters =>
            {
                // try to get the kiosk parameter
                string kiosk = null;

                try
                {
                    string kioskStr = parameters.kiosk;

                    if (!string.IsNullOrWhiteSpace(kioskStr))
                    {
                        kiosk = kioskStr;
                    }
                }
                catch
                {
                }

                // defines the function for calling GetStatus method
                Func <string, IPOSResponse> func = (bodyStr) =>
                {
                    StatusPOSResponse statusPOSResponse = new StatusPOSResponse();

                    if (string.IsNullOrWhiteSpace(kiosk))
                    {
                        // the kiosk parameter was not specified
                        statusPOSResponse.SetPOSError(Errors.KioskNotSpecified);
                    }
                    else
                    {
                        try
                        {
                            // call the POS and get the status for the specified kiosk
                            statusPOSResponse = GetStatus(kiosk);
                        }
                        catch (Exception ex)
                        {
                            statusPOSResponse = new StatusPOSResponse();
                            statusPOSResponse.SetPOSError(Errors.POSError, ex.Message);
                        }
                    }

                    return(statusPOSResponse);
                };

                // call GetStatus function
                IPOSResponse response = ExecuteRESTCall(func);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    return(new TextResponse(response.HttpStatusCode, response.ResponseContent));
                }
                else
                {
                    return(response.HttpStatusCode);
                }
            };

            Post["/order"] = parameters =>
            {
                // defines the function for calling OrderTransaction method
                Func <string, IPOSResponse> func = (bodyStr) =>
                {
                    OrderCreatePOSResponse posResponse = new OrderCreatePOSResponse();
                    Order order = posResponse.OrderCreateResponse.Order;
                    OrderCreateRequest request = null;

                    try
                    {
                        // deserialize request
                        request = JsonConvert.DeserializeObject <OrderCreateRequest>(bodyStr);
                    }
                    catch (Exception ex)
                    {
                        posResponse.SetPOSError(Errors.ErrorDeserializeRequest, ex.Message);
                    }

                    if (!order.HasErrors)
                    {
                        // no deserialize errors => check some elements
                        if (request.DOTOrder == null)
                        {
                            posResponse.SetPOSError(Errors.OrderMissing);
                        }
                        else if (string.IsNullOrWhiteSpace(request.DOTOrder.Kiosk))
                        {
                            posResponse.SetPOSError(Errors.KioskNotSpecified);
                        }
                        else if (string.IsNullOrWhiteSpace(request.DOTOrder.RefInt))
                        {
                            posResponse.SetPOSError(Errors.RefIntNotSpecified);
                        }
                        else if (request.DOTOrder.IsNewOrder && !request.DOTOrder.Items.Any())
                        {
                            posResponse.SetPOSError(Errors.ItemListNotSpecified);
                        }
                        else if (request.DOTOrder.IsTenderOrder &&
                                 ((request.DOTOrder.Tender == null) ||
                                  (request.DOTOrder.Tender.TenderItems == null) ||
                                  !request.DOTOrder.Tender.TenderItems.Any()))
                        {
                            posResponse.SetPOSError(Errors.TenderItemListNotSpecified);
                        }
                        else if (request.DOTOrder.IsExistingOrder && string.IsNullOrWhiteSpace(request.DOTOrder.OrderID))
                        {
                            posResponse.SetPOSError(Errors.OrderIDNotSpecified);
                        }
                    }

                    if (!order.HasErrors)
                    {
                        try
                        {
                            posResponse = OrderTransaction(request);
                        }
                        catch (Exception ex)
                        {
                            posResponse = new OrderCreatePOSResponse();
                            posResponse.SetPOSError(Errors.POSError, ex.Message);
                        }
                    }

                    return(posResponse);
                };

                // call OrderTransaction method
                IPOSResponse response = ExecuteRESTCall(func);

                if (response.HttpStatusCode == HttpStatusCode.Created)
                {
                    return(new TextResponse(response.HttpStatusCode, response.ResponseContent));
                }
                else
                {
                    return(response.HttpStatusCode);
                }
            };

            Get["/testdiag/{culturename?}"] = parameters =>
            {
                // try to get the culture name
                string culturename = null;

                try
                {
                    culturename = parameters.culturename;
                }
                catch
                {
                }

                // defines the function for calling TestDiag method
                Func <string, IPOSResponse> func = (bodyStr) =>
                {
                    TestDiagPOSResponse posResponse = new TestDiagPOSResponse();

                    if (string.IsNullOrWhiteSpace(culturename))
                    {
                        posResponse.SetPOSError(Errors.CultureNameNotSpecified);
                    }
                    else
                    {
                        try
                        {
                            posResponse = TestDiag(culturename);
                        }
                        catch (Exception ex)
                        {
                            posResponse = new TestDiagPOSResponse();
                            posResponse.SetPOSError(Errors.POSError, ex.Message);
                        }
                    }

                    return(posResponse);
                };

                // call TestDiag method
                IPOSResponse response = ExecuteRESTCall(func);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    return(new TextResponse(response.HttpStatusCode, response.ResponseContent));
                }
                else
                {
                    return(response.HttpStatusCode);
                }
            };
        }
Exemplo n.º 2
0
        /// <summary>
        /// Call the checkBasket Stored procedure
        /// </summary>
        public OrderCreatePOSResponse CheckBasketStoredProcs()
        {
            // Create a new SqlConnection object
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = RESTNancyModule.ConnectionString;
                con.Open();
                Log.Info("Connected to the Database");

                /****  BASKETID ***************************************************************
                 * 1) Execute the OrderBasketAdd stored proc to get the Id for the new Basket
                 * this must be called first for a new transaction
                 * ******************************************************************************/
                basketId = ExecuteOrderBasketAdd(con);

                if (basketId < 1)
                {
                    Log.Error("\nOrder Basket Add Failed ");
                    return(null);
                }

                /******************************************************************
                 * Start to populate response with transaction items
                 * ***************************************************************/

                //set response Kiosk and refInt
                response.OrderCreateResponse.Order.Kiosk  = request.DOTOrder.Kiosk;
                response.OrderCreateResponse.Order.RefInt = request.DOTOrder.RefInt;

                //check Kiosk is valid
                if (string.IsNullOrEmpty(response.OrderCreateResponse.Order.Kiosk))
                {
                    reason += "\nKiosk Value Null or Empty.";
                    response.OrderCreateResponse.Order.Reason = reason;
                    response.SetPOSError(Errors.KioskNotSpecified);
                    return(null);
                }

                //check Refint is valid
                if (string.IsNullOrEmpty(response.OrderCreateResponse.Order.RefInt))
                {
                    reason += "\nRefint Value Null or Empty.";
                    response.OrderCreateResponse.Order.Reason = reason;
                    response.SetPOSError(Errors.RefIntNotSpecified);
                    return(null);
                }


                /*****  ADDITEMS **************************************************************
                 * 2) Call the OrderBasketAddItem stored proc to get the parentId for all items
                 * ******************************************************************************/
                for (int i = 0; i < request.DOTOrder.Items.Count; i++)
                {
                    Item item = request.DOTOrder.Items[i];
                    ProcessItem(con, item, basketId, 0);
                }

                /****  CHECKBASKET  ************************************************************
                 * 3) Execute the stored proc OrderBasket_API_CheckBasket to get the payload
                 * *******************************************************************************/
                payLoad = ExecuteOrderBasket_API_Checkbasket(con, basketId);

                /**  *****CHECKBASKET ******************************************************
                 * 3a) Get the CheckBasket API Response call
                 * ***************************************************************************/
                IRestResponse checkBasketResp = ApiPost(RESTNancyModule.CheckBasketUrl,
                                                        RESTNancyModule.FlytKeyType1,
                                                        RESTNancyModule.FlytAPIKey1,
                                                        RESTNancyModule.ContentType,
                                                        payLoad);

                /****** CHECKBASKET ***********************************************************
                 * 3b) Execute the store proc OrderBasket_APIResponse_CheckBasket this will
                 * Insert the response of the OrderBasket_API_CheckBasket Store Proc
                 *
                 * ************************************************************************************/
                if (checkBasketResp.IsSuccessful)
                {
                    ExecuteOrderBasket_APIResponse_Checkbasket(con, basketId, checkBasketResp.Content);

                    /****  ORDER ***************************************************************************
                     * 4) Execute the store proc Generate payload for Order API Call
                     *
                     * ******************************************************************************/
                    payLoad = string.Empty;
                    payLoad = ExecuteOrderBasket_API_Order(con, basketId);

                    /****  ORDER *******************************************************************
                     * 4a)  Get the Order API Response call
                     * ******************************************************************************/
                    IRestResponse checkOrderResp = ApiPost(RESTNancyModule.OrderUrl,
                                                           RESTNancyModule.FlytKeyType2,
                                                           RESTNancyModule.FlytAPIKey2,
                                                           RESTNancyModule.ContentType,
                                                           payLoad);

                    if (checkOrderResp.IsSuccessful)
                    {
                        /****  ORDER *************************************************************************
                         * 4b)   Insert the response from the Order api call into the OrderBasketAPIResponse
                         * Update orderBasket with API Response   and get the Id
                         ************************************************************************************/
                        string orderId = ExecuteOrderBasket_APIResponse_Order(con, basketId, checkOrderResp.Content);

                        Log.Info($"Order Id:{orderId}");

                        /******************************************************************
                         * Continue to populate response with transaction items
                         * ***************************************************************/

                        response.OrderCreateResponse.Order.OrderID          = orderId;
                        response.OrderCreateResponse.Order.Totals.AmountDue = request.DOTOrder.OrderAmount;

                        //check orderId is valid need to use this in FulFillment call if it is
                        if (string.IsNullOrEmpty(response.OrderCreateResponse.Order.OrderID))
                        {
                            reason += "\nOrderId Null or Empty";
                            response.OrderCreateResponse.Order.Reason = reason;
                            response.SetPOSError(Errors.OrderIDNotSpecified);
                        }


                        /*****  FULFILLMENT  **************************************************************************
                         *  5) Execute the store proc OrderBasket_API_CollectionByCustomer this will
                         * Generate  the payload for Collection By Customer API Call
                         * *****************************************************************************/
                        payLoad = string.Empty;
                        payLoad = ExecuteOrderBasket_API_CollectionByCustomer(con, basketId);

                        /****  FULFILLMENT *******************************************************************
                         * 5a)  Get the Order API Response call
                         *      create the Fullfillment URL to Post To
                         * ******************************************************************************/
                        string fullFillUrl = RESTNancyModule.OrderUrl + "/" + orderId + RESTNancyModule.FullFillmentUrl;

                        // IRestResponse checkFullfilmentResp = restCalls.Fullfillment(payLoad, orderId);
                        IRestResponse checkFullfilmentResp = ApiPost(fullFillUrl,
                                                                     RESTNancyModule.FlytKeyType2,
                                                                     RESTNancyModule.FlytAPIKey2,
                                                                     RESTNancyModule.ContentType,
                                                                     payLoad);

                        if (checkFullfilmentResp.IsSuccessful)
                        {
                            /********FULFILLMENT ****************************************************************
                             * 5b) Update orderBasket with API Response
                             * ******************************************************************************/
                            ExecuteOrderBasket_APIResponse_CollectionByCustomer(con, basketId, checkFullfilmentResp.Content);
                        }
                        else
                        {
                            Log.Error("Fullfillment API POST error");
                            return(null);
                        }
                    }
                    else
                    {
                        Log.Error("Order API POST error");
                        return(null);
                    }
                }
                else
                {
                    Log.Error("CheckBasket API POST error");
                    return(null);
                }
            }

            return(response);
        }