From c7de56fbccc4059e26df24e7bdf88fac97e8d9d2 Mon Sep 17 00:00:00 2001 From: Giovani Brady Date: Sat, 12 Sep 2015 11:27:35 -0700 Subject: [PATCH] Plans parameter updates --- src/PlanFrequency.cs | 1 + src/StripeClient.Plans.cs | 76 +++++++++++++++++++++++++++++++--- test/Stripe.Tests/PlanTests.cs | 16 +++++++ 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/PlanFrequency.cs b/src/PlanFrequency.cs index cb4fd06..4e410a5 100644 --- a/src/PlanFrequency.cs +++ b/src/PlanFrequency.cs @@ -5,6 +5,7 @@ namespace Stripe { public enum PlanFrequency { + Day, Month, Year } diff --git a/src/StripeClient.Plans.cs b/src/StripeClient.Plans.cs index 2323b02..6bd9ddf 100644 --- a/src/StripeClient.Plans.cs +++ b/src/StripeClient.Plans.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using RestSharp; using RestSharp.Validation; @@ -7,7 +8,22 @@ namespace Stripe { public partial class StripeClient { - public StripeObject CreatePlan(string planId, decimal amount, string currency, PlanFrequency interval, string name, int? trialPeriodDays = null) + /// + /// You can create plans easily via the plan management page of the Stripe dashboard. Plan creation is also accessible via the API if you need to create plans on the fly. + /// + /// Unique string of your choice that will be used to identify this plan when subscribing a customer. + /// A positive integer in cents (or 0 for a free plan) representing how much to charge (on a recurring basis). + /// 3-letter ISO code for currency. + /// Specifies billing frequency. Either day, week, month or year. + /// Name of the plan, to be displayed on invoices and in the web interface. + /// Specifies a trial period in (an integer number of) days. + /// The number of intervals between each subscription billing. For example, interval=month and interval_count=3 bills every 3 months. + /// An arbitrary string to be displayed on your customer’s credit card statement. + /// A set of key/value pairs that you can attach to a plan object. + /// Returns the plan object. + public StripeObject CreatePlan(string planId, decimal amount, string currency, PlanFrequency interval, + string name, int? trialPeriodDays = null, int? intervalCount = 1, string statementDescriptor = null, + IDictionary metaData = null) { Require.Argument("planId", planId); Require.Argument("amount", amount); @@ -29,11 +45,20 @@ public StripeObject CreatePlan(string planId, decimal amount, string currency, P request.AddParameter("currency", currency); request.AddParameter("interval", interval.ToString().ToLowerInvariant()); request.AddParameter("name", name); - if (trialPeriodDays.HasValue) request.AddParameter("trial_period_days", trialPeriodDays); + + if (intervalCount.HasValue) request.AddParameter("interval_count", intervalCount); + if (trialPeriodDays.HasValue) request.AddParameter("trial_period_days", trialPeriodDays); + if (statementDescriptor.HasValue()) request.AddParameter("statement_descriptor", statementDescriptor); + if (metaData != null) AddDictionaryParameter(metaData, "metadata", ref request); return ExecuteObject(request); } + /// + /// Retrieves the plan with the given ID. + /// + /// The ID of the desired plan. + /// Returns a plan if a valid plan ID was provided. Returns an error otherwise. public StripeObject RetreivePlan(string planId) { Require.Argument("planId", planId); @@ -46,6 +71,11 @@ public StripeObject RetreivePlan(string planId) return ExecuteObject(request); } + /// + /// You can delete plans via the plan management page of the Stripe dashboard. However, deleting a plan does not affect any current subscribers to the plan; it merely means that new subscribers can’t be added to that plan. You can also delete plans via the API. + /// + /// The identifier of the plan to be deleted. + /// An object with the deleted plan’s ID and a deleted flag upon success. Otherwise, this call returns an error, such as if the plan has already been deleted. public StripeObject DeletePlan(string planId) { Require.Argument("planId", planId); @@ -59,15 +89,51 @@ public StripeObject DeletePlan(string planId) return ExecuteObject(request); } - public StripeArray ListPlans(int? count = null, int? offset = null) + /// + /// Returns a list of your plans. + /// + /// A filter on the list based on the object created field. + /// A cursor for use in pagination. ending_before is an object ID that defines your place in the list. + /// A limit on the number of objects to be returned. Limit can range between 1 and 100 items. + /// A cursor for use in pagination. starting_after is an object ID that defines your place in the list. + /// A dictionary with a data property that contains an array of up to limit plans, starting after plan starting_after. Each entry in the array is a separate plan object. If no more plans are available, the resulting array will be empty. + public StripeArray ListPlans(IDictionary created = null, string endingBefore = null, int? limit = 10, string startingAfter = null) { var request = new RestRequest(); request.Resource = "plans"; + request.Method = Method.GET; - if (count.HasValue) request.AddParameter("count", count.Value); - if (offset.HasValue) request.AddParameter("offset", offset.Value); + if (limit.HasValue) request.AddQueryParameter("limit", limit.ToString()); + if (created != null) AddDictionaryParameter(created, "created", ref request); + if (endingBefore.HasValue()) request.AddParameter("ending_before", endingBefore); + if (startingAfter.HasValue()) request.AddParameter("starting_after", startingAfter); return ExecuteArray(request); } + + /// + /// Updates the name of a plan. Other plan details (price, interval, etc.) are, by design, not editable. + /// + /// The identifier of the plan to be updated. + /// Name of the plan, to be displayed on invoices and in the web interface. + /// An arbitrary string to be displayed on your customer’s credit card statement. This may be up to 22 characters. + /// A set of key/value pairs that you can attach to a plan object. + /// The updated plan object is returned upon success. + public StripeObject UpdatePlan(string planId, string name = null, string statementDescriptor = null, IDictionary metadata = null) + { + Require.Argument("planId", planId); + + var request = new RestRequest(); + request.Resource = "plans/{planId}"; + request.Method = Method.POST; + + request.AddUrlSegment("planId", planId); + + if (name.HasValue()) request.AddParameter("name", name); + if (statementDescriptor.HasValue()) request.AddParameter("statement_descriptor", statementDescriptor); + if (metadata != null) AddDictionaryParameter(metadata, "metadata", ref request); + + return ExecuteObject(request); + } } } diff --git a/test/Stripe.Tests/PlanTests.cs b/test/Stripe.Tests/PlanTests.cs index 06b02e0..b323975 100644 --- a/test/Stripe.Tests/PlanTests.cs +++ b/test/Stripe.Tests/PlanTests.cs @@ -61,5 +61,21 @@ public void ListPlans_Test() Assert.False(response.IsError); Assert.True(response.Any()); } + + [Fact] + public void UpdatePlan_Test() + { + var id = Guid.NewGuid().ToString(); + + string newName = "Testing123"; + + dynamic plan = _client.CreatePlan(id, 400M, "usd", PlanFrequency.Month, id); + dynamic response = _client.UpdatePlan(id, newName); + + Assert.NotNull(response); + Assert.False(response.IsError); + Assert.Equal(plan.Id, response.Id); + Assert.Equal(response.name, newName); + } } }