public string GetPaymentProcessingStatus(ProcessPaymentRequest processPaymentRequest, CacheDetails cacheDetails) { string result = ""; if (processPaymentRequest.Amount < 20) { result = _cheapPaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount); return(result); } else if (processPaymentRequest.Amount > 20 && processPaymentRequest.Amount < 500) { if (cacheDetails.ExpensiveGatewayAvailability) // if expensive payment gateway is available use it else switch to cheap gateway { result = _expensivePaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount); return(result); } else { result = _cheapPaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount); return(result); } } int i = 0; while (i < Constants.PremiumCount && result != Constants.Processed) //retry three times if transaction not successful { result = _premiumPaymentService.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount); } return(result); }
public bool ProcessPaymentLessThan21(PaymentDetail paymentDetail, int transactionType = 0) { var referenceNumber = Utility.Utility.GenerateReferenceNumber(); paymentDetail.PaymentReference = referenceNumber; paymentDetail.PaymentLogs = new List <PaymentLog> { new PaymentLog { Amount = paymentDetail.Amount, PaymentDate = DateTime.Now, PaymentState = PaymentState.Pending, PaymentGateway = PaymentGateway.CheapGateway, PaymentReference = referenceNumber } }; detailRepository.Add(paymentDetail); detailRepository.Save(); var gatewayResult = cheapPaymentGateway.ProcessPayment(paymentDetail, transactionType); var result = paymentLogRepository.GetPaymentLogByFilter(c => c.PaymentReference == referenceNumber);//.FirstOrDefault(); if (gatewayResult.Code == ResponseCodes.OK) { result.PaymentState = PaymentState.Processed; paymentLogRepository.Update(result); paymentLogRepository.Save(); return(true); } else { result.PaymentState = PaymentState.Failed; paymentLogRepository.Update(result); paymentLogRepository.Save(); return(false); } }
public async Task <string> ProcessPayment(PaymentRequest paymentRequest) { try { if (paymentRequest.Amount <= 20) { string response = await _cheapPayment.ProcessPayment(paymentRequest); return(response); } if (paymentRequest.Amount >= 20 && paymentRequest.Amount <= 500) { if (await _expensiveRepo.CheckIfPaymentGetwayExist(paymentRequest)) { string response = await _expensiveRepo.ProcessPayment(paymentRequest); } else { string response = await _cheapPayment.ProcessPayment(paymentRequest); return(response); } } bool result = false; if (paymentRequest.Amount >= 500) // PremiumPaymentService { result = await _premiumRepo.ProcessPayment(paymentRequest); if (!result) { for (int i = 1; i < 3; i++) { result = await _premiumRepo.ProcessPayment(paymentRequest); if (result) { break; } } } } if (result) { return("Success"); } else { return("Faild"); } } catch (Exception) { return("Exception"); } }
private void ProcessAmountGreaterThan20AndLessThanEqualTo500(int paymentId) { var results = RetryMechanism.RetryWithAlternate(() => _expensivePaymentGateway.ProcessPayment(paymentId), () => _cheapPaymentGateway.ProcessPayment(paymentId)); if (!results) { ProcessFailedPayment(paymentId); } }
public async Task <Payment> ProcessPaymentAsync(ProcessPaymentRequest processPaymentRequest) { var payment = await AddNewPayment(processPaymentRequest).ConfigureAwait(false); bool isPaymentSuceed; if (processPaymentRequest.Amount <= 20) { var paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber, processPaymentRequest.CardHolder, processPaymentRequest.ExpirationDate, processPaymentRequest.SecurityCode, processPaymentRequest.Amount); isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1); } else if (processPaymentRequest.Amount <= 500) { var paymentTask = _ExpensivePaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber, processPaymentRequest.CardHolder, processPaymentRequest.ExpirationDate, processPaymentRequest.SecurityCode, processPaymentRequest.Amount); isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1); if (!isPaymentSuceed) { paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber, processPaymentRequest.CardHolder, processPaymentRequest.ExpirationDate, processPaymentRequest.SecurityCode, processPaymentRequest.Amount); isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1); } } else { var paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber, processPaymentRequest.CardHolder, processPaymentRequest.ExpirationDate, processPaymentRequest.SecurityCode, processPaymentRequest.Amount); isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 3); } payment.Status = isPaymentSuceed ? "processed" : "failed"; await UpdatePayment(payment); return(payment); }
public ResponseModel ProcessPayment(PaymentModel request) { try { request.CreditCardNumber = request.CreditCardNumber.Replace(" ", "").Trim(); if (request.Amount < appSettings.Value.CheapAmount) { //call cheap service return(_cheapPaymentGateway.ProcessPayment(request)); } if (request.Amount >= appSettings.Value.ExpensiveAmountMin && request.Amount <= appSettings.Value.ExpensiveAmountMax) { //call expensive service return(_expensivePaymentGateway.ProcessExpensivePayment(request)); } if (request.Amount > appSettings.Value.PremiumAmount) { //premium service. return(_premiumPaymentService.ProcessPremiumPayment(request)); } return(ResponseDictionary.ProvideResponse("04")); } catch (Exception ex) { Trace.TraceInformation($"An error occurred, {ex.Message}"); return(ResponseDictionary.ProvideResponse("06")); } }
public IActionResult ProcessPayment(PaymentDto paymentDto) { int count = 0; if (!ModelState.IsValid) { return(BadRequest()); } if (paymentDto.Amount <= 20) { _cheapPaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto)); } else if (paymentDto.Amount > 20 && paymentDto.Amount < 501) { if (!_expensivePaymentGateway.isAvailable) { do { _cheapPaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto)); count++; } while (count < 1); } else { _expensivePaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto)); } } else if (paymentDto.Amount > 500) { do { bool res = _premiumPaymentService.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto)); if (!res) { count++; } else { break; } } while (count < 3); } return(Ok()); }
public async Task <PaymentServiceResponse> MakePayment(PaymentViewModel payment) { var gatewayResult = new PaymentGatewayResponse(); if (payment.Amount <= 20) { gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount); } else if (payment.Amount <= 500) { gatewayResult = await _expensivePaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount); if (gatewayResult.StatusCode != 200) { gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount); } } else if (payment.Amount > 500) { int numberOfRetries = 3; int i = 0; while (i < numberOfRetries) { gatewayResult = await _premiumPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount); if (gatewayResult.StatusCode == 200) { break; } i++; } } if (gatewayResult.StatusCode == 200) { SavePayment(payment, gatewayResult.PaymentStatus); } var result = new PaymentServiceResponse { StatusCode = gatewayResult.StatusCode, Message = gatewayResult.Message }; return(result); }
public void ProcessPayment(Payment p) { //If upto 20 euros then use Cheap Payment Gateway if (p.Amount <= 20) { var result = _cheapPaymentGateway.ProcessPayment(); SavePaymentDetails(result, p); } else if (p.Amount > 20 && p.Amount <= 500) { //Check if Expensive Gateway is Available if (_expensivePaymentGateway.IsAvailable()) { var result = _expensivePaymentGateway.ProcessPayment(); SavePaymentDetails(result, p); } else { var result = _cheapPaymentGateway.ProcessPayment(); SavePaymentDetails(result, p); } } else if (p.Amount > 500) { //This will be retried 3 times _retryPolicy.Execute(() => { var result = _premiumPaymentGateway.ProcessPayment(); if (result == 0) { //Save Failed Status SavePaymentDetails(result, p); //Throw Payment Failed Exception for Polly Retry throw new Exception("Premium Payment Failed"); } SavePaymentDetails(result, p); return(null); }); } }
public bool ProcessPayment(PaymentRequest model) { bool paymentStatus = false; if (model.Amount >= 0 && model.Amount <= 20) { paymentStatus = _cheapPaymentManager.ProcessPayment(model); } if (model.Amount >= 21 && model.Amount <= 500) { if (IsIExpensivePaymentGatewayAvailable()) { paymentStatus = _expensivePaymentManager.ProcessPayment(model); } else { paymentStatus = _cheapPaymentManager.ProcessPayment(model); } } if (model.Amount > 500) { int i = 0; do { if (IsPaymentProcessed()) { paymentStatus = _premiumPaymentManager.ProcessPayment(model); break; } i++; }while (i < 3); } return(paymentStatus); }
public PaymentResponse ProcessPayment(Payment payment) { PaymentResponse response = null; IPaymentGateway gateway; if (payment.Amount < 20) { gateway = new ICheapPaymentGateway(); response = gateway.ProcessPayment(payment); } else if (payment.Amount > 20 && payment.Amount <= 500) { gateway = new IExpensivePaymentGateway(); response = gateway.ProcessPayment(payment); if (!response.Success) { gateway = new ICheapPaymentGateway(); response = gateway.ProcessPayment(payment); } } else { gateway = new IPremiumPaymentGateway(); var retries = 3; while (retries > 0) { response = gateway.ProcessPayment(payment); if (response.Success) { return(response); } retries--; } response.ErrorMessage += " Retried: 3 times."; } return(response); }
public async Task <dynamic> ProcessPayment(RequestDto request) { if (request == null) { return(BadRequest(ModelState)); } if (!ValidateCreditCard(request.CreditCardNumber)) { ModelState.AddModelError("", "Credit Card Number is invalid!"); return(StatusCode(404, ModelState)); } if (!ValidateExpirationDate(request.ExpirationDate)) { ModelState.AddModelError("", "Card expired!"); return(StatusCode(404, ModelState)); } if (request.SecurityCode.Length > 0 && !ValidateSecurityCode(request.SecurityCode)) { ModelState.AddModelError("", "Security code is invalid!"); return(StatusCode(404, ModelState)); } if (!ValidateAmount(request.Amount)) { ModelState.AddModelError("", "Amount can't be negative!"); return(StatusCode(404, ModelState)); } var requestObj = _mapper.Map <Request>(request); var addRequest = _expensivePaymentGateway.AddRequest(requestObj); dynamic process; if (request.Amount <= 20) { process = await _cheapPaymentGateway.ProcessPayment(requestObj); } else if (request.Amount > 20 && request.Amount <= 500) { process = await _expensivePaymentGateway.ProcessPayment(requestObj); } else { process = await _premiumPaymentService.ProcessPayment(requestObj); } var state = new PaymentStateDto { RequestId = addRequest.Id, Status = process }; var stateObj = _mapper.Map <PaymentState>(state); _expensivePaymentGateway.AddPaymentState(stateObj); return(process); }
public async Task <IActionResult> ProcessPayment([FromBody] PaymentDto paymentDto) { //return await NoContent(); paymentDto.CreditCardNumber = Regex.Replace(paymentDto.CreditCardNumber, @"[^\d]", ""); var validator = new PaymentValidator(); var validate = await validator.ValidateAsync(paymentDto); if (!validate.IsValid) { var errors = validator.GetErrorList(validate.Errors); return(BadRequest(new { message = "Bad request", errors })); } AppPayment payment; try { payment = _mapper.Map <AppPayment>(paymentDto); if (paymentDto.Amount < 20) { await _cheapPaymentGateway.ProcessPayment(payment); } else if (paymentDto.Amount >= 20 && paymentDto.Amount <= 500) { try { await _expensivePaymentGateway.ProcessPayment(payment); } catch (Exception) { await _cheapPaymentGateway.ProcessPayment(payment); } } else { try { await _premiumPaymentService.ProcessPayment(payment); } catch (Exception) { int i = 1; do { var response = await _cheapPaymentGateway.ProcessPayment(payment); if (response) { break; } i++; }while (i < 4); } } } catch (Exception e) { _logger.LogError(e.Message); return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" })); } try { var result = await _unitOfWork.appPaymentStatusRepository.GetPaymentStatusById(payment.Id); return(Ok(new { message = "Success", data = result })); } catch (Exception e) { _logger.LogError(e.Message); return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" })); } }
public async Task <IActionResult> ProcessPayment([FromBody] PaymentToCreateDTO model) { model.CreditCardNumber = Regex.Replace(model.CreditCardNumber, @"[^\d]", ""); var validator = new PaymentValidator(); var validate = await validator.ValidateAsync(model); if (!validate.IsValid) { var errors = validator.GetErrorMessage(validate.Errors); return(BadRequest(new { message = "Bad request", errors })); } Payment payment; try { payment = _mapper.Map <Payment>(model); if (model.Amount < 21) { await _cheapPayment.ProcessPayment(payment); } else if (model.Amount < 501) { try { await _expensivePayment.ProcessPayment(payment); } catch (Exception) { await _cheapPayment.ProcessPayment(payment); } } else { try { await _premiumPayment.ProcessPayment(payment); } catch (Exception) { int i = 1; do { var response = await _cheapPayment.ProcessPayment(payment); if (response) { break; } i++; }while (i < 4); } } } catch (Exception e) { _logger.LogError(e.Message); return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" })); } try { var result = await _transactionRepository.GetByPaymentId(payment.Id); return(Ok(new { message = "Success", data = result })); } catch (Exception e) { _logger.LogError(e.Message); return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" })); } }
public async Task <bool> ProcessPayment(PaymentRequest paymentRequest) { var result = await _cheapPaymentGateway.ProcessPayment(paymentRequest); return(result); }
public async Task <IActionResult> ProcessPayment(PaymentDto payment) { if (!ModelState.IsValid) { return(BadRequest(new { message = "Bad Request", errors = "Invalid credentials provided" })); } //Strip any non-numeric values payment.CreditCardNumber = Regex.Replace(payment.CreditCardNumber, @"[^\d]", ""); if (Utilities.IsModelValid(payment)) { var model = new Payment(); try { model = _mapper.Map <Payment>(payment); if (model.Amount < 21) { await _cheapPayment.ProcessPayment(model); } else if (model.Amount < 501) { try { await _expensivePayment.ProcessPayment(model); } catch (Exception) { await _cheapPayment.ProcessPayment(model); } } else { try { await _premiumPayment.ProcessPayment(model); } catch (Exception) { int i = 1; do { var response = await _cheapPayment.ProcessPayment(model); if (response) { break; } i++; }while (i < 4); } } } catch (Exception e) { _logger.LogError(e.Message); } try { var result = await _transactionRepository.GetById(model.Id); return(Ok(result)); } catch (Exception e) { _logger.LogError(e.Message); } } return(StatusCode(500, "internal error")); }
public int PaymentProcessBusinessLogic(CardInformationDTO cardModel) { try { PaymentStatusDTO payment = new PaymentStatusDTO(); var cardTable = _mapper.Map <CardInformation>(cardModel); var paymentTable = _mapper.Map <PaymentStatus>(payment); if (Common.isValid((long.Parse(cardTable.CreditCardNumber)))) { if (cardTable.ExpirationDate.Year >= DateTime.Now.Year && cardTable.ExpirationDate.Month >= DateTime.Now.Month) { Task <int> finishedTask = null; if (cardModel.Amount <= 20) { Task <int> finishedTask1 = Task.Run(() => _cheapPaymentGateway.ProcessPayment(cardTable)); if (!finishedTask1.IsCompleted) { int result = finishedTask1.Result; paymentTable.Status = "Processed"; Save(cardTable, paymentTable); return(200); } } else if (cardModel.Amount >= 21 && cardModel.Amount <= 500) { finishedTask = Task.Run(() => _expensivePaymentGateway.ProcessPayment(cardTable)); if (!finishedTask.IsCompleted) { Task <int> finishedTask2 = Task.Run(() => _cheapPaymentGateway.ProcessPayment(cardTable)); int result = finishedTask2.Result; paymentTable.Status = "Pending"; Save(cardTable, paymentTable); return(400); } else { paymentTable.Status = "Processed"; Save(cardTable, paymentTable); return(200); } } else if (cardModel.Amount > 500) { var attempts = 3; do { try { attempts++; finishedTask = Task.Run(() => _preminumServiceGateway.ProcessPayment(cardTable)); if (finishedTask.IsCompleted) { paymentTable.Status = "Processed"; Save(cardTable, paymentTable); break; } } catch (Exception ex) { paymentTable.Status = "Failed"; Save(cardTable, paymentTable); if (attempts == 3) { throw; } return(500); } } while (true); return(200); } } else { paymentTable.Status = "Pending"; Save(cardTable, paymentTable); return(400); } } else { paymentTable.Status = "Pending"; Save(cardTable, paymentTable); return(400); } return(200); } catch (Exception) { return(500); } }