From 9c210ae2e4ac9184d453cfa9642601723b95ca12 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Sep 2019 21:56:54 +0530 Subject: [PATCH] Fix bugs: gettings all plans and putting them back to db --- stripe_utils.py | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/stripe_utils.py b/stripe_utils.py index a4d79b9..2d61026 100644 --- a/stripe_utils.py +++ b/stripe_utils.py @@ -1,8 +1,9 @@ +import json import re import stripe import config -from config import logging as logger +from config import logging as logger, etcd_client as client stripe.api_key = config.STRIPE_API_PRIVATE_KEY @@ -264,17 +265,16 @@ class StripeUtils(object): return charge def _get_all_stripe_plans(self): - from config import etcd_client as client - all_stripe_plans = client.get("/v1/stripe_plans", value_in_json=True) - all_stripe_plans_list = [] - if (all_stripe_plans and - len(all_stripe_plans.value["stripe_plans"]) > 0): - all_stripe_plans_list = all_stripe_plans.value["stripe_plans"] - return all_stripe_plans_list + all_stripe_plans = client.get("/v1/stripe_plans") + all_stripe_plans_set = set() + all_stripe_plans_obj = json.loads(all_stripe_plans.value) + if (all_stripe_plans_obj and + len(all_stripe_plans_obj['plans']) > 0): + all_stripe_plans_set = set(all_stripe_plans_obj["plans"]) + return all_stripe_plans_set def _save_all_stripe_plans(self, stripe_plans): - from config import etcd_client as client - client.put("/v1/stripe_plans", {"stripe_plans": stripe_plans}) + client.put("/v1/stripe_plans", json.dumps({"plans": list(stripe_plans)})) @handleStripeError def get_or_create_stripe_plan(self, amount, name, stripe_plan_id): @@ -295,24 +295,26 @@ class StripeUtils(object): """ all_stripe_plans = self._get_all_stripe_plans() if stripe_plan_id in all_stripe_plans: - logger.debug("{} plan exists.".format(stripe_plan_id)) + logger.debug("{} plan exists in db.".format(stripe_plan_id)) else: - logger.debug("{} plan DOES NOT exist. " - "Creating".format(stripe_plan_id)) + logger.debug(("{} plan DOES NOT exist in db. " + "Creating").format(stripe_plan_id)) try: - self.stripe.Plan.create( - amount=amount, - interval=self.INTERVAL, - name=name, - currency=self.CURRENCY, - id=stripe_plan_id) - all_stripe_plans.append(stripe_plan_id) - + plan_obj = self.stripe.Plan.retrieve(id=stripe_plan_id) + logger.debug("{} plan exists in Stripe".format(stripe_plan_id)) + all_stripe_plans.add(stripe_plan_id) except stripe.error.InvalidRequestError as e: - if self.STRIPE_PLAN_ALREADY_EXISTS in str(e): + if "No such plan" in str(e): + logger.debug("Plan {} does not exist in Stripe, Creating") + plan_obj = self.stripe.Plan.create( + amount=amount, + interval=self.INTERVAL, + name=name, + currency=self.CURRENCY, + id=stripe_plan_id) logger.debug( self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id)) - all_stripe_plans.append(stripe_plan_id) + all_stripe_plans.add(stripe_plan_id) self._save_all_stripe_plans(all_stripe_plans) return stripe_plan_id