Exemplo n.º 1
0
        public async Task <Result> Handle(RequestPaymentCommand command, CancellationToken cancellationToken)
        {
            var merchant = await _merchantRepository.GetByApiKey(command.ApiKey);

            var payment = BuildPayment(merchant, command);

            try
            {
                await _paymentRequestRepository.AddPayment(payment);
            }
            catch (ArgumentException ex)
            {
                //Concurrency exception, transaction already processed (or in progress) for that merchant
                _logger.LogInformation($"Concurrency exception registering payment for Transaction: {command.MerchantTransactionId}, Merchant: {merchant.Code}. ex: {ex}");

                return(Result.Empty());
            }

            var acquirerResponse = await _acquiringBank.ProcessPayment(BuildAcquirerRequest(command));

            payment.RegisterAcquirerResponse(acquirerResponse);

            await _paymentRequestRepository.UpdatePayment(payment);

            return(Result.Empty());
        }
        public async Task <Result <GetPaymentByTransactionIdQueryResponse> > Handle(GetPaymentByTransactionIdQuery request, CancellationToken cancellationToken)
        {
            var merchant = await _merchantRepository.GetByApiKey(request.ApiKey);

            var payment = await _paymentRepository.GetByTransactionId(merchant.Id, request.TransactionId);

            if (payment == null)
            {
                return(new Result <GetPaymentByTransactionIdQueryResponse>());
            }

            return(new Result <GetPaymentByTransactionIdQueryResponse>
            {
                Value = new GetPaymentByTransactionIdQueryResponse
                {
                    Amount = payment.Amount,
                    Currency = payment.Currency,
                    CardNumber = $"****{payment.CardDetails.CardNumber.Substring(payment.CardDetails.CardNumber.Length - 4)}",
                    ExpiryYear = payment.CardDetails.ExpiryYear,
                    ExpiryMonth = payment.CardDetails.ExpiryMonth,
                    StatusCode = payment.GetPaymentStatus().StatusCode,
                    TransactionId = payment.TransactionId,
                    Cvv = payment.CardDetails.Cvv
                }
            });
        }
        public async Task <Result <GetPaymentByMerchantTransactionIdQueryResponse> > Handle(GetPaymentByMerchantTransactionIdQuery request, CancellationToken cancellationToken)
        {
            var merchant = await _merchantRepository.GetByApiKey(request.ApiKey);

            var payment = await _paymentRepository.GetByMerchantTransactionId(merchant.Id, request.MerchantTransactionId);

            if (payment == null)
            {
                return(null);
            }

            var transactionStatus = payment.GetPaymentStatus();

            return(new Result <GetPaymentByMerchantTransactionIdQueryResponse>
            {
                Value = new GetPaymentByMerchantTransactionIdQueryResponse
                {
                    TransactionId = payment.TransactionId,
                    IsSuccessfull = transactionStatus.Status == Domain.Models.PaymentStatusEnum.FinishedSuccesfully,
                    ResultCode = transactionStatus.StatusCode,
                    ResultDescription = transactionStatus.StatusReason
                }
            });
        }
Exemplo n.º 4
0
        public async Task GivenExistingPayment_WhenExecuteIsCalled_ThenPaymentIsReturnedAsExpected()
        {
            var apiKey     = "new api key";
            var merchantId = Guid.NewGuid();
            var merchant   = new Merchant {
                Id = merchantId
            };
            var merchantTransactionId = "merchant tranid";
            var transactionId         = Guid.NewGuid();
            var amount                    = 15.4m;
            var currency                  = "EUR";
            var cardNumber                = "1234123412341234";
            var expiryMonth               = 12;
            var expiryYear                = 2020;
            var cvv                       = "123";
            var acquirerResultCode        = "ok";
            var acquirerResultDescription = "Result descroption";
            var acquirerTransactionId     = "transciontionId";
            var query                     = new GetPaymentByTransactionIdQuery {
                ApiKey = apiKey, TransactionId = transactionId
            };
            var payment = new Payment
            {
                MerchantTransactionId = merchantTransactionId,
                TransactionId         = transactionId,
                Amount      = amount,
                Currency    = currency,
                CardDetails = new Card
                {
                    CardNumber  = cardNumber,
                    ExpiryMonth = expiryMonth,
                    ExpiryYear  = expiryYear,
                    Cvv         = cvv
                },
                AcquirerResponse = new AcquirerResponse
                {
                    TransactionId     = acquirerTransactionId,
                    IsSuccess         = true,
                    ResultCode        = acquirerResultCode,
                    ResultDescription = acquirerResultDescription
                }
            };

            _merchantRepository.GetByApiKey(apiKey).Returns(merchant);
            _paymentrepository.GetByTransactionId(merchant.Id, transactionId).Returns(payment);


            var response = await _handler.Handle(query, new System.Threading.CancellationToken());

            response.Errors.Should().BeEmpty();

            response.Value.Should().BeEquivalentTo(new GetPaymentByTransactionIdQueryResponse
            {
                TransactionId = transactionId,
                Amount        = amount,
                Currency      = currency,
                CardNumber    = "****1234",
                ExpiryYear    = expiryYear,
                ExpiryMonth   = expiryMonth,
                Cvv           = cvv,
                StatusCode    = acquirerResultCode
            });
        }
Exemplo n.º 5
0
        public void GiveValidCommand_WhenValidateIsCalled_ThenIsValidIsTrue()
        {
            var apiKey     = "new api key";
            var merchantId = Guid.NewGuid();
            var merchant   = new Merchant {
                Id = merchantId, SigningKey = "singkey"
            };
            var command = BuildValidCommand(apiKey);

            _merchantRepository.GetByApiKey(apiKey).Returns(merchant);
            _signatureValidator.IsValidSignature(Arg.Any <Dictionary <string, string> >(), command.Signature, merchant.SigningKey).Returns(true);

            var result = _validator.TestValidate(command);

            result.IsValid.Should().BeTrue();
        }
Exemplo n.º 6
0
        private async Task <bool> IsValidSignature(RequestPaymentCommand command)
        {
            var merchantSigningKey = (await _merchantRepository.GetByApiKey(command.ApiKey)).SigningKey;

            return(_signatureValidator.IsValidSignature(command.ToDictionary(), command.Signature, merchantSigningKey));
        }
Exemplo n.º 7
0
        public async Task GivenValidPaymentRequest_WhenExecuteIsCalled_ThenPaymentIsCreated()
        {
            var apiKey     = "new api key";
            var merchantId = Guid.NewGuid();
            var merchant   = new Merchant {
                Id = merchantId
            };
            var merchantTransactionId = "tranid";
            var amount                    = 15.4m;
            var currency                  = "EUR";
            var cardNumber                = "1234";
            var expiryMonth               = 12;
            var expityYear                = 2020;
            var cvv                       = "123";
            var acquirerResultCode        = "ok";
            var acquirerResultDescription = "Result descroption";
            var acquirerTransactionId     = "transciontionId";

            var requestPaymentCommand = new RequestPaymentCommand
            {
                MerchantTransactionId = merchantTransactionId,
                ApiKey      = apiKey,
                Amount      = amount,
                Currency    = currency,
                CardNumber  = cardNumber,
                ExpiryMonth = expiryMonth,
                ExpiryYear  = expityYear,
                Cvv         = cvv
            };

            var acquirerSuccessResponse = new ProcessPaymentResponse {
                IsSuccess = true, ResultCode = acquirerResultCode, ResultDescription = acquirerResultDescription, TransactionId = acquirerTransactionId
            };

            _merchantRepository.GetByApiKey(apiKey).Returns(merchant);

            _acquiringBank.ProcessPayment(Arg.Is <ProcessPaymentRequest>(
                                              p => p.Amount == amount &&
                                              p.Currency == currency &&
                                              p.CardNumber == cardNumber &&
                                              p.ExpiryMonth == expiryMonth &&
                                              p.ExpiryYear == expityYear &&
                                              p.Cvv == cvv))
            .Returns(acquirerSuccessResponse);


            await _handler.Handle(requestPaymentCommand, new System.Threading.CancellationToken());


            await _paymentrepository.Received(1).AddPayment(Arg.Is <Payment>(p => p.MerchantId.Equals(merchantId) &&
                                                                             p.Amount == amount &&
                                                                             p.Currency == currency &&
                                                                             p.CardDetails.CardNumber == cardNumber &&
                                                                             p.CardDetails.ExpiryMonth == expiryMonth &&
                                                                             p.CardDetails.ExpiryYear == expityYear &&
                                                                             p.CardDetails.Cvv == cvv));

            await _paymentrepository.Received(1).UpdatePayment(Arg.Is <Payment>(p => p.MerchantId.Equals(merchantId) &&
                                                                                p.Amount == amount &&
                                                                                p.Currency == currency &&
                                                                                p.CardDetails.CardNumber == cardNumber &&
                                                                                p.CardDetails.ExpiryMonth == expiryMonth &&
                                                                                p.CardDetails.ExpiryYear == expityYear &&
                                                                                p.CardDetails.Cvv == cvv &&
                                                                                p.AcquirerResponse.IsSuccess == true &&
                                                                                p.AcquirerResponse.ResultCode == acquirerResultCode &&
                                                                                p.AcquirerResponse.ResultDescription == acquirerResultDescription &&
                                                                                p.AcquirerResponse.TransactionId == acquirerTransactionId));
        }