private AzureUsage GetAzureDataUsage()
        {
            var azureAuthorizationToken = ConfigHelper.GetAzureApiManagementKey();

            var urlFormat = "https://{0}/reports/byUser?$filter=timestamp ge datetime'{1}T00:00:00' and timestamp le datetime'{2}T23:59:59' &api-version=2014-02-14";

            WebClient client = new WebClient();

            client.Headers["Authorization"] = azureAuthorizationToken;


            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime endDate   = startDate.AddMonths(1).AddDays(-1);

            string response = client.DownloadString(string.Format(urlFormat, ConfigHelper.GetApiManagementDomain(), startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd")));


            RootObject json = Newtonsoft.Json.JsonConvert.DeserializeObject <RootObject>(response);

            AzureUsage usage = new AzureUsage();

            usage.CustomerName = "Dave Nielsen";
            usage.Description  = "Invoice " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
            ;
            usage.Month       = DateTime.Now.Year + "-" + DateTime.Now.ToString("MMMM");
            usage.RatePerCall = 5;
            usage.TotalCalls  = json.value.Sum(s => s.callCountSuccess);
            usage.TotalAmount = usage.RatePerCall * usage.TotalCalls;

            return(usage);
        }
        public ActionResult CreateInvoice(string id)
        {
            AzureUsage usage = GetAzureDataUsage();

            usage.StripeCustomerId = id;

            Stripe.StripeInvoiceItemService invoiceItemService = new Stripe.StripeInvoiceItemService();
            invoiceItemService.ApiKey = ConfigHelper.GetStripeApiKey();
            invoiceItemService.Create(new Stripe.StripeInvoiceItemCreateOptions
            {
                Amount      = usage.TotalAmount * 100,
                Currency    = "usd",
                CustomerId  = id,
                Description = usage.Description,
            });

            Response.Redirect(Url.RouteUrl(new { controller = "Metered", action = "Customer", id = id }));
            return(View());
        }