From 947efa8001fd46f996db145fd117fac5af58e4cc Mon Sep 17 00:00:00 2001 From: David Pham <58667176+synominit@users.noreply.github.com> Date: Tue, 28 Dec 2021 10:37:59 -0500 Subject: [PATCH] Added Subscription Class (#49) * Added add data object to machine method * Added method to remove data object from machine * Added method to list machine data objects * Added method to list data objects for key * Update the reference to method parameters * Added Subscription and Analytics Classes Added Subscription Class with method to increment meter for stripe billing. Added Analytics Class with methods to register single events and get events. * Update methods.py * Removed Redundant Analytics class (See AI Class) * Small typo fix * Update methods.py Co-authored-by: Artem Los --- licensing/methods.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/licensing/methods.py b/licensing/methods.py index fb6b575..4250cba 100644 --- a/licensing/methods.py +++ b/licensing/methods.py @@ -1081,4 +1081,43 @@ def HasFeature(license_key, feature_name): if not(found): return False - return True \ No newline at end of file + return True + +class Subscription: + """ + Subscription related methods + """ + + @staticmethod + def record_usage_to_stripe(token, product_id, key, amount=""): + + """ + This method records uses Stripe's metered billing to record usage for a certain subscription. In order to use this method, + you need to have set up recurring billing. A record will be created using Stripe's API with action set to 'increment' + + More docs: https://app.cryptolens.io/docs/api/v3/RecordUsage + """ + + try: + response = HelperMethods.send_request("/subscription/RecordUsage/",\ + {"token":token,\ + "ProductId" : product_id,\ + "Key" : key,\ + "Amount" : amount, + }) + except HTTPError as e: + response = e.read() + except URLError as e: + return (None, "Could not contact the server. Error message: " + str(e)) + except Exception: + return (None, "Could not contact the server.") + + jobj = json.loads(response) + + if jobj == None or not("result" in jobj) or jobj["result"] == 1: + if jobj != None: + return (None, jobj["message"]) + else: + return (None, "Could not contact the server.") + + return (jobj, "")