示例#1
0
        public void WhenInputIsValid_ReturnsNoErrors()
        {
            var response = _target.Validate(new Payment
            {
                CardNumber  = "1234-1234-1234-1234",
                Amount      = 120.49m,
                ExpiryYear  = DateTime.Now.Year + 1,
                ExpiryMonth = DateTime.Now.Month + 1
            });

            response.Should().BeEmpty();
        }
        public async Task <(bool Success, Payment Data, string Errors)> MakePayment(Payment payment, Guid merchantId)
        {
            var validationErrors = string.Join("\n", _businessRulesValidator.Validate(payment));

            if (validationErrors.Length > 0)
            {
                return(false, payment, validationErrors);
            }

            payment.Merchant = new Merchant {
                MerchantId = merchantId
            };
            await _paymentRepository.Create(payment);

            // Little silly business rule that allows the choice of payment provider
            var providerName    = (payment.CardNumber.Trim()[0] - '4') > 0 ? "LehmanSisters" : "DebitSuisse";
            var paymentProvider = _paymentProviderFactory.Create(providerName);

            var paymentProcessResults = await paymentProvider.ProcessPayment(payment);

            payment.Status           = paymentProcessResults.Success ? PaymentStatus.Succeeded : PaymentStatus.Failed;
            payment.Transaction      = paymentProcessResults.TranscactionIdentifier;
            payment.RejectionReasons = string.Join(Environment.NewLine, paymentProcessResults.RejectionReasons);
            await _paymentRepository.Update(payment);

            return(paymentProcessResults.Success, payment, payment.RejectionReasons);
        }