Esempio n. 1
0
        public async Task <BankResponseModel> MakeBankPayment(PaymentBindingModels Payment)
        {
            //build final url
            String url = baseUrl + "api/Bank";

            using (HttpResponseMessage response = await Api.ApiConnector.PostAsJsonAsync(url, Payment))
            {
                BankResponseModel objBankResponse = await response.Content.ReadAsAsync <BankResponseModel>();

                using (var scope = DI.DI.Container.BeginLifetimeScope())
                {
                    var obj = scope.Resolve <ILoggerInterface>();
                    //Logs API call response status
                    string msg = String.Format("STATUS: {0} | URL: {1} | MSG: {2}", objBankResponse.Status, url, objBankResponse.Message);
                    obj.LogMessage("BankApi.MakeBankPayment", msg);
                }

                return(objBankResponse);
            }
        }
        // POST: Make a payment
        public async Task <HttpResponseMessage> Post([FromBody] PaymentBindingModels Payment)
        {
            try
            {
                //In case POST send with no data
                if (Payment == null)
                {
                    throw new Exception("No data provided");
                }

                if (!ModelState.IsValid)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }


                using (ApplicationDbContext entities = new ApplicationDbContext())
                {
                    //Retrieve Authenticated User
                    var AuthenticatedUser = entities.Users.FirstOrDefault(x => x.Email == User.Identity.Name);

                    if (AuthenticatedUser == null)
                    {
                        throw new Exception("Unable to retrieve authenticated user");
                    }


                    //Request Bank Payment
                    BankApi           objBank     = new BankApi();
                    BankResponseModel objresponse = await objBank.MakeBankPayment(Payment);


                    PaymentModel PayModel = new PaymentModel()
                    {
                        CardHolderName = Payment.CardHolderName,
                        CardNumber     = Payment.GetMaskCardNumber(),
                        Amount         = Payment.Amount,
                        Currency       = Payment.Currency.ToUpper(),
                        AuthUser       = AuthenticatedUser,
                        CardExpDate    = Payment.CardExpDate,
                        Cvv            = Payment.Cvv,
                        RequestDate    = DateTime.Now,
                        BankIdentifer  = objresponse.Identifier,
                        BankStatus     = objresponse.Status
                    };

                    //Save Payment
                    entities.Payments.Add(PayModel);
                    entities.SaveChanges();

                    if (objresponse.Status != "OK")
                    {
                        throw new Exception("Error processing payment with bank");
                    }

                    var objPayResponse = new
                    {
                        status = "OK",
                        PaymentBankIdentifer = PayModel.BankIdentifer,
                        GetPayment           = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/api/Payment/" + PayModel.Id
                    };

                    var messsage = Request.CreateResponse(HttpStatusCode.Created, objPayResponse);

                    return(messsage);
                }
            }
            catch (Exception ex)
            {
                using (var scope = DI.DI.Container.BeginLifetimeScope())
                {
                    var obj = scope.Resolve <ILoggerInterface>();
                    //Logger error Message
                    obj.LogMessage("PaymentsController.Post", ex.Message);
                }

                ErrorResponseModel objResponse = new ErrorResponseModel()
                {
                    Status = "ERROR", Message = ex.Message
                };
                return(Request.CreateResponse(HttpStatusCode.BadRequest, objResponse));
            }
        }