Skip to content

Commit

Permalink
add delay product change cancel method
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed May 12, 2016
1 parent f04cb7f commit c40d04e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
41 changes: 35 additions & 6 deletions Source/Chargify.NET/ChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3057,19 +3057,48 @@ public ISubscription EditSubscriptionProduct(int SubscriptionID, string ProductH
if (existingSubscription == null) throw new ArgumentException("Subscription not found", "Subscription.SubscriptionID");

// create XML for creation of customer
StringBuilder SubscriptionXML = new StringBuilder(GetXMLStringIfApplicable());
SubscriptionXML.Append("<subscription>");
SubscriptionXML.AppendFormat("<product_handle>{0}</product_handle>", ProductHandle);
var 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.AppendFormat("<product_change_delayed>{0}</product_change_delayed>", ProductChangeDelayed.Value.ToString().ToLowerInvariant());
}
SubscriptionXML.Append("</subscription>");
subscriptionXml.Append("</subscription>");
try
{
// now make the request
string response = this.DoRequest(string.Format("subscriptions/{0}.{1}", SubscriptionID, GetMethodExtension()), HttpRequestMethod.Put, SubscriptionXML.ToString());
string response = this.DoRequest(string.Format("subscriptions/{0}.{1}", SubscriptionID, GetMethodExtension()), HttpRequestMethod.Put, subscriptionXml.ToString());
// change the response to the object
return response.ConvertResponseTo<Subscription>("subscription");
}
catch (ChargifyException cex)
{
if (cex.StatusCode == HttpStatusCode.NotFound) throw new InvalidOperationException("Subscription not found");
throw;
}
}

/// <summary>
/// Change the delayed product, or cancel by setting it null
/// </summary>
/// <param name="subscriptionId">The id of the subscription</param>
/// <returns>The subscription</returns>
public ISubscription CancelDelayedProductChange(int subscriptionId)
{
if (subscriptionId <= 0) throw new ArgumentNullException("subscriptionId");

// create XML for creation of customer
var subscriptionXml = new StringBuilder(GetXMLStringIfApplicable());
subscriptionXml.Append("<subscription>");
subscriptionXml.AppendFormat("<next_product_id>{0}</next_product_id>", string.Empty);
subscriptionXml.Append("</subscription>");

try
{
// now make the request
string response = this.DoRequest(string.Format("subscriptions/{0}.{1}", subscriptionId, GetMethodExtension()), HttpRequestMethod.Put, subscriptionXml.ToString());
// change the response to the object
return response.ConvertResponseTo<Subscription>("subscription");
}
Expand Down
6 changes: 6 additions & 0 deletions Source/Chargify.NET/Interfaces/IChargifyConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ public interface IChargifyConnect

#region Product
/// <summary>
/// Cancel the delayed product change
/// </summary>
/// <param name="subscriptionId">The id of the subscription</param>
/// <returns>The subscription</returns>
ISubscription CancelDelayedProductChange(int subscriptionId);
/// <summary>
/// Method that updates a product
/// </summary>
/// <param name="ProductID">The ID of the product to update</param>
Expand Down
17 changes: 17 additions & 0 deletions Source/ChargifyDotNetTests/SubscriptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ public class SubscriptionTests : ChargifyTestBase
{
#region Tests

[Test]
public void Subscription_Can_Cancel_Delayed_Product_Change()
{
// Arrange
var subscription = Chargify.GetSubscriptionList().FirstOrDefault(s => s.Value.State == SubscriptionState.Active && s.Value.PaymentProfile != null && s.Value.NextProductId <= 0).Value;
var otherProduct = Chargify.GetProductList().FirstOrDefault(p => p.Key != subscription.Product.ID);
var updatedSubscription = Chargify.EditSubscriptionProduct(subscription.SubscriptionID, otherProduct.Value.Handle, true);
Assert.AreEqual(otherProduct.Key, updatedSubscription.NextProductId);

// Act
var result = Chargify.CancelDelayedProductChange(updatedSubscription.SubscriptionID);

// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.NextProductId <= 0);
}

[Test]
public void Subscription_Create_UsingOptions_ProductHandle()
{
Expand Down

0 comments on commit c40d04e

Please sign in to comment.