Beispiel #1
0
        public async Task PreviewUpcomingInvoiceAndPayAsync(ISubscriber subscriber, string planId,
                                                            int prorateThreshold = 500)
        {
            var invoiceService  = new StripeInvoiceService();
            var upcomingPreview = await invoiceService.UpcomingAsync(subscriber.GatewayCustomerId,
                                                                     new StripeUpcomingInvoiceOptions
            {
                SubscriptionId = subscriber.GatewaySubscriptionId
            });

            var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data?
                                  .TakeWhile(i => i.Plan.Id == planId && i.Proration).Sum(i => i.Amount);

            if (prorationAmount.GetValueOrDefault() >= prorateThreshold)
            {
                try
                {
                    // Owes more than prorateThreshold on next invoice.
                    // Invoice them and pay now instead of waiting until next month.
                    var invoice = await invoiceService.CreateAsync(subscriber.GatewayCustomerId,
                                                                   new StripeInvoiceCreateOptions
                    {
                        SubscriptionId = subscriber.GatewaySubscriptionId
                    });

                    if (invoice.AmountDue > 0)
                    {
                        await invoiceService.PayAsync(invoice.Id);
                    }
                }
                catch (StripeException) { }
            }
        }
Beispiel #2
0
        private async Task PreviewUpcomingAndPayAsync(Organization org, Plan plan)
        {
            var invoiceService  = new StripeInvoiceService();
            var upcomingPreview = await invoiceService.UpcomingAsync(org.StripeCustomerId,
                                                                     new StripeUpcomingInvoiceOptions
            {
                SubscriptionId = org.StripeSubscriptionId
            });

            var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data?
                                  .TakeWhile(i => i.Plan.Id == plan.StripeSeatPlanId && i.Proration).Sum(i => i.Amount);

            if (prorationAmount.GetValueOrDefault() >= 500)
            {
                try
                {
                    // Owes more than $5.00 on next invoice. Invoice them and pay now instead of waiting until next month.
                    var invoice = await invoiceService.CreateAsync(org.StripeCustomerId,
                                                                   new StripeInvoiceCreateOptions
                    {
                        SubscriptionId = org.StripeSubscriptionId
                    });

                    if (invoice.AmountDue > 0)
                    {
                        await invoiceService.PayAsync(invoice.Id);
                    }
                }
                catch (StripeException) { }
            }
        }