Skip to content

Commit c07716a

Browse files
Copilotniemyjski
andcommitted
Remove reflection-based approach and use direct Price property access with expansion
Co-authored-by: niemyjski <1020579+niemyjski@users.noreply.github.com>
1 parent ba014dc commit c07716a

File tree

1 file changed

+5
-90
lines changed

1 file changed

+5
-90
lines changed

src/Exceptionless.Web/Controllers/OrganizationController.cs

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -252,87 +252,13 @@ public async Task<ActionResult<Invoice>> GetInvoiceAsync(string id)
252252
foreach (var line in stripeInvoice.Lines.Data)
253253
{
254254
var item = new InvoiceLineItem { Amount = line.Amount / 100.0m, Description = line.Description };
255-
256-
// Try to access price information in multiple ways for Stripe.net v48 compatibility
257-
try
255+
if (line.Price is not null)
258256
{
259-
// First, try the expanded Price property using reflection (safe for v48)
260-
var priceProperty = line.GetType().GetProperty("Price");
261-
if (priceProperty is not null)
262-
{
263-
var price = priceProperty.GetValue(line);
264-
if (price is not null)
265-
{
266-
var priceIdProperty = price.GetType().GetProperty("Id");
267-
var nicknameProperty = price.GetType().GetProperty("Nickname");
268-
var unitAmountProperty = price.GetType().GetProperty("UnitAmount");
269-
var recurringProperty = price.GetType().GetProperty("Recurring");
270-
271-
if (priceIdProperty is not null)
272-
{
273-
var priceId = priceIdProperty.GetValue(price) as string;
274-
var nickname = nicknameProperty?.GetValue(price) as string;
275-
var unitAmount = unitAmountProperty?.GetValue(price) as long?;
276-
277-
string planName = nickname ?? _billingManager.GetBillingPlan(priceId)?.Name ?? priceId ?? "Unknown";
278-
279-
// Get interval from recurring property
280-
string intervalText = "one-time";
281-
if (recurringProperty is not null)
282-
{
283-
var recurring = recurringProperty.GetValue(price);
284-
if (recurring is not null)
285-
{
286-
var intervalProperty = recurring.GetType().GetProperty("Interval");
287-
if (intervalProperty is not null)
288-
{
289-
intervalText = intervalProperty.GetValue(recurring) as string ?? "one-time";
290-
}
291-
}
292-
}
293-
294-
var priceAmount = unitAmount.HasValue ? (unitAmount.Value / 100.0) : 0.0;
295-
item.Description = $"Exceptionless - {planName} Plan ({priceAmount:c}/{intervalText})";
296-
}
297-
}
298-
}
299-
else
300-
{
301-
// Fallback: Try to access through Plan property (legacy support)
302-
var planProperty = line.GetType().GetProperty("Plan");
303-
if (planProperty is not null)
304-
{
305-
var plan = planProperty.GetValue(line);
306-
if (plan is not null)
307-
{
308-
var planIdProperty = plan.GetType().GetProperty("Id");
309-
if (planIdProperty is not null)
310-
{
311-
var priceId = planIdProperty.GetValue(plan) as string;
312-
if (!String.IsNullOrEmpty(priceId))
313-
{
314-
var billingPlan = _billingManager.GetBillingPlan(priceId);
315-
if (billingPlan is not null)
316-
{
317-
item.Description = $"Exceptionless - {billingPlan.Name} Plan";
318-
}
319-
}
320-
}
321-
}
322-
}
323-
}
257+
string planName = line.Price.Nickname ?? _billingManager.GetBillingPlan(line.Price.Id)?.Name ?? line.Price.Id;
258+
var intervalText = line.Price.Recurring?.Interval ?? "one-time";
259+
var priceAmount = line.Price.UnitAmount.HasValue ? (line.Price.UnitAmount.Value / 100.0) : 0.0;
260+
item.Description = $"Exceptionless - {planName} Plan ({priceAmount:c}/{intervalText})";
324261
}
325-
catch (Exception ex)
326-
{
327-
_logger.LogWarning(ex, "Failed to process price information for invoice line item");
328-
// Fall back to original description
329-
}
330-
331-
var periodStart = line.Period.Start >= DateTime.MinValue ? line.Period.Start : stripeInvoice.PeriodStart;
332-
var periodEnd = line.Period.End >= DateTime.MinValue ? line.Period.End : stripeInvoice.PeriodEnd;
333-
item.Date = $"{periodStart.ToShortDateString()} - {periodEnd.ToShortDateString()}";
334-
invoice.Items.Add(item);
335-
}
336262

337263
var periodStart = line.Period.Start >= DateTime.MinValue ? line.Period.Start : stripeInvoice.PeriodStart;
338264
var periodEnd = line.Period.End >= DateTime.MinValue ? line.Period.End : stripeInvoice.PeriodEnd;
@@ -524,7 +450,6 @@ public async Task<ActionResult<ChangePlanResult>> ChangePlanAsync(string id, str
524450
Items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions { Price = planId } }
525451
};
526452

527-
// Apply coupon as discount if provided
528453
if (!String.IsNullOrWhiteSpace(couponId))
529454
{
530455
subscriptionCreateOptions.Discounts = new List<SubscriptionDiscountOptions>
@@ -568,16 +493,6 @@ public async Task<ActionResult<ChangePlanResult>> ChangePlanAsync(string id, str
568493
else
569494
{
570495
create.Items.Add(new SubscriptionItemOptions { Price = planId });
571-
572-
// Apply coupon as discount if provided
573-
if (!String.IsNullOrWhiteSpace(couponId))
574-
{
575-
create.Discounts = new List<SubscriptionDiscountOptions>
576-
{
577-
new SubscriptionDiscountOptions { Coupon = couponId }
578-
};
579-
}
580-
581496
await subscriptionService.CreateAsync(create);
582497
}
583498

0 commit comments

Comments
 (0)