2015-05-14 20:47:38 +00:00
|
|
|
""" Subscribe to plans. """
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
import logging
|
|
|
|
import stripe
|
|
|
|
|
2014-04-10 19:20:16 +00:00
|
|
|
from app import billing
|
2014-03-17 20:57:35 +00:00
|
|
|
from endpoints.api import request_error, log_action, NotFound
|
2014-03-13 19:19:49 +00:00
|
|
|
from data import model
|
2014-04-10 19:20:16 +00:00
|
|
|
from data.billing import PLANS
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2014-04-06 04:36:19 +00:00
|
|
|
import features
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-08 18:53:27 +00:00
|
|
|
def check_repository_usage(user_or_org, plan_found):
|
|
|
|
private_repos = model.user.get_private_repo_count(user_or_org.username)
|
|
|
|
if plan_found is None:
|
|
|
|
repos_allowed = 0
|
|
|
|
else:
|
|
|
|
repos_allowed = plan_found['privateRepos']
|
|
|
|
|
|
|
|
if private_repos > repos_allowed:
|
|
|
|
model.notification.create_unique_notification('over_private_usage', user_or_org,
|
|
|
|
{'namespace': user_or_org.username})
|
|
|
|
else:
|
|
|
|
model.notification.delete_notifications_by_kind(user_or_org, 'over_private_usage')
|
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
def carderror_response(exc):
|
|
|
|
return {'carderror': exc.message}, 402
|
|
|
|
|
2014-08-25 19:30:29 +00:00
|
|
|
def connection_response(exc):
|
|
|
|
return {'message': 'Could not contact Stripe. Please try again.'}, 503
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
def subscription_view(stripe_subscription, used_repos):
|
2014-04-23 05:10:31 +00:00
|
|
|
view = {
|
2014-04-23 05:17:34 +00:00
|
|
|
'hasSubscription': True,
|
|
|
|
'isExistingCustomer': True,
|
2014-03-13 19:19:49 +00:00
|
|
|
'currentPeriodStart': stripe_subscription.current_period_start,
|
|
|
|
'currentPeriodEnd': stripe_subscription.current_period_end,
|
|
|
|
'plan': stripe_subscription.plan.id,
|
|
|
|
'usedPrivateRepos': used_repos,
|
2014-04-23 05:10:31 +00:00
|
|
|
'trialStart': stripe_subscription.trial_start,
|
|
|
|
'trialEnd': stripe_subscription.trial_end
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 05:10:31 +00:00
|
|
|
return view
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
def subscribe(user, plan, token, require_business_plan):
|
2014-04-06 04:36:19 +00:00
|
|
|
if not features.BILLING:
|
|
|
|
return
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
plan_found = None
|
|
|
|
for plan_obj in PLANS:
|
|
|
|
if plan_obj['stripeId'] == plan:
|
|
|
|
plan_found = plan_obj
|
|
|
|
|
|
|
|
if not plan_found or plan_found['deprecated']:
|
|
|
|
logger.warning('Plan not found or deprecated: %s', plan)
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
if (require_business_plan and not plan_found['bus_features'] and not
|
2015-07-15 21:25:41 +00:00
|
|
|
plan_found['price'] == 0):
|
2014-03-13 19:19:49 +00:00
|
|
|
logger.warning('Business attempting to subscribe to personal plan: %s',
|
|
|
|
user.username)
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(message='No matching plan found')
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
private_repos = model.user.get_private_repo_count(user.username)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
# This is the default response
|
|
|
|
response_json = {
|
|
|
|
'plan': plan,
|
|
|
|
'usedPrivateRepos': private_repos,
|
|
|
|
}
|
|
|
|
status_code = 200
|
|
|
|
|
|
|
|
if not user.stripe_id:
|
|
|
|
# Check if a non-paying user is trying to subscribe to a free plan
|
|
|
|
if not plan_found['price'] == 0:
|
|
|
|
# They want a real paying plan, create the customer and plan
|
|
|
|
# simultaneously
|
|
|
|
card = token
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
try:
|
2014-04-10 19:20:16 +00:00
|
|
|
cus = billing.Customer.create(email=user.email, plan=plan, card=card)
|
2014-03-13 19:19:49 +00:00
|
|
|
user.stripe_id = cus.id
|
|
|
|
user.save()
|
2014-03-19 19:39:44 +00:00
|
|
|
check_repository_usage(user, plan_found)
|
2014-03-13 19:19:49 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
|
|
|
except stripe.CardError as e:
|
|
|
|
return carderror_response(e)
|
2014-08-25 19:30:29 +00:00
|
|
|
except stripe.APIConnectionError as e:
|
|
|
|
return connection_response(e)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
response_json = subscription_view(cus.subscription, private_repos)
|
|
|
|
status_code = 201
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Change the plan
|
2014-08-25 19:30:29 +00:00
|
|
|
try:
|
|
|
|
cus = billing.Customer.retrieve(user.stripe_id)
|
|
|
|
except stripe.APIConnectionError as e:
|
|
|
|
return connection_response(e)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
if plan_found['price'] == 0:
|
|
|
|
if cus.subscription is not None:
|
|
|
|
# We only have to cancel the subscription if they actually have one
|
2014-08-25 19:30:29 +00:00
|
|
|
try:
|
|
|
|
cus.cancel_subscription()
|
|
|
|
cus.save()
|
|
|
|
except stripe.APIConnectionError as e:
|
|
|
|
return connection_response(e)
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
check_repository_usage(user, plan_found)
|
2014-03-13 19:19:49 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
|
|
|
|
|
|
|
else:
|
|
|
|
# User may have been a previous customer who is resubscribing
|
|
|
|
if token:
|
|
|
|
cus.card = token
|
|
|
|
|
|
|
|
cus.plan = plan
|
|
|
|
|
|
|
|
try:
|
|
|
|
cus.save()
|
|
|
|
except stripe.CardError as e:
|
|
|
|
return carderror_response(e)
|
2014-08-25 19:30:29 +00:00
|
|
|
except stripe.APIConnectionError as e:
|
|
|
|
return connection_response(e)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
response_json = subscription_view(cus.subscription, private_repos)
|
2014-03-19 19:39:44 +00:00
|
|
|
check_repository_usage(user, plan_found)
|
2014-03-13 19:19:49 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
|
|
|
|
|
|
|
return response_json, status_code
|