Skip to content

Commit

Permalink
encapsulated StripeSubscription and StripeCustomer
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Dec 4, 2024
1 parent 92c320b commit 02088c9
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Modules.Subscriptions.Features.DomainFeatures.StripeCustomers
{
public class StripeCustomer : Entity
{
public Guid UserId { get; set; }
public string StripePortalCustomerId { get; set; }
public Guid UserId { get; private set; }
public string StripePortalCustomerId { get; private set; }

public static StripeCustomer Create(Guid userId, string stripePortalCustomerId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptions.Appl
{
public class CreateTrialingSubscription : Command
{
public Guid UserId { get; set; }
public Guid TenantId { get; set; }
public string StripeCustomerId { get; set; }
public Stripe.Subscription CreatedStripeSubscription { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task HandleAsync(PauseActiveSubscription command, CancellationToken
{
var stripeSubscription = await module.SubscriptionsDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);

stripeSubscription.Status = StripeSubscriptionStatus.Paused;
stripeSubscription.UpdateStatus(StripeSubscriptionStatus.Paused);

await module.SubscriptionsDbContext.SaveChangesAsync(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public async Task HandleAsync(UpdateSubscriptionPeriod command, CancellationToke

if (stripeSubscription.Status != StripeSubscriptionStatus.Active)
{
stripeSubscription.Status = StripeSubscriptionStatus.Active;
stripeSubscription.UpdateStatus(StripeSubscriptionStatus.Active);
}
stripeSubscription.ExpirationDate = command.Subscription.CurrentPeriodEnd;
stripeSubscription.UpdateExpirationDate(command.Subscription.CurrentPeriodEnd);

await module.SubscriptionsDbContext.SaveChangesAsync(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
namespace Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptions.Application.Queries
{
[AuthorizeTenantAdmin]
public class GetSubscriptionForTenant : Query<object>
public class GetSubscriptionForTenant : Query<StripeSubscription>
{
public Guid UserId { get; set; }
public Guid TenantId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class StripeSubscription : Entity
{
private StripeSubscription() { }

public string StripePortalSubscriptionId { get; set; }
public Guid StripeCustomerId { get; set; }
public StripeCustomer StripeCustomer { get; set; }
public DateTime? ExpirationDate { get; set; }
public SubscriptionPlanType PlanType { get; set; }
public StripeSubscriptionStatus Status { get; set; }
public string StripePortalSubscriptionId { get; private set; }
public Guid StripeCustomerId { get; private set; }
public StripeCustomer StripeCustomer { get; private set; }
public DateTime? ExpirationDate { get; private set; }
public SubscriptionPlanType PlanType { get; private set; }
public StripeSubscriptionStatus Status { get; private set; }

public static StripeSubscription Create(
DateTime? expirationDate,
Expand All @@ -35,6 +35,16 @@ public static StripeSubscription Create(
StripeCustomer = stripeCustomer
};
}

public void UpdateStatus(StripeSubscriptionStatus status)
{
Status = status;
}

public void UpdateExpirationDate(DateTime expirationDate)
{
ExpirationDate = expirationDate;
}
}

public class StripeSubscriptionEFConfiguration : IEntityTypeConfiguration<StripeSubscription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ namespace Modules.Subscriptions.Server.Controllers
[AuthorizeTenantAdmin]
public class StripeSessionController : BaseController
{
public StripeSessionController(IServiceProvider serviceProvider) : base(serviceProvider)
{

}
public StripeSessionController(IServiceProvider serviceProvider) : base(serviceProvider) { }

[HttpPost("checkout/{subscriptionPlanType}")]
public async Task<ActionResult> RedirectToStripePremiumSubscription([FromRoute] SubscriptionPlanType subscriptionPlanType)
Expand Down
18 changes: 7 additions & 11 deletions Source/Modules/Subscriptions/Web/Server/WebHooks/StripeWebhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
using Microsoft.AspNetCore.Mvc;
using Stripe;
using Shared.Features.Server;
using Modules.Subscriptions.Features.Infrastructure.Configuration;
using Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptions.Application.Commands;
using Modules.Subscriptions.Features;

namespace Modules.Subscriptions.Server.WebHooks
{
[Route("api/[controller]")]
[ApiController]
public class StripeWebhook : BaseController
public class StripeWebhook : BaseController<SubscriptionsModule>
{
private readonly SubscriptionsConfiguration subscriptionConfiguration;
public StripeWebhook(SubscriptionsConfiguration subscriptionConfiguration, IServiceProvider serviceProvider) : base(serviceProvider)
{
this.subscriptionConfiguration = subscriptionConfiguration;
}
public StripeWebhook(IServiceProvider serviceProvider) : base(serviceProvider) { }

[HttpPost]
[IgnoreAntiforgeryToken]
Expand All @@ -28,7 +24,7 @@ public async Task<IActionResult> Index()
var stripeEvent = EventUtility.ParseEvent(json, false);
var signatureHeader = Request.Headers["Stripe-Signature"];
stripeEvent = EventUtility.ConstructEvent(json,
signatureHeader, subscriptionConfiguration.StripeEndpointSecret, throwOnApiVersionMismatch: false);
signatureHeader, module.SubscriptionsConfiguration.StripeEndpointSecret, throwOnApiVersionMismatch: false);

// Minimum Events copied from https://stripe.com/docs/billing/subscriptions/build-subscriptions
// Sent when a customer clicks the Pay or Subscribe button in Checkout, informing you of a new purchase. (Stripe)
Expand All @@ -41,7 +37,7 @@ public async Task<IActionResult> Index()

var createTrialingSubscription = new CreateTrialingSubscription
{
UserId = Guid.Parse(userId),
ExecutingUserId = Guid.Parse(userId),
StripeCustomerId = subscription.CustomerId,
CreatedStripeSubscription = subscription
};
Expand All @@ -57,10 +53,10 @@ public async Task<IActionResult> Index()

var updateSubscriptionPeriod = new UpdateSubscriptionPeriod
{

Subscription = subscription
};


await commandDispatcher.DispatchAsync(updateSubscriptionPeriod);
}
// Sent each billing interval if there is an issue with your customer’s payment method. (Stripe)
else if (stripeEvent.Type == Events.InvoicePaymentFailed)
Expand Down
3 changes: 2 additions & 1 deletion Source/Web/Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"SQLServerConnectionString_Dev": "Server=127.0.0.1,1433;Database=ModularMonolith;User Id=SA;Password=YourSTRONG!Passw0rd;Encrypt=False;"
},
"SubscriptionsConfiguration": {
"StripeProfessionalPlanId": "_"
"StripeProfessionalPlanId": "prod_PKIbSqk40BHlHI",
"StripeEndpointSecret": "whsec_bb85d6becda7bb888045cb77583e8c580ab4394dc99a5ad86c96dfedaa54902a"
},
"TenantIdentityConfiguration": {
"GoogleClientId": "_",
Expand Down

0 comments on commit 02088c9

Please sign in to comment.