1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00
bitwarden-server/util/BillingUpdater/Program.cs

54 lines
1.7 KiB
C#
Raw Normal View History

2017-09-08 17:43:47 +02:00
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 });
}
}
}
}
}
}