mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 23:52:50 -05:00
billing updater program
This commit is contained in:
13
util/BillingUpdater/BillingUpdater.csproj
Normal file
13
util/BillingUpdater/BillingUpdater.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RootNamespace>Bit.BillingUpdater</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
53
util/BillingUpdater/Program.cs
Normal file
53
util/BillingUpdater/Program.cs
Normal file
@ -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<Organization>(
|
||||
"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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user