Skip to content

Commit

Permalink
update stripe subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Mar 27, 2024
1 parent 56a4cdb commit 3b5ae4f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public async Task HandleAsync(CreateTrialingSubscription command, CancellationTo

var tenantId = new Guid(command.CreatedStripeSubscription.Metadata["TenantId"]);

var stripeSubscription = StripeSubscription.Create(command.CreatedStripeSubscription.TrialEnd, subscriptionType, StripeSubscriptionStatus.Trialing, tenantId, stripeCustomer);
var stripeSubscription = StripeSubscription.Create(
command.CreatedStripeSubscription.TrialEnd,
command.CreatedStripeSubscription.Id,
subscriptionType,
StripeSubscriptionStatus.Trialing,
tenantId,
stripeCustomer);

subscriptionDbContext.StripeSubscriptions.Add(stripeSubscription);

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

namespace Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptionAggregate.Application.Commands
{
public class UpdateSubscriptionPeriod : ICommand
public class PauseActiveSubscription : ICommand
{
public Stripe.Subscription Subscription { get; set; }
}

public class UpdateSubscriptionPerioEndCommandHandler : ICommandHandler<UpdateSubscriptionPeriod>
public class PauseActiveSubscriptionCommandHandler : ICommandHandler<PauseActiveSubscription>
{
public Task HandleAsync(UpdateSubscriptionPeriod command, CancellationToken cancellationToken)
public Task HandleAsync(PauseActiveSubscription command, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Shared.Features.CQRS.Command;
using Microsoft.EntityFrameworkCore;
using Modules.Subscription.Features.Infrastructure.Configuration;
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 +11,31 @@ public class UpdateSubscriptionPeriod : ICommand
public Stripe.Subscription Subscription { get; set; }
}

public class UpdateSubscriptionPerioEndCommandHandler : ICommandHandler<UpdateSubscriptionPeriod>
public class UpdateSubscriptionPerioEndCommandHandler : ServerExecutionBase, ICommandHandler<UpdateSubscriptionPeriod>
{
public Task HandleAsync(UpdateSubscriptionPeriod command, CancellationToken cancellationToken)
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)
{
throw new NotImplementedException();
var stripeSubscription = await subscriptionDbContext.StripeSubscriptions.FirstAsync(stripeSubscription => stripeSubscription.StripePortalSubscriptionId == command.Subscription.Id);

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

await subscriptionDbContext.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class StripeSubscription : AggregateRoot
{
private StripeSubscription() { }

public string StripePortalSubscriptionId { get; set; }
public Guid StripeCustomerId { get; set; }
public StripeCustomer StripeCustomer { get; set; }
public DateTime? ExpirationDate { get; set; }
Expand All @@ -16,6 +17,7 @@ private StripeSubscription() { }

public static StripeSubscription Create(
DateTime? expirationDate,
string stripePortalSubscriptionId,
SubscriptionPlanType subscriptionPlanType,
StripeSubscriptionStatus stripeSubscriptionStatus,
Guid tenantId,
Expand All @@ -24,6 +26,7 @@ public static StripeSubscription Create(
return new StripeSubscription()
{
ExpirationDate = expirationDate,
StripePortalSubscriptionId = stripePortalSubscriptionId,
PlanType = subscriptionPlanType,
Status = stripeSubscriptionStatus,
TenantId = tenantId,
Expand Down

0 comments on commit 3b5ae4f

Please sign in to comment.