Skip to content

Commit

Permalink
Fixes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed May 4, 2016
1 parent 26ef266 commit bfb7141
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
23 changes: 16 additions & 7 deletions Source/Chargify.NET/ChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2999,13 +2999,14 @@ public ISubscription MigrateSubscriptionProduct(int SubscriptionID, string Produ
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="Subscription">The suscription to update</param>
/// <param name="Product">The new product</param>
/// <param name="ProductChangeDelayed">Optional, determines if the product change should be done immediately or at the time of assessment.</param>
/// <returns>The new subscription resulting from the change</returns>
public ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product)
public ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product, bool? ProductChangeDelayed = null)
{
// make sure data is OK
if (Subscription == null) throw new ArgumentNullException("Subscription");
if (Product == null) throw new ArgumentNullException("Product");
return EditSubscriptionProduct(Subscription.SubscriptionID, Product.Handle);
return EditSubscriptionProduct(Subscription.SubscriptionID, Product.Handle, ProductChangeDelayed);
}

/// <summary>
Expand All @@ -3014,12 +3015,13 @@ public ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduc
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="SubscriptionID">The ID of the suscription to update</param>
/// <param name="Product">The new product</param>
/// <param name="ProductChangeDelayed">Optional, determines if the product change should be done immediately or at the time of assessment.</param>
/// <returns>The new subscription resulting from the change</returns>
public ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product)
public ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product, bool? ProductChangeDelayed = null)
{
// make sure data is OK
if (Product == null) throw new ArgumentNullException("Product");
return EditSubscriptionProduct(SubscriptionID, Product.Handle);
return EditSubscriptionProduct(SubscriptionID, Product.Handle, ProductChangeDelayed);
}

/// <summary>
Expand All @@ -3028,12 +3030,13 @@ public ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Produc
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="Subscription">The suscription to update</param>
/// <param name="ProductHandle">The handle to the new product</param>
/// <param name="ProductChangeDelayed">Optional, determines if the product change should be done immediately or at the time of assessment.</param>
/// <returns>The new subscription resulting from the change</returns>
public ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle)
public ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle, bool? ProductChangeDelayed = null)
{
// make sure data is OK
if (Subscription == null) throw new ArgumentNullException("Subscription");
return EditSubscriptionProduct(Subscription.SubscriptionID, ProductHandle);
return EditSubscriptionProduct(Subscription.SubscriptionID, ProductHandle, ProductChangeDelayed);
}

/// <summary>
Expand All @@ -3042,8 +3045,9 @@ public ISubscription EditSubscriptionProduct(ISubscription Subscription, string
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="SubscriptionID">The ID of the suscription to update</param>
/// <param name="ProductHandle">The handle to the new product</param>
/// <param name="ProductChangeDelayed">Optional, determines if the product change should be done immediately or at the time of assessment.</param>
/// <returns>The new subscription resulting from the change</returns>
public ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle)
public ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle, bool? ProductChangeDelayed = null)
{
if (SubscriptionID == int.MinValue) throw new ArgumentNullException("SubscriptionID");
if (string.IsNullOrEmpty(ProductHandle)) throw new ArgumentNullException("ProductHandle");
Expand All @@ -3056,6 +3060,11 @@ public ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductH
StringBuilder SubscriptionXML = new StringBuilder(GetXMLStringIfApplicable());
SubscriptionXML.Append("<subscription>");
SubscriptionXML.AppendFormat("<product_handle>{0}</product_handle>", ProductHandle);
if (ProductChangeDelayed != null && ProductChangeDelayed.HasValue)
{
//product_change_delayed
SubscriptionXML.AppendFormat("<product_change_delayed>{0}</product_change_delayed>", ProductChangeDelayed.Value.ToString().ToLowerInvariant());
}
SubscriptionXML.Append("</subscription>");
try
{
Expand Down
9 changes: 5 additions & 4 deletions Source/Chargify.NET/Interfaces/IChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,31 +681,32 @@ public interface IChargifyConnect
/// <param name="Subscription">The suscription to update</param>
/// <param name="Product">The new product</param>
/// <returns>The new subscription resulting from the change</returns>
ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product);
ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product, bool? ProductChangeDelayed = null);
/// <summary>
/// Update the product information for an existing subscription
/// </summary>
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="Subscription">The suscription to update</param>
/// <param name="ProductHandle">The handle to the new product</param>
/// <returns>The new subscription resulting from the change</returns>
ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle);
ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle, bool? ProductChangeDelayed = null);
/// <summary>
/// Update the product information for an existing subscription
/// </summary>
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="SubscriptionID">The ID of the suscription to update</param>
/// <param name="Product">The new product</param>
/// <returns>The new subscription resulting from the change</returns>
ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product);
ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product, bool? ProductChangeDelayed = null);
/// <summary>
/// Update the product information for an existing subscription
/// </summary>
/// <remarks>Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work.</remarks>
/// <param name="SubscriptionID">The ID of the suscription to update</param>
/// <param name="ProductHandle">The handle to the new product</param>
/// <param name="ProductChangeDelayed">Optional value of whether to delay the product change until next assessment</param>
/// <returns>The new subscription resulting from the change</returns>
ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle);
ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle, bool? ProductChangeDelayed = null);
/// <summary>
/// Method to allow users to change the next_assessment_at date
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Source/Chargify.NET/Interfaces/ISubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,11 @@ public interface ISubscription : IComparable<ISubscription>
/// At what price was the product on when initial subscribed? (in dollars and cents)
/// </summary>
decimal ProductPrice { get; }

/// <summary>
/// If a delayed product change is scheduled, the ID of the product that the subscription
/// will be changed to at the next renewal.
/// </summary>
int NextProductId { get; }
}
}
14 changes: 14 additions & 0 deletions Source/Chargify.NET/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class Subscription : ChargifyBase, ISubscription, IComparable<Subscriptio
private const string ProductKey = "product";
private const string ProductVersionNumberKey = "product_version_number";
private const string ProductPriceInCentsKey = "product_price_in_cents";
private const string NextProductIdKey = "next_product_id";
#endregion

#region Constructors
Expand Down Expand Up @@ -226,6 +227,9 @@ private void LoadFromJSON(JsonObject obj)
case CustomerKey:
_customer = obj.GetJSONContentAsCustomer(key);
break;
case NextProductIdKey:
_nextProductId = obj.GetJSONContentAsInt(key);
break;
default:
break;
}
Expand Down Expand Up @@ -325,6 +329,9 @@ private void LoadFromNode(XmlNode subscriptionNode)
case CustomerKey:
_customer = dataNode.GetNodeContentAsCustomer();
break;
case NextProductIdKey:
_nextProductId = dataNode.GetNodeContentAsInt();
break;
default:
break;

Expand Down Expand Up @@ -666,6 +673,13 @@ public decimal ProductPrice
get { return Convert.ToDecimal(this._productPriceInCents) / 100; }
}

/// <summary>
/// If a delayed product change is scheduled, the ID of the product that the subscription
/// will be changed to at the next renewal.
/// </summary>
public int NextProductId { get { return _nextProductId; } }
private int _nextProductId = int.MinValue;

#endregion

#region Operators
Expand Down
32 changes: 32 additions & 0 deletions Source/ChargifyDotNetTests/SubscriptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,38 @@ public void Subscription_ReactivateExpired()
}
}

[Test]
public void Subscription_Can_EditProduct_WithDelay()
{
// Arrange
var subscription = Chargify.GetSubscriptionList().FirstOrDefault(s => s.Value.State == SubscriptionState.Active && s.Value.PaymentProfile != null).Value as Subscription;
var otherProduct = Chargify.GetProductList().Values.Where(p => p.Handle != subscription.Product.Handle).FirstOrDefault();

// Act
var result = Chargify.EditSubscriptionProduct(subscription.SubscriptionID, otherProduct.Handle, true);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(subscription.Product.Handle, result.Product.Handle);
Assert.AreNotEqual(int.MinValue, subscription.NextProductId);
Assert.AreEqual(otherProduct.ID, result.NextProductId);
}

[Test]
public void Subscription_Can_EditProduct_NoDelay()
{
// Arrange
var subscription = Chargify.GetSubscriptionList().FirstOrDefault(s => s.Value.State == SubscriptionState.Active && s.Value.PaymentProfile != null).Value as Subscription;
var otherProduct = Chargify.GetProductList().Values.Where(p => p.Handle != subscription.Product.Handle).FirstOrDefault();

// Act
var result = Chargify.EditSubscriptionProduct(subscription.SubscriptionID, otherProduct.Handle);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(otherProduct.Handle, result.Product.Handle);
}

[Test]
public void Subscription_Can_Reactivate_Without_Trial()
{
Expand Down

0 comments on commit bfb7141

Please sign in to comment.