private async Task <int> InsertPaymentAndShopperAsync(PurchaseRequestDto request, Payment payment) { string cardNumber = request.CardNumber.Encrypt(_encryptionKey); string cvv = request.Cvv.Encrypt(_encryptionKey); var shopper = await _dbContext.Shoppers .Include(x => x.Payments) .SingleOrDefaultAsync(x => x.CardNumber == cardNumber && x.Cvv == cvv); if (shopper is null) { shopper = new Shopper() { CardNumber = cardNumber, Cvv = cvv, FirstName = request.FirstName, LastName = request.LastName, ExpireMonth = request.ExpireMonth, ExpireYear = request.ExpireYear }; _logger.LogInformation($"New shopper created: {request.FirstName} {request.LastName}"); } payment.Shopper = shopper; _dbContext.Payments.Add(payment); return(await _dbContext.SaveChangesAsync()); }
public async Task <PurchaseResultDto> ProcessPaymentAsync(PurchaseRequestDto purchaseRequestDto) { Random rnd = new(); bool isSuccessful = rnd.NextDouble() >= 0.5; var status = isSuccessful ? PaymentStatus.Successful : PaymentStatus.Failed; var result = new PurchaseResultDto(Guid.NewGuid(), status); return(await Task.FromResult(result)); }
public async Task <ActionResult <PurchaseResultDto> > PurchaseProductAsync(PurchaseRequestDto buyProductDto) { var result = await _paymentService.PurchaseProductAsync(buyProductDto); if (result.PaymentStatus == PaymentStatus.Successful) { return(Ok(result)); } else { return(BadRequest(result)); } }
private async Task CreatePaymentAsync(PurchaseRequestDto request, PurchaseResultDto response) { var product = Constants.ProductPrices[request.Product]; var payment = new Payment { Id = response.Id, Amount = product.Price, Currency = product.Currency, PaymentStatus = response.PaymentStatus, CreatedDate = DateTime.UtcNow, MerchantId = product.MerchantId }; int total = await InsertPaymentAndShopperAsync(request, payment); _logger.LogInformation($"Number of entries written to db: {total}"); }
public async Task <PurchaseIdDto> Purchase([FromBody] PurchaseRequestDto req) { var prizeId = req.PrizeId; var userEmail = User.FindFirst(ClaimTypes.Email).Value; var prize = await _prizeManager.GetPrizeById(prizeId); var canBuy = await _purchaseLogic.CheckIfUserCanBuy(userEmail, prizeId); if (!canBuy) { throw new Exception(); //Don't know what kind of specific exception should be here } var id = _purchaseRepository.Create(userEmail, prizeId, prize.Price); return(new PurchaseIdDto { Id = id }); }
public async Task <PurchaseResultDto> PurchaseProductAsync(PurchaseRequestDto dto) { var isValid = CreditCard.Validate(dto.CardNumber); if (!isValid) { return(new PurchaseResultDto(Guid.Empty, PaymentStatus.Failed)); } // Validate if product exists if (!Enum.IsDefined(typeof(Product), dto.Product)) { return(new PurchaseResultDto(Guid.Empty, PaymentStatus.Failed)); } var response = await _bankService.ProcessPaymentAsync(dto); _logger.LogInformation($"Bank Payment Status : {response.PaymentStatus}"); await CreatePaymentAsync(dto, response); return(response); }
public PurchaseResponseDto PurchaseMovie(Guid id, [FromBody] PurchaseRequestDto purchaseRequestDto) { return(customersService.PurchaseMovie(id, purchaseRequestDto.MovieId)); }
public async Task <PurchaseResultDto> ProcessPaymentAsync(PurchaseRequestDto purchaseRequestDto) { var result = new PurchaseResultDto(TestConstants.TestPaymentId, PaymentStatus.Successful); return(await Task.FromResult(result)); }