Example #1
0
        private StringContent SerializeContent(ApiRequestBody body)
        {
            var serialized = JsonConvert.SerializeObject(body);
            var content    = new StringContent(serialized, Encoding.UTF8, "application/json");

            return(content);
        }
Example #2
0
        public async Task UpdateStatistics(UserProfile profile)
        {
            var body = new ApiRequestBody
            {
                Action = "update_stats",
                User   = profile
            };

            var response = await GetResponse(body);

            if (!response.Success)
            {
                throw new KFlearningException(response.Reason);
            }
        }
Example #3
0
        protected async Task <ApiResponseBody> GetResponse(ApiRequestBody body)
        {
            using (var message = new HttpRequestMessage(HttpMethod.Post, BaseUri))
            {
                message.Headers.Add("KF-Authorization",
                                    _authorizationService.GenerateAuthorization(body.User.Username));
                message.Content = SerializeContent(body);

                var result = await Client.SendAsync(message);

                var res = await result.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <ApiResponseBody>(res));
            }
        }
Example #4
0
        public async Task <UserProfile> GetStatistics(string username)
        {
            var body = new ApiRequestBody
            {
                Action = "get_stats",
                User   = new UserProfile
                {
                    Username = username
                }
            };

            var response = await GetResponse(body);

            if (!response.Success)
            {
                throw new KFlearningException(response.Reason);
            }
            return(response.User);
        }
Example #5
0
        public async Task <string> GetUsername(string token)
        {
            var body = new ApiRequestBody
            {
                Action = "get_username",
                User   = new UserProfile
                {
                    Username = token
                }
            };

            var response = await GetResponse(body);

            if (!response.Success)
            {
                throw new KFlearningException(response.Reason);
            }
            return(response.User.Username);
        }
Example #6
0
        public IActionResult Payment(PaymentModel model)
        {
            if (ModelState.IsValid)
            {
                ApiRequestBody reqestBody = BuildRequestBody(model);
                ErrorViewModel errorModel = new ErrorViewModel();

                ResponseDetails response = _apiHelper.SendPaymentApiRequest(reqestBody);

                if (response.Success)
                {
                    ApiResponseBody responseBody = JsonConvert.DeserializeObject <ApiResponseBody> (response.Data.ToString());

                    if (responseBody.Messages.ResultCode.ToLower() == "ok")
                    {
                        if (responseBody.TransactionResponse.ResponseCode == "1")
                        {
                            int newId = Convert.ToInt32(responseBody.RefId);

                            response = _apiHelper.SendApiRequest("", "booking/confirm-booking/" + newId, HttpMethod.Post);

                            if (response.Success)
                            {
                                return(RedirectToAction("Receipt", new { id = _dataProtector.Protect(newId) }));
                            }
                            else
                            {
                                errorModel.Message = response.Data.ToString();
                            }
                        }
                        else
                        {
                            string error = responseBody.TransactionResponse.Errors.First().ErrorText;
                            if (string.IsNullOrEmpty(error))
                            {
                                errorModel.Message = "Transaction failed.";
                            }
                            else
                            {
                                errorModel.Message = error;
                            }
                        }
                    }
                    else
                    {
                        errorModel.Message = responseBody.Messages.Message.First().Text;
                    }
                }
                else
                {
                    errorModel.Message = response.Data.ToString();
                }

                return(View("Error", errorModel));
            }
            else
            {
                ModelState.AddModelError("", "Validation Failed.");
                return(RedirectToAction("Payment", new { id = _dataProtector.Protect(model.Booking.Id) }));
            }
        }