From 6573bf44ab96bed2d309c004c98c37b82afa332f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 8 Sep 2017 11:43:47 -0400 Subject: [PATCH] billing updater program --- bitwarden-core.sln | 9 +++- util/BillingUpdater/BillingUpdater.csproj | 13 ++++++ util/BillingUpdater/Program.cs | 53 +++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 util/BillingUpdater/BillingUpdater.csproj create mode 100644 util/BillingUpdater/Program.cs diff --git a/bitwarden-core.sln b/bitwarden-core.sln index 0e8fbe4a3..ffceb40e3 100644 --- a/bitwarden-core.sln +++ b/bitwarden-core.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.3 +VisualStudioVersion = 15.0.26730.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}" EndProject @@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jobs", "src\Jobs\Jobs.cspro EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper", "..\Dapper\Dapper\Dapper.csproj", "{6951E73D-1761-41F6-B5D3-BEF4C2F73EA3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BillingUpdater", "util\BillingUpdater\BillingUpdater.csproj", "{A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -89,6 +91,10 @@ Global {6951E73D-1761-41F6-B5D3-BEF4C2F73EA3}.Debug|Any CPU.Build.0 = Debug|Any CPU {6951E73D-1761-41F6-B5D3-BEF4C2F73EA3}.Release|Any CPU.ActiveCfg = Release|Any CPU {6951E73D-1761-41F6-B5D3-BEF4C2F73EA3}.Release|Any CPU.Build.0 = Release|Any CPU + {A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -104,6 +110,7 @@ Global {66B0A682-658A-4A82-B606-A077A4871448} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84E} {7DCEBD8F-E5F3-4A3C-BD35-B64341590B74} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} {6951E73D-1761-41F6-B5D3-BEF4C2F73EA3} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} + {A0FBA4DF-2F24-45A6-B188-EBDBD2FAF445} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E01CBF68-2E20-425F-9EDB-E0A6510CA92F} diff --git a/util/BillingUpdater/BillingUpdater.csproj b/util/BillingUpdater/BillingUpdater.csproj new file mode 100644 index 000000000..2bd0da58b --- /dev/null +++ b/util/BillingUpdater/BillingUpdater.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp2.0 + Bit.BillingUpdater + + + + + + + diff --git a/util/BillingUpdater/Program.cs b/util/BillingUpdater/Program.cs new file mode 100644 index 000000000..979b4e116 --- /dev/null +++ b/util/BillingUpdater/Program.cs @@ -0,0 +1,53 @@ +using System; +using System.Data.SqlClient; +using System.Threading.Tasks; +using Bit.Core.Models.Table; +using Dapper; +using Stripe; + +namespace Bit.BillingUpdater +{ + public class Program + { + public static void Main(string[] args) + { + MainAsync().Wait(); + } + + public async static Task MainAsync() + { + var connectionString = ""; + var stripeApiKey = ""; + var stripeSubscriptionService = new StripeSubscriptionService(stripeApiKey); + + using(var connection = new SqlConnection(connectionString)) + { + //Paid orgs + + var orgs = await connection.QueryAsync( + "SELECT * FROM [Organization] WHERE [Enabled] = 1 AND AND GatewaySubscriptionId IS NOT NULL"); + + foreach(var org in orgs) + { + DateTime? expDate = null; + if(org.Gateway == Core.Enums.GatewayType.Stripe) + { + var sub = await stripeSubscriptionService.GetAsync(org.GatewaySubscriptionId); + if(sub != null) + { + expDate = sub.CurrentPeriodEnd; + } + } + + if(expDate.HasValue) + { + Console.WriteLine("Updating org {0} exp to {1}.", org.Id, expDate.Value); + await connection.ExecuteAsync( + "UPDATE [Organization] SET [ExpirationDate] = @Date WHERE [Id] = @Id", + new { Date = expDate, Id = org.Id }); + } + } + } + } + } +}