Example #1
0
        public SimpleTextResponse ValidateTopupRequest(TopupRequestReq pRequest)
        {
            SimpleTextResponse response = new SimpleTextResponse();

            try
            {
                if (pRequest.amount <= 0)
                {
                    response.result  = false;
                    response.Message = "Amount must be greater than zero";
                }
                else if (pRequest.operatorID <= 0)
                {
                    response.result  = false;
                    response.Message = "Operator ID is not valid";
                }
                else if (String.IsNullOrEmpty(pRequest.targetPhoneNumber))
                {
                    response.result  = false;
                    response.Message = "Target phone numner is required";
                }
                else if (pRequest.vendorCode <= 0)
                {
                    response.result  = false;
                    response.Message = "Vendor code is not valid";
                }
                else
                {
                    response.result  = true;
                    response.Message = "Ok";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(response);
        }
        public async Task <HttpResponseMessage> requestTopup(HttpRequestMessage pRequest, [FromBody] TopupRequestReq data)
        {
            IEnumerable <string> authKey = null;

            pRequest.Headers.TryGetValues("authenticationKey", out authKey);

            if (authKey != null)
            {
                var consumerVerified = consumerBL.AuthenticateConsumer(authKey.FirstOrDefault());

                if (consumerVerified != null)
                {
                    if (consumerVerified.IsValidKey)
                    {
                        TopupInteractor interactor = new TopupInteractor();
                        var             validation = interactor.ValidateTopupRequest(data);

                        if (!validation.result)
                        {
                            response.HttpCode = 400;
                            response.Message  = validation.Message;
                            return(Request.CreateResponse <IResponse>(HttpStatusCode.BadRequest, response));
                        }
                        else
                        {
                            bool result = await topupBL.RequestTopup(consumerVerified.ConsumerID, consumerVerified.Nickname, data.targetPhoneNumber, data.vendorCode, data.amount, data.operatorID, data.CategoryID);

                            SimpleTextResponse textResponse = new SimpleTextResponse();
                            textResponse.result = result;

                            textResponse.Message = (result) ? "Succes" : "Request was made sucesfully, but notification failed";

                            return(Request.CreateResponse <IResponse>(HttpStatusCode.OK, textResponse));
                        }
                    }
                    else
                    {
                        response.HttpCode = 401;
                        response.Message  = "Authentication key is not valid";
                        return(Request.CreateResponse <IResponse>(HttpStatusCode.Unauthorized, response));
                    }
                }
                else
                {
                    response.HttpCode = 401;
                    response.Message  = "Invalid authentication key";
                    return(Request.CreateResponse <IResponse>(HttpStatusCode.Unauthorized, response));
                }
            }
            else
            {
                response.HttpCode = 401;
                response.Message  = "Authentication key is required";
                return(Request.CreateResponse <IResponse>(HttpStatusCode.Unauthorized, response));
            }
        }