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 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