Skip to content

Commit

Permalink
stripeSubscription PortalId column
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Mar 28, 2024
1 parent 3b5ae4f commit 1a5849b
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Shared.Features.CQRS.Command;
using Microsoft.EntityFrameworkCore;
using Modules.Subscription.Features.Infrastructure.EFCore;
using Shared.Features.CQRS.Command;
using Shared.Features.Server;

namespace Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptionAggregate.Application.Commands
{
Expand All @@ -7,11 +10,24 @@ public class PauseActiveSubscription : ICommand
public Stripe.Subscription Subscription { get; set; }
}

public class PauseActiveSubscriptionCommandHandler : ICommandHandler<PauseActiveSubscription>
public class PauseActiveSubscriptionCommandHandler : ServerExecutionBase, ICommandHandler<PauseActiveSubscription>
{
public Task HandleAsync(PauseActiveSubscription command, CancellationToken cancellationToken)
private readonly SubscriptionsDbContext subscriptionDbContext;

public PauseActiveSubscriptionCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
IServiceProvider serviceProvider) : base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
}

public async Task HandleAsync(PauseActiveSubscription command, CancellationToken cancellationToken)
{
throw new NotImplementedException();
var stripeSubscription = await subscriptionDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);

stripeSubscription.Status = StripeSubscriptionStatus.Paused;

await subscriptionDbContext.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Modules.Subscription.Features.Infrastructure.Configuration;
using Modules.Subscription.Features.Infrastructure.EFCore;
using Shared.Features.CQRS.Command;
using Shared.Features.Server;
Expand All @@ -14,15 +13,12 @@ public class UpdateSubscriptionPeriod : ICommand
public class UpdateSubscriptionPerioEndCommandHandler : ServerExecutionBase, ICommandHandler<UpdateSubscriptionPeriod>
{
private readonly SubscriptionsDbContext subscriptionDbContext;
private readonly SubscriptionsConfiguration subscriptionConfiguration;

public UpdateSubscriptionPerioEndCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
SubscriptionsConfiguration subscriptionConfiguration,
IServiceProvider serviceProvider) : base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
this.subscriptionConfiguration = subscriptionConfiguration;
}

public async Task HandleAsync(UpdateSubscriptionPeriod command, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum StripeSubscriptionStatus
{
Active,
Trialing,
Paused
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ public class SubscriptionsConfiguration : IModuleConfiguration
public string StripeAPIKey { get; set; }
public string StripeEndpointSecret { get; set; }
public string StripeProfessionalPlanPriceId { get; set; }
public string StripeEnterprisePlanPriceId { get; set; }

public StripeSubscriptionType GetSubscriptionType(SubscriptionPlanType subscriptionPlanType)
public StripeSubscriptionPlan GetSubscriptionType(SubscriptionPlanType subscriptionPlanType)
{
return Subscriptions.Single(s => s.Type == subscriptionPlanType);
}

public List<StripeSubscriptionType> Subscriptions => new List<StripeSubscriptionType>()
public List<StripeSubscriptionPlan> Subscriptions => new List<StripeSubscriptionPlan>()
{
new StripeSubscriptionType
new StripeSubscriptionPlan
{
Type = SubscriptionPlanType.Professional,
TrialPeriodDays = 14,
StripePriceId = StripeProfessionalPlanPriceId
},
new StripeSubscriptionPlan
{
Type = SubscriptionPlanType.Enterprise,
TrialPeriodDays = 14,
StripePriceId = StripeEnterprisePlanPriceId
}
};
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Modules.Subscriptions.Features.Infrastructure.EFCore.Migrations
{
/// <inheritdoc />
public partial class StripeSubscriptionPortalId : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "StripePortalSubscriptionId",
schema: "Subscriptions",
table: "StripeSubscriptions",
type: "nvarchar(max)",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "StripePortalSubscriptionId",
schema: "Subscriptions",
table: "StripeSubscriptions");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid>("StripeCustomerId")
.HasColumnType("uniqueidentifier");

b.Property<string>("StripePortalSubscriptionId")
.HasColumnType("nvarchar(max)");

b.Property<Guid>("TenantId")
.HasColumnType("uniqueidentifier");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Modules.Subscriptions.Features.Infrastructure.StripePayments
{
public class CreateStripeCheckoutSession : ICommand<Stripe.Checkout.Session>
public class CreateStripeCheckoutSession : ICommand<Session>
{
public SubscriptionPlanType SubscriptionPlanType { get; set; }
public Guid UserId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class StripeSubscriptionPlan
{
public string StripePriceId { get; set; }
public SubscriptionPlanType Type { get; set; }
public int TrialPeriodDays { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptionAggregate.Application.Commands;
using Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptionAggregate.Application.Queries;
using Shared.Features.Server;
using Shared.Kernel.BuildingBlocks.Auth.Attributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Shared.Features.EFCore;
using Shared.Features.Modules;
using Shared.Features.Modules.Configuration;
using Stripe;
using System.Reflection;

namespace Modules.Subscription.Server
Expand All @@ -19,6 +20,8 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config
{
services.RegisterDbContext<SubscriptionsDbContext>();
services.RegisterModuleConfiguration<SubscriptionsConfiguration, SubscriptionsConfigurationValidator>(config);

StripeConfiguration.ApiKey = services.BuildServiceProvider().GetRequiredService<SubscriptionsConfiguration>().StripeAPIKey;
}

public void Configure(IApplicationBuilder app, IHostEnvironment env)
Expand Down

0 comments on commit 1a5849b

Please sign in to comment.