Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle DateTimes directly in the encoder #1283

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ private static List<Parameter> FlattenParamsValue(object value, string keyPrefix

flatParams.Add(new Parameter(keyPrefix, paramValue));
}
else if (value is DateTime)
{
flatParams = new List<Parameter>();
DateTime? dateTime = (DateTime)value;
if (dateTime.HasValue)
{
flatParams.Add(new Parameter(keyPrefix, dateTime?.ConvertDateTimeToEpoch().ToString()));
}
}
else if (value == null)
{
flatParams = new List<Parameter>();
Expand Down
15 changes: 1 addition & 14 deletions src/Stripe.net/Services/Account/StripeAccountSharedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,8 @@ public abstract class StripeAccountSharedOptions : StripeBaseOptions, ISupportMe

#region Tos Acceptance

public DateTime? TosAcceptanceDate { get; set; }

[JsonProperty("tos_acceptance[date]")]
internal long? TosAcceptanceDateInternal
{
get
{
if (!this.TosAcceptanceDate.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.TosAcceptanceDate.Value);
}
}
public DateTime? TosAcceptanceDate { get; set; }

[JsonProperty("tos_acceptance[ip]")]
public string TosAcceptanceIp { get; set; }
Expand Down
15 changes: 1 addition & 14 deletions src/Stripe.net/Services/Coupons/StripeCouponCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ public class StripeCouponCreateOptions : StripeBaseOptions, ISupportMetadata
[JsonProperty("name")]
public string Name { get; set; }

public DateTime? RedeemBy { get; set; }

[JsonProperty("redeem_by")]
internal long? RedeemByInternal
{
get
{
if (!this.RedeemBy.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.RedeemBy.Value);
}
}
public DateTime? RedeemBy { get; set; }
}
}
15 changes: 1 addition & 14 deletions src/Stripe.net/Services/FileLinks/StripeFileLinkSharedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,8 @@ public class StripeFileLinkSharedOptions : StripeBaseOptions, ISupportMetadata
/// <summary>
/// A future timestamp after which the link will no longer be usable.
/// </summary>
public DateTime? ExpiresAt { get; set; }

[JsonProperty("expires_at")]
internal long? ExpiresAtInternal
{
get
{
if (!this.ExpiresAt.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.ExpiresAt.Value);
}
}
public DateTime? ExpiresAt { get; set; }

/// <summary>
/// Set of key-value pairs that you can attach to an object. This can be useful for storing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public class StripeInvoiceCreateOptions : StripeBaseOptions, ISupportMetadata
/// <summary>
/// The date on which payment for this invoice is due. Only valid for invoices where billing=send_invoice.
/// </summary>
public DateTime? DueDate { get; set; }

[JsonProperty("due_date")]
internal long? DueDateInternal => this.DueDate?.ConvertDateTimeToEpoch();
public DateTime? DueDate { get; set; }

[JsonProperty("metadata")]
public Dictionary<string, string> Metadata { get; set; }
Expand All @@ -45,4 +43,4 @@ public class StripeInvoiceCreateOptions : StripeBaseOptions, ISupportMetadata
[JsonProperty("tax_percent")]
public decimal? TaxPercent { get; set; }
}
}
}
45 changes: 3 additions & 42 deletions src/Stripe.net/Services/Invoices/StripeUpcomingInvoiceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,8 @@ public class StripeUpcomingInvoiceOptions : StripeBaseOptions
/// <summary>
/// A future date to anchor the subscription’s <see href="https://stripe.com/docs/subscriptions/billing-cycle">billing cycle</see>. This is used to determine the date of the first full invoice, and, for plans with <c>month</c> or <c>year</c> intervals, the day of the month for subsequent invoices.
/// </summary>
public DateTime? SubscriptionBillingCycleAnchor { get; set; }

[JsonProperty("subscription_billing_cycle_anchor")]
internal long? SubscriptionBillingCycleAnchorInternal
{
get
{
if (!this.SubscriptionBillingCycleAnchor.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.SubscriptionBillingCycleAnchor.Value);
}
}
public DateTime? SubscriptionBillingCycleAnchor { get; set; }

[JsonProperty("subscription")]
public string SubscriptionId { get; set; }
Expand All @@ -44,43 +31,17 @@ internal long? SubscriptionBillingCycleAnchorInternal
[JsonProperty("subscription_prorate")]
public bool? SubscriptionProrate { get; set; }

public DateTime? SubscriptionProrationDate { get; set; }

[JsonProperty("subscription_proration_date")]
internal long? SubscriptionProrationDateInternal
{
get
{
if (!this.SubscriptionProrationDate.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.SubscriptionProrationDate.Value);
}
}
public DateTime? SubscriptionProrationDate { get; set; }

[JsonProperty("subscription_quantity")]
public int? SubscriptionQuantity { get; set; }

[JsonProperty("subscription_tax_percent")]
public decimal? SubscriptionTaxPercent { get; set; }

public DateTime? SubscriptionTrialEnd { get; set; }

[JsonProperty("subscription_trial_end")]
internal long? SubscriptionTrialEndInternal
{
get
{
if (!this.SubscriptionTrialEnd.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.SubscriptionTrialEnd.Value);
}
}
public DateTime? SubscriptionTrialEnd { get; set; }

[JsonProperty("subscription_trial_from_plan")]
public bool? SubscriptionTrialFromPlan { get; set; }
Expand Down
30 changes: 2 additions & 28 deletions src/Stripe.net/Services/Reporting/ReportRuns/ParametersOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,11 @@ public class ParametersOptions : INestedOptions
[JsonProperty("currency")]
public string Currency { get; set; }

public DateTime? IntervalEnd { get; set; }

[JsonProperty("interval_end")]
internal long? IntervalEndInternal
{
get
{
if (!this.IntervalEnd.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.IntervalEnd.Value);
}
}

public DateTime? IntervalStart { get; set; }
public DateTime? IntervalEnd { get; set; }

[JsonProperty("interval_start")]
internal long? IntervalStartInternal
{
get
{
if (!this.IntervalStart.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.IntervalStart.Value);
}
}
public DateTime? IntervalStart { get; set; }

[JsonProperty("payout")]
public string Payout { get; set; }
Expand Down
15 changes: 1 addition & 14 deletions src/Stripe.net/Services/Sources/StripeSourceMandateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,8 @@ namespace Stripe

public class StripeSourceMandateOptions : INestedOptions
{
public DateTime? MandateAcceptanceDate { get; set; }

[JsonProperty("acceptance[date]")]
internal long? MandateAcceptanceDateInternal
{
get
{
if (!this.MandateAcceptanceDate.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.MandateAcceptanceDate.Value);
}
}
public DateTime? MandateAcceptanceDate { get; set; }

[JsonProperty("acceptance[ip]")]
public string MandateAcceptanceIp { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,8 @@ public abstract class SubscriptionItemSharedOptions : StripeBaseOptions
/// <summary>
/// If set, the proration will be calculated as though the subscription was updated at the given time. This can be used to apply the same proration that was previewed with the upcoming invoice endpoint.
/// </summary>
public DateTime? ProrationDate { get; set; }

[JsonProperty("proration_date")]
internal long? ProrationDateInternal
{
get
{
if (!this.ProrationDate.HasValue)
{
return null;
}

return this.ProrationDate.Value.ConvertDateTimeToEpoch();
}
}
public DateTime? ProrationDate { get; set; }

/// <summary>
/// The quantity you’d like to apply to the subscription item you’re creating.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,8 @@ public class StripeSubscriptionCreateOptions : SubscriptionSharedOptions
/// <summary>
/// A future date to anchor the subscription’s <see href="https://stripe.com/docs/subscriptions/billing-cycle">billing cycle</see>. This is used to determine the date of the first full invoice, and, for plans with <c>month</c> or <c>year</c> intervals, the day of the month for subsequent invoices.
/// </summary>
public DateTime? BillingCycleAnchor { get; set; }

[JsonProperty("billing_cycle_anchor")]
internal long? BillingCycleAnchorInternal
{
get
{
if (!this.BillingCycleAnchor.HasValue)
{
return null;
}

return EpochTime.ConvertDateTimeToEpoch(this.BillingCycleAnchor.Value);
}
}
public DateTime? BillingCycleAnchor { get; set; }

/// <summary>
/// REQUIRED: The identifier of the customer to subscribe.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,10 @@ internal string BillingCycleAnchorInternal
[JsonProperty("items")]
public List<StripeSubscriptionItemUpdateOption> Items { get; set; }

#region ProrationDate

/// <summary>
/// Boolean indicating whether this subscription should cancel at the end of the current period.
/// </summary>
public DateTime? ProrationDate { get; set; }

[JsonProperty("proration_date")]
internal string ProrationDateInternal => this.ProrationDate?.ConvertDateTimeToEpoch().ToString();
#endregion
public DateTime? ProrationDate { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ public class StripeUsageRecordCreateOptions : StripeBaseOptions
[JsonIgnore]
public string SubscriptionItem { get; set; }

public DateTime Timestamp { get; set; }

[JsonProperty("timestamp")]
internal string TimestampInternal
{
get { return this.Timestamp.ConvertDateTimeToEpoch().ToString(); }
}
public DateTime Timestamp { get; set; }

[JsonProperty("quantity")]
public int Quantity { get; set; }
}
}
}
10 changes: 10 additions & 0 deletions src/StripeTests/Infrastructure/ParameterBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public void EncodesParameters()
want = "?bool=False&date_filter[lt]=946702800&date_filter[gte]=946684800&decimal=0&enum=test_one&int=0"
},

// DateTimeNullable
new
{
data = new TestOptions
{
DateTimeNullable = DateTime.Parse("Sat, 01 Jan 2000 00:00:00Z"),
},
want = "?bool=False&datetime_nullable=946684800&decimal=0&enum=test_one&int=0"
},

// Decimal
new
{
Expand Down
3 changes: 3 additions & 0 deletions src/StripeTests/Infrastructure/TestData/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public enum TestEnum
[JsonProperty("date_filter")]
public StripeDateFilter DateFilter { get; set; }

[JsonProperty("datetime_nullable")]
public DateTime? DateTimeNullable { get; set; }

[JsonProperty("decimal")]
public decimal Decimal { get; set; }

Expand Down