public async Task <IActionResult> ProcessPayment(PaymentCreateDto paymentRequest) { try { if (ModelState.IsValid) { var paymentState = await _paymentRequestRepo.MakePayment(paymentRequest); var paymentResponse = new PaymentDto() { IsProcessed = paymentState.PaymentState == PaymentStateEnum.Processed, PaymentState = paymentState }; if (!paymentResponse.IsProcessed) { return(StatusCode(500, new { error = "Payment could not be processed" })); } return(StatusCode(201, new { paymentResponse })); //return Ok(paymentResponse); } else { return(BadRequest(ModelState)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(StatusCode(500)); } }
public PaymentStateDto ProcessPayment(PaymentCreateDto paymentRequest) { Random rnd = new Random(); var num = rnd.Next(1, 12); if (num % 4 == 0 || num % 6 == 0) { throw new Exception("Call failed"); } if (num % 2 == 0) { return new PaymentStateDto() { PaymentState = PaymentStateEnum.Failed, PaymentStateDate = DateTime.Now } } ; else { return new PaymentStateDto() { PaymentState = PaymentStateEnum.Processed, PaymentStateDate = DateTime.Now } }; } }
public void CreatePyment_ValidValuesPassed_ReturnsCreatedAtRouteResult() { PaymentCreateDto payment = new PaymentCreateDto() { ClientId = 100, category = "ELECTRICITY", period = 202102 }; var response = controller.CreatePayment(payment); Assert.IsType <CreatedAtRouteResult>(response.Result); }
public void CreatePyment_PaidPeriodPassed_ReturnsBadRequestResult() { PaymentCreateDto payment = new PaymentCreateDto() { ClientId = 100, category = "WATER", period = 202101 }; var response = controller.CreatePayment(payment); Assert.IsType <BadRequestResult>(response.Result); }
public void CreatePyment_InvalidServiceCategoryPassed_ReturnsBadRequestResult() { PaymentCreateDto payment = new PaymentCreateDto() { ClientId = 100, category = "PHONE", period = 202002 }; var response = controller.CreatePayment(payment); Assert.IsType <BadRequestResult>(response.Result); }
public static Payment ToEntity(this PaymentCreateDto source) { return(new Payment { CreateDate = source.CreateDate, FullName = source.FullName, PhoneNumber = source.PhoneNumber, Price = source.Price, Type = source.Type }); }
public async Task Test_PaymentRequestRepository_ProcessPayment(PaymentCreateDto paymentRequestDto, PaymentStateDto cheapGatewayResponseDto, int timesCheapGatewayCalled, PaymentStateDto expensiveGatewayResponseDto, int timesExpensiveGatewayCalled, PaymentStateEnum expectedPaymentStateEnum) { //arrange _cheapPaymentGateway.Setup(s => s.ProcessPayment(paymentRequestDto)).Returns(cheapGatewayResponseDto); _expensivePaymentGateway.Setup(s => s.ProcessPayment(paymentRequestDto)).Returns(expensiveGatewayResponseDto); //act var paymentStateDto = await _paymentRequestRepository.MakePayment(paymentRequestDto); //assert Assert.IsNotNull(paymentStateDto); Assert.AreEqual(paymentStateDto.PaymentState, expectedPaymentStateEnum); _cheapPaymentGateway.Verify(s => s.ProcessPayment(paymentRequestDto), Times.Exactly(timesCheapGatewayCalled)); _expensivePaymentGateway.Verify(s => s.ProcessPayment(paymentRequestDto), Times.Exactly(timesExpensiveGatewayCalled)); }
public ActionResult <PaymentReadDto> CreatePayment(PaymentCreateDto paymentCreateDto) { var serviceEntity = repository.GetServiceByShortname(paymentCreateDto.category); if (serviceEntity == null) { return(BadRequest()); } var clientEntity = repository.GetClientById(paymentCreateDto.ClientId); if (clientEntity == null) { return(BadRequest()); } int period = paymentCreateDto.period; var regex = @"^\d{4}(0[1-9]|1[0-2])$"; var match = Regex.Match(period.ToString(), regex, RegexOptions.IgnoreCase); if (!match.Success) { return(BadRequest()); } var billEntity = repository.GetBillByClientServiceAndPeriod(clientEntity, serviceEntity, period); if (billEntity == null || billEntity.Status == BillingStatus.Paid) { return(BadRequest()); } Payment payment = new Payment(); billEntity.Status = BillingStatus.Paid; payment.Bill = billEntity; repository.CreatePayment(payment); repository.SaveChanges(); var paymentReadDto = mapper.Map <PaymentReadDto>(payment); return(CreatedAtRoute(nameof(GetPaymentById), new { Id = paymentReadDto.Id }, paymentReadDto)); }
public ServiceResult <int> CreatePayment(PaymentCreateDto dto) { var serviceResult = new ServiceResult <int>(true); #region validation if (!string.IsNullOrEmpty(dto.FullName) && dto.FullName.Length > 300) { serviceResult.AddError("نام و نام خانوداگی نمی تواند بیش از 300 کارکتر را شامل شود".ToPersianNumbers()); } if (!string.IsNullOrEmpty(dto.PhoneNumber) && dto.PhoneNumber.Length > 100) { serviceResult.AddError("شماره همراه نمی تواتد بیش از 100 کارکتر را شامل شود".ToPersianNumbers()); } if (!string.IsNullOrEmpty(dto.PhoneNumber) && !dto.PhoneNumber.IsValidIranianMobileNumber()) { serviceResult.AddError("ساختار شماره همراه وارد شده معتبر نمی باشد"); } if (dto.Price < 1000) { serviceResult.AddError("مبلغ وارد شده نباید کمتر از 1000 تومان باشد".ToPersianNumbers()); } #endregion if (serviceResult.IsSuccess) { var entity = dto.ToEntity(); _context.Payments.Add(entity); if (_context.SaveChanges() == 0) { serviceResult.AddError("در انجام عملیات خطایی رخ داد مجددا تلاش نمایید"); } else { serviceResult.Data = entity.Id; } } return(serviceResult); }
public async Task <PaymentStateDto> MakePayment(PaymentCreateDto paymentCreateDto) { var paymentEntity = _mapper.Map <PaymentCreateDto, Payment>(paymentCreateDto); paymentEntity = await _paymentRepository.CreatePaymentRecord(paymentEntity); var paymentStateEntity = new PaymentState() { Payment = paymentEntity, PaymentId = paymentEntity.PaymentId, DateCreated = DateTime.Now, State = PaymentStateEnum.Pending.ToString() }; paymentStateEntity = await _paymentStateRepository.CreatePaymentRecord(paymentStateEntity); //implement payment gateway processing and make records persistent on DB if (paymentCreateDto.Amount <= 20) { var paymentStateDto = await ProcessPaymentStateDto(_cheapPaymentGateway, paymentCreateDto, paymentEntity); return(paymentStateDto); } else if (paymentCreateDto.Amount > 20 && paymentCreateDto.Amount <= 500) { PaymentStateDto paymentStateDto = new PaymentStateDto() { PaymentState = PaymentStateEnum.Failed, PaymentStateDate = DateTime.Now }; int tryCount = 0; try { paymentStateDto = await ProcessPaymentStateDto(_expensivePaymentGateway, paymentCreateDto, paymentEntity); if (paymentStateDto != null && paymentStateDto.PaymentState == PaymentStateEnum.Processed) { return(paymentStateDto); } else { tryCount++; paymentStateDto = await ProcessPaymentStateDto(_cheapPaymentGateway, paymentCreateDto, paymentEntity); return(paymentStateDto); } } catch (Exception ex) { if (tryCount == 0) { paymentStateDto = await ProcessPaymentStateDto(_cheapPaymentGateway, paymentCreateDto, paymentEntity); return(paymentStateDto); } } return(paymentStateDto); } else { int tryCount = 0; PaymentStateDto paymentStateDto = new PaymentStateDto() { PaymentState = PaymentStateEnum.Failed, PaymentStateDate = DateTime.Now };; while (tryCount < 3) { try { paymentStateDto = await ProcessPaymentStateDto(_expensivePaymentGateway, paymentCreateDto, paymentEntity); if (paymentStateDto != null && paymentStateDto.PaymentState == PaymentStateEnum.Processed) { return(paymentStateDto); } } catch (Exception ex) { //_logger.LogError(ex, ex.Message); } finally { tryCount++; } } return(paymentStateDto); } throw new Exception("Payment could not be processed"); }
private async Task <PaymentStateDto> ProcessPaymentStateDto(IPaymentGateway paymentGateway, PaymentCreateDto paymentCreateDto, Payment paymentEntity) { var paymentStateDto = paymentGateway.ProcessPayment(paymentCreateDto); var paymentStateEntityProcessed = new PaymentState() { Payment = paymentEntity, PaymentId = paymentEntity.PaymentId, DateCreated = paymentStateDto.PaymentStateDate, State = paymentStateDto.PaymentState.ToString() }; paymentStateEntityProcessed = await _paymentStateRepository.CreatePaymentRecord(paymentStateEntityProcessed); return(paymentStateDto); }