Exemple #1
0
        public HttpResponseMessage GetTables()
        {
            if (!_clientValidator.Validate(Request.GetClientCertificate(), Request.Headers.Authorization, out string message))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException(message)));
            }

            log.DebugEx("GET ~/api/tables");
            try
            {
                var r = new PATResponse {
                    Tables = _tablesRepository.GetTables()
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in GET ~/api/tables.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("ResourceNotFoundException in GET ~/api/tables.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
        public HttpResponseMessage CreateEFTPOSCommand([FromBody] PATRequest commandRequest)
        {
            if (!_clientValidator.Validate(Request.GetClientCertificate(), Request.Headers.Authorization, out string message))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException(message)));
            }

            log.DebugEx(tr => tr.Set("POST ~/api/eftpos/commands", commandRequest));

            // Extract the eftpos command from the request
            if (commandRequest == null || commandRequest.EFTPOSCommand == null)
            {
                log.ErrorEx(tr => tr.Set("TenderRequest.EFTPOSCommand==NULL in POST ~/api/eftpos/commands."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "PATRequest.EFTPOSCommand==NULL"));
            }

            try
            {
                var r = new PATResponse {
                    EFTPOSCommand = _eftposRepository.CreateEFTPOSCommand(commandRequest.EFTPOSCommand)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/eftpos/commands.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/eftpos/commands.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
Exemple #3
0
        public HttpResponseMessage CreateTender([FromBody] PATRequest tenderRequest)
        {
            if (!_clientValidator.Validate(Request.GetClientCertificate(), Request.Headers.Authorization, out string message))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException(message)));
            }

            log.DebugEx(tr => tr.Set("POST ~/api/tenders", tenderRequest));

            // Extract the tender from the request
            if (tenderRequest == null || tenderRequest.Tender == null)
            {
                log.ErrorEx(tr => tr.Set("TenderRequest.Tender==NULL in POST ~/api/tenders."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "TenderRequest.Tender==NULL"));
            }

            try
            {
                var r = new PATResponse {
                    Tender = _tendersRepository.CreateTender(tenderRequest.Tender)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.Created, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("ResourceNotFoundException in POST ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
Exemple #4
0
        public HttpResponseMessage GetOrderCustomerReceipt(string id, string receiptOptionId = null)
        {
            if (!_clientValidator.Validate(Request.GetClientCertificate(), Request.Headers.Authorization, out string message))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException(message)));
            }

            var option = (!string.IsNullOrEmpty(receiptOptionId) ? receiptOptionId : string.Empty);

            log.DebugEx(tr => tr.Set(String.Format("GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));

            try
            {
                var r = new PATResponse()
                {
                    Receipt = _ordersRepository.GetCustomerReceiptFromOrderId(id, receiptOptionId ?? string.Empty)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set(String.Format("InvalidRequestException in GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set(String.Format("ResourceNotFoundException in GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
        public HttpResponseMessage GetOrderCustomerReceipt(string id, string receiptOptionId = null)
        {
            var option = (!string.IsNullOrEmpty(receiptOptionId) ? receiptOptionId : string.Empty);

            log.DebugEx(tr => tr.Set(String.Format("GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));

            try
            {
                var r = new PATResponse()
                {
                    Receipt = _ordersRepository.GetCustomerReceiptFromOrderId(id, receiptOptionId ?? string.Empty)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set(String.Format("InvalidRequestException in GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set(String.Format("ResourceNotFoundException in GET ~/api/orders/{0}/receipt?receiptOptionId={1}", id, option)));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
        public HttpResponseMessage CreateEFTPOSCommand([FromBody] PATRequest commandRequest)
        {
            log.DebugEx(tr => tr.Set("POST ~/api/eftpos/commands", commandRequest));

            // Extract the eftpos command from the request
            if (commandRequest == null || commandRequest.EFTPOSCommand == null)
            {
                log.ErrorEx(tr => tr.Set("TenderRequest.EFTPOSCommand==NULL in POST ~/api/eftpos/commands."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "PATRequest.EFTPOSCommand==NULL"));
            }

            try
            {
                var r = new PATResponse {
                    EFTPOSCommand = _eftposRepository.CreateEFTPOSCommand(commandRequest.EFTPOSCommand)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/eftpos/commands.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/eftpos/commands.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
        public HttpResponseMessage CreateTender([FromBody] PATRequest tenderRequest)
        {
            log.DebugEx(tr => tr.Set("POST ~/api/tenders", tenderRequest));

            // Extract the tender from the request
            if (tenderRequest == null || tenderRequest.Tender == null)
            {
                log.ErrorEx(tr => tr.Set("TenderRequest.Tender==NULL in POST ~/api/tenders."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "TenderRequest.Tender==NULL"));
            }

            try
            {
                var r = new PATResponse {
                    Tender = _tendersRepository.CreateTender(tenderRequest.Tender)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in POST ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("ResourceNotFoundException in POST ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
Exemple #8
0
        public void Run()
        {
            PATResponse   r = null;
            List <Table>  tables = null;
            Order         order = null;
            Table         table = null;
            Tender        tender = null;
            Settings      settings = null;
            string        uri = null;
            EFTPOSCommand eftposRequestCommand = null, eftposEventCommand = null;
            string        eftposCommandId = string.Empty;

            // Get our settings
            GetSettings(ref settings);

            // Get tables
            GetTables(ref tables);

            // Find the first available order
            GetFirstOrderFromTables(tables, ref order, ref table);

            // Grab a receipt for this order
            if (settings.ReceiptOptions != null && settings.ReceiptOptions.Count > 0)
            {
                GetReceipt(order, settings.ReceiptOptions[0]);
            }

            // Create a tender (we can't continue without a default tender option)
            if (settings.TenderOptions == null || settings.TenderOptions.Count == 0)
            {
                throw new Exception("Can't continue without a tender option");
            }
            CreateTender(ref tender, settings.TenderOptions[0], order);

            // Send our txn request EFTPOS command\
            CreateEFTPOSRequest(ref eftposRequestCommand, tender);
            eftposCommandId = eftposRequestCommand.Id;

            // Send some display events
            uri = String.Format("{0}/api/eftpos/commands?key={1}", BaseUri, ApiKey);
            PostDisplayEvent(uri, DelayEFTPOSCommands, DebugTrace, tender.Id, eftposRequestCommand.Id, "     SWIPE CARD                         ", "100000003", "0");
            PostDisplayEvent(uri, DelayEFTPOSCommands, DebugTrace, tender.Id, eftposRequestCommand.Id, "   SELECT ACCOUNT                       ", "100000004", "0");
            PostDisplayEvent(uri, DelayEFTPOSCommands, DebugTrace, tender.Id, eftposRequestCommand.Id, "     ENTER  PIN                         ", "100000005", "0");
            PostDisplayEvent(uri, DelayEFTPOSCommands, DebugTrace, tender.Id, eftposRequestCommand.Id, "     PROCESSING         PLEASE WAIT     ", "000000000", "0");
            PostDisplayEvent(uri, DelayEFTPOSCommands, DebugTrace, tender.Id, eftposRequestCommand.Id, "     TRANSACTION          APPROVED      ", "100000005", "0");

            // Send the transaction event
            CreateEFTPOSEvent(ref eftposEventCommand, eftposRequestCommand, tender);

            // Update the tender
            UpdateTender(ref tender, order);

            // Check the order details. It should be complete.
            ValidateOrderComplete(order);

            Console.WriteLine("Complete");
        }
        void HandleRequest(POSAPIMsg request, Func <PATResponse> func)
        {
            Log($"Processing {request.Header.RequestType} {request.Header.RequestMethod}");

            // Get content
            ResponseCode rc      = ResponseCode.Ok;
            string       content = null;

            try
            {
                PATResponse patResponse = func.Invoke();
                content = JsonConvert.SerializeObject(patResponse);
            }
            catch (InvalidRequestException ex)
            {
                Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.BadRequest;
            }
            catch (ResourceNotFoundException ex)
            {
                Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.NotFound;
            }
            catch (Exception ex)
            {
                Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.ServerError;
            }

            // Build response
            POSAPIMsg resp = new POSAPIMsg()
            {
                Header = new POSAPIMsgHeader()
                {
                    RequestMethod = request.Header.RequestMethod,
                    RequestType   = request.Header.RequestType,
                    ResponseCode  = content != null ? ResponseCode.Ok : rc,
                    ContentLength = content?.Length ?? 0
                },
                Content = content
            };

            var respString = resp.ToString();

            Log(new LogData(respString, $"TX (Response): {respString}"));

            eft.TxnType            = "@";
            eft.CsdReservedString1 = respString;
            eft.CsdReservedMethod3();
        }
 public HttpResponseMessage GetTables()
 {
     log.DebugEx("GET ~/api/tables");
     try
     {
         var r = new PATResponse {
             Tables = _tablesRepository.GetTables()
         };
         log.DebugEx(tr => tr.Set("return", r));
         return(Request.CreateResponse(HttpStatusCode.OK, r));
     }
     catch (InvalidRequestException ex)
     {
         log.ErrorEx(tr => tr.Set("InvalidRequestException in GET ~/api/tables.", ex));
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
     catch (ResourceNotFoundException ex)
     {
         log.ErrorEx(tr => tr.Set("ResourceNotFoundException in GET ~/api/tables.", ex));
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
     }
 }
 public HttpResponseMessage GetOrdersByTableId(string id)
 {
     log.DebugEx(tr => tr.Set(String.Format("GET ~/api/tables/{0}/orders", id)));
     try
     {
         var r = new PATResponse {
             Orders = _ordersRepository.GetOrdersFromTable(id)
         };
         log.DebugEx(tr => tr.Set("return", r));
         return(Request.CreateResponse(HttpStatusCode.OK, r));
     }
     catch (InvalidRequestException ex)
     {
         log.ErrorEx(tr => tr.Set(String.Format("InvalidRequestException in GET ~/api/tables/{0}/orders", id), ex));
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
     catch (ResourceNotFoundException ex)
     {
         log.ErrorEx(tr => tr.Set(String.Format("ResourceNotFoundException in GET ~/api/tables/{0}/orders", id), ex));
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
     }
 }
        public HttpResponseMessage UpdateTender(string id, [FromBody] PATRequest tenderRequest)
        {
            log.DebugEx(tr => tr.Set("PUT ~/api/tenders", tenderRequest));

            // Extract the tender from the request
            if (tenderRequest == null || tenderRequest.Tender == null)
            {
                log.ErrorEx(tr => tr.Set("TenderRequest.Tender==NULL in PUT ~/api/tenders."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "TenderRequest.Tender==NULL"));
            }

            // Validate the tender id
            if (!tenderRequest.Tender.Id.Equals(id))
            {
                log.ErrorEx(tr => tr.Set("tenderRequest.Tender.Id != param id in PUT ~/api/tenders."));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "tenderRequest.Tender.Id != param id"));
            }

            try
            {
                var r = new PATResponse {
                    Tender = _tendersRepository.UpdateTender(tenderRequest.Tender)
                };
                log.DebugEx(tr => tr.Set("return", r));
                return(Request.CreateResponse(HttpStatusCode.OK, r));
            }
            catch (InvalidRequestException ex)
            {
                log.ErrorEx(tr => tr.Set("InvalidRequestException in PUT ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
            catch (ResourceNotFoundException ex)
            {
                log.ErrorEx(tr => tr.Set("ResourceNotFoundException in PUT ~/api/tenders.", ex));
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
        }
Exemple #13
0
        async Task HandleRequest(POSAPIMsg message, Func <PATResponse> func)
        {
            _logger.Log($"Processing {message.Header.RequestType} {message.Header.RequestMethod}");

            // Get content
            ResponseCode rc      = ResponseCode.Ok;
            string       content = null;

            try
            {
                PATResponse patResponse = func.Invoke();
                content = JsonConvert.SerializeObject(patResponse);
            }
            catch (InvalidRequestException ex)
            {
                _logger.Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.BadRequest;
            }
            catch (ResourceNotFoundException ex)
            {
                _logger.Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.NotFound;
            }
            catch (Exception ex)
            {
                _logger.Log(ex.Message, LogType.ERROR);
                rc = ResponseCode.ServerError;
            }

            // Build response
            POSAPIMsg resp = new POSAPIMsg()
            {
                Header = new POSAPIMsgHeader()
                {
                    RequestMethod = message.Header.RequestMethod,
                    RequestType   = message.Header.RequestType,
                    ResponseCode  = content != null ? ResponseCode.Ok : rc,
                    ContentLength = content?.Length ?? 0
                },
                Content = content
            };

            var respString = resp.ToString();

            _logger.Log(new LogData(respString, $"TX (Building request): {respString}"));

            try
            {
                // build eft request
                EFTPayAtTableRequest eftRequest = new EFTPayAtTableRequest();
                eftRequest.Header  = resp.Header.ToString();
                eftRequest.Content = resp.Content;

                await SendRequest(eftRequest);
            }
            catch (Exception ex)
            {
                _logger.Log(ex.Message, LogType.ERROR);
                throw;
            }
        }