Ejemplo n.º 1
0
        public ActionResult <Invoice> RetrieveUpcomingInvoice([FromBody] RetrieveUpcomingInvoiceRequest req)
        {
            var service      = new SubscriptionService();
            var subscription = service.Get(req.Subscription);

            var invoiceService = new InvoiceService();
            var options        = new UpcomingInvoiceOptions
            {
                Customer          = req.Customer,
                Subscription      = req.Subscription,
                SubscriptionItems = new List <InvoiceSubscriptionItemOptions>
                {
                    new InvoiceSubscriptionItemOptions
                    {
                        Id      = subscription.Items.Data[0].Id,
                        Deleted = true,
                    },
                    new InvoiceSubscriptionItemOptions
                    {
                        // TODO: This should be Price, but isnt in Stripe.net yet.
                        Plan    = Environment.GetEnvironmentVariable(req.NewPrice),
                        Deleted = false,
                    },
                }
            };
            Invoice upcoming = invoiceService.Upcoming(options);

            return(upcoming);
        }
        public ActionResult <RetrieveUpcomingInvoiceResponse> RetrieveUpcomingInvoice([FromBody] RetrieveUpcomingInvoiceRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(this.FailWithMessage("invalid params"));
            }
            var newPrice = Environment.GetEnvironmentVariable(req.NewPrice.ToUpper());

            if (newPrice is null || newPrice == "")
            {
                return(this.FailWithMessage($"No price with the new price ID ({req.NewPrice}) found in .env"));
            }

            List <InvoiceSubscriptionItemOptions> items;
            Subscription subscription = null;

            if (req.Subscription != "" && req.Subscription != null)
            {
                var subscriptionService = new SubscriptionService();
                subscription = subscriptionService.Get(req.Subscription);

                var currentPrice = subscription.Items.Data[0].Price.Id;
                if (currentPrice == newPrice)
                {
                    items = new List <InvoiceSubscriptionItemOptions> {
                        new InvoiceSubscriptionItemOptions
                        {
                            Id       = subscription.Items.Data[0].Id,
                            Quantity = req.Quantity,
                        }
                    };
                }
                else
                {
                    items = new List <InvoiceSubscriptionItemOptions> {
                        new InvoiceSubscriptionItemOptions
                        {
                            Id      = subscription.Items.Data[0].Id,
                            Deleted = true,
                        },
                        new InvoiceSubscriptionItemOptions
                        {
                            Price    = newPrice,
                            Quantity = req.Quantity,
                        },
                    };
                }
            }
            else
            {
                items = new List <InvoiceSubscriptionItemOptions> {
                    new InvoiceSubscriptionItemOptions
                    {
                        Price    = newPrice,
                        Quantity = req.Quantity,
                    },
                };
            }

            var invoiceService = new InvoiceService();
            var options        = new UpcomingInvoiceOptions
            {
                Customer          = req.Customer,
                Subscription      = req.Subscription,
                SubscriptionItems = items,
            };
            Invoice upcomingInvoice = invoiceService.Upcoming(options);

            if (req.Subscription == "" || req.Subscription is null)
            {
                return(new RetrieveUpcomingInvoiceResponse
                {
                    Invoice = upcomingInvoice,
                });
            }
            else
            {
                var  currentPeriodEnd = subscription.CurrentPeriodEnd;
                long immediateTotal   = 0;
                long nextInvoiceSum   = 0;
                foreach (var lineItem in upcomingInvoice.Lines.Data)
                {
                    if (lineItem.Period.End == currentPeriodEnd)
                    {
                        immediateTotal += lineItem.Amount;
                    }
                    else
                    {
                        nextInvoiceSum += lineItem.Amount;
                    }
                }

                return(new RetrieveUpcomingInvoiceResponse
                {
                    ImmediateTotal = immediateTotal,
                    NextInvoiceSum = nextInvoiceSum,
                    Invoice = upcomingInvoice,
                });
            }
        }