From 4dc264e8e132961b6c825b591475b0e837f9f93a Mon Sep 17 00:00:00 2001 From: Joshua Tompkins Date: Fri, 9 Jan 2015 15:15:41 -0500 Subject: [PATCH] Resolves #21 - Add method to cancel a single sub. --- src/StripeClient.Subscriptions.cs | 15 +++++++++++++++ test/Stripe.Tests/SubscriptionTests.cs | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/StripeClient.Subscriptions.cs b/src/StripeClient.Subscriptions.cs index b6fb354..f63d963 100644 --- a/src/StripeClient.Subscriptions.cs +++ b/src/StripeClient.Subscriptions.cs @@ -45,5 +45,20 @@ public StripeObject CancelCustomersSubscription(string customerId, bool? atPerio return ExecuteObject(request); } + + public StripeObject CancelCustomersSubscription(string customerId, string subscriptionId, bool? atPeriodEnd = null) { + Require.Argument("customerId", customerId); + + var request = new RestRequest(); + request.Method = Method.DELETE; + request.Resource = "customers/{customerId}/subscriptions/{subscriptionId}"; + + request.AddUrlSegment("customerId", customerId); + request.AddUrlSegment("subscriptionId", subscriptionId); + + if (atPeriodEnd.HasValue) request.AddParameter("at_period_end", atPeriodEnd.Value); + + return ExecuteObject(request); + } } } diff --git a/test/Stripe.Tests/SubscriptionTests.cs b/test/Stripe.Tests/SubscriptionTests.cs index 1defeca..1ae977c 100644 --- a/test/Stripe.Tests/SubscriptionTests.cs +++ b/test/Stripe.Tests/SubscriptionTests.cs @@ -46,5 +46,22 @@ public void CancelCustomersSubscription_Test() Assert.NotNull(response.CanceledAt); Assert.NotNull(response.EndedAt); } + + [Fact] + public void CancelSingleCustomersSubscription_Test() { + dynamic subscriptionResponse = _client.UpdateCustomersSubscription(_customer.Id, _plan.Id); + + Assert.NotNull(subscriptionResponse); + Assert.False(subscriptionResponse.IsError); + + var subId = subscriptionResponse["id"]; + + dynamic response = _client.CancelCustomersSubscription(_customer.Id, subId); + + Assert.NotNull(response); + Assert.False(response.IsError); + Assert.NotNull(response.CanceledAt); + Assert.NotNull(response.EndedAt); + } } }