2017-08-17 16:21:53 +00:00
|
|
|
import logging
|
2016-04-23 07:22:44 +00:00
|
|
|
import stripe
|
|
|
|
from django.conf import settings
|
2017-08-16 22:53:22 +00:00
|
|
|
from datacenterlight.models import StripePlan
|
|
|
|
|
2016-05-01 12:13:12 +00:00
|
|
|
stripe.api_key = settings.STRIPE_API_PRIVATE_KEY
|
2017-08-17 07:31:05 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-23 07:22:44 +00:00
|
|
|
|
2016-04-27 06:54:15 +00:00
|
|
|
|
|
|
|
def handleStripeError(f):
|
2016-05-04 15:36:54 +00:00
|
|
|
def handleProblems(*args, **kwargs):
|
|
|
|
response = {
|
|
|
|
'paid': False,
|
|
|
|
'response_object': None,
|
|
|
|
'error': None
|
|
|
|
}
|
2017-06-04 22:05:48 +00:00
|
|
|
|
2017-08-07 06:56:57 +00:00
|
|
|
common_message = "Currently it's not possible to make payments."
|
2016-05-04 15:36:54 +00:00
|
|
|
try:
|
|
|
|
response_object = f(*args, **kwargs)
|
2016-04-27 06:54:15 +00:00
|
|
|
response = {
|
2016-05-04 15:36:54 +00:00
|
|
|
'response_object': response_object,
|
2016-04-27 06:54:15 +00:00
|
|
|
'error': None
|
|
|
|
}
|
2016-05-04 15:36:54 +00:00
|
|
|
return response
|
|
|
|
except stripe.error.CardError as e:
|
|
|
|
# Since it's a decline, stripe.error.CardError will be caught
|
|
|
|
body = e.json_body
|
|
|
|
err = body['error']
|
|
|
|
response.update({'error': err['message']})
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
return response
|
|
|
|
except stripe.error.RateLimitError as e:
|
2017-08-24 05:01:57 +00:00
|
|
|
response.update(
|
|
|
|
{'error': "Too many requests made to the API too quickly"})
|
2016-05-04 15:36:54 +00:00
|
|
|
return response
|
|
|
|
except stripe.error.InvalidRequestError as e:
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
response.update({'error': "Invalid parameters"})
|
|
|
|
return response
|
|
|
|
except stripe.error.AuthenticationError as e:
|
|
|
|
# Authentication with Stripe's API failed
|
|
|
|
# (maybe you changed API keys recently)
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
response.update({'error': common_message})
|
|
|
|
return response
|
|
|
|
except stripe.error.APIConnectionError as e:
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
response.update({'error': common_message})
|
|
|
|
return response
|
|
|
|
except stripe.error.StripeError as e:
|
|
|
|
# maybe send email
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
response.update({'error': common_message})
|
|
|
|
return response
|
|
|
|
except Exception as e:
|
|
|
|
# maybe send email
|
2017-10-01 23:35:28 +00:00
|
|
|
logger.error(str(e))
|
2016-05-04 15:36:54 +00:00
|
|
|
response.update({'error': common_message})
|
|
|
|
return response
|
2016-04-27 06:54:15 +00:00
|
|
|
|
2016-05-04 15:36:54 +00:00
|
|
|
return handleProblems
|
2016-04-27 06:54:15 +00:00
|
|
|
|
|
|
|
|
2016-05-01 12:13:12 +00:00
|
|
|
class StripeUtils(object):
|
2016-04-23 07:22:44 +00:00
|
|
|
CURRENCY = 'chf'
|
|
|
|
INTERVAL = 'month'
|
|
|
|
SUCCEEDED_STATUS = 'succeeded'
|
2017-08-17 10:42:34 +00:00
|
|
|
STRIPE_PLAN_ALREADY_EXISTS = 'Plan already exists'
|
|
|
|
STRIPE_NO_SUCH_PLAN = 'No such plan'
|
2017-08-17 07:31:05 +00:00
|
|
|
PLAN_EXISTS_ERROR_MSG = 'Plan {} exists already.\nCreating a local StripePlan now.'
|
2017-08-17 10:42:34 +00:00
|
|
|
PLAN_DOES_NOT_EXIST_ERROR_MSG = 'Plan {} does not exist.'
|
2016-04-23 07:22:44 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.stripe = stripe
|
2016-05-01 12:13:12 +00:00
|
|
|
|
2016-12-19 14:33:15 +00:00
|
|
|
def update_customer_token(self, customer, token):
|
2017-01-20 16:33:25 +00:00
|
|
|
customer.source = token
|
|
|
|
customer.save()
|
2016-12-19 14:33:15 +00:00
|
|
|
|
2017-01-20 16:33:25 +00:00
|
|
|
@handleStripeError
|
|
|
|
def update_customer_card(self, customer_id, token):
|
|
|
|
customer = stripe.Customer.retrieve(customer_id)
|
|
|
|
current_card_token = customer.default_source
|
|
|
|
customer.sources.retrieve(current_card_token).delete()
|
2016-12-19 14:33:15 +00:00
|
|
|
customer.source = token
|
|
|
|
customer.save()
|
2017-01-20 16:33:25 +00:00
|
|
|
credit_card_raw_data = customer.sources.data.pop()
|
|
|
|
new_card_data = {
|
|
|
|
'last4': credit_card_raw_data.last4,
|
|
|
|
'brand': credit_card_raw_data.brand
|
|
|
|
}
|
|
|
|
return new_card_data
|
2016-12-19 14:33:15 +00:00
|
|
|
|
2017-06-30 05:38:00 +00:00
|
|
|
@handleStripeError
|
|
|
|
def get_card_details(self, customer_id, token):
|
|
|
|
customer = stripe.Customer.retrieve(customer_id)
|
|
|
|
credit_card_raw_data = customer.sources.data.pop()
|
|
|
|
card_details = {
|
|
|
|
'last4': credit_card_raw_data.last4,
|
|
|
|
'brand': credit_card_raw_data.brand
|
|
|
|
}
|
|
|
|
return card_details
|
|
|
|
|
2016-05-04 15:36:54 +00:00
|
|
|
def check_customer(self, id, user, token):
|
2016-05-01 12:13:12 +00:00
|
|
|
customers = self.stripe.Customer.all()
|
|
|
|
if not customers.get('data'):
|
2017-08-15 10:35:52 +00:00
|
|
|
customer = self.create_customer(token, user.email, user.name)
|
2016-05-01 12:13:12 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
customer = stripe.Customer.retrieve(id)
|
|
|
|
except stripe.InvalidRequestError:
|
2017-08-15 10:35:52 +00:00
|
|
|
customer = self.create_customer(token, user.email, user.name)
|
2017-08-24 05:01:57 +00:00
|
|
|
user.stripecustomer.stripe_id = customer.get(
|
|
|
|
'response_object').get('id')
|
2016-05-01 12:13:12 +00:00
|
|
|
user.stripecustomer.save()
|
|
|
|
return customer
|
2016-04-23 07:22:44 +00:00
|
|
|
|
2017-01-20 16:33:25 +00:00
|
|
|
@handleStripeError
|
|
|
|
def get_customer(self, id):
|
|
|
|
customer = stripe.Customer.retrieve(id)
|
|
|
|
# data = customer.get('response_object')
|
|
|
|
return customer
|
|
|
|
|
2016-04-27 06:54:15 +00:00
|
|
|
@handleStripeError
|
2017-08-15 10:35:52 +00:00
|
|
|
def create_customer(self, token, email, name=None):
|
|
|
|
if name is None or name.strip() == "":
|
|
|
|
name = email
|
2016-04-27 06:54:15 +00:00
|
|
|
customer = self.stripe.Customer.create(
|
2016-05-01 12:13:12 +00:00
|
|
|
source=token,
|
2017-08-15 10:35:52 +00:00
|
|
|
description=name,
|
2016-05-01 12:13:12 +00:00
|
|
|
email=email
|
2016-04-26 06:16:03 +00:00
|
|
|
)
|
|
|
|
return customer
|
|
|
|
|
2016-04-27 06:54:15 +00:00
|
|
|
@handleStripeError
|
2016-04-26 06:16:03 +00:00
|
|
|
def make_charge(self, amount=None, customer=None):
|
2017-05-25 18:04:29 +00:00
|
|
|
_amount = float(amount)
|
|
|
|
amount = int(_amount * 100) # stripe amount unit, in cents
|
2016-04-26 06:16:03 +00:00
|
|
|
charge = self.stripe.Charge.create(
|
2016-05-04 15:36:54 +00:00
|
|
|
amount=amount, # in cents
|
|
|
|
currency=self.CURRENCY,
|
|
|
|
customer=customer
|
2016-04-26 06:16:03 +00:00
|
|
|
)
|
|
|
|
return charge
|
|
|
|
|
2016-04-27 06:54:15 +00:00
|
|
|
@handleStripeError
|
2017-08-17 10:42:34 +00:00
|
|
|
def get_or_create_stripe_plan(self, amount, name, stripe_plan_id):
|
2017-08-16 22:53:22 +00:00
|
|
|
"""
|
2017-08-24 05:01:57 +00:00
|
|
|
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.
|
2017-08-16 22:53:22 +00:00
|
|
|
|
|
|
|
:param amount: The amount in CHF
|
|
|
|
:param name: The name of the Stripe plan to be created.
|
2017-08-24 05:01:57 +00:00
|
|
|
: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
|
2017-08-16 22:53:22 +00:00
|
|
|
"""
|
|
|
|
_amount = float(amount)
|
|
|
|
amount = int(_amount * 100) # stripe amount unit, in cents
|
2017-08-17 09:04:22 +00:00
|
|
|
stripe_plan_db_obj = None
|
2017-08-16 22:53:22 +00:00
|
|
|
try:
|
2017-08-24 05:01:57 +00:00
|
|
|
stripe_plan_db_obj = StripePlan.objects.get(
|
|
|
|
stripe_plan_id=stripe_plan_id)
|
2017-08-16 22:53:22 +00:00
|
|
|
except StripePlan.DoesNotExist:
|
2017-08-17 07:31:05 +00:00
|
|
|
try:
|
2017-08-17 10:42:34 +00:00
|
|
|
self.stripe.Plan.create(
|
2017-08-17 07:31:05 +00:00
|
|
|
amount=amount,
|
|
|
|
interval=self.INTERVAL,
|
|
|
|
name=name,
|
|
|
|
currency=self.CURRENCY,
|
|
|
|
id=stripe_plan_id)
|
2017-08-24 05:01:57 +00:00
|
|
|
stripe_plan_db_obj = StripePlan.objects.create(
|
|
|
|
stripe_plan_id=stripe_plan_id)
|
2017-08-17 07:31:05 +00:00
|
|
|
except stripe.error.InvalidRequestError as e:
|
2017-08-17 10:42:34 +00:00
|
|
|
if self.STRIPE_PLAN_ALREADY_EXISTS in str(e):
|
2017-08-24 05:01:57 +00:00
|
|
|
logger.debug(
|
|
|
|
self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id))
|
|
|
|
stripe_plan_db_obj = StripePlan.objects.create(
|
|
|
|
stripe_plan_id=stripe_plan_id)
|
2017-08-16 22:53:22 +00:00
|
|
|
return stripe_plan_db_obj
|
|
|
|
|
2017-08-17 10:42:34 +00:00
|
|
|
@handleStripeError
|
|
|
|
def delete_stripe_plan(self, stripe_plan_id):
|
|
|
|
"""
|
2017-08-24 05:01:57 +00:00
|
|
|
Deletes the Plan in Stripe and also deletes the local db copy
|
|
|
|
of the plan if it exists
|
2017-08-17 10:42:34 +00:00
|
|
|
|
2017-08-24 05:01:57 +00:00
|
|
|
: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.
|
2017-08-17 10:42:34 +00:00
|
|
|
"""
|
|
|
|
return_value = False
|
|
|
|
try:
|
|
|
|
plan = self.stripe.Plan.retrieve(stripe_plan_id)
|
|
|
|
plan.delete()
|
|
|
|
return_value = True
|
2017-08-24 05:01:57 +00:00
|
|
|
StripePlan.objects.filter(
|
|
|
|
stripe_plan_id=stripe_plan_id).all().delete()
|
2017-08-17 10:42:34 +00:00
|
|
|
except stripe.error.InvalidRequestError as e:
|
|
|
|
if self.STRIPE_NO_SUCH_PLAN in str(e):
|
2017-08-24 05:01:57 +00:00
|
|
|
logger.debug(
|
|
|
|
self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id))
|
2017-08-17 10:42:34 +00:00
|
|
|
return return_value
|
|
|
|
|
2017-08-16 22:53:22 +00:00
|
|
|
@handleStripeError
|
|
|
|
def subscribe_customer_to_plan(self, customer, plans):
|
|
|
|
"""
|
2017-08-17 07:31:05 +00:00
|
|
|
Subscribes the given customer to the list of given plans
|
2017-08-16 22:53:22 +00:00
|
|
|
|
|
|
|
:param customer: The stripe customer identifier
|
2017-08-24 05:01:57 +00:00
|
|
|
:param plans: A list of stripe plans.
|
|
|
|
Ref: https://stripe.com/docs/api/python#create_subscription-items
|
|
|
|
e.g.
|
|
|
|
plans = [
|
|
|
|
{
|
|
|
|
"plan": "dcl-v1-cpu-2-ram-5gb-ssd-10gb",
|
|
|
|
},
|
|
|
|
]
|
2017-08-16 22:53:22 +00:00
|
|
|
:return: The subscription StripeObject
|
|
|
|
"""
|
2017-08-17 07:31:05 +00:00
|
|
|
|
|
|
|
subscription_result = self.stripe.Subscription.create(
|
2017-08-16 22:53:22 +00:00
|
|
|
customer=customer,
|
2017-08-17 07:31:05 +00:00
|
|
|
items=plans,
|
2017-08-16 22:53:22 +00:00
|
|
|
)
|
2017-08-17 07:31:05 +00:00
|
|
|
return subscription_result
|
2016-04-23 07:22:44 +00:00
|
|
|
|
2017-10-03 11:06:26 +00:00
|
|
|
@handleStripeError
|
|
|
|
def unsubscribe_customer(self, subscription_id):
|
|
|
|
"""
|
|
|
|
Cancels a given subscription
|
|
|
|
|
|
|
|
:param subscription_id: The Stripe subscription id string
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
sub = stripe.Subscription.retrieve(subscription_id)
|
|
|
|
return sub.delete()
|
|
|
|
|
2016-05-04 15:36:54 +00:00
|
|
|
@handleStripeError
|
2017-06-29 14:34:40 +00:00
|
|
|
def make_payment(self, customer, amount, token):
|
2016-05-04 15:36:54 +00:00
|
|
|
charge = self.stripe.Charge.create(
|
|
|
|
amount=amount, # in cents
|
|
|
|
currency=self.CURRENCY,
|
|
|
|
customer=customer
|
|
|
|
)
|
|
|
|
return charge
|
2017-08-16 22:53:22 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-08-21 20:42:43 +00:00
|
|
|
def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None):
|
2017-08-16 22:53:22 +00:00
|
|
|
"""
|
2017-09-22 21:47:21 +00:00
|
|
|
Returns the Stripe plan id string of the form
|
2017-08-24 05:01:57 +00:00
|
|
|
`dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters
|
2017-08-16 22:53:22 +00:00
|
|
|
|
|
|
|
:param cpu: The number of cores
|
|
|
|
:param ram: The size of the RAM in GB
|
2017-08-21 20:42:43 +00:00
|
|
|
:param ssd: The size of ssd storage in GB
|
|
|
|
:param hdd: The size of hdd storage in GB
|
2017-08-16 22:53:22 +00:00
|
|
|
:param version: The version of the Stripe plans
|
2017-08-24 05:01:57 +00:00
|
|
|
:param app: The application to which the stripe plan belongs
|
|
|
|
to. By default it is 'dcl'
|
2017-08-16 22:53:22 +00:00
|
|
|
:return: A string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb`
|
|
|
|
"""
|
2017-08-24 05:01:57 +00:00
|
|
|
dcl_plan_string = 'cpu-{cpu}-ram-{ram}gb-ssd-{ssd}gb'.format(cpu=cpu,
|
|
|
|
ram=ram,
|
|
|
|
ssd=ssd)
|
2017-08-21 20:42:43 +00:00
|
|
|
if hdd is not None:
|
2017-08-24 05:01:57 +00:00
|
|
|
dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format(
|
|
|
|
dcl_plan_string=dcl_plan_string, hdd=hdd)
|
2017-09-22 21:55:48 +00:00
|
|
|
stripe_plan_id_string = '{app}-v{version}-{plan}'.format(
|
|
|
|
app=app,
|
|
|
|
version=version,
|
|
|
|
plan=dcl_plan_string)
|
2017-08-17 11:23:49 +00:00
|
|
|
return stripe_plan_id_string
|
2017-09-22 21:47:21 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_stripe_plan_name(cpu, memory, disk_size):
|
|
|
|
"""
|
|
|
|
Returns the Stripe plan name
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format(
|
2017-09-22 21:55:48 +00:00
|
|
|
cpu=cpu,
|
|
|
|
memory=memory,
|
|
|
|
disk_size=disk_size)
|