Exemplo n.º 1
0
        public async Task <int> SubmitPayment(PaymentRequestModel requestModel)
        {
            if (!_currentUser.IsInRole("merchant"))
            {
                throw new UnauthorizedResourceAccessException("Only merchants can make payments");
            }

            string idString = _currentUser.Claims
                              .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;

            requestModel.MerchantId = GetCurrentUserId() ?? 0;

            _validator.ValidateForServices(requestModel);

            var payment = _mapper.Map <Payment>(requestModel);

            payment.Submitted  = DateTime.Now;
            payment.Status     = PaymentStatus.NotSubmitted;
            payment.CardNumber = $"************{payment.CardNumber.Substring(12)}";
            await _repository.AddPayment(payment);

            await _unitOfWork.Commit();

            var command = _mapper.Map <SubmitPaymentCommand>(payment);
            await _bus.Publish(command);

            payment.Status = PaymentStatus.Submitted;
            await _unitOfWork.Commit();

            return(payment.Id);
        }