From c3467d98d89b9b0361856e15e6ccc4eb75f6850c Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Sep 2019 18:32:26 +0530 Subject: [PATCH] Add/delete Stripe plans --- stripe_utils.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/stripe_utils.py b/stripe_utils.py index 84b815c..a4d79b9 100644 --- a/stripe_utils.py +++ b/stripe_utils.py @@ -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): """