diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index a04d21909d..7e022b2014 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -33,6 +33,7 @@ using Bit.Api.IdentityServer; using Bit.Core.Enums; using Microsoft.AspNetCore.DataProtection; using Microsoft.WindowsAzure.Storage; +using Stripe; namespace Bit.Api { @@ -83,6 +84,9 @@ namespace Bit.Api .ProtectKeysWithCertificate(dataProtectionCert); } + // Stripe Billing + StripeConfiguration.SetApiKey(globalSettings.StripeApiKey); + // Repositories services.AddSingleton(); services.AddSingleton(); diff --git a/src/Api/settings.json b/src/Api/settings.json index c988704017..f407acb316 100644 --- a/src/Api/settings.json +++ b/src/Api/settings.json @@ -3,6 +3,7 @@ "siteName": "bitwarden", "baseVaultUri": "http://localhost:4001/#", "jwtSigningKey": "THIS IS A SECRET. IT KEEPS YOUR TOKEN SAFE. :)", + "stripeApiKey": "SECRET", "sqlServer": { "connectionString": "SECRET" }, diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index cddb6183a6..09f44db549 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -22,6 +22,7 @@ + diff --git a/src/Core/Enums/PlanType.cs b/src/Core/Enums/PlanType.cs index 64bef70fec..657352dc21 100644 --- a/src/Core/Enums/PlanType.cs +++ b/src/Core/Enums/PlanType.cs @@ -3,7 +3,7 @@ public enum PlanType : byte { Free = 0, - Family = 1, + Personal = 1, Teams = 2, Enterprise = 3, Custom = 4 diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index 1b3e79b661..2a072219e6 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -5,6 +5,7 @@ public virtual string SiteName { get; set; } public virtual string BaseVaultUri { get; set; } public virtual string JwtSigningKey { get; set; } + public virtual string StripeApiKey { get; set; } public virtual SqlServerSettings SqlServer { get; set; } = new SqlServerSettings(); public virtual MailSettings Mail { get; set; } = new MailSettings(); public virtual PushSettings Push { get; set; } = new PushSettings(); diff --git a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs index e226aa82c7..870d8dc5b8 100644 --- a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs @@ -10,6 +10,7 @@ namespace Bit.Core.Models.Api public string Name { get; set; } public PlanType PlanType { get; set; } public string Key { get; set; } + public string CardToken { get; set; } public virtual OrganizationSignup ToOrganizationSignup(User user) { @@ -18,7 +19,8 @@ namespace Bit.Core.Models.Api Owner = user, OwnerKey = Key, Name = Name, - Plan = PlanType + Plan = PlanType, + PaymentToken = CardToken }; } } diff --git a/src/Core/Models/Business/OrganizationSignup.cs b/src/Core/Models/Business/OrganizationSignup.cs index c11baee759..340b561f4f 100644 --- a/src/Core/Models/Business/OrganizationSignup.cs +++ b/src/Core/Models/Business/OrganizationSignup.cs @@ -9,12 +9,6 @@ namespace Bit.Core.Models.Business public User Owner { get; set; } public string OwnerKey { get; set; } public Enums.PlanType Plan { get; set; } - public PaymentDetails Payment { get; set; } - - public class PaymentDetails - { - public string Name { get; set; } - public string Token { get; set; } - } + public string PaymentToken { get; set; } } } diff --git a/src/Core/Models/StaticStore/Plan.cs b/src/Core/Models/StaticStore/Plan.cs index 1c3baaed4b..3663eb9f39 100644 --- a/src/Core/Models/StaticStore/Plan.cs +++ b/src/Core/Models/StaticStore/Plan.cs @@ -5,6 +5,8 @@ namespace Bit.Core.Models.StaticStore { public class Plan { + public string Name { get; set; } + public string StripeId { get; set; } public PlanType Type { get; set; } public short MaxUsers { get; set; } public decimal Price { get; set; } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index e1eacddd08..134630db06 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -8,6 +8,7 @@ using Bit.Core.Utilities; using Bit.Core.Exceptions; using System.Collections.Generic; using Microsoft.AspNetCore.DataProtection; +using Stripe; namespace Bit.Core.Services { @@ -47,6 +48,15 @@ namespace Bit.Core.Services throw new BadRequestException("Plan not found."); } + var customerService = new StripeCustomerService(); + var customer = await customerService.CreateAsync(new StripeCustomerCreateOptions + { + SourceToken = signup.PaymentToken + }); + + var subscriptionService = new StripeSubscriptionService(); + var subscription = await subscriptionService.CreateAsync(customer.Id, plan.StripeId); + var organization = new Organization { Name = signup.Name, diff --git a/src/Core/Utilities/StaticStore.cs b/src/Core/Utilities/StaticStore.cs index cdaacae55e..a70b2dc8d0 100644 --- a/src/Core/Utilities/StaticStore.cs +++ b/src/Core/Utilities/StaticStore.cs @@ -99,11 +99,14 @@ namespace Bit.Core.Utilities }, new Plan { - Type = PlanType.Family, + Type = PlanType.Personal, MaxUsers = 5, Price = 1, Trial = new TimeSpan(14, 0, 0, 0), - Cycle = now => now.AddYears(1) - now + Cycle = now => now.AddYears(1) - now, + Name = "Personal", + StripeId = "premium-yearly" + }, new Plan {