Fix bugs: gettings all plans and putting them back to db

This commit is contained in:
PCoder 2019-09-14 21:56:54 +05:30
parent eb241c8b85
commit 9c210ae2e4
1 changed files with 25 additions and 23 deletions

View File

@ -1,8 +1,9 @@
import json
import re import re
import stripe import stripe
import config 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 stripe.api_key = config.STRIPE_API_PRIVATE_KEY
@ -264,17 +265,16 @@ class StripeUtils(object):
return charge return charge
def _get_all_stripe_plans(self): def _get_all_stripe_plans(self):
from config import etcd_client as client all_stripe_plans = client.get("/v1/stripe_plans")
all_stripe_plans = client.get("/v1/stripe_plans", value_in_json=True) all_stripe_plans_set = set()
all_stripe_plans_list = [] all_stripe_plans_obj = json.loads(all_stripe_plans.value)
if (all_stripe_plans and if (all_stripe_plans_obj and
len(all_stripe_plans.value["stripe_plans"]) > 0): len(all_stripe_plans_obj['plans']) > 0):
all_stripe_plans_list = all_stripe_plans.value["stripe_plans"] all_stripe_plans_set = set(all_stripe_plans_obj["plans"])
return all_stripe_plans_list return all_stripe_plans_set
def _save_all_stripe_plans(self, stripe_plans): def _save_all_stripe_plans(self, stripe_plans):
from config import etcd_client as client client.put("/v1/stripe_plans", json.dumps({"plans": list(stripe_plans)}))
client.put("/v1/stripe_plans", {"stripe_plans": stripe_plans})
@handleStripeError @handleStripeError
def get_or_create_stripe_plan(self, amount, name, stripe_plan_id): 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() all_stripe_plans = self._get_all_stripe_plans()
if stripe_plan_id in 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: else:
logger.debug("{} plan DOES NOT exist. " logger.debug(("{} plan DOES NOT exist in db. "
"Creating".format(stripe_plan_id)) "Creating").format(stripe_plan_id))
try: try:
self.stripe.Plan.create( plan_obj = self.stripe.Plan.retrieve(id=stripe_plan_id)
amount=amount, logger.debug("{} plan exists in Stripe".format(stripe_plan_id))
interval=self.INTERVAL, all_stripe_plans.add(stripe_plan_id)
name=name,
currency=self.CURRENCY,
id=stripe_plan_id)
all_stripe_plans.append(stripe_plan_id)
except stripe.error.InvalidRequestError as e: 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( logger.debug(
self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id)) 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) self._save_all_stripe_plans(all_stripe_plans)
return stripe_plan_id return stripe_plan_id