public async Task<IActionResult> TokenizeCard([FromBody] TokenizeCardRequest request) { var merchantIdentity = await _merchantQuery.GetMerchantIdAsync(GetAuthorizationKey()); if (merchantIdentity.Equals(Guid.Empty)) return new UnauthorizedObjectResult(ExternalErrorReason.InvalidSecretKey); var innerCommand = new AddNewCardCommand(request.Name, request.Number, request.ExpiryMonth, request.ExpiryYear, request.Cvv, null, HttpContext.TraceIdentifier); var command = new IdentifiedCommand<AddNewCardCommand, Result>(innerCommand); var result = await _mediator.Send(command); if (result.IsSuccess) { var card = await _cardQuery.GetCardByCardNumberAsync(request.Number); return Ok(card); } return UnprocessableEntity(result.Error); }
public async Task <Result> Handle(AddNewCardCommand request, CancellationToken cancellationToken) { _logger.LogInformation("Sending card validation request for cardNumber : {cardNumber} with correlation id : {correlationId} to bank ", request.Number, request.CorrelationId); var bankResult = await _bankService.ValidateCard(request.Name, request.Number, request.ExpiryMonth, request.ExpiryYear, request.Cvv); var existingCard = await _cardQuery.GetCardByCardNumberAsync(request.Number); if (existingCard != null && existingCard.Cvv == request.Cvv) { return(Result.Ok()); } if (!bankResult.IsSuccess) { _logger.LogInformation("Card was flagged invalid by bank for cardNumber : {cardNumber} for correlationid : {correlationId}", request.Number, request.CorrelationId); return(Result.Fail <CreateCardResponse>(ExternalErrorReason.CardValidationFailedByBank)); } _logger.LogInformation("card validation request was successful for correlationid : {correlationId}", request.CorrelationId); var card = new Domain.Entities.Card(request.Number, request.ExpiryMonth, request.ExpiryYear, request.Name, request.Cvv); _cardRepository.Add(card); var persistenceResult = await _cardRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); if (!persistenceResult.IsSuccess) { return(Result.Fail <CreateCardResponse>(persistenceResult.Error)); } var successResponse = new CreateCardResponse { CardToken = card.CardToken, MaskedCardNumber = card.MaskedCardNumber }; return(Result.Ok(successResponse)); }