Skip to content

Commit

Permalink
proration_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
smagdicatrecurly committed May 7, 2024
1 parent 21e5fdc commit db37eef
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Library/ProrationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;

namespace Recurly
{
public class ProrationSettings
{
public enum Options
{
ProratedAmount,
FullAmount,
None
}

/// <summary>
/// Proration behavior to be applied to charges for the subscription change.
/// Can be set to FullAmount, ProratedAmount, or None.
/// When not set, default proration settings will be applied.
/// </summary>
public Options? Charge { get; set; }

/// <summary>
/// Proration behavior to be applied to credits for the subscription change.
/// Can be set to FullAmount, ProratedAmount, or None.
/// When not set, default proration settings will be applied.
/// </summary>
public Options? Credit { get; set; }
}
}
20 changes: 20 additions & 0 deletions Library/SubscriptionChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public enum ChangeTimeframe : short

public string PlanCode { get; set; }

/// <summary>
/// Proration settings to be applied to the subscription change.
/// If not set, the default proration behavior will apply.
/// </summary>
public ProrationSettings? ProrationSettings { get; set; }

Check failure on line 31 in Library/SubscriptionChange.cs

View workflow job for this annotation

GitHub Actions / Dotnet 5.0.x tests

Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.

Check failure on line 31 in Library/SubscriptionChange.cs

View workflow job for this annotation

GitHub Actions / Dotnet 5.0.x tests

Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.

/// <summary>
/// List of custom fields
/// </summary>
Expand Down Expand Up @@ -127,6 +133,20 @@ internal void WriteChangeSubscriptionXml(XmlTextWriter xmlWriter)

xmlWriter.WriteStringIfValid("plan_code", PlanCode);

if (ProrationSettings != null)
{
xmlWriter.WriteStartElement("proration_settings");

if (ProrationSettings.Charge != null)
xmlWriter.WriteElementString("charge", ProrationSettings.Charge.Value.ToString().EnumNameToTransportCase());

if (ProrationSettings.Credit != null)
xmlWriter.WriteElementString("credit", ProrationSettings.Credit.Value.ToString().EnumNameToTransportCase());

xmlWriter.WriteEndElement();
}


if (AddOns != null)
xmlWriter.WriteIfCollectionHasAny("subscription_add_ons", AddOns);

Expand Down
48 changes: 48 additions & 0 deletions Test/SubscriptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,54 @@ public void UpdateSubscription()
Assert.Equal(newSubscription.NetTermsType, NetTermsType.NET);
}

[RecurlyFact(TestEnvironment.Type.Integration)]
public void UpdateSubscriptionWithProrationSettings()
{
var plan = new Plan(GetMockPlanCode(), GetMockPlanName())
{
Description = "Update Subscription Plan 1"
};
plan.UnitAmountInCents.Add("USD", 1500);
plan.Create();
PlansToDeactivateOnDispose.Add(plan);

var plan2 = new Plan(GetMockPlanCode(), GetMockPlanName())
{
Description = "Update Subscription Plan 2"
};
plan2.UnitAmountInCents.Add("USD", 750);
plan2.Create();
PlansToDeactivateOnDispose.Add(plan2);

var account = CreateNewAccountWithBillingInfo();

var sub = new Subscription(account, plan, "USD");
sub.Create();

var subChange = new SubscriptionChange()
{
PlanCode = plan2.PlanCode,
ProrationSettings = new ProrationSettings()
{
Charge = ProrationSettings.Options.None,
Credit = ProrationSettings.Options.FullAmount
}
};

Assert.Equal(subChange.ProrationSettings.Charge, ProrationSettings.Options.None);
Assert.Equal(subChange.ProrationSettings.Credit, ProrationSettings.Options.FullAmount);

Subscription.ChangeSubscription(sub.Uuid, subChange);

var newSubscription = Subscriptions.Get(sub.Uuid);
var invoices = account.GetInvoices();
var chargeInvoice = invoices.FirstOrDefault(invoice => invoice.Type == "charge");
var creditInvoice = invoices.FirstOrDefault(invoice => invoice.Type == "credit");

Assert.Equal(0, chargeInvoice.SubtotalInCents);
Assert.Equal(-1500, creditInvoice.SubtotalInCents);
}

[RecurlyFact(TestEnvironment.Type.Integration)]
public void CancelSubscription()
{
Expand Down

0 comments on commit db37eef

Please sign in to comment.