Add/delete Stripe plans

This commit is contained in:
PCoder 2019-09-14 18:32:26 +05:30
parent 5749b644ee
commit c3467d98d8

View file

@ -263,6 +263,85 @@ 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
def _save_all_stripe_plans(self, stripe_plans):
from config import etcd_client as client
client.put("/v1/stripe_plans", {"stripe_plans": stripe_plans})
@handleStripeError
def get_or_create_stripe_plan(self, amount, name, stripe_plan_id):
"""
This function checks if a StripePlan with the given
stripe_plan_id already exists. If it exists then the function
returns this object otherwise it creates a new StripePlan and
returns the new object.
:param amount: The amount in CHF cents
:param name: The name of the Stripe plan to be created.
:param stripe_plan_id: The id of the Stripe plan to be
created. Use get_stripe_plan_id_string function to
obtain the name of the plan to be created
:return: The StripePlan object if it exists else creates a
Plan object in Stripe and a local StripePlan and
returns it. Returns None in case of Stripe error
"""
all_stripe_plans = self._get_all_stripe_plans()
if stripe_plan_id in all_stripe_plans:
logger.debug("{} plan exists.".format(stripe_plan_id))
else:
logger.debug("{} plan DOES NOT exist. "
"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)
except stripe.error.InvalidRequestError as e:
if self.STRIPE_PLAN_ALREADY_EXISTS in str(e):
logger.debug(
self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id))
all_stripe_plans.append(stripe_plan_id)
self._save_all_stripe_plans(all_stripe_plans)
return stripe_plan_id
@handleStripeError
def delete_stripe_plan(self, stripe_plan_id):
"""
Deletes the Plan in Stripe and also deletes the local db copy
of the plan if it exists
:param stripe_plan_id: The stripe plan id that needs to be
deleted
:return: True if the plan was deleted successfully from
Stripe, False otherwise.
"""
return_value = False
try:
plan = self.stripe.Plan.retrieve(stripe_plan_id)
plan.delete()
return_value = True
all_stripe_plans = self._get_all_stripe_plans()
all_stripe_plans.remove(stripe_plan_id)
self._save_all_stripe_plans(all_stripe_plans)
except stripe.error.InvalidRequestError as e:
if self.STRIPE_NO_SUCH_PLAN in str(e):
logger.debug(
self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id))
return return_value
@handleStripeError
def subscribe_customer_to_plan(self, customer, plans, trial_end=None):
"""