public async Task <IActionResult> ViewPointOfSale(string appId,
                                                          [ModelBinder(typeof(InvariantDecimalModelBinder))] decimal amount,
                                                          string email,
                                                          string orderId,
                                                          string notificationUrl,
                                                          string redirectUrl,
                                                          string choiceKey,
                                                          string posData = null, CancellationToken cancellationToken = default)
        {
            var app = await _AppService.GetApp(appId, AppType.PointOfSale);

            if (string.IsNullOrEmpty(choiceKey) && amount <= 0)
            {
                return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId }));
            }
            if (app == null)
            {
                return(NotFound());
            }
            var settings = app.GetSettings <PointOfSaleSettings>();

            if (string.IsNullOrEmpty(choiceKey) && !settings.ShowCustomAmount && !settings.EnableShoppingCart)
            {
                return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId }));
            }
            string title = null;
            var    price = 0.0m;

            ViewPointOfSaleViewModel.Item choice = null;
            if (!string.IsNullOrEmpty(choiceKey))
            {
                var choices = _AppService.Parse(settings.Template, settings.Currency);
                choice = choices.FirstOrDefault(c => c.Id == choiceKey);
                if (choice == null)
                {
                    return(NotFound());
                }
                title = choice.Title;
                price = choice.Price.Value;
                if (amount > price)
                {
                    price = amount;
                }
            }
            else
            {
                if (!settings.ShowCustomAmount && !settings.EnableShoppingCart)
                {
                    return(NotFound());
                }
                price = amount;
                title = settings.Title;
            }
            var store = await _AppService.GetStore(app);

            store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id));
            var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
            {
                ItemCode        = choice?.Id,
                ItemDesc        = title,
                Currency        = settings.Currency,
                Price           = price,
                BuyerEmail      = email,
                OrderId         = orderId,
                NotificationURL =
                    string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl,
                NotificationEmail     = settings.NotificationEmail,
                RedirectURL           = redirectUrl ?? Request.GetDisplayUrl(),
                FullNotifications     = true,
                ExtendedNotifications = true,
                PosData = string.IsNullOrEmpty(posData) ? null : posData,
                RedirectAutomatically = settings.RedirectAutomatically,
            }, store, HttpContext.Request.GetAbsoluteRoot(),
                                                                     new List <string>() { AppService.GetAppInternalTag(appId) },
                                                                     cancellationToken);

            return(RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id }));
        }
        public async Task <IActionResult> ContributeToCrowdfund(string appId, ContributeToCrowdfund request, CancellationToken cancellationToken)
        {
            var app = await _AppService.GetApp(appId, AppType.Crowdfund, true);

            if (app == null)
            {
                return(NotFound());
            }
            var settings = app.GetSettings <CrowdfundSettings>();


            var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null;

            if (!settings.Enabled && !isAdmin)
            {
                return(NotFound("Crowdfund is not currently active"));
            }

            var info = (ViewCrowdfundViewModel)await _AppService.GetAppInfo(appId);

            info.HubPath = AppHub.GetHubPath(this.Request);
            if (!isAdmin &&
                ((settings.StartDate.HasValue && DateTime.Now < settings.StartDate) ||
                 (settings.EndDate.HasValue && DateTime.Now > settings.EndDate) ||
                 (settings.EnforceTargetAmount &&
                  (info.Info.PendingProgressPercentage.GetValueOrDefault(0) +
                   info.Info.ProgressPercentage.GetValueOrDefault(0)) >= 100)))
            {
                return(NotFound("Crowdfund is not currently active"));
            }

            var store = await _AppService.GetStore(app);

            var title = settings.Title;
            var price = request.Amount;

            ViewPointOfSaleViewModel.Item choice = null;
            if (!string.IsNullOrEmpty(request.ChoiceKey))
            {
                var choices = _AppService.Parse(settings.PerksTemplate, settings.TargetCurrency);
                choice = choices.FirstOrDefault(c => c.Id == request.ChoiceKey);
                if (choice == null)
                {
                    return(NotFound("Incorrect option provided"));
                }
                title = choice.Title;
                price = choice.Price.Value;
                if (request.Amount > price)
                {
                    price = request.Amount;
                }
            }

            if (!isAdmin && (settings.EnforceTargetAmount && info.TargetAmount.HasValue && price >
                             (info.TargetAmount - (info.Info.CurrentAmount + info.Info.CurrentPendingAmount))))
            {
                return(NotFound("Contribution Amount is more than is currently allowed."));
            }

            store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id));
            try
            {
                var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
                {
                    OrderId               = AppService.GetCrowdfundOrderId(appId),
                    Currency              = settings.TargetCurrency,
                    ItemCode              = request.ChoiceKey ?? string.Empty,
                    ItemDesc              = title,
                    BuyerEmail            = request.Email,
                    Price                 = price,
                    NotificationURL       = settings.NotificationUrl,
                    NotificationEmail     = settings.NotificationEmail,
                    FullNotifications     = true,
                    ExtendedNotifications = true,
                    RedirectURL           = request.RedirectUrl ??
                                            new Uri(new Uri(new Uri(HttpContext.Request.GetAbsoluteRoot()), _BtcPayServerOptions.RootPath), $"apps/{appId}/crowdfund").ToString()
                }, store, HttpContext.Request.GetAbsoluteRoot(),
                                                                         new List <string> {
                    AppService.GetAppInternalTag(appId)
                },
                                                                         cancellationToken : cancellationToken);

                if (request.RedirectToCheckout)
                {
                    return(RedirectToAction(nameof(InvoiceController.Checkout), "Invoice",
                                            new { invoiceId = invoice.Data.Id }));
                }
                else
                {
                    return(Ok(invoice.Data.Id));
                }
            }
            catch (BitpayHttpException e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> ContributeToCrowdfund(string appId, ContributeToCrowdfund request, CancellationToken cancellationToken)
        {
            var app = await _AppService.GetApp(appId, AppType.Crowdfund, true);

            if (app == null)
            {
                return(NotFound());
            }
            var settings = app.GetSettings <CrowdfundSettings>();

            var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null;

            if (!settings.Enabled && !isAdmin)
            {
                return(NotFound("Crowdfund is not currently active"));
            }

            var info = await GetAppInfo(appId);

            if (!isAdmin &&
                ((settings.StartDate.HasValue && DateTime.Now < settings.StartDate) ||
                 (settings.EndDate.HasValue && DateTime.Now > settings.EndDate) ||
                 (settings.EnforceTargetAmount &&
                  (info.Info.PendingProgressPercentage.GetValueOrDefault(0) +
                   info.Info.ProgressPercentage.GetValueOrDefault(0)) >= 100)))
            {
                return(NotFound("Crowdfund is not currently active"));
            }

            var store = await _AppService.GetStore(app);

            var     title = settings.Title;
            decimal?price = request.Amount;
            Dictionary <string, InvoiceSupportedTransactionCurrency> paymentMethods = null;

            ViewPointOfSaleViewModel.Item choice = null;
            if (!string.IsNullOrEmpty(request.ChoiceKey))
            {
                var choices = _AppService.GetPOSItems(settings.PerksTemplate, settings.TargetCurrency);
                choice = choices.FirstOrDefault(c => c.Id == request.ChoiceKey);
                if (choice == null)
                {
                    return(NotFound("Incorrect option provided"));
                }
                title = choice.Title;

                if (choice.Price.Type == ViewPointOfSaleViewModel.Item.ItemPrice.ItemPriceType.Topup)
                {
                    price = null;
                }
                else
                {
                    price = choice.Price.Value;
                    if (request.Amount > price)
                    {
                        price = request.Amount;
                    }
                }
                if (choice.Inventory.HasValue)
                {
                    if (choice.Inventory <= 0)
                    {
                        return(NotFound("Option was out of stock"));
                    }
                }
                if (choice?.PaymentMethods?.Any() is true)
                {
                    paymentMethods = choice?.PaymentMethods.ToDictionary(s => s,
                                                                         s => new InvoiceSupportedTransactionCurrency()
                    {
                        Enabled = true
                    });
                }
            }
            else
            {
                if (request.Amount < 0)
                {
                    return(NotFound("Please provide an amount greater than 0"));
                }

                price = request.Amount;
            }

            if (!isAdmin && (settings.EnforceTargetAmount && info.TargetAmount.HasValue && price >
                             (info.TargetAmount - (info.Info.CurrentAmount + info.Info.CurrentPendingAmount))))
            {
                return(NotFound("Contribution Amount is more than is currently allowed."));
            }

            try
            {
                var invoice = await _InvoiceController.CreateInvoiceCore(new BitpayCreateInvoiceRequest()
                {
                    OrderId                        = AppService.GetCrowdfundOrderId(appId),
                    Currency                       = settings.TargetCurrency,
                    ItemCode                       = request.ChoiceKey ?? string.Empty,
                    ItemDesc                       = title,
                    BuyerEmail                     = request.Email,
                    Price                          = price,
                    NotificationURL                = settings.NotificationUrl,
                    FullNotifications              = true,
                    ExtendedNotifications          = true,
                    SupportedTransactionCurrencies = paymentMethods,
                    RedirectURL                    = request.RedirectUrl ??
                                                     new Uri(new Uri(new Uri(HttpContext.Request.GetAbsoluteRoot()), _BtcPayServerOptions.RootPath), $"apps/{appId}/crowdfund").ToString()
                }, store, HttpContext.Request.GetAbsoluteRoot(),
                                                                         new List <string> {
                    AppService.GetAppInternalTag(appId)
                },
                                                                         cancellationToken : cancellationToken);

                if (request.RedirectToCheckout)
                {
                    return(RedirectToAction(nameof(InvoiceController.Checkout), "Invoice",
                                            new { invoiceId = invoice.Data.Id }));
                }
                else
                {
                    return(Ok(invoice.Data.Id));
                }
            }
            catch (BitpayHttpException e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> ViewPointOfSale(string appId,
                                                          PosViewType viewType,
                                                          [ModelBinder(typeof(InvariantDecimalModelBinder))] decimal amount,
                                                          string email,
                                                          string orderId,
                                                          string notificationUrl,
                                                          string redirectUrl,
                                                          string choiceKey,
                                                          string posData = null, CancellationToken cancellationToken = default)
        {
            var app = await _AppService.GetApp(appId, AppType.PointOfSale);

            if (string.IsNullOrEmpty(choiceKey) && amount <= 0)
            {
                return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId }));
            }
            if (app == null)
            {
                return(NotFound());
            }
            var settings = app.GetSettings <PointOfSaleSettings>();

            settings.DefaultView = settings.EnableShoppingCart ? PosViewType.Cart : settings.DefaultView;
            if (string.IsNullOrEmpty(choiceKey) && !settings.ShowCustomAmount && settings.DefaultView != PosViewType.Cart)
            {
                return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId, viewType = viewType }));
            }
            string title = null;
            var    price = 0.0m;
            Dictionary <string, InvoiceSupportedTransactionCurrency> paymentMethods = null;

            ViewPointOfSaleViewModel.Item choice = null;
            if (!string.IsNullOrEmpty(choiceKey))
            {
                var choices = _AppService.Parse(settings.Template, settings.Currency);
                choice = choices.FirstOrDefault(c => c.Id == choiceKey);
                if (choice == null)
                {
                    return(NotFound());
                }
                title = choice.Title;
                price = choice.Price.Value;
                if (amount > price)
                {
                    price = amount;
                }

                if (choice.Inventory.HasValue)
                {
                    if (choice.Inventory <= 0)
                    {
                        return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId }));
                    }
                }

                if (choice?.PaymentMethods?.Any() is true)
                {
                    paymentMethods = choice?.PaymentMethods.ToDictionary(s => s,
                                                                         s => new InvoiceSupportedTransactionCurrency()
                    {
                        Enabled = true
                    });
                }
            }
            else
            {
                if (!settings.ShowCustomAmount && settings.DefaultView != PosViewType.Cart)
                {
                    return(NotFound());
                }
                price = amount;
                title = settings.Title;

                //if cart IS enabled and we detect posdata that matches the cart system's, check inventory for the items
                if (!string.IsNullOrEmpty(posData) &&
                    settings.DefaultView == PosViewType.Cart &&
                    AppService.TryParsePosCartItems(posData, out var cartItems))
                {
                    var choices = _AppService.Parse(settings.Template, settings.Currency);
                    foreach (var cartItem in cartItems)
                    {
                        var itemChoice = choices.FirstOrDefault(c => c.Id == cartItem.Key);
                        if (itemChoice == null)
                        {
                            return(NotFound());
                        }

                        if (itemChoice.Inventory.HasValue)
                        {
                            switch (itemChoice.Inventory)
                            {
                            case int i when i <= 0:
                                return(RedirectToAction(nameof(ViewPointOfSale), new { appId }));

                            case int inventory when inventory < cartItem.Value:
                                return(RedirectToAction(nameof(ViewPointOfSale), new { appId }));
                            }
                        }
                    }
                }
            }
            var store = await _AppService.GetStore(app);

            try
            {
                var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
                {
                    ItemCode        = choice?.Id,
                    ItemDesc        = title,
                    Currency        = settings.Currency,
                    Price           = price,
                    BuyerEmail      = email,
                    OrderId         = orderId,
                    NotificationURL =
                        string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl,
                    RedirectURL           = redirectUrl ?? Request.GetDisplayUrl(),
                    FullNotifications     = true,
                    ExtendedNotifications = true,
                    PosData = string.IsNullOrEmpty(posData) ? null : posData,
                    RedirectAutomatically          = settings.RedirectAutomatically,
                    SupportedTransactionCurrencies = paymentMethods,
                }, store, HttpContext.Request.GetAbsoluteRoot(),
                                                                         new List <string>() { AppService.GetAppInternalTag(appId) },
                                                                         cancellationToken);

                return(RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id }));
            }
            catch (BitpayHttpException e)
            {
                TempData.SetStatusMessageModel(new StatusMessageModel()
                {
                    Html         = e.Message.Replace("\n", "<br />", StringComparison.OrdinalIgnoreCase),
                    Severity     = StatusMessageModel.StatusSeverity.Error,
                    AllowDismiss = true
                });
                return(RedirectToAction(nameof(ViewPointOfSale), new { appId = appId }));
            }
        }
        public async Task <IActionResult> ContributeToCrowdfund(string appId, ContributeToCrowdfund request)
        {
            var app = await _AppsHelper.GetApp(appId, AppType.Crowdfund, true);

            if (app == null)
            {
                return(NotFound());
            }
            var settings = app.GetSettings <CrowdfundSettings>();


            var isAdmin = await _AppsHelper.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null;

            if (!settings.Enabled)
            {
                if (!isAdmin)
                {
                    return(NotFound("Crowdfund is not currently active"));
                }
            }

            var info = await _CrowdfundHubStreamer.GetCrowdfundInfo(appId);

            if (!isAdmin &&

                ((settings.StartDate.HasValue && DateTime.Now < settings.StartDate) ||
                 (settings.EndDate.HasValue && DateTime.Now > settings.EndDate) ||
                 (settings.EnforceTargetAmount &&
                  (info.Info.PendingProgressPercentage.GetValueOrDefault(0) +
                   info.Info.ProgressPercentage.GetValueOrDefault(0)) >= 100)))
            {
                return(NotFound("Crowdfund is not currently active"));
            }

            var store = await _AppsHelper.GetStore(app);

            var title = settings.Title;
            var price = request.Amount;

            ViewPointOfSaleViewModel.Item choice = null;
            if (!string.IsNullOrEmpty(request.ChoiceKey))
            {
                var choices = _AppsHelper.Parse(settings.PerksTemplate, settings.TargetCurrency);
                choice = choices.FirstOrDefault(c => c.Id == request.ChoiceKey);
                if (choice == null)
                {
                    return(NotFound("Incorrect option provided"));
                }
                title = choice.Title;
                price = choice.Price.Value;
                if (request.Amount > price)
                {
                    price = request.Amount;
                }
            }

            if (!isAdmin && (settings.EnforceTargetAmount && info.TargetAmount.HasValue && price >
                             (info.TargetAmount - (info.Info.CurrentAmount + info.Info.CurrentPendingAmount))))
            {
                return(NotFound("Contribution Amount is more than is currently allowed."));
            }

            store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id));
            var invoice = await _InvoiceController.CreateInvoiceCore(new Invoice()
            {
                OrderId               = $"{CrowdfundHubStreamer.CrowdfundInvoiceOrderIdPrefix}{appId}",
                Currency              = settings.TargetCurrency,
                ItemCode              = request.ChoiceKey ?? string.Empty,
                ItemDesc              = title,
                BuyerEmail            = request.Email,
                Price                 = price,
                NotificationURL       = settings.NotificationUrl,
                FullNotifications     = true,
                ExtendedNotifications = true,
                RedirectURL           = request.RedirectUrl ?? Request.GetDisplayUrl(),
            }, store, HttpContext.Request.GetAbsoluteRoot());

            if (request.RedirectToCheckout)
            {
                return(RedirectToAction(nameof(InvoiceController.Checkout), "Invoice",
                                        new { invoiceId = invoice.Data.Id }));
            }
            else
            {
                return(Ok(invoice.Data.Id));
            }
        }