public async Task UpdatePaymentRequestStateIfNeeded(PaymentRequestData pr)
        {
            var blob          = pr.GetBlob();
            var currentStatus = pr.Status;

            if (blob.ExpiryDate.HasValue)
            {
                if (blob.ExpiryDate.Value <= DateTimeOffset.UtcNow)
                {
                    currentStatus = Client.Models.PaymentRequestData.PaymentRequestStatus.Expired;
                }
            }

            if (currentStatus == Client.Models.PaymentRequestData.PaymentRequestStatus.Pending)
            {
                var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider);
                var invoices  = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(pr.Id);

                var contributions = _AppService.GetContributionsByPaymentMethodId(blob.Currency, invoices, true);
                if (contributions.TotalCurrency >= blob.Amount)
                {
                    currentStatus = Client.Models.PaymentRequestData.PaymentRequestStatus.Completed;
                }
            }

            if (currentStatus != pr.Status)
            {
                pr.Status = currentStatus;
                await _PaymentRequestRepository.UpdatePaymentRequestStatus(pr.Id, currentStatus);
            }
        }
        public async Task UpdatePaymentRequestStateIfNeeded(PaymentRequestData pr)
        {
            var blob          = pr.GetBlob();
            var currentStatus = pr.Status;

            if (blob.ExpiryDate.HasValue)
            {
                if (blob.ExpiryDate.Value <= DateTimeOffset.UtcNow)
                {
                    currentStatus = PaymentRequestData.PaymentRequestStatus.Expired;
                }
            }
            else if (pr.Status == PaymentRequestData.PaymentRequestStatus.Pending)
            {
                var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider);
                var invoices  = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(pr.Id);

                var paymentStats    = _AppService.GetCurrentContributionAmountStats(invoices, true);
                var amountCollected =
                    await _AppService.GetCurrentContributionAmount(paymentStats, blob.Currency, rateRules);

                if (amountCollected >= blob.Amount)
                {
                    currentStatus = PaymentRequestData.PaymentRequestStatus.Completed;
                }
            }

            if (currentStatus != pr.Status)
            {
                pr.Status = currentStatus;
                await _PaymentRequestRepository.UpdatePaymentRequestStatus(pr.Id, currentStatus);
            }
        }
Exemple #3
0
        public ViewPaymentRequestViewModel(PaymentRequestData data)
        {
            Id = data.Id;
            var blob = data.GetBlob();

            Title                     = blob.Title;
            Amount                    = blob.Amount;
            Currency                  = blob.Currency;
            Description               = blob.Description;
            ExpiryDate                = blob.ExpiryDate;
            Email                     = blob.Email;
            EmbeddedCSS               = blob.EmbeddedCSS;
            CustomCSSLink             = blob.CustomCSSLink;
            AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts;
            switch (data.Status)
            {
            case PaymentRequestData.PaymentRequestStatus.Pending:
                Status    = ExpiryDate.HasValue ? $"Expires on {ExpiryDate.Value:g}" : "Pendiente";
                IsPending = true;
                break;

            case PaymentRequestData.PaymentRequestStatus.Completed:
                Status = "Aceptado";
                break;

            case PaymentRequestData.PaymentRequestStatus.Expired:
                Status = "Expirado";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public async Task <IActionResult> EditPaymentRequest(string id, UpdatePaymentRequestViewModel viewModel)
        {
            if (string.IsNullOrEmpty(viewModel.Currency) ||
                _Currencies.GetCurrencyData(viewModel.Currency, false) == null)
            {
                ModelState.AddModelError(nameof(viewModel.Currency), "Invalid currency");
            }

            var data = await _PaymentRequestRepository.FindPaymentRequest(id, GetUserId());

            if (data == null && !string.IsNullOrEmpty(id))
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                viewModel.Stores = new SelectList(await _StoreRepository.GetStoresByUserId(GetUserId()),
                                                  nameof(StoreData.Id),
                                                  nameof(StoreData.StoreName), data?.StoreDataId);

                return(View(viewModel));
            }

            if (data == null)
            {
                data = new PaymentRequestData();
            }

            data.StoreDataId = viewModel.StoreId;
            var blob = data.GetBlob();

            blob.Title                     = viewModel.Title;
            blob.Email                     = viewModel.Email;
            blob.Description               = viewModel.Description;
            blob.Amount                    = viewModel.Amount;
            blob.ExpiryDate                = viewModel.ExpiryDate?.ToUniversalTime();
            blob.Currency                  = viewModel.Currency;
            blob.EmbeddedCSS               = viewModel.EmbeddedCSS;
            blob.CustomCSSLink             = viewModel.CustomCSSLink;
            blob.AllowCustomPaymentAmounts = viewModel.AllowCustomPaymentAmounts;

            data.SetBlob(blob);
            if (string.IsNullOrEmpty(id))
            {
                data.Created = DateTimeOffset.UtcNow;
            }
            data = await _PaymentRequestRepository.CreateOrUpdatePaymentRequest(data);

            _EventAggregator.Publish(new PaymentRequestUpdated()
            {
                Data             = data,
                PaymentRequestId = data.Id,
            });

            TempData[WellKnownTempData.SuccessMessage] = "Saved";
            return(RedirectToAction("EditPaymentRequest", new { id = data.Id }));
        }
        public async Task <IActionResult> ProcessPayment([FromBody] PaymentRequestData model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            await _paymentService.ProcessPaymentAsync(model);

            return(Ok());
        }
        public async Task <bool> ProcessPaymentAsync(PaymentRequestData paymentRequestData)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            return(RetryMechanism.Do(() =>
            {
                Random gen = new Random();
                int prob = gen.Next(100);
                return prob <= 20 ? true : throw new DataException();
            }, TimeSpan.FromSeconds(1), 3));
        }
        public void TestProcessingPremium()
        {
            var reqData = new PaymentRequestData
            {
                CreditCardNumber = "4556394887258090",
                CardHolder       = "Client",
                ExpirationDate   = new DateTime(2019, 5, 10),
                SecurityCode     = "123",
                Amount           = 521
            };

            Assert.DoesNotThrow(() => _mockPaymentService.ProcessPaymentAsync(reqData));
        }
        public UpdatePaymentRequestViewModel(PaymentRequestData data)
        {
            if (data == null)
            {
                return;
            }

            Id      = data.Id;
            StoreId = data.StoreDataId;

            var blob = data.GetBlob();

            Title                     = blob.Title;
            Amount                    = blob.Amount;
            Currency                  = blob.Currency;
            Description               = blob.Description;
            ExpiryDate                = blob.ExpiryDate;
            Email                     = blob.Email;
            CustomCSSLink             = blob.CustomCSSLink;
            EmbeddedCSS               = blob.EmbeddedCSS;
            AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts;
        }
Exemple #9
0
        public ViewPaymentRequestViewModel(PaymentRequestData data)
        {
            Id = data.Id;
            var blob = data.GetBlob();

            Archived                  = data.Archived;
            Title                     = blob.Title;
            Amount                    = blob.Amount;
            Currency                  = blob.Currency;
            Description               = blob.Description;
            ExpiryDate                = blob.ExpiryDate;
            Email                     = blob.Email;
            EmbeddedCSS               = blob.EmbeddedCSS;
            CustomCSSLink             = blob.CustomCSSLink;
            AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts;
            if (!string.IsNullOrEmpty(EmbeddedCSS))
            {
                EmbeddedCSS = $"<style>{EmbeddedCSS}</style>";
            }
            switch (data.Status)
            {
            case PaymentRequestData.PaymentRequestStatus.Pending:
                Status    = ExpiryDate.HasValue ? $"Expires on {ExpiryDate.Value:g}" : "Pending";
                IsPending = true;
                break;

            case PaymentRequestData.PaymentRequestStatus.Completed:
                Status = "Settled";
                break;

            case PaymentRequestData.PaymentRequestStatus.Expired:
                Status = "Expired";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public async Task ProcessPaymentAsync(PaymentRequestData paymentProcessData)
        {
            var paymentProcessingGateway = await _gatewayFactory.GetPaymentProcessingGatewayAsync(paymentProcessData.Amount);

            await paymentProcessingGateway?.ProcessPaymentAsync(paymentProcessData);
        }
        public async Task <bool> ProcessPaymentAsync(PaymentRequestData paymentRequestData)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            return(true);
        }
Exemple #12
0
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PolicyRequirement requirement)
        {
            if (context.User.Identity.AuthenticationType != AuthenticationSchemes.Cookie)
            {
                return;
            }

            var userId = _userManager.GetUserId(context.User);

            if (string.IsNullOrEmpty(userId))
            {
                return;
            }

            bool success = false;
            var  isAdmin = context.User.IsInRole(Roles.ServerAdmin);

            AppData            app            = null;
            StoreData          store          = null;
            InvoiceEntity      invoice        = null;
            PaymentRequestData paymentRequest = null;
            string             storeId;
            var explicitResource = false;

            if (context.Resource is string s)
            {
                explicitResource = true;
                storeId          = s;
            }
            else
            {
                storeId = _httpContext.GetImplicitStoreId();
            }
            var routeData = _httpContext.GetRouteData();

            if (routeData != null)
            {
                // resolve from app
                if (routeData.Values.TryGetValue("appId", out var vAppId) && vAppId is string appId)
                {
                    app = await _appService.GetAppDataIfOwner(userId, appId);

                    if (storeId == null)
                    {
                        storeId = app?.StoreDataId ?? String.Empty;
                    }
                    else if (app?.StoreDataId != storeId)
                    {
                        app = null;
                    }
                }
                // resolve from payment request
                if (routeData.Values.TryGetValue("payReqId", out var vPayReqId) && vPayReqId is string payReqId)
                {
                    paymentRequest = await _paymentRequestRepository.FindPaymentRequest(payReqId, userId);

                    if (storeId == null)
                    {
                        storeId = paymentRequest?.StoreDataId ?? String.Empty;
                    }
                    else if (paymentRequest?.StoreDataId != storeId)
                    {
                        paymentRequest = null;
                    }
                }
                // resolve from invoice
                if (routeData.Values.TryGetValue("invoiceId", out var vInvoiceId) && vInvoiceId is string invoiceId)
                {
                    invoice = await _invoiceRepository.GetInvoice(invoiceId);

                    if (storeId == null)
                    {
                        storeId = invoice?.StoreId ?? String.Empty;
                    }
                    else if (invoice?.StoreId != storeId)
                    {
                        invoice = null;
                    }
                }
            }

            // Fall back to user prefs cookie
            if (storeId == null)
            {
                storeId = _httpContext.GetUserPrefsCookie()?.CurrentStoreId;
            }

            if (string.IsNullOrEmpty(storeId))
            {
                storeId = null;
            }
            if (storeId != null)
            {
                store = await _storeRepository.FindStore(storeId, userId);
            }

            switch (requirement.Policy)
            {
            case Policies.CanModifyServerSettings:
                if (isAdmin)
                {
                    success = true;
                }
                break;

            case Policies.CanModifyStoreSettings:
                if (store != null && (store.Role == StoreRoles.Owner))
                {
                    success = true;
                }
                break;

            case Policies.CanViewInvoices:
            case Policies.CanViewStoreSettings:
            case Policies.CanCreateInvoice:
                if (store != null)
                {
                    success = true;
                }
                break;

            case Policies.CanViewProfile:
            case Policies.CanViewNotificationsForUser:
            case Policies.CanManageNotificationsForUser:
            case Policies.CanModifyStoreSettingsUnscoped:
                if (context.User != null)
                {
                    success = true;
                }
                break;
            }

            if (success)
            {
                context.Succeed(requirement);
                if (!explicitResource)
                {
                    if (store != null)
                    {
                        _httpContext.SetStoreData(store);

                        // cache associated entities if present
                        if (app != null)
                        {
                            _httpContext.SetAppData(app);
                        }
                        if (invoice != null)
                        {
                            _httpContext.SetInvoiceData(invoice);
                        }
                        if (paymentRequest != null)
                        {
                            _httpContext.SetPaymentRequestData(paymentRequest);
                        }
                    }
                }
            }
        }