Beispiel #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);
        }
Beispiel #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));
        }
        public creating_and_updating_invoices_with_manual_invoicing()
        {
            var customerService    = new StripeCustomerService(Cache.ApiKey);
            var invoiceItemService = new StripeInvoiceItemService(Cache.ApiKey);
            var invoiceService     = new StripeInvoiceService(Cache.ApiKey);

            var CustomerCreateOptions = new StripeCustomerCreateOptions
            {
                Email = "*****@*****.**",
            };
            var Customer = customerService.Create(CustomerCreateOptions);

            var InvoiceItemCreateOptions = new StripeInvoiceItemCreateOptions
            {
                Amount     = 1000,
                Currency   = "usd",
                CustomerId = Customer.Id
            };
            var InvoiceItem = invoiceItemService.Create(InvoiceItemCreateOptions);

            var InvoiceCreateOptions = new StripeInvoiceCreateOptions
            {
                Billing      = StripeBilling.SendInvoice,
                DaysUntilDue = 7,
            };

            InvoiceCreated = invoiceService.Create(Customer.Id, InvoiceCreateOptions);

            var InvoiceUpdateOptions = new StripeInvoiceUpdateOptions
            {
                Paid = true,
            };

            InvoiceUpdated = invoiceService.Update(InvoiceCreated.Id, InvoiceUpdateOptions);
        }
Beispiel #4
0
        private object InvoiceCreated(StripeCreateInvoice invoiceData)
        {
            using (var db = new Email2SmsContext())
            {
                var sub = db.Subscriptions.Where(f => f.StripeCustomer == invoiceData.Customer).FirstOrDefault();
                if (sub == null)
                {
                    Metrics.Error($"Tried to create invoice for {invoiceData.Customer}, who doesn't seem to be a subscriber");
                    return("Not Known");
                }

                var end = DateTimeOffset.FromUnixTimeSeconds(invoiceData.Period_End).UtcDateTime;

                var charges = db.InvoiceItems.Where(
                    f => f.SendTime < end &&
                    f.SendTime >= f.SendTo.Subscription.LastInvoiceUtc &&
                    f.SendTo.Subscription.StripeCustomer == invoiceData.Customer
                    ).Select(f => f.Price).ToArray();
                var sum = (int)((charges.Sum() ?? 0.0M) * 100);
                if (sum > 0)
                {
                    var stripe = new StripeInvoiceItemService(ConfigurationManager.AppSettings["stripe:token_secret"]);
                    Metrics.Info($"Charging {sub.StripeCustomer} ${charges.Sum().Value} for {charges.Length} messages");
                    var response = stripe.Create(new StripeInvoiceItemCreateOptions
                    {
                        Amount      = sum,
                        Currency    = "usd",
                        CustomerId  = invoiceData.Customer,
                        Description = $"Messages x {charges.Length}"
                    });
                }

                sub.LastInvoiceUtc = end;
                db.SaveChanges();
            }
            return("OK");
        }
Beispiel #5
0
        public StripeInvoiceItemServiceTest()
        {
            this.service = new StripeInvoiceItemService();

            this.createOptions = new StripeInvoiceItemCreateOptions()
            {
                Amount     = 123,
                Currency   = "usd",
                CustomerId = "cus_123",
            };

            this.updateOptions = new StripeInvoiceItemUpdateOptions()
            {
                Metadata = new Dictionary <string, string>()
                {
                    { "key", "value" },
                },
            };

            this.listOptions = new StripeInvoiceItemListOptions()
            {
                Limit = 1,
            };
        }