-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #532 from stripe/remi-add-subscription-schedules
Add support for Subscription Schedules
- Loading branch information
Showing
10 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from stripe import util | ||
from stripe.api_resources.abstract import CreateableAPIResource | ||
from stripe.api_resources.abstract import UpdateableAPIResource | ||
from stripe.api_resources.abstract import ListableAPIResource | ||
from stripe.api_resources.abstract import nested_resource_class_methods | ||
|
||
|
||
@nested_resource_class_methods("revision", operations=["retrieve", "list"]) | ||
class SubscriptionSchedule( | ||
CreateableAPIResource, UpdateableAPIResource, ListableAPIResource | ||
): | ||
OBJECT_NAME = "subscription_schedule" | ||
|
||
def cancel(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/cancel" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("post", url, params, headers)) | ||
return self | ||
|
||
def release(self, idempotency_key=None, **params): | ||
url = self.instance_url() + "/release" | ||
headers = util.populate_headers(idempotency_key) | ||
self.refresh_from(self.request("post", url, params, headers)) | ||
return self | ||
|
||
def revisions(self, **params): | ||
return self.request("get", self.instance_url() + "/revisions", params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from stripe import util | ||
from stripe.api_resources.abstract.api_resource import APIResource | ||
from stripe.api_resources.subscription_schedule import SubscriptionSchedule | ||
from stripe.six.moves.urllib.parse import quote_plus | ||
|
||
|
||
class SubscriptionScheduleRevision(APIResource): | ||
OBJECT_NAME = "subscription_schedule_revision" | ||
|
||
def instance_url(self): | ||
token = util.utf8(self.id) | ||
schedule = util.utf8(self.schedule) | ||
base = SubscriptionSchedule.class_url() | ||
schedule_extn = quote_plus(schedule) | ||
extn = quote_plus(token) | ||
return "%s/%s/revisions/%s" % (base, schedule_extn, extn) | ||
|
||
@classmethod | ||
def retrieve(cls, id, api_key=None, **params): | ||
raise NotImplementedError( | ||
"Can't retrieve a subscription schedule revision without a schedule " | ||
"ID. Use schedule.revisions.retrieve('revision_id')" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import stripe | ||
|
||
|
||
TEST_RESOURCE_ID = "sub_sched_123" | ||
TEST_REVISION_ID = "sub_sched_rev_123" | ||
|
||
|
||
class TestSubscriptionScheduleSchedule(object): | ||
def test_is_listable(self, request_mock): | ||
resources = stripe.SubscriptionSchedule.list() | ||
request_mock.assert_requested("get", "/v1/subscription_schedules") | ||
assert isinstance(resources.data, list) | ||
assert isinstance(resources.data[0], stripe.SubscriptionSchedule) | ||
|
||
def test_is_retrievable(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.retrieve(TEST_RESOURCE_ID) | ||
request_mock.assert_requested( | ||
"get", "/v1/subscription_schedules/%s" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.SubscriptionSchedule) | ||
|
||
def test_is_creatable(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.create(customer="cus_123") | ||
request_mock.assert_requested("post", "/v1/subscription_schedules") | ||
assert isinstance(resource, stripe.SubscriptionSchedule) | ||
|
||
def test_is_saveable(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.retrieve(TEST_RESOURCE_ID) | ||
resource.metadata["key"] = "value" | ||
resource.save() | ||
request_mock.assert_requested( | ||
"post", "/v1/subscription_schedules/%s" % TEST_RESOURCE_ID | ||
) | ||
|
||
def test_is_modifiable(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.modify( | ||
TEST_RESOURCE_ID, metadata={"key": "value"} | ||
) | ||
request_mock.assert_requested( | ||
"post", "/v1/subscription_schedules/%s" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.SubscriptionSchedule) | ||
|
||
def test_can_cancel(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.retrieve(TEST_RESOURCE_ID) | ||
resource = resource.cancel() | ||
request_mock.assert_requested( | ||
"post", "/v1/subscription_schedules/%s/cancel" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.SubscriptionSchedule) | ||
|
||
def test_can_release(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.retrieve(TEST_RESOURCE_ID) | ||
resource = resource.release() | ||
request_mock.assert_requested( | ||
"post", "/v1/subscription_schedules/%s/release" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resource, stripe.SubscriptionSchedule) | ||
|
||
|
||
class TestSubscriptionScheduleRevisions(object): | ||
def test_is_listable(self, request_mock): | ||
resources = stripe.SubscriptionSchedule.list_revisions( | ||
TEST_RESOURCE_ID | ||
) | ||
request_mock.assert_requested( | ||
"get", "/v1/subscription_schedules/%s/revisions" % TEST_RESOURCE_ID | ||
) | ||
assert isinstance(resources.data, list) | ||
assert isinstance( | ||
resources.data[0], stripe.SubscriptionScheduleRevision | ||
) | ||
|
||
def test_is_retrievable(self, request_mock): | ||
resource = stripe.SubscriptionSchedule.retrieve_revision( | ||
TEST_RESOURCE_ID, TEST_REVISION_ID | ||
) | ||
request_mock.assert_requested( | ||
"get", | ||
"/v1/subscription_schedules/%s/revisions/%s" | ||
% (TEST_RESOURCE_ID, TEST_REVISION_ID), | ||
) | ||
assert isinstance(resource, stripe.SubscriptionScheduleRevision) |
32 changes: 32 additions & 0 deletions
32
tests/api_resources/test_subscription_schedule_revision.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import pytest | ||
|
||
import stripe | ||
|
||
|
||
TEST_RESOURCE_ID = "sub_sched_rev_123" | ||
|
||
|
||
class TestSubscriptionScheduleRevision(object): | ||
def construct_resource(self): | ||
revision_dict = { | ||
"id": TEST_RESOURCE_ID, | ||
"object": "subscription_schedule_revision", | ||
"schedule": "sub_sched_123", | ||
} | ||
return stripe.SubscriptionScheduleRevision.construct_from( | ||
revision_dict, stripe.api_key | ||
) | ||
|
||
def test_has_instance_url(self, request_mock): | ||
resource = self.construct_resource() | ||
assert ( | ||
resource.instance_url() | ||
== "/v1/subscription_schedules/sub_sched_123/revisions/%s" | ||
% TEST_RESOURCE_ID | ||
) | ||
|
||
def test_is_not_retrievable(self, request_mock): | ||
with pytest.raises(NotImplementedError): | ||
stripe.SubscriptionScheduleRevision.retrieve(TEST_RESOURCE_ID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters