From bfb71411075a921b4fc770df03c6e5c4e4335306 Mon Sep 17 00:00:00 2001 From: Kori Francis Date: Tue, 3 May 2016 22:38:21 -0400 Subject: [PATCH] Fixes #32 --- Source/Chargify.NET/ChargifyConnect.cs | 23 +++++++++---- .../Interfaces/IChargifyConnect.cs | 9 +++--- .../Chargify.NET/Interfaces/ISubscription.cs | 6 ++++ Source/Chargify.NET/Subscription.cs | 14 ++++++++ .../ChargifyDotNetTests/SubscriptionTests.cs | 32 +++++++++++++++++++ 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Source/Chargify.NET/ChargifyConnect.cs b/Source/Chargify.NET/ChargifyConnect.cs index ed09a19..1bab5b9 100644 --- a/Source/Chargify.NET/ChargifyConnect.cs +++ b/Source/Chargify.NET/ChargifyConnect.cs @@ -2999,13 +2999,14 @@ public ISubscription MigrateSubscriptionProduct(int SubscriptionID, string Produ /// Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work. /// The suscription to update /// The new product + /// Optional, determines if the product change should be done immediately or at the time of assessment. /// The new subscription resulting from the change - 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); } /// @@ -3014,12 +3015,13 @@ public ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduc /// Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work. /// The ID of the suscription to update /// The new product + /// Optional, determines if the product change should be done immediately or at the time of assessment. /// The new subscription resulting from the change - 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); } /// @@ -3028,12 +3030,13 @@ public ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Produc /// Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work. /// The suscription to update /// The handle to the new product + /// Optional, determines if the product change should be done immediately or at the time of assessment. /// The new subscription resulting from the change - 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); } /// @@ -3042,8 +3045,9 @@ public ISubscription EditSubscriptionProduct(ISubscription Subscription, string /// Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work. /// The ID of the suscription to update /// The handle to the new product + /// Optional, determines if the product change should be done immediately or at the time of assessment. /// The new subscription resulting from the change - 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"); @@ -3056,6 +3060,11 @@ public ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductH StringBuilder SubscriptionXML = new StringBuilder(GetXMLStringIfApplicable()); SubscriptionXML.Append(""); SubscriptionXML.AppendFormat("{0}", ProductHandle); + if (ProductChangeDelayed != null && ProductChangeDelayed.HasValue) + { + //product_change_delayed + SubscriptionXML.AppendFormat("{0}", ProductChangeDelayed.Value.ToString().ToLowerInvariant()); + } SubscriptionXML.Append(""); try { diff --git a/Source/Chargify.NET/Interfaces/IChargifyConnect.cs b/Source/Chargify.NET/Interfaces/IChargifyConnect.cs index e2a3a77..f5ee5d7 100644 --- a/Source/Chargify.NET/Interfaces/IChargifyConnect.cs +++ b/Source/Chargify.NET/Interfaces/IChargifyConnect.cs @@ -681,7 +681,7 @@ public interface IChargifyConnect /// The suscription to update /// The new product /// The new subscription resulting from the change - ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product); + ISubscription EditSubscriptionProduct(ISubscription Subscription, IProduct Product, bool? ProductChangeDelayed = null); /// /// Update the product information for an existing subscription /// @@ -689,7 +689,7 @@ public interface IChargifyConnect /// The suscription to update /// The handle to the new product /// The new subscription resulting from the change - ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle); + ISubscription EditSubscriptionProduct(ISubscription Subscription, string ProductHandle, bool? ProductChangeDelayed = null); /// /// Update the product information for an existing subscription /// @@ -697,15 +697,16 @@ public interface IChargifyConnect /// The ID of the suscription to update /// The new product /// The new subscription resulting from the change - ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product); + ISubscription EditSubscriptionProduct(int SubscriptionID, IProduct Product, bool? ProductChangeDelayed = null); /// /// Update the product information for an existing subscription /// /// Does NOT prorate. Use MigrateSubscriptionProduct to get proration to work. /// The ID of the suscription to update /// The handle to the new product + /// Optional value of whether to delay the product change until next assessment /// The new subscription resulting from the change - ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle); + ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductHandle, bool? ProductChangeDelayed = null); /// /// Method to allow users to change the next_assessment_at date /// diff --git a/Source/Chargify.NET/Interfaces/ISubscription.cs b/Source/Chargify.NET/Interfaces/ISubscription.cs index 20dc899..a248378 100644 --- a/Source/Chargify.NET/Interfaces/ISubscription.cs +++ b/Source/Chargify.NET/Interfaces/ISubscription.cs @@ -247,5 +247,11 @@ public interface ISubscription : IComparable /// At what price was the product on when initial subscribed? (in dollars and cents) /// decimal ProductPrice { get; } + + /// + /// If a delayed product change is scheduled, the ID of the product that the subscription + /// will be changed to at the next renewal. + /// + int NextProductId { get; } } } diff --git a/Source/Chargify.NET/Subscription.cs b/Source/Chargify.NET/Subscription.cs index 2fd938c..90da327 100644 --- a/Source/Chargify.NET/Subscription.cs +++ b/Source/Chargify.NET/Subscription.cs @@ -74,6 +74,7 @@ public class Subscription : ChargifyBase, ISubscription, IComparable + /// If a delayed product change is scheduled, the ID of the product that the subscription + /// will be changed to at the next renewal. + /// + public int NextProductId { get { return _nextProductId; } } + private int _nextProductId = int.MinValue; + #endregion #region Operators diff --git a/Source/ChargifyDotNetTests/SubscriptionTests.cs b/Source/ChargifyDotNetTests/SubscriptionTests.cs index 0d40cf0..54e150a 100644 --- a/Source/ChargifyDotNetTests/SubscriptionTests.cs +++ b/Source/ChargifyDotNetTests/SubscriptionTests.cs @@ -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() {