Skip to content

Commit

Permalink
Handle value types in AnyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Apr 5, 2019
1 parent 0c6ada6 commit 7ca274b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 37 deletions.
18 changes: 7 additions & 11 deletions src/Stripe.net/Services/Subscriptions/SubscriptionSharedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,16 @@ public abstract class SubscriptionSharedOptions : BaseOptions
[JsonProperty("tax_percent")]
public decimal? TaxPercent { get; set; }

#region TrialEnd

/// <summary>
/// Date representing the end of the trial period the customer will get before being charged for the first time. Set <see cref="EndTrialNow"/> to <c>true</c> to end the customer’s trial immediately.
/// <see cref="DateTime"/> representing the end of the trial period the customer will get
/// before being charged for the first time. This will always overwrite any trials that
/// might apply via a subscribed plan. If set, <see cref="TrialEnd"/> will override the
/// default trial period of the plan the customer is being subscribed to. The special value
/// <see cref="SubscriptionTrialEnd.Now"/> can be provided to end the customer’s trial
/// immediately.
/// </summary>
[JsonIgnore]
public DateTime? TrialEnd { get; set; }

[JsonIgnore]
public bool EndTrialNow { get; set; }

[JsonProperty("trial_end")]
internal string TrialEndInternal => this.EndTrialNow ? "now" : this.TrialEnd?.ConvertDateTimeToEpoch().ToString();
#endregion
public AnyOf<DateTime?, SubscriptionTrialEnd> TrialEnd { get; set; }

/// <summary>
/// Boolean. Decide whether to use the default trial on the plan when creating a subscription.
Expand Down
14 changes: 14 additions & 0 deletions src/Stripe.net/Services/Subscriptions/SubscriptionTrialEnd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Stripe
{
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public enum SubscriptionTrialEnd
{
/// <summary>Special value used to end a customer's trial immediately.</summary>
[EnumMember(Value = "now")]
Now,
}
}
62 changes: 37 additions & 25 deletions src/Stripe.net/Services/_base/AnyOf.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Stripe
{
using System.Collections.Generic;
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -32,6 +32,7 @@ public class AnyOf<T1, T2> : AnyOf
{
private readonly T1 value1;
private readonly T2 value2;
private readonly Values setValue;

/// <summary>
/// Initializes a new instance of the <see cref="AnyOf{T1, T2}"/> class with type <c>T1</c>.
Expand All @@ -40,6 +41,7 @@ public class AnyOf<T1, T2> : AnyOf
public AnyOf(T1 value)
{
this.value1 = value;
this.setValue = Values.Value1;
}

/// <summary>
Expand All @@ -49,6 +51,13 @@ public AnyOf(T1 value)
public AnyOf(T2 value)
{
this.value2 = value;
this.setValue = Values.Value2;
}

private enum Values
{
Value1,
Value2,
}

/// <summary>
Expand Down Expand Up @@ -89,17 +98,14 @@ public AnyOf(T2 value)
/// <returns>The current value.</returns>
public override object GetValue()
{
if (!EqualityComparer<T1>.Default.Equals(this.value1, default(T1)))
{
return this.value1;
}
else if (!EqualityComparer<T2>.Default.Equals(this.value2, default(T2)))
{
return this.value2;
}
else
switch (this.setValue)
{
return null;
case Values.Value1:
return this.value1;
case Values.Value2:
return this.value2;
default:
throw new InvalidOperationException($"Unexpected state, setValue={this.setValue}");
}
}
}
Expand All @@ -120,6 +126,7 @@ public class AnyOf<T1, T2, T3> : AnyOf
private readonly T1 value1;
private readonly T2 value2;
private readonly T3 value3;
private readonly Values setValue;

/// <summary>
/// Initializes a new instance of the <see cref="AnyOf{T1, T2, T3}"/> class with type
Expand All @@ -129,6 +136,7 @@ public class AnyOf<T1, T2, T3> : AnyOf
public AnyOf(T1 value)
{
this.value1 = value;
this.setValue = Values.Value1;
}

/// <summary>
Expand All @@ -139,6 +147,7 @@ public AnyOf(T1 value)
public AnyOf(T2 value)
{
this.value2 = value;
this.setValue = Values.Value2;
}

/// <summary>
Expand All @@ -149,6 +158,14 @@ public AnyOf(T2 value)
public AnyOf(T3 value)
{
this.value3 = value;
this.setValue = Values.Value3;
}

private enum Values
{
Value1,
Value2,
Value3,
}

/// <summary>
Expand Down Expand Up @@ -206,21 +223,16 @@ public AnyOf(T3 value)
/// <returns>The current value.</returns>
public override object GetValue()
{
if (!EqualityComparer<T1>.Default.Equals(this.value1, default(T1)))
{
return this.value1;
}
else if (!EqualityComparer<T2>.Default.Equals(this.value2, default(T2)))
{
return this.value2;
}
else if (!EqualityComparer<T3>.Default.Equals(this.value3, default(T3)))
{
return this.value3;
}
else
switch (this.setValue)
{
return null;
case Values.Value1:
return this.value1;
case Values.Value2:
return this.value2;
case Values.Value3:
return this.value3;
default:
throw new InvalidOperationException($"Unexpected state, setValue={this.setValue}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace StripeTests
{
using System;
using System.Collections.Generic;
using Stripe;
using Stripe.Infrastructure.FormEncoding;
Expand All @@ -8,7 +9,7 @@ namespace StripeTests
public class SubscriptionCreateOptionsTest : BaseStripeTest
{
[Fact]
public void Serialize()
public void SerializeItems()
{
var options = new SubscriptionCreateOptions
{
Expand All @@ -34,5 +35,27 @@ public void Serialize()
"items[1][plan]=plan_124&items[1][quantity]=3",
FormEncoder.CreateQueryString(options));
}

[Fact]
public void SerializeTrialEndDateTime()
{
var options = new SubscriptionCreateOptions
{
TrialEnd = DateTime.Parse("Fri, 13 Feb 2009 23:31:30Z"),
};

Assert.Equal("trial_end=1234567890", FormEncoder.CreateQueryString(options));
}

[Fact]
public void SerializeTrialEndNow()
{
var options = new SubscriptionCreateOptions
{
TrialEnd = SubscriptionTrialEnd.Now,
};

Assert.Equal("trial_end=now", FormEncoder.CreateQueryString(options));
}
}
}

0 comments on commit 7ca274b

Please sign in to comment.