Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Api/Controllers/PaymentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ public PaymentsController(ILogger<PaymentsController> logger, SubscriptionReposi
StripeConfiguration.ApiKey = stripeSettings.ApiKey;
}

[Authorize]
[HttpGet("manage")]
public async Task<IActionResult> Manage()
{
var userId = GetUserId();
var lastSubscription = await subscriptionRepository.GetLastSubscriptionByUserId(userId);
if (lastSubscription == null)
{
return BadRequest("User has no subscription");
}

string referrer = "";
if (Request.Headers.ContainsKey("Referer"))
{
referrer = Request.Headers["Referer"].ToString();
}
if (!referrer.StartsWith(frontendUrl)) {
referrer = frontendUrl;
}

var service = new Stripe.BillingPortal.SessionService();
var options = new Stripe.BillingPortal.SessionCreateOptions
{
Customer = lastSubscription.CustomerId,
ReturnUrl = referrer,
};
var session = await service.CreateAsync(options);
var portalUrl = session.Url;

return Ok(portalUrl);
}

[Authorize]
[HttpGet("intent")]
public async Task<IActionResult> CreateCheckoutSession()
Expand Down Expand Up @@ -112,7 +144,8 @@ await subscriptionRepository.UpsertSubscription(
CustomerId = subscription.CustomerId,
Status = subscription.Status.ToSubscriptionStatus(),
ExpiresOn = subscription.CurrentPeriodEnd,
CreatedOn = stripeEvent.Created
CreatedOn = stripeEvent.Created,
CancelAtPeriodEnd = subscription.CancelAtPeriodEnd
}
);
return Ok();
Expand Down
9 changes: 8 additions & 1 deletion Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public async Task<UserInfo> GetUserInfo()
{

var isPremium = await subscriptionRepository.IsUserPremium(GetUserId());
return new UserInfo() { IsPremium = isPremium, ChatData = new ChatData() };
DateTime? expireDate = await subscriptionRepository.GetExpireDate(GetUserId());
bool? isAutoRenewActive = await subscriptionRepository.IsAutoRenewActive(GetUserId());
return new UserInfo() {
IsPremium = isPremium,
ExpireDate = expireDate,
IsAutoRenewActive = isAutoRenewActive,
ChatData = new ChatData()
};
}
}
2 changes: 2 additions & 0 deletions Api/Dto/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class UserInfo
{
public bool IsPremium { get; set; }
public DateTime? ExpireDate { get; set; }
public bool? IsAutoRenewActive { get; set; }
public ChatData ChatData { get; set; } = null!;
}

Expand Down
20 changes: 20 additions & 0 deletions Application/Payments/SubscriptionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,24 @@ public async Task<bool> IsUserPremium(string userId)
.FirstOrDefaultAsync(); //take the last added
return result?.Status == SubscriptionStatus.Active; //and check if it is active
}

public async Task<DateTime?> GetExpireDate(string userId)
{
var result = await context.Subscriptions //take the subscriptions
.Where(s => s.UserId == userId //of the user
&& s.ExpiresOn > DateTime.UtcNow) //that are not expired
.OrderByDescending(s => s.CreatedOn)
.FirstOrDefaultAsync(); //take the last added
return result?.ExpiresOn;
}

public async Task<bool?> IsAutoRenewActive(string userId)
{
var result = await context.Subscriptions //take the subscriptions
.Where(s => s.UserId == userId //of the user
&& s.ExpiresOn > DateTime.UtcNow) //that are not expired
.OrderByDescending(s => s.CreatedOn)
.FirstOrDefaultAsync(); //take the last added
return !(result?.CancelAtPeriodEnd ?? false);
}
}
2 changes: 2 additions & 0 deletions Domain/Subscription/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public partial class Subscription
public DateTime ExpiresOn { get; set; }

public DateTime CreatedOn { get; set; }

public bool CancelAtPeriodEnd { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");

b.Property<bool>("CancelAtPeriodEnd")
.HasColumnType("bit");

b.Property<DateTime>("CreatedOn")
.HasColumnType("datetime2");

Expand Down

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

29 changes: 29 additions & 0 deletions Infrastructure/Migrations/20231004175522_HandleSubCancelation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace AiPlugin.Migrations
{
/// <inheritdoc />
public partial class HandleSubCancelation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "CancelAtPeriodEnd",
table: "Subscriptions",
type: "bit",
nullable: false,
defaultValue: false);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CancelAtPeriodEnd",
table: "Subscriptions");
}
}
}