mirror of
https://github.com/bitwarden/server.git
synced 2025-04-07 14:08:13 -05:00
premium renewal reminders job for braintree
This commit is contained in:
parent
476ee53931
commit
938b7f1230
@ -2,6 +2,7 @@
|
||||
{
|
||||
public class BillingSettings
|
||||
{
|
||||
public virtual string JobsKey { get; set; }
|
||||
public virtual string StripeWebhookKey { get; set; }
|
||||
public virtual string StripeWebhookSecret { get; set; }
|
||||
public virtual string BraintreeWebhookKey { get; set; }
|
||||
|
57
src/Billing/Controllers/JobsController.cs
Normal file
57
src/Billing/Controllers/JobsController.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using Bit.Core;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.Billing.Controllers
|
||||
{
|
||||
[Route("jobs")]
|
||||
public class JobsController : Controller
|
||||
{
|
||||
private readonly BillingSettings _billingSettings;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMailService _mailService;
|
||||
|
||||
public JobsController(
|
||||
IOptions<BillingSettings> billingSettings,
|
||||
GlobalSettings globalSettings,
|
||||
IUserRepository userRepository,
|
||||
IMailService mailService)
|
||||
{
|
||||
_billingSettings = billingSettings?.Value;
|
||||
_globalSettings = globalSettings;
|
||||
_userRepository = userRepository;
|
||||
_mailService = mailService;
|
||||
}
|
||||
|
||||
[HttpPost("premium-renewal-reminders")]
|
||||
public async Task<IActionResult> GetPremiumRenewalReminders([FromQuery] string key)
|
||||
{
|
||||
if(key != _billingSettings.JobsKey)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
var users = await _userRepository.GetManyByPremiumRenewalAsync();
|
||||
foreach(var user in users)
|
||||
{
|
||||
var paymentService = user.GetPaymentService(_globalSettings);
|
||||
var upcomingInvoice = await paymentService.GetUpcomingInvoiceAsync(user);
|
||||
if(upcomingInvoice?.Date != null)
|
||||
{
|
||||
var items = new List<string> { "1 × Premium Membership (Annually)" };
|
||||
await _mailService.SendInvoiceUpcomingAsync(user.Email, upcomingInvoice.Amount,
|
||||
upcomingInvoice.Date.Value, items, false);
|
||||
}
|
||||
await _userRepository.UpdateRenewalReminderDateAsync(user.Id, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
}
|
||||
}
|
@ -46,6 +46,7 @@
|
||||
}
|
||||
},
|
||||
"billingSettings": {
|
||||
"jobsKey": "SECRET",
|
||||
"stripeWebhookKey": "SECRET",
|
||||
"stripeWebhookSecret": "SECRET",
|
||||
"braintreeWebhookKey": "SECRET"
|
||||
|
@ -10,8 +10,10 @@ namespace Bit.Core.Repositories
|
||||
Task<User> GetByEmailAsync(string email);
|
||||
Task<ICollection<User>> SearchAsync(string email, int skip, int take);
|
||||
Task<ICollection<User>> GetManyByPremiumAsync(bool premium);
|
||||
Task<ICollection<User>> GetManyByPremiumRenewalAsync();
|
||||
Task<string> GetPublicKeyAsync(Guid id);
|
||||
Task<DateTime> GetAccountRevisionDateAsync(Guid id);
|
||||
Task UpdateStorageAsync(Guid id);
|
||||
Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate);
|
||||
}
|
||||
}
|
||||
|
@ -63,6 +63,18 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<User>> GetManyByPremiumRenewalAsync()
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<User>(
|
||||
"[dbo].[User_ReadByPremiumRenewal]",
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetPublicKeyAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
@ -117,5 +129,16 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
commandTimeout: 180);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
$"[{Schema}].[User_UpdateRenewalReminderDate]",
|
||||
new { Id = id, RenewalReminderDate = renewalReminderDate },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace Bit.Core.Services
|
||||
Task CancelSubscriptionAsync(ISubscriber subscriber, bool endOfPeriod = false);
|
||||
Task ReinstateSubscriptionAsync(ISubscriber subscriber);
|
||||
Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, string paymentToken);
|
||||
Task<BillingInfo.BillingInvoice> GetUpcomingInvoiceAsync(ISubscriber subscriber);
|
||||
Task<BillingInfo> GetBillingAsync(ISubscriber subscriber);
|
||||
}
|
||||
}
|
||||
|
@ -156,6 +156,24 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BillingInfo.BillingInvoice> GetUpcomingInvoiceAsync(ISubscriber subscriber)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
|
||||
{
|
||||
var sub = await _gateway.Subscription.FindAsync(subscriber.GatewaySubscriptionId);
|
||||
if(sub != null)
|
||||
{
|
||||
var cancelAtEndDate = !sub.NeverExpires.GetValueOrDefault();
|
||||
var cancelled = sub.Status == SubscriptionStatus.CANCELED;
|
||||
if(!cancelled && !cancelAtEndDate && sub.NextBillingDate.HasValue)
|
||||
{
|
||||
return new BillingInfo.BillingInvoice(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<BillingInfo> GetBillingAsync(ISubscriber subscriber)
|
||||
{
|
||||
var billingInfo = new BillingInfo();
|
||||
|
@ -320,6 +320,32 @@ namespace Bit.Core.Services
|
||||
return updatedSubscriber;
|
||||
}
|
||||
|
||||
public async Task<BillingInfo.BillingInvoice> GetUpcomingInvoiceAsync(ISubscriber subscriber)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
|
||||
{
|
||||
var subscriptionService = new StripeSubscriptionService();
|
||||
var invoiceService = new StripeInvoiceService();
|
||||
var sub = await subscriptionService.GetAsync(subscriber.GatewaySubscriptionId);
|
||||
if(sub != null)
|
||||
{
|
||||
if(!sub.CanceledAt.HasValue && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
|
||||
{
|
||||
try
|
||||
{
|
||||
var upcomingInvoice = await invoiceService.UpcomingAsync(subscriber.GatewayCustomerId);
|
||||
if(upcomingInvoice != null)
|
||||
{
|
||||
return new BillingInfo.BillingInvoice(upcomingInvoice);
|
||||
}
|
||||
}
|
||||
catch(StripeException) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<BillingInfo> GetBillingAsync(ISubscriber subscriber)
|
||||
{
|
||||
var billingInfo = new BillingInfo();
|
||||
|
@ -226,5 +226,7 @@
|
||||
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationIdEmail.sql" />
|
||||
<Build Include="dbo\Stored Procedures\CipherDetails_ReadWithoutOrganizationsByUserId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\CollectionCipher_UpdateCollectionsForCiphers.sql" />
|
||||
<Build Include="dbo\Stored Procedures\User_ReadByPremiumRenewal.sql" />
|
||||
<Build Include="dbo\Stored Procedures\User_UpdateRenewalReminderDate.sql" />
|
||||
</ItemGroup>
|
||||
</Project>
|
23
src/Sql/dbo/Stored Procedures/User_ReadByPremiumRenewal.sql
Normal file
23
src/Sql/dbo/Stored Procedures/User_ReadByPremiumRenewal.sql
Normal file
@ -0,0 +1,23 @@
|
||||
CREATE PROCEDURE [dbo].[User_ReadByPremiumRenewal]
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
DECLARE @WindowRef DATETIME2(7) = GETUTCDATE()
|
||||
DECLARE @WindowStart DATETIME2(7) = DATEADD (day, -15, @WindowRef)
|
||||
DECLARE @WindowEnd DATETIME2(7) = DATEADD (day, 15, @WindowRef)
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[UserView]
|
||||
WHERE
|
||||
[Premium] = 1
|
||||
AND [PremiumExpirationDate] >= @WindowRef
|
||||
AND [PremiumExpirationDate] < @WindowEnd
|
||||
AND (
|
||||
[RenewalReminderDate] IS NULL
|
||||
OR [RenewalReminderDate] < @WindowStart
|
||||
)
|
||||
AND [Gateway] = 1 -- Braintree
|
||||
END
|
@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[User_UpdateRenewalReminderDate]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@RenewalReminderDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE
|
||||
[dbo].[User]
|
||||
SET
|
||||
[RenewalReminderDate] = @RenewalReminderDate
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
@ -7,6 +7,59 @@ BEGIN
|
||||
END
|
||||
GO
|
||||
|
||||
IF OBJECT_ID('[dbo].[User_UpdateRenewalReminderDate]') IS NOT NULL
|
||||
BEGIN
|
||||
DROP PROCEDURE [dbo].[User_UpdateRenewalReminderDate]
|
||||
END
|
||||
GO
|
||||
|
||||
CREATE PROCEDURE [dbo].[User_UpdateRenewalReminderDate]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@RenewalReminderDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE
|
||||
[dbo].[User]
|
||||
SET
|
||||
[RenewalReminderDate] = @RenewalReminderDate
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
||||
GO
|
||||
|
||||
IF OBJECT_ID('[dbo].[User_ReadByPremiumRenewal]') IS NOT NULL
|
||||
BEGIN
|
||||
DROP PROCEDURE [dbo].[User_ReadByPremiumRenewal]
|
||||
END
|
||||
GO
|
||||
|
||||
CREATE PROCEDURE [dbo].[User_ReadByPremiumRenewal]
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
DECLARE @WindowRef DATETIME2(7) = GETUTCDATE()
|
||||
DECLARE @WindowStart DATETIME2(7) = DATEADD (day, -15, @WindowRef)
|
||||
DECLARE @WindowEnd DATETIME2(7) = DATEADD (day, 15, @WindowRef)
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[UserView]
|
||||
WHERE
|
||||
[Premium] = 1
|
||||
AND [PremiumExpirationDate] >= @WindowRef
|
||||
AND [PremiumExpirationDate] < @WindowEnd
|
||||
AND (
|
||||
[RenewalReminderDate] IS NULL
|
||||
OR [RenewalReminderDate] < @WindowStart
|
||||
)
|
||||
AND [Gateway] = 1 -- Braintree
|
||||
END
|
||||
GO
|
||||
|
||||
IF OBJECT_ID('[dbo].[Collection_ReadByUserId]') IS NOT NULL
|
||||
BEGIN
|
||||
DROP PROCEDURE [dbo].[Collection_ReadByUserId]
|
||||
|
Loading…
x
Reference in New Issue
Block a user