Formatted utils/stripe_utils.py

This commit is contained in:
PCoder 2017-08-24 10:31:57 +05:30
parent a964f2fb5b
commit b5e488e326

View file

@ -30,7 +30,8 @@ def handleStripeError(f):
response.update({'error': err['message']}) response.update({'error': err['message']})
return response return response
except stripe.error.RateLimitError as e: except stripe.error.RateLimitError as e:
response.update({'error': "Too many requests made to the API too quickly"}) response.update(
{'error': "Too many requests made to the API too quickly"})
return response return response
except stripe.error.InvalidRequestError as e: except stripe.error.InvalidRequestError as e:
response.update({'error': "Invalid parameters"}) response.update({'error': "Invalid parameters"})
@ -104,7 +105,8 @@ class StripeUtils(object):
customer = stripe.Customer.retrieve(id) customer = stripe.Customer.retrieve(id)
except stripe.InvalidRequestError: except stripe.InvalidRequestError:
customer = self.create_customer(token, user.email, user.name) customer = self.create_customer(token, user.email, user.name)
user.stripecustomer.stripe_id = customer.get('response_object').get('id') user.stripecustomer.stripe_id = customer.get(
'response_object').get('id')
user.stripecustomer.save() user.stripecustomer.save()
return customer return customer
@ -139,21 +141,26 @@ class StripeUtils(object):
@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):
""" """
This function checks if a StripePlan with the given stripe_plan_id already exists. If it exists then the This function checks if a StripePlan with the given
function returns this object otherwise it creates a new StripePlan and returns the new object. 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 :param amount: The amount in CHF
:param name: The name of the Stripe plan to be created. :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 :param stripe_plan_id: The id of the Stripe plan to be
the name of the plan to be created created. Use get_stripe_plan_id_string function to
:return: The StripePlan object if it exists else creates a Plan object in Stripe and a local StripePlan and obtain the name of the plan to be created
returns it. Returns None in case of Stripe error :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
""" """
_amount = float(amount) _amount = float(amount)
amount = int(_amount * 100) # stripe amount unit, in cents amount = int(_amount * 100) # stripe amount unit, in cents
stripe_plan_db_obj = None stripe_plan_db_obj = None
try: try:
stripe_plan_db_obj = StripePlan.objects.get(stripe_plan_id=stripe_plan_id) stripe_plan_db_obj = StripePlan.objects.get(
stripe_plan_id=stripe_plan_id)
except StripePlan.DoesNotExist: except StripePlan.DoesNotExist:
try: try:
self.stripe.Plan.create( self.stripe.Plan.create(
@ -162,30 +169,38 @@ class StripeUtils(object):
name=name, name=name,
currency=self.CURRENCY, currency=self.CURRENCY,
id=stripe_plan_id) id=stripe_plan_id)
stripe_plan_db_obj = StripePlan.objects.create(stripe_plan_id=stripe_plan_id) stripe_plan_db_obj = StripePlan.objects.create(
stripe_plan_id=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 self.STRIPE_PLAN_ALREADY_EXISTS in str(e):
logger.debug(self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id)) logger.debug(
stripe_plan_db_obj = StripePlan.objects.create(stripe_plan_id=stripe_plan_id) self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id))
stripe_plan_db_obj = StripePlan.objects.create(
stripe_plan_id=stripe_plan_id)
return stripe_plan_db_obj return stripe_plan_db_obj
@handleStripeError @handleStripeError
def delete_stripe_plan(self, stripe_plan_id): 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 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 :param stripe_plan_id: The stripe plan id that needs to be
:return: True if the plan was deleted successfully from Stripe, False otherwise. deleted
:return: True if the plan was deleted successfully from
Stripe, False otherwise.
""" """
return_value = False return_value = False
try: try:
plan = self.stripe.Plan.retrieve(stripe_plan_id) plan = self.stripe.Plan.retrieve(stripe_plan_id)
plan.delete() plan.delete()
return_value = True return_value = True
StripePlan.objects.filter(stripe_plan_id=stripe_plan_id).all().delete() StripePlan.objects.filter(
stripe_plan_id=stripe_plan_id).all().delete()
except stripe.error.InvalidRequestError as e: except stripe.error.InvalidRequestError as e:
if self.STRIPE_NO_SUCH_PLAN in str(e): if self.STRIPE_NO_SUCH_PLAN in str(e):
logger.debug(self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id)) logger.debug(
self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id))
return return_value return return_value
@handleStripeError @handleStripeError
@ -194,13 +209,14 @@ class StripeUtils(object):
Subscribes the given customer to the list of given plans Subscribes the given customer to the list of given plans
:param customer: The stripe customer identifier :param customer: The stripe customer identifier
:param plans: A list of stripe plans. Ref: https://stripe.com/docs/api/python#create_subscription-items :param plans: A list of stripe plans.
e.g. Ref: https://stripe.com/docs/api/python#create_subscription-items
plans = [ e.g.
{ plans = [
"plan": "dcl-v1-cpu-2-ram-5gb-ssd-10gb", {
}, "plan": "dcl-v1-cpu-2-ram-5gb-ssd-10gb",
] },
]
:return: The subscription StripeObject :return: The subscription StripeObject
""" """
@ -222,18 +238,25 @@ class StripeUtils(object):
@staticmethod @staticmethod
def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None): def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None):
""" """
Returns the stripe plan id string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters Returns the stripe plan id string of the form
`dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters
:param cpu: The number of cores :param cpu: The number of cores
:param ram: The size of the RAM in GB :param ram: The size of the RAM in GB
:param ssd: The size of ssd storage in GB :param ssd: The size of ssd storage in GB
:param hdd: The size of hdd storage in GB :param hdd: The size of hdd storage in GB
:param version: The version of the Stripe plans :param version: The version of the Stripe plans
:param app: The application to which the stripe plan belongs to. By default it is 'dcl' :param app: The application to which the stripe plan belongs
to. By default it is 'dcl'
:return: A string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` :return: A string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb`
""" """
dcl_plan_string = 'cpu-{cpu}-ram-{ram}gb-ssd-{ssd}gb'.format(cpu=cpu, ram=ram, ssd=ssd) dcl_plan_string = 'cpu-{cpu}-ram-{ram}gb-ssd-{ssd}gb'.format(cpu=cpu,
ram=ram,
ssd=ssd)
if hdd is not None: if hdd is not None:
dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format(dcl_plan_string=dcl_plan_string, hdd=hdd) dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format(
stripe_plan_id_string = '{app}-v{version}-{plan}'.format(app=app, version=version, plan=dcl_plan_string) dcl_plan_string=dcl_plan_string, hdd=hdd)
stripe_plan_id_string = '{app}-v{version}-{plan}'.format(app=app,
version=version,
plan=dcl_plan_string)
return stripe_plan_id_string return stripe_plan_id_string