Skip to content
Draft
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion src/Exceptionless.Core/Exceptionless.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.8" />
<PackageReference Include="Stripe.net" Version="47.4.0" />
<PackageReference Include="Stripe.net" Version="48.0.2" />
<PackageReference Include="System.DirectoryServices" Version="9.0.8" />
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Foundatio.Repositories.Elasticsearch" Version="7.17.17" Condition="'$(ReferenceFoundatioRepositoriesSource)' == '' OR '$(ReferenceFoundatioRepositoriesSource)' == 'false'" />
Expand Down
132 changes: 119 additions & 13 deletions src/Exceptionless.Web/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@ public async Task<ActionResult<Invoice>> GetInvoiceAsync(string id)
{
var client = new StripeClient(_options.StripeOptions.StripeApiKey);
var invoiceService = new InvoiceService(client);
stripeInvoice = await invoiceService.GetAsync(id);

// In Stripe.net v48, expand to include all necessary price information
var options = new InvoiceGetOptions
{
Expand = new List<string> { "lines", "lines.data.price" }
};
stripeInvoice = await invoiceService.GetAsync(id, options);
}
catch (Exception ex)
{
Expand All @@ -239,26 +245,102 @@ public async Task<ActionResult<Invoice>> GetInvoiceAsync(string id)
OrganizationId = organization.Id,
OrganizationName = organization.Name,
Date = stripeInvoice.Created,
Paid = stripeInvoice.Paid,
Paid = String.Equals(stripeInvoice.Status, "paid"),
Total = stripeInvoice.Total / 100.0m
};

foreach (var line in stripeInvoice.Lines.Data)
{
var item = new InvoiceLineItem { Amount = line.Amount / 100.0m, Description = line.Description };
if (line.Plan is not null)

// Try to access price information in multiple ways for Stripe.net v48 compatibility
try
{
string planName = line.Plan.Nickname ?? _billingManager.GetBillingPlan(line.Plan.Id)?.Name ?? line.Plan.Id;
item.Description = $"Exceptionless - {planName} Plan ({(line.Plan.Amount / 100.0):c}/{line.Plan.Interval})";
// First, try the expanded Price property using reflection (safe for v48)
var priceProperty = line.GetType().GetProperty("Price");
if (priceProperty is not null)
{
var price = priceProperty.GetValue(line);
if (price is not null)
{
var priceIdProperty = price.GetType().GetProperty("Id");
var nicknameProperty = price.GetType().GetProperty("Nickname");
var unitAmountProperty = price.GetType().GetProperty("UnitAmount");
var recurringProperty = price.GetType().GetProperty("Recurring");

if (priceIdProperty is not null)
{
var priceId = priceIdProperty.GetValue(price) as string;
var nickname = nicknameProperty?.GetValue(price) as string;
var unitAmount = unitAmountProperty?.GetValue(price) as long?;

string planName = nickname ?? _billingManager.GetBillingPlan(priceId)?.Name ?? priceId ?? "Unknown";

// Get interval from recurring property
string intervalText = "one-time";
if (recurringProperty is not null)
{
var recurring = recurringProperty.GetValue(price);
if (recurring is not null)
{
var intervalProperty = recurring.GetType().GetProperty("Interval");
if (intervalProperty is not null)
{
intervalText = intervalProperty.GetValue(recurring) as string ?? "one-time";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot never use reflection

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed all reflection usage and reverted to direct Price property access with proper invoice expansion. The approach now uses lines.data.price expansion to restore access to Price information without reflection. Fixed in c07716a.

}

var priceAmount = unitAmount.HasValue ? (unitAmount.Value / 100.0) : 0.0;
item.Description = $"Exceptionless - {planName} Plan ({priceAmount:c}/{intervalText})";
}
}
}
else
{
// Fallback: Try to access through Plan property (legacy support)
var planProperty = line.GetType().GetProperty("Plan");
if (planProperty is not null)
{
var plan = planProperty.GetValue(line);
if (plan is not null)
{
var planIdProperty = plan.GetType().GetProperty("Id");
if (planIdProperty is not null)
{
var priceId = planIdProperty.GetValue(plan) as string;
if (!String.IsNullOrEmpty(priceId))
{
var billingPlan = _billingManager.GetBillingPlan(priceId);
if (billingPlan is not null)
{
item.Description = $"Exceptionless - {billingPlan.Name} Plan";
}
}
}
}
}
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to process price information for invoice line item");
// Fall back to original description
}

var periodStart = line.Period.Start >= DateTime.MinValue ? line.Period.Start : stripeInvoice.PeriodStart;
var periodEnd = line.Period.End >= DateTime.MinValue ? line.Period.End : stripeInvoice.PeriodEnd;
item.Date = $"{periodStart.ToShortDateString()} - {periodEnd.ToShortDateString()}";
invoice.Items.Add(item);
}

var periodStart = line.Period.Start >= DateTime.MinValue ? line.Period.Start : stripeInvoice.PeriodStart;
var periodEnd = line.Period.End >= DateTime.MinValue ? line.Period.End : stripeInvoice.PeriodEnd;
item.Date = $"{periodStart.ToShortDateString()} - {periodEnd.ToShortDateString()}";
invoice.Items.Add(item);
}

var coupon = stripeInvoice.Discount?.Coupon;
var coupon = stripeInvoice.Discounts?.FirstOrDefault(d => d.Deleted is false)?.Coupon;
if (coupon is not null)
{
if (coupon.AmountOff.HasValue)
Expand Down Expand Up @@ -429,15 +511,29 @@ public async Task<ActionResult<ChangePlanResult>> ChangePlanAsync(string id, str
var createCustomer = new CustomerCreateOptions
{
Source = stripeToken,
Plan = planId,
Description = organization.Name,
Email = CurrentUser.EmailAddress
};

var customer = await customerService.CreateAsync(createCustomer);

// Create subscription separately since Plan is deprecated in CustomerCreateOptions
var subscriptionCreateOptions = new SubscriptionCreateOptions
{
Customer = customer.Id,
Items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions { Price = planId } }
};

// Apply coupon as discount if provided
if (!String.IsNullOrWhiteSpace(couponId))
createCustomer.Coupon = couponId;
{
subscriptionCreateOptions.Discounts = new List<SubscriptionDiscountOptions>
{
new SubscriptionDiscountOptions { Coupon = couponId }
};
}

var customer = await customerService.CreateAsync(createCustomer);
await subscriptionService.CreateAsync(subscriptionCreateOptions);

organization.BillingStatus = BillingStatus.Active;
organization.RemoveSuspension();
Expand All @@ -446,8 +542,8 @@ public async Task<ActionResult<ChangePlanResult>> ChangePlanAsync(string id, str
}
else
{
var update = new SubscriptionUpdateOptions { Items = [] };
var create = new SubscriptionCreateOptions { Customer = organization.StripeCustomerId, Items = [] };
var update = new SubscriptionUpdateOptions { Items = new List<SubscriptionItemOptions>() };
var create = new SubscriptionCreateOptions { Customer = organization.StripeCustomerId, Items = new List<SubscriptionItemOptions>() };
bool cardUpdated = false;

var customerUpdateOptions = new CustomerUpdateOptions { Description = organization.Name };
Expand All @@ -466,12 +562,22 @@ public async Task<ActionResult<ChangePlanResult>> ChangePlanAsync(string id, str
var subscription = subscriptionList.FirstOrDefault(s => !s.CanceledAt.HasValue);
if (subscription is not null)
{
update.Items.Add(new SubscriptionItemOptions { Id = subscription.Items.Data[0].Id, Plan = planId });
update.Items.Add(new SubscriptionItemOptions { Id = subscription.Items.Data[0].Id, Price = planId });
await subscriptionService.UpdateAsync(subscription.Id, update);
}
else
{
create.Items.Add(new SubscriptionItemOptions { Plan = planId });
create.Items.Add(new SubscriptionItemOptions { Price = planId });

// Apply coupon as discount if provided
if (!String.IsNullOrWhiteSpace(couponId))
{
create.Discounts = new List<SubscriptionDiscountOptions>
{
new SubscriptionDiscountOptions { Coupon = couponId }
};
}

await subscriptionService.CreateAsync(create);
}

Expand Down