private async Task ValidatePaymentHistoryAddingAsync(AddPaymentHistoryDto paymentHistoryDto, CancellationToken ct)
        {
            var paymentMethodDto = await _unitOfWork.PaymentMethodRepository.GetAsync(paymentHistoryDto.PaymentMethodId, ct);

            if (paymentMethodDto == null)
            {
                _logger.LogInformation("Payment method with id {PaymentMethodId} not found", paymentHistoryDto.PaymentMethodId);
                throw new ItemNotFoundException();
            }

            if (paymentMethodDto.AccountId != paymentHistoryDto.AccountId)
            {
                _logger.LogInformation("Access to this payment method is not allowed for account {AccountId}", paymentHistoryDto.AccountId);
                throw new AccessToItemForbiddenException();
            }

            var isAlreadyPaid =
                await _unitOfWork.PaymentHistoryRepository.IsExistsAsync(paymentMethodDto.AccountId, paymentHistoryDto.OrderId, ct);

            if (isAlreadyPaid)
            {
                _logger.LogInformation("Order with Id {OrderId} already paid", paymentHistoryDto.OrderId);
                throw new OrderAlreadyPaidException();
            }

            if (paymentMethodDto.Cvv != StringEncryptionHelper.Encrypt(paymentHistoryDto.Cvv))
            {
                _logger.LogInformation("Cvv code for payment method {PaymentMethodId} is wrong", paymentHistoryDto.PaymentMethodId);
                throw new CvvIsWrongException();
            }
        }
        public async Task <GetPaymentHistoryDto> AddAsync(AddPaymentHistoryDto addPaymentHistoryDto, CancellationToken ct = default)
        {
            _logger.LogInformation(
                "Pay order with {PaymentMethodId} for account {AccountId}",
                addPaymentHistoryDto.PaymentMethodId,
                addPaymentHistoryDto.AccountId);

            await ValidatePaymentHistoryAddingAsync(addPaymentHistoryDto, ct);

            var paymentHistory = PaymentHistory.Create(
                addPaymentHistoryDto.AccountId,
                addPaymentHistoryDto.OrderId,
                addPaymentHistoryDto.PaymentMethodId,
                addPaymentHistoryDto.Amount);

            _unitOfWork.PaymentHistoryRepository.Create(paymentHistory);

            await _unitOfWork.SaveAsync(ct);

            return(_mapper.Map <GetPaymentHistoryDto>(paymentHistory));
        }
示例#3
0
        protected override async Task <GetPaymentHistoryDto> ExecuteAsync(CreatePaymentHistoryCommand request, CancellationToken ct)
        {
            AddPaymentHistoryDto paymentHistoryDto = _mapper.Map <AddPaymentHistoryDto>(request);

            return(await _paymentHistoryService.AddAsync(paymentHistoryDto, ct));
        }