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

Support for flexible billing and usage records #421

Merged
merged 1 commit into from
Apr 24, 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
1 change: 1 addition & 0 deletions stripe/api_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@
from stripe.api_resources.token import Token
from stripe.api_resources.topup import Topup
from stripe.api_resources.transfer import Transfer
from stripe.api_resources.usage_record import UsageRecord
26 changes: 26 additions & 0 deletions stripe/api_resources/usage_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import absolute_import, division, print_function

from stripe.api_resources.abstract.api_resource import APIResource
from stripe import api_requestor, util


class UsageRecord(APIResource):
OBJECT_NAME = 'usage_record'

@classmethod
def create(cls, api_key=None, idempotency_key=None,
stripe_version=None, stripe_account=None, **params):
if 'subscription_item' not in params:
raise ValueError("Params must have a subscription_item key")

subscription_item = params.pop('subscription_item')

requestor = api_requestor.APIRequestor(api_key,
api_version=stripe_version,
account=stripe_account)
url = "/v1/subscription_items/%s/usage_records" % subscription_item
headers = util.populate_headers(idempotency_key)
response, api_key = requestor.request('post', url, params, headers)

return util.convert_to_stripe_object(response, api_key, stripe_version,
stripe_account)
1 change: 1 addition & 0 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def load_object_classes():
api_resources.Token.OBJECT_NAME: api_resources.Token,
api_resources.Topup.OBJECT_NAME: api_resources.Topup,
api_resources.Transfer.OBJECT_NAME: api_resources.Transfer,
api_resources.UsageRecord.OBJECT_NAME: api_resources.UsageRecord,
}


Expand Down
32 changes: 32 additions & 0 deletions tests/api_resources/test_usage_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase


TEST_SUBSCRIPTION_ITEM_ID = 'si_123'


class UsageRecordTest(StripeTestCase):
def test_is_creatable(self):
resource = stripe.UsageRecord.create(
subscription_item=TEST_SUBSCRIPTION_ITEM_ID,
quantity=5000,
timestamp=1524182400,
action='increment',
)
self.assert_requested(
'post',
'/v1/subscription_items/%s/usage_records' % (
TEST_SUBSCRIPTION_ITEM_ID
)
)
self.assertIsInstance(resource, stripe.UsageRecord)

def test_raises_when_creating_without_subscription_item(self):
with self.assertRaises(ValueError):
stripe.UsageRecord.create(
quantity=5000,
timestamp=1524182400,
action='increment',
)