예제 #1
0
        public async Task <bool> CreateInvoiceAsync(Domain.Order order)
        {
            var customer = await getOrCreateCustomer(order);

            var service = new StripeInvoiceItemService();

            foreach (var line in order.OrderLines)
            {
                var createInvoiceLineOptions = new StripeInvoiceItemCreateOptions
                {
                    Amount      = (int)(line.LineTotal * 100m), // inclusive of quantity & tax
                    Currency    = "nok",                        // TODO: read this from config
                    CustomerId  = customer.Id,
                    Description = !string.IsNullOrWhiteSpace(line.ProductVariantName) ? $"{line.ProductName} ({line.ProductVariantName})" : line.ProductName,
                };
                var invoiceLineItem = await service.CreateAsync(createInvoiceLineOptions);
            }

            var eventInfo            = order.Registration.EventInfo;
            var createInvoiceOptions = new StripeInvoiceCreateOptions
            {
                Billing      = StripeBilling.SendInvoice,
                DaysUntilDue = eventInfo.DateStart.HasValue ? ((eventInfo.LastCancellationDate ?? eventInfo.LastRegistrationDate ?? eventInfo.DateStart) - DateTime.UtcNow).Value.Days : 30,
                Description  = $"Deltakelse for {order.Registration.ParticipantName} på {order.Registration.EventInfo.Title} "
            };
            var createInvoiceService = new StripeInvoiceService();
            await createInvoiceService.CreateAsync(customer.Id, createInvoiceOptions);

            return(true);
        }
예제 #2
0
        public async Task <InvoiceResult> CreateInvoiceAsync(InvoiceInfo info)
        {
            var customer = await GetOrCreateCustomer(info);

            var service = new StripeInvoiceItemService();

            foreach (var line in info.Lines.Where(l => l.Type == InvoiceLineType.Product))
            {
                await service.CreateAsync(new StripeInvoiceItemCreateOptions
                {
                    Amount      = (int)(line.Total ?? 0 * 100m), // inclusive of quantity & tax
                    Currency    = line.Currency,
                    CustomerId  = customer.Id,
                    Description = line.Description,
                });
            }

            var createInvoiceOptions = new StripeInvoiceCreateOptions
            {
                Billing      = StripeBilling.SendInvoice,
                DaysUntilDue = info.DueDate.HasValue
                    ? (info.DueDate.Value - SystemClock.Instance.Today()).Days
                    : 30,
                Description = string.Join(", ", info.Lines
                                          .Where(l => l.Type == InvoiceLineType.Text)
                                          .Select(l => l.Description))
            };
            var createInvoiceService = new StripeInvoiceService();
            var stripeInvoice        = await createInvoiceService.CreateAsync(customer.Id, createInvoiceOptions);

            return(new InvoiceResult(stripeInvoice.Id));
        }
예제 #3
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) { }
            }
        }
예제 #4
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) { }
            }
        }