Browse Source

Added subscription portal

feature/billing
Teknikode 7 months ago
parent
commit
87d06e06cc
  1. 2
      BillingCore/BillingService.cs
  2. 13
      BillingCore/Models/PortalSession.cs
  3. 38
      BillingCore/StripeService.cs
  4. 4
      Teknik/Areas/User/Controllers/UserController.cs
  5. 2
      Teknik/Areas/User/ViewModels/SubscriptionViewModel.cs
  6. 2
      Teknik/Areas/User/Views/User/Settings/BillingSettings.cshtml

2
BillingCore/BillingService.cs

@ -36,6 +36,8 @@ namespace Teknik.BillingCore @@ -36,6 +36,8 @@ namespace Teknik.BillingCore
public abstract CheckoutSession CreateCheckoutSession(string customerId, string priceId, string successUrl, string cancelUrl);
public abstract CheckoutSession GetCheckoutSession(string sessionId);
public abstract PortalSession CreatePortalSession(string customerId, string returnUrl);
public abstract Task<Event> ParseEvent(HttpRequest request, string apiKey);
public abstract CheckoutSession ProcessCheckoutCompletedEvent(Event e);
public abstract Subscription ProcessSubscriptionEvent(Event e);

13
BillingCore/Models/PortalSession.cs

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.BillingCore.Models
{
public class PortalSession
{
public string Url { get; set; }
}
}

38
BillingCore/StripeService.cs

@ -264,6 +264,16 @@ namespace Teknik.BillingCore @@ -264,6 +264,16 @@ namespace Teknik.BillingCore
return ConvertCheckoutSession(session);
}
public override CheckoutSession GetCheckoutSession(string sessionId)
{
var checkoutService = new Stripe.Checkout.SessionService();
var sessionOptions = new Stripe.Checkout.SessionGetOptions();
sessionOptions.AddExpand("customer");
var session = checkoutService.Get(sessionId, sessionOptions);
return ConvertCheckoutSession(session);
}
public override async Task<Models.Event> ParseEvent(HttpRequest request, string apiKey)
{
var json = await new StreamReader(request.Body).ReadToEndAsync();
@ -300,14 +310,16 @@ namespace Teknik.BillingCore @@ -300,14 +310,16 @@ namespace Teknik.BillingCore
return ConvertSubscription(subscription);
}
public override CheckoutSession GetCheckoutSession(string sessionId)
public override PortalSession CreatePortalSession(string customerId, string returnUrl)
{
var checkoutService = new Stripe.Checkout.SessionService();
var sessionOptions = new Stripe.Checkout.SessionGetOptions();
sessionOptions.AddExpand("customer");
var session = checkoutService.Get(sessionId, sessionOptions);
return ConvertCheckoutSession(session);
var portalService = new Stripe.BillingPortal.SessionService();
var sessionOptions = new Stripe.BillingPortal.SessionCreateOptions()
{
Customer = customerId,
ReturnUrl = returnUrl
};
var session = portalService.Create(sessionOptions);
return ConvertPortalSession(session);
}
private Models.Product ConvertProduct(Stripe.Product product)
@ -415,7 +427,6 @@ namespace Teknik.BillingCore @@ -415,7 +427,6 @@ namespace Teknik.BillingCore
if (session == null)
return null;
var paymentStatus = PaymentStatus.Unpaid;
switch (session.PaymentStatus)
{
@ -440,6 +451,17 @@ namespace Teknik.BillingCore @@ -440,6 +451,17 @@ namespace Teknik.BillingCore
};
}
private PortalSession ConvertPortalSession(Stripe.BillingPortal.Session session)
{
if (session == null)
return null;
return new PortalSession()
{
Url = session.Url
};
}
private Models.Customer ConvertCustomer(Stripe.Customer customer)
{
var returnCust = new Models.Customer()

4
Teknik/Areas/User/Controllers/UserController.cs

@ -372,6 +372,7 @@ namespace Teknik.Areas.Users.Controllers @@ -372,6 +372,7 @@ namespace Teknik.Areas.Users.Controllers
foreach (var price in sub.Prices)
{
var product = billingService.GetProduct(price.ProductId);
var portalSession = billingService.CreatePortalSession(user.BillingCustomer.CustomerId, Url.SubRouteUrl("account", "User.BillingSettings"));
var subView = new SubscriptionViewModel()
{
SubscriptionId = sub.Id,
@ -379,7 +380,8 @@ namespace Teknik.Areas.Users.Controllers @@ -379,7 +380,8 @@ namespace Teknik.Areas.Users.Controllers
ProductName = product.Name,
Storage = price.Storage,
Price = price.Amount,
Interval = price.Interval.ToString()
Interval = price.Interval.ToString(),
PortalUrl = portalSession?.Url
};
model.Subscriptions.Add(subView);
}

2
Teknik/Areas/User/ViewModels/SubscriptionViewModel.cs

@ -19,5 +19,7 @@ namespace Teknik.Areas.Users.ViewModels @@ -19,5 +19,7 @@ namespace Teknik.Areas.Users.ViewModels
public string Interval { get; set; }
public long Storage { get; set; }
public string PortalUrl { get; set; }
}
}

2
Teknik/Areas/User/Views/User/Settings/BillingSettings.cshtml

@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
{
<li class="list-group-item">
<h4 class="list-group-item-heading">@subscription.ProductName: <strong>@(StringHelper.GetBytesReadable(subscription.Storage))</strong> for <strong>@($"${subscription.Price:0.00} / {subscription.Interval}")</strong></h4>
<p><a href="@(Url.SubRouteUrl("billing", "Billing.CancelSubscription", new { subscriptionId = subscription.SubscriptionId, productId = subscription.ProductId }))">Cancel Subscription</a></p>
<p><a href="@subscription.PortalUrl">Subscription Details</a> | <a href="@(Url.SubRouteUrl("billing", "Billing.CancelSubscription", new { subscriptionId = subscription.SubscriptionId, productId = subscription.ProductId }))">Cancel Subscription</a></p>
</li>
}
}

Loading…
Cancel
Save