Пример #1
0
        public Task <Result> Send(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            return(CreateLink(paymentLinkCreationData)
                   .Bind(SendMail)
                   .Finally(WriteLog));


            Task <Result> SendMail(PaymentLinkData link)
            {
                var paymentUrl = GeneratePaymentUri(link).ToString();

                return(_notificationService.SendLink(link, paymentUrl));
            }

            Result WriteLog(Result result)
            {
                if (result.IsFailure)
                {
                    _logger.LogExternalPaymentLinkSendFailed(paymentLinkCreationData.Email, result.Error);
                }
                else
                {
                    _logger.LogExternalPaymentLinkSendSuccess(paymentLinkCreationData.Email);
                }

                return(result);
            }
        }
Пример #2
0
        public async Task <IActionResult> GenerateUrl([FromBody] PaymentLinkCreationRequest creationRequest)
        {
            var(isSuccess, _, uri, error) = await _paymentLinkService.GenerateUri(creationRequest);

            return(isSuccess
                ? Ok(uri)
                : (IActionResult)BadRequest(ProblemDetailsBuilder.Build(error)));
        }
Пример #3
0
        public async Task <IActionResult> SendLink([FromBody] PaymentLinkCreationRequest creationRequest)
        {
            var(isSuccess, _, error) = await _paymentLinkService.Send(creationRequest);

            return(isSuccess
                ? NoContent()
                : (IActionResult)BadRequest(ProblemDetailsBuilder.Build(error)));
        }
Пример #4
0
        public async Task Register_link_should_fail_for_invalid_data(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            var linksStorage = CreateService();

            var(_, isSendFailure, _) = await linksStorage.Register(paymentLinkCreationData);

            Assert.True(isSendFailure);
        }
Пример #5
0
 private void AssertLinkDataIsStored(PaymentLinkCreationRequest paymentLinkCreationData)
 {
     Assert.Equal(paymentLinkCreationData.Amount, LastCreatedLink.Amount);
     Assert.Equal(paymentLinkCreationData.Comment, LastCreatedLink.Comment);
     Assert.Equal(paymentLinkCreationData.Currency, LastCreatedLink.Currency);
     Assert.Equal(paymentLinkCreationData.Email, LastCreatedLink.Email);
     Assert.Equal(paymentLinkCreationData.ServiceType, LastCreatedLink.ServiceType);
 }
Пример #6
0
        public async Task Valid_links_should_be_registered_successfully(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            var linksStorage = CreateService();

            var(_, isFailure, _, _) = await linksStorage.Register(paymentLinkCreationData);

            Assert.False(isFailure);
            AssertLinkDataIsStored(paymentLinkCreationData);
        }
Пример #7
0
        public Task <Result <PaymentLink> > Register(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            return(Validate(paymentLinkCreationData)
                   .Map(CreateLink));


            Result Validate(PaymentLinkCreationRequest linkData)
            {
                var linkSettings = _paymentLinkOptions.ClientSettings;

                return(GenericValidator <PaymentLinkCreationRequest> .Validate(v =>
                {
                    v.RuleFor(data => data.ServiceType).IsInEnum();
                    v.RuleFor(data => data.Currency).IsInEnum();
                    v.RuleFor(data => data.Amount).GreaterThan(decimal.Zero);
                    v.RuleFor(data => data.Email).EmailAddress();
                    v.RuleFor(data => data.Comment).NotEmpty();

                    v.RuleFor(data => data.Currency)
                    .Must(linkSettings.Currencies.Contains);

                    v.RuleFor(data => data.ServiceType)
                    .Must(serviceType => linkSettings.ServiceTypes.ContainsKey(serviceType));
                }, linkData));
            }

            async Task <PaymentLink> CreateLink()
            {
                var referenceCode = await _tagProcessor.GenerateNonSequentialReferenceCode(paymentLinkCreationData.ServiceType, LinkDestinationCode);

                var paymentLink = new PaymentLink
                {
                    Email            = paymentLinkCreationData.Email,
                    Amount           = paymentLinkCreationData.Amount,
                    Currency         = paymentLinkCreationData.Currency,
                    ServiceType      = paymentLinkCreationData.ServiceType,
                    Comment          = paymentLinkCreationData.Comment,
                    Created          = _dateTimeProvider.UtcNow(),
                    Code             = Base64UrlEncoder.Encode(Guid.NewGuid().ToByteArray()),
                    ReferenceCode    = referenceCode,
                    PaymentProcessor = paymentLinkCreationData.PaymentProcessor,
                    InvoiceNumber    = paymentLinkCreationData.InvoiceNumber
                };

                _context.PaymentLinks.Add(paymentLink);
                await _context.SaveChangesAsync();

                return(paymentLink);
            }
        }
Пример #8
0
        private Task<Result<PaymentLinkData>> CreateLink(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            return RegisterLink()
                .Map(PaymentLinkExtensions.ToLinkData)
                .Finally(WriteLog);

            
            Task<Result<PaymentLink>> RegisterLink() => _storage.Register(paymentLinkCreationData);
            
            Result<PaymentLinkData> WriteLog(Result<PaymentLinkData> result)
            {
                if (result.IsFailure)
                    _logger.LogExternalPaymentLinkSendFailed($"Error generating payment link for {paymentLinkCreationData.Email}: {result.Error}");
                else
                    _logger.LogExternalPaymentLinkSendSuccess($"Successfully generated payment link for {paymentLinkCreationData.Email}");

                return result;
            }
        }
Пример #9
0
        private Task <Result <PaymentLinkData> > CreateLink(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            return(RegisterLink()
                   .Map(PaymentLinkExtensions.ToLinkData)
                   .Finally(WriteLog));


            Task <Result <PaymentLink> > RegisterLink() => _storage.Register(paymentLinkCreationData);

            Result <PaymentLinkData> WriteLog(Result <PaymentLinkData> result)
            {
                if (result.IsFailure)
                {
                    _logger.LogExternalPaymentLinkGenerationFailed(paymentLinkCreationData.Email, result.Error);
                }
                else
                {
                    _logger.LogExternalPaymentLinkGenerationSuccess(paymentLinkCreationData.Email);
                }

                return(result);
            }
        }
Пример #10
0
        public async Task Registering_link_should_validate_against_client_settings(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            var linksStorage = CreateService(GetOptions());

            var(_, isGenerateFailure, _, _) = await linksStorage.Register(paymentLinkCreationData);

            Assert.True(isGenerateFailure);

            IOptions <PaymentLinkOptions> GetOptions()
            => Options.Create(new PaymentLinkOptions
            {
                ClientSettings = new ClientSettings
                {
                    Currencies = new List <Currencies> {
                        Currencies.AED, Currencies.EUR
                    },
                    ServiceTypes = new Dictionary <ServiceTypes, string>
                    {
                        { ServiceTypes.HTL, "Hotel booking" }
                    }
                }
            });
        }
Пример #11
0
        public Task<Result> Send(PaymentLinkCreationRequest paymentLinkCreationData)
        {
            return CreateLink(paymentLinkCreationData)
                .Bind(SendMail)
                .Finally(WriteLog);


            Task<Result> SendMail(PaymentLinkData link)
            {
                var paymentUrl = GeneratePaymentUri(link).ToString();
                return _notificationService.SendLink(link, paymentUrl);
            }
            
            
            Result WriteLog(Result result)
            {
                if (result.IsFailure)
                    _logger.LogExternalPaymentLinkSendFailed($"Error sending email to {paymentLinkCreationData.Email}: {result.Error}");
                else
                    _logger.LogExternalPaymentLinkSendSuccess($"Successfully sent e-mail to {paymentLinkCreationData.Email}");

                return result;
            }
        }
Пример #12
0
 public Task <Result <Uri> > GenerateUri(PaymentLinkCreationRequest paymentLinkCreationData)
 {
     return(CreateLink(paymentLinkCreationData)
            .Map(GeneratePaymentUri));
 }