Skip to content

Commit 668429f

Browse files
alexander-stripeob-stripe
authored andcommitted
support for flexible billing and usage records
1 parent 49860ea commit 668429f

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

stripe/api_resources/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@
4545
from stripe.api_resources.token import Token
4646
from stripe.api_resources.topup import Topup
4747
from stripe.api_resources.transfer import Transfer
48+
from stripe.api_resources.usage_record import UsageRecord
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
from stripe.api_resources.abstract.api_resource import APIResource
4+
from stripe import api_requestor, util
5+
6+
7+
class UsageRecord(APIResource):
8+
OBJECT_NAME = 'usage_record'
9+
10+
@classmethod
11+
def create(cls, api_key=None, idempotency_key=None,
12+
stripe_version=None, stripe_account=None, **params):
13+
if 'subscription_item' not in params:
14+
raise ValueError("Params must have a subscription_item key")
15+
16+
subscription_item = params.pop('subscription_item')
17+
18+
requestor = api_requestor.APIRequestor(api_key,
19+
api_version=stripe_version,
20+
account=stripe_account)
21+
url = "/v1/subscription_items/%s/usage_records" % subscription_item
22+
headers = util.populate_headers(idempotency_key)
23+
response, api_key = requestor.request('post', url, params, headers)
24+
25+
return util.convert_to_stripe_object(response, api_key, stripe_version,
26+
stripe_account)

stripe/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def load_object_classes():
218218
api_resources.Token.OBJECT_NAME: api_resources.Token,
219219
api_resources.Topup.OBJECT_NAME: api_resources.Topup,
220220
api_resources.Transfer.OBJECT_NAME: api_resources.Transfer,
221+
api_resources.UsageRecord.OBJECT_NAME: api_resources.UsageRecord,
221222
}
222223

223224

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
import stripe
4+
from tests.helper import StripeTestCase
5+
6+
7+
TEST_SUBSCRIPTION_ITEM_ID = 'si_123'
8+
9+
10+
class UsageRecordTest(StripeTestCase):
11+
def test_is_creatable(self):
12+
resource = stripe.UsageRecord.create(
13+
subscription_item=TEST_SUBSCRIPTION_ITEM_ID,
14+
quantity=5000,
15+
timestamp=1524182400,
16+
action='increment',
17+
)
18+
self.assert_requested(
19+
'post',
20+
'/v1/subscription_items/%s/usage_records' % (
21+
TEST_SUBSCRIPTION_ITEM_ID
22+
)
23+
)
24+
self.assertIsInstance(resource, stripe.UsageRecord)
25+
26+
def test_raises_when_creating_without_subscription_item(self):
27+
with self.assertRaises(ValueError):
28+
stripe.UsageRecord.create(
29+
quantity=5000,
30+
timestamp=1524182400,
31+
action='increment',
32+
)

0 commit comments

Comments
 (0)