Ejemplo n.º 1
0
        public void Validation_Should_Fail_For_A_Null_Or_Empty_Card_Number(string cardNumber)
        {
            //Arrange
            var cardDetails = new CardDetails
            {
                CardNumber = cardNumber
            };

            //Act
            var result = _cardService.Validate(cardDetails);

            //Assert
            Assert.False(result);
        }
Ejemplo n.º 2
0
        public async Task <Guid> Handle(ExecutePayment command, CancellationToken cancellationToken)
        {
            var transactionId = Guid.NewGuid();

            try
            {
                if (!_cardsService.Validate(command.CardDetails))
                {
                    var statusCode  = HttpStatusCode.NotAcceptable.ToString();
                    var description = "Invalid card";

                    await _mediator.Publish(new PaymentExecuted(
                                                transactionId : transactionId,
                                                merchantId : command.MerchantId,
                                                amount : command.Amount,
                                                cardHolderName : command.CardDetails.CardHolderName,
                                                encrypetdCardNumber : _cardsService.Encrypt(command.CardDetails?.CardNumber),
                                                statusCode : statusCode,
                                                description : description,
                                                successful : false
                                                ));

                    return(transactionId);
                }

                var transactionAuthResponse = await _bankAuthProvider.VerifyAsync(new TransactionAuthRequest(
                                                                                      cardDetails : command.CardDetails,
                                                                                      amount : command.Amount));

                transactionId = transactionAuthResponse.TransactionId;
                _mediator.Publish(new PaymentExecuted(
                                      transactionId: transactionAuthResponse.TransactionId,
                                      merchantId: command.MerchantId,
                                      amount: command.Amount,
                                      cardHolderName: command.CardDetails.CardHolderName,
                                      encrypetdCardNumber: _cardsService.Encrypt(command.CardDetails.CardNumber),
                                      statusCode: transactionAuthResponse.Code,
                                      description: transactionAuthResponse.Description,
                                      successful: transactionAuthResponse.Verified
                                      ));
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    ex,
                    "Unhandled Exception for Command {Name} {@Command}",
                    nameof(ExecutePayment),
                    command);

                var statusCode  = HttpStatusCode.ServiceUnavailable.ToString();
                var description = "Something went wrong. Please try again later.";
                await _mediator.Publish(new PaymentExecuted(
                                            transactionId : transactionId,
                                            merchantId : command.MerchantId,
                                            amount : command.Amount,
                                            cardHolderName : command.CardDetails.CardHolderName,
                                            encrypetdCardNumber : _cardsService.Encrypt(command.CardDetails?.CardNumber),
                                            statusCode : statusCode,
                                            description : description,
                                            successful : false
                                            ));
            }

            return(transactionId);
        }