2018-08-06 06:47:15 +02:00
|
|
|
from typing import Any, Dict, Optional, Tuple, Union, cast
|
2018-07-13 13:33:05 +02:00
|
|
|
import logging
|
2013-10-17 22:55:09 +02:00
|
|
|
|
2018-08-06 06:47:15 +02:00
|
|
|
from django.core import signing
|
2018-05-04 01:40:46 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import validate_email, URLValidator
|
|
|
|
from django.db import IntegrityError
|
2018-03-31 04:13:44 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.utils import timezone
|
2018-02-15 20:50:37 +01:00
|
|
|
from django.utils.translation import ugettext as _, ugettext as err_
|
2018-03-31 04:13:44 +02:00
|
|
|
from django.shortcuts import redirect, render
|
|
|
|
from django.urls import reverse
|
2018-01-13 19:38:13 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.views.decorators.http import require_GET
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
2018-05-04 18:04:01 +02:00
|
|
|
from zerver.decorator import require_post, zulip_login_required, InvalidZulipServerKeyError
|
2017-10-28 00:07:31 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2016-10-27 23:55:31 +02:00
|
|
|
from zerver.lib.push_notifications import send_android_push_notification, \
|
|
|
|
send_apple_push_notification
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2016-10-27 23:55:31 +02:00
|
|
|
from zerver.lib.response import json_error, json_success
|
2018-05-04 01:40:46 +02:00
|
|
|
from zerver.lib.validator import check_int, check_string, check_url, \
|
|
|
|
validate_login_email, check_capped_string, check_string_fixed_length
|
2018-03-31 04:13:44 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2018-01-30 02:02:32 +01:00
|
|
|
from zerver.models import UserProfile, Realm
|
2017-07-07 18:23:36 +02:00
|
|
|
from zerver.views.push_notifications import validate_token
|
2018-08-06 06:16:29 +02:00
|
|
|
from zilencer.lib.stripe import STRIPE_PUBLISHABLE_KEY, \
|
2018-08-06 18:23:48 +02:00
|
|
|
stripe_get_customer, stripe_get_upcoming_invoice, get_seat_count, \
|
2018-07-27 17:47:03 +02:00
|
|
|
extract_current_subscription, process_initial_upgrade, sign_string, \
|
2018-09-06 15:14:54 +02:00
|
|
|
unsign_string, BillingError, process_downgrade, do_replace_payment_source
|
2018-03-31 04:13:44 +02:00
|
|
|
from zilencer.models import RemotePushDeviceToken, RemoteZulipServer, \
|
|
|
|
Customer, Plan
|
2018-01-18 02:03:12 +01:00
|
|
|
|
2018-07-13 13:33:05 +02:00
|
|
|
billing_logger = logging.getLogger('zilencer.stripe')
|
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def validate_entity(entity: Union[UserProfile, RemoteZulipServer]) -> None:
|
2017-05-08 14:25:40 +02:00
|
|
|
if not isinstance(entity, RemoteZulipServer):
|
2018-02-15 20:50:37 +01:00
|
|
|
raise JsonableError(err_("Must validate with valid Zulip server API key"))
|
2017-05-08 14:25:40 +02:00
|
|
|
|
2017-10-27 12:57:54 +02:00
|
|
|
def validate_bouncer_token_request(entity: Union[UserProfile, RemoteZulipServer],
|
|
|
|
token: bytes, kind: int) -> None:
|
2017-07-07 18:29:45 +02:00
|
|
|
if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]:
|
2018-02-15 20:50:37 +01:00
|
|
|
raise JsonableError(err_("Invalid token type"))
|
2017-05-08 14:25:40 +02:00
|
|
|
validate_entity(entity)
|
2017-07-07 18:23:36 +02:00
|
|
|
validate_token(token, kind)
|
2017-05-08 14:25:40 +02:00
|
|
|
|
2018-05-04 01:40:46 +02:00
|
|
|
@csrf_exempt
|
|
|
|
@require_post
|
|
|
|
@has_request_variables
|
|
|
|
def register_remote_server(
|
|
|
|
request: HttpRequest,
|
|
|
|
zulip_org_id: str=REQ(str_validator=check_string_fixed_length(RemoteZulipServer.UUID_LENGTH)),
|
|
|
|
zulip_org_key: str=REQ(str_validator=check_string_fixed_length(RemoteZulipServer.API_KEY_LENGTH)),
|
|
|
|
hostname: str=REQ(str_validator=check_capped_string(RemoteZulipServer.HOSTNAME_MAX_LENGTH)),
|
|
|
|
contact_email: str=REQ(str_validator=check_string),
|
|
|
|
new_org_key: Optional[str]=REQ(str_validator=check_string_fixed_length(
|
|
|
|
RemoteZulipServer.API_KEY_LENGTH), default=None),
|
|
|
|
) -> HttpResponse:
|
|
|
|
# REQ validated the the field lengths, but we still need to
|
|
|
|
# validate the format of these fields.
|
|
|
|
try:
|
|
|
|
# TODO: Ideally we'd not abuse the URL validator this way
|
|
|
|
url_validator = URLValidator()
|
|
|
|
url_validator('http://' + hostname)
|
|
|
|
except ValidationError:
|
|
|
|
raise JsonableError(_('%s is not a valid hostname') % (hostname,))
|
|
|
|
|
|
|
|
try:
|
|
|
|
validate_email(contact_email)
|
|
|
|
except ValidationError as e:
|
|
|
|
raise JsonableError(e.message)
|
|
|
|
|
|
|
|
remote_server, created = RemoteZulipServer.objects.get_or_create(
|
|
|
|
uuid=zulip_org_id,
|
|
|
|
defaults={'hostname': hostname, 'contact_email': contact_email,
|
|
|
|
'api_key': zulip_org_key})
|
|
|
|
|
|
|
|
if not created:
|
|
|
|
if remote_server.api_key != zulip_org_key:
|
2018-05-04 18:04:01 +02:00
|
|
|
raise InvalidZulipServerKeyError(zulip_org_id)
|
2018-05-04 01:40:46 +02:00
|
|
|
else:
|
|
|
|
remote_server.hostname = hostname
|
|
|
|
remote_server.contact_email = contact_email
|
|
|
|
if new_org_key is not None:
|
|
|
|
remote_server.api_key = new_org_key
|
|
|
|
remote_server.save()
|
|
|
|
|
|
|
|
return json_success({'created': created})
|
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
@has_request_variables
|
2018-04-29 00:06:26 +02:00
|
|
|
def register_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
|
2017-12-20 20:56:11 +01:00
|
|
|
user_id: int=REQ(), token: bytes=REQ(),
|
|
|
|
token_kind: int=REQ(validator=check_int),
|
2018-05-11 01:38:41 +02:00
|
|
|
ios_app_id: Optional[str]=None) -> HttpResponse:
|
2017-07-07 18:23:36 +02:00
|
|
|
validate_bouncer_token_request(entity, token, token_kind)
|
2016-10-27 23:55:31 +02:00
|
|
|
server = cast(RemoteZulipServer, entity)
|
|
|
|
|
|
|
|
# If a user logged out on a device and failed to unregister,
|
|
|
|
# we should delete any other user associations for this token
|
|
|
|
# & RemoteServer pair
|
|
|
|
RemotePushDeviceToken.objects.filter(
|
|
|
|
token=token, kind=token_kind, server=server).exclude(user_id=user_id).delete()
|
|
|
|
|
|
|
|
# Save or update
|
|
|
|
remote_token, created = RemotePushDeviceToken.objects.update_or_create(
|
|
|
|
user_id=user_id,
|
|
|
|
server=server,
|
|
|
|
kind=token_kind,
|
|
|
|
token=token,
|
|
|
|
defaults=dict(
|
|
|
|
ios_app_id=ios_app_id,
|
|
|
|
last_updated=timezone.now()))
|
|
|
|
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
@has_request_variables
|
2018-04-29 00:07:47 +02:00
|
|
|
def unregister_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
|
2017-12-20 20:56:11 +01:00
|
|
|
token: bytes=REQ(),
|
|
|
|
token_kind: int=REQ(validator=check_int),
|
2018-05-11 01:38:41 +02:00
|
|
|
ios_app_id: Optional[str]=None) -> HttpResponse:
|
2017-07-07 18:23:36 +02:00
|
|
|
validate_bouncer_token_request(entity, token, token_kind)
|
2016-10-27 23:55:31 +02:00
|
|
|
server = cast(RemoteZulipServer, entity)
|
|
|
|
deleted = RemotePushDeviceToken.objects.filter(token=token,
|
|
|
|
kind=token_kind,
|
|
|
|
server=server).delete()
|
|
|
|
if deleted[0] == 0:
|
2018-02-15 20:50:37 +01:00
|
|
|
return json_error(err_("Token does not exist"))
|
2016-10-27 23:55:31 +02:00
|
|
|
|
|
|
|
return json_success()
|
2017-05-08 13:48:16 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
2017-12-20 20:56:11 +01:00
|
|
|
def remote_server_notify_push(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
|
|
|
|
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
|
2017-05-08 13:48:16 +02:00
|
|
|
validate_entity(entity)
|
2017-05-09 10:31:47 +02:00
|
|
|
server = cast(RemoteZulipServer, entity)
|
|
|
|
|
|
|
|
user_id = payload['user_id']
|
|
|
|
gcm_payload = payload['gcm_payload']
|
|
|
|
apns_payload = payload['apns_payload']
|
|
|
|
|
|
|
|
android_devices = list(RemotePushDeviceToken.objects.filter(
|
|
|
|
user_id=user_id,
|
|
|
|
kind=RemotePushDeviceToken.GCM,
|
|
|
|
server=server
|
|
|
|
))
|
|
|
|
|
|
|
|
apple_devices = list(RemotePushDeviceToken.objects.filter(
|
|
|
|
user_id=user_id,
|
|
|
|
kind=RemotePushDeviceToken.APNS,
|
|
|
|
server=server
|
|
|
|
))
|
|
|
|
|
|
|
|
if android_devices:
|
2017-05-16 21:15:45 +02:00
|
|
|
send_android_push_notification(android_devices, gcm_payload, remote=True)
|
2017-05-09 10:31:47 +02:00
|
|
|
|
|
|
|
if apple_devices:
|
2018-05-21 20:20:23 +02:00
|
|
|
send_apple_push_notification(user_id, apple_devices, apns_payload, remote=True)
|
2017-05-09 10:31:47 +02:00
|
|
|
|
2017-05-08 13:48:16 +02:00
|
|
|
return json_success()
|
2018-01-13 19:38:13 +01:00
|
|
|
|
2018-08-06 06:47:15 +02:00
|
|
|
def unsign_and_check_upgrade_parameters(user: UserProfile, plan_nickname: str,
|
|
|
|
signed_seat_count: str, salt: str) -> Tuple[Plan, int]:
|
|
|
|
if plan_nickname not in [Plan.CLOUD_ANNUAL, Plan.CLOUD_MONTHLY]:
|
|
|
|
billing_logger.warning("Tampered plan during realm upgrade. user: %s, realm: %s (%s)."
|
|
|
|
% (user.id, user.realm.id, user.realm.string_id))
|
|
|
|
raise BillingError('tampered plan', BillingError.CONTACT_SUPPORT)
|
|
|
|
plan = Plan.objects.get(nickname=plan_nickname)
|
|
|
|
|
|
|
|
try:
|
|
|
|
seat_count = int(unsign_string(signed_seat_count, salt))
|
|
|
|
except signing.BadSignature:
|
|
|
|
billing_logger.warning("Tampered seat count during realm upgrade. user: %s, realm: %s (%s)."
|
|
|
|
% (user.id, user.realm.id, user.realm.string_id))
|
|
|
|
raise BillingError('tampered seat count', BillingError.CONTACT_SUPPORT)
|
|
|
|
return plan, seat_count
|
|
|
|
|
2018-01-13 19:38:13 +01:00
|
|
|
@zulip_login_required
|
2018-03-31 04:13:44 +02:00
|
|
|
def initial_upgrade(request: HttpRequest) -> HttpResponse:
|
2018-09-01 02:40:20 +02:00
|
|
|
if not settings.BILLING_ENABLED:
|
2018-07-25 13:34:09 +02:00
|
|
|
return render(request, "404.html")
|
|
|
|
|
2018-01-13 19:38:13 +01:00
|
|
|
user = request.user
|
2018-07-13 13:33:05 +02:00
|
|
|
error_message = ""
|
2018-08-06 06:16:29 +02:00
|
|
|
error_description = "" # only used in tests
|
2018-07-13 13:33:05 +02:00
|
|
|
|
2018-08-14 03:33:31 +02:00
|
|
|
customer = Customer.objects.filter(realm=user.realm).first()
|
|
|
|
if customer is not None and customer.has_billing_relationship:
|
2018-03-31 04:13:44 +02:00
|
|
|
return HttpResponseRedirect(reverse('zilencer.views.billing_home'))
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
2018-07-27 17:47:03 +02:00
|
|
|
try:
|
2018-08-06 06:47:15 +02:00
|
|
|
plan, seat_count = unsign_and_check_upgrade_parameters(
|
|
|
|
user, request.POST['plan'], request.POST['signed_seat_count'], request.POST['salt'])
|
|
|
|
process_initial_upgrade(user, plan, seat_count, request.POST['stripeToken'])
|
2018-08-06 06:16:29 +02:00
|
|
|
except BillingError as e:
|
|
|
|
error_message = e.message
|
|
|
|
error_description = e.description
|
|
|
|
except Exception as e:
|
|
|
|
billing_logger.exception("Uncaught exception in billing: %s" % (e,))
|
|
|
|
error_message = BillingError.CONTACT_SUPPORT
|
2018-07-27 17:47:03 +02:00
|
|
|
else:
|
2018-07-13 13:33:05 +02:00
|
|
|
return HttpResponseRedirect(reverse('zilencer.views.billing_home'))
|
|
|
|
|
|
|
|
seat_count = get_seat_count(user.realm)
|
|
|
|
signed_seat_count, salt = sign_string(str(seat_count))
|
2018-03-31 04:13:44 +02:00
|
|
|
context = {
|
|
|
|
'publishable_key': STRIPE_PUBLISHABLE_KEY,
|
|
|
|
'email': user.email,
|
2018-07-13 13:33:05 +02:00
|
|
|
'seat_count': seat_count,
|
|
|
|
'signed_seat_count': signed_seat_count,
|
|
|
|
'salt': salt,
|
2018-03-31 04:13:44 +02:00
|
|
|
'plan': "Zulip Premium",
|
|
|
|
'nickname_monthly': Plan.CLOUD_MONTHLY,
|
|
|
|
'nickname_annual': Plan.CLOUD_ANNUAL,
|
2018-07-13 13:33:05 +02:00
|
|
|
'error_message': error_message,
|
2018-08-07 15:37:22 +02:00
|
|
|
'cloud_monthly_price': 8,
|
|
|
|
'cloud_annual_price': 80,
|
|
|
|
'cloud_annual_price_per_month': 6.67,
|
2018-01-13 19:38:13 +01:00
|
|
|
} # type: Dict[str, Any]
|
2018-08-06 06:16:29 +02:00
|
|
|
response = render(request, 'zilencer/upgrade.html', context=context)
|
|
|
|
response['error_description'] = error_description
|
|
|
|
return response
|
2018-01-30 02:02:32 +01:00
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
PLAN_NAMES = {
|
|
|
|
Plan.CLOUD_ANNUAL: "Zulip Premium (billed annually)",
|
|
|
|
Plan.CLOUD_MONTHLY: "Zulip Premium (billed monthly)",
|
|
|
|
}
|
2018-01-13 19:38:13 +01:00
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
@zulip_login_required
|
|
|
|
def billing_home(request: HttpRequest) -> HttpResponse:
|
|
|
|
user = request.user
|
|
|
|
customer = Customer.objects.filter(realm=user.realm).first()
|
|
|
|
if customer is None:
|
|
|
|
return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade'))
|
2018-08-14 03:33:31 +02:00
|
|
|
if not customer.has_billing_relationship:
|
|
|
|
return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade'))
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-22 07:49:48 +02:00
|
|
|
if not user.is_realm_admin and not user.is_billing_admin:
|
2018-07-11 16:36:52 +02:00
|
|
|
context = {'admin_access': False} # type: Dict[str, Any]
|
|
|
|
return render(request, 'zilencer/billing.html', context=context)
|
|
|
|
context = {'admin_access': True}
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-06 18:22:55 +02:00
|
|
|
stripe_customer = stripe_get_customer(customer.stripe_customer_id)
|
2018-07-11 17:12:59 +02:00
|
|
|
subscription = extract_current_subscription(stripe_customer)
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-31 02:47:28 +02:00
|
|
|
prorated_charges = stripe_customer.account_balance
|
2018-07-11 17:12:59 +02:00
|
|
|
if subscription:
|
2018-03-31 04:13:44 +02:00
|
|
|
plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname]
|
|
|
|
seat_count = subscription.quantity
|
|
|
|
# Need user's timezone to do this properly
|
|
|
|
renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(
|
|
|
|
dt=timestamp_to_datetime(subscription.current_period_end))
|
2018-08-06 18:23:48 +02:00
|
|
|
upcoming_invoice = stripe_get_upcoming_invoice(customer.stripe_customer_id)
|
2018-08-31 02:47:28 +02:00
|
|
|
renewal_amount = subscription.plan.amount * subscription.quantity
|
|
|
|
prorated_charges += upcoming_invoice.total - renewal_amount
|
2018-08-23 03:43:11 +02:00
|
|
|
# Can only get here by subscribing and then downgrading. We don't support downgrading
|
|
|
|
# yet, but keeping this code here since we will soon.
|
|
|
|
else: # nocoverage
|
2018-07-11 17:12:59 +02:00
|
|
|
plan_name = "Zulip Free"
|
|
|
|
seat_count = 0
|
|
|
|
renewal_date = ''
|
|
|
|
renewal_amount = 0
|
2018-08-31 02:47:28 +02:00
|
|
|
|
|
|
|
prorated_credits = 0
|
|
|
|
if prorated_charges < 0: # nocoverage
|
|
|
|
prorated_credits = -prorated_charges
|
2018-07-11 17:12:59 +02:00
|
|
|
prorated_charges = 0
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
payment_method = None
|
2018-07-24 14:40:46 +02:00
|
|
|
if stripe_customer.default_source is not None:
|
|
|
|
payment_method = "Card ending in %(last4)s" % {'last4': stripe_customer.default_source.last4}
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-07-11 16:36:52 +02:00
|
|
|
context.update({
|
2018-03-31 04:13:44 +02:00
|
|
|
'plan_name': plan_name,
|
|
|
|
'seat_count': seat_count,
|
|
|
|
'renewal_date': renewal_date,
|
2018-08-31 02:47:28 +02:00
|
|
|
'renewal_amount': '{:,.2f}'.format(renewal_amount / 100.),
|
2018-03-31 04:13:44 +02:00
|
|
|
'payment_method': payment_method,
|
2018-08-31 02:47:28 +02:00
|
|
|
'prorated_charges': '{:,.2f}'.format(prorated_charges / 100.),
|
|
|
|
'prorated_credits': '{:,.2f}'.format(prorated_credits / 100.),
|
2018-09-06 15:14:54 +02:00
|
|
|
'publishable_key': STRIPE_PUBLISHABLE_KEY,
|
|
|
|
'stripe_email': stripe_customer.email,
|
2018-07-11 16:36:52 +02:00
|
|
|
})
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
return render(request, 'zilencer/billing.html', context=context)
|
2018-08-31 20:09:36 +02:00
|
|
|
|
|
|
|
def downgrade(request: HttpRequest, user: UserProfile) -> HttpResponse:
|
|
|
|
if not user.is_realm_admin and not user.is_billing_admin:
|
|
|
|
return json_error(_('Access denied'))
|
|
|
|
try:
|
|
|
|
process_downgrade(user)
|
|
|
|
except BillingError as e:
|
|
|
|
return json_error(e.message, data={'error_description': e.description})
|
|
|
|
return json_success()
|
2018-09-06 15:14:54 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def replace_payment_source(request: HttpRequest, user: UserProfile,
|
|
|
|
stripe_token: str=REQ("stripe_token", validator=check_string)) -> HttpResponse:
|
|
|
|
if not user.is_realm_admin and not user.is_billing_admin:
|
|
|
|
return json_error(_("Access denied"))
|
|
|
|
try:
|
|
|
|
do_replace_payment_source(user, stripe_token)
|
|
|
|
except BillingError as e:
|
|
|
|
return json_error(e.message, data={'error_description': e.description})
|
|
|
|
return json_success()
|