2018-03-31 04:13:44 +02:00
|
|
|
import datetime
|
2018-01-30 21:03:59 +01:00
|
|
|
from functools import wraps
|
2018-01-30 20:49:25 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2018-07-03 21:49:55 +02:00
|
|
|
from typing import Any, Callable, Dict, Optional, TypeVar, Tuple
|
2018-06-28 00:48:51 +02:00
|
|
|
import ujson
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2018-06-28 00:48:51 +02:00
|
|
|
from django.db import transaction
|
2018-01-30 21:03:59 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-07-13 17:34:39 +02:00
|
|
|
from django.core.signing import Signer
|
2018-01-30 20:49:25 +01:00
|
|
|
import stripe
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-01-30 20:49:25 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
2018-07-13 17:34:39 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.models import Realm, UserProfile, RealmAuditLog
|
2018-07-03 21:49:55 +02:00
|
|
|
from zilencer.models import Customer, Plan, BillingProcessor
|
2018-01-30 20:49:25 +01:00
|
|
|
from zproject.settings import get_secret
|
|
|
|
|
|
|
|
STRIPE_PUBLISHABLE_KEY = get_secret('stripe_publishable_key')
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe.api_key = get_secret('stripe_secret_key')
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
BILLING_LOG_PATH = os.path.join('/var/log/zulip'
|
|
|
|
if not settings.DEVELOPMENT
|
|
|
|
else settings.DEVELOPMENT_LOG_DIRECTORY,
|
|
|
|
'billing.log')
|
|
|
|
billing_logger = logging.getLogger('zilencer.stripe')
|
|
|
|
log_to_file(billing_logger, BILLING_LOG_PATH)
|
|
|
|
log_to_file(logging.getLogger('stripe'), BILLING_LOG_PATH)
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
# To generate the fixture data in stripe_fixtures.json:
|
|
|
|
# * Set PRINT_STRIPE_FIXTURE_DATA to True
|
|
|
|
# * ./manage.py setup_stripe
|
|
|
|
# * Customer.objects.all().delete()
|
|
|
|
# * Log in as a user, and go to http://localhost:9991/upgrade/
|
|
|
|
# * Click Add card. Enter the following billing details:
|
|
|
|
# Name: Ada Starr, Street: Under the sea, City: Pacific,
|
|
|
|
# Zip: 33333, Country: United States
|
|
|
|
# Card number: 4242424242424242, Expiry: 03/33, CVV: 333
|
|
|
|
# * Click Make payment.
|
|
|
|
# * Copy out the 4 blobs of json from the dev console into stripe_fixtures.json.
|
|
|
|
# The contents of that file are '{\n' + concatenate the 4 json blobs + '\n}'.
|
|
|
|
# Then you can run e.g. `M-x mark-whole-buffer` and `M-x indent-region` in emacs
|
|
|
|
# to prettify the file (and make 4 space indents).
|
|
|
|
# * Copy out the customer id, plan id, and quantity values into
|
|
|
|
# zilencer.tests.test_stripe.StripeTest.setUp.
|
|
|
|
# * Set PRINT_STRIPE_FIXTURE_DATA to False
|
|
|
|
PRINT_STRIPE_FIXTURE_DATA = False
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
CallableT = TypeVar('CallableT', bound=Callable[..., Any])
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
def get_seat_count(realm: Realm) -> int:
|
|
|
|
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count()
|
|
|
|
|
2018-07-13 17:34:39 +02:00
|
|
|
def sign_string(string: str) -> Tuple[str, str]:
|
|
|
|
salt = generate_random_token(64)
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.sign(string), salt
|
|
|
|
|
|
|
|
def unsign_string(signed_string: str, salt: str) -> str:
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.unsign(signed_string)
|
|
|
|
|
2018-07-27 17:47:03 +02:00
|
|
|
class BillingError(Exception):
|
2018-08-06 06:16:29 +02:00
|
|
|
# error messages
|
2018-08-11 01:23:42 +02:00
|
|
|
CONTACT_SUPPORT = _("Something went wrong. Please contact %s." % (settings.ZULIP_ADMINISTRATOR,))
|
2018-08-06 06:16:29 +02:00
|
|
|
TRY_RELOADING = _("Something went wrong. Please reload the page.")
|
|
|
|
|
|
|
|
# description is used only for tests
|
|
|
|
def __init__(self, description: str, message: str) -> None:
|
|
|
|
self.description = description
|
|
|
|
self.message = message
|
2018-07-27 17:47:03 +02:00
|
|
|
|
2018-08-06 23:07:26 +02:00
|
|
|
class StripeCardError(BillingError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class StripeConnectionError(BillingError):
|
|
|
|
pass
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
def catch_stripe_errors(func: CallableT) -> CallableT:
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped(*args: Any, **kwargs: Any) -> Any:
|
2018-07-26 10:16:20 +02:00
|
|
|
if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage
|
|
|
|
if STRIPE_PUBLISHABLE_KEY is None:
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('missing stripe config', "Missing Stripe config. "
|
|
|
|
"See https://zulip.readthedocs.io/en/latest/subsystems/billing.html.")
|
2018-07-26 10:16:20 +02:00
|
|
|
if not Plan.objects.exists():
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('missing plans',
|
|
|
|
"Plan objects not created. Please run ./manage.py setup_stripe")
|
2018-01-30 21:03:59 +01:00
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
2018-08-06 23:07:26 +02:00
|
|
|
# See https://stripe.com/docs/api/python#error_handling, though
|
|
|
|
# https://stripe.com/docs/api/ruby#error_handling suggests there are additional fields, and
|
|
|
|
# https://stripe.com/docs/error-codes gives a more detailed set of error codes
|
2018-01-30 21:03:59 +01:00
|
|
|
except stripe.error.StripeError as e:
|
2018-08-06 23:07:26 +02:00
|
|
|
err = e.json_body.get('error', {})
|
|
|
|
billing_logger.error("Stripe error: %s %s %s %s" % (
|
|
|
|
e.http_status, err.get('type'), err.get('code'), err.get('param')))
|
2018-01-30 21:03:59 +01:00
|
|
|
if isinstance(e, stripe.error.CardError):
|
2018-08-06 23:07:26 +02:00
|
|
|
# TODO: Look into i18n for this
|
|
|
|
raise StripeCardError('card error', err.get('message'))
|
|
|
|
if isinstance(e, stripe.error.RateLimitError) or \
|
|
|
|
isinstance(e, stripe.error.APIConnectionError): # nocoverage TODO
|
|
|
|
raise StripeConnectionError(
|
|
|
|
'stripe connection error',
|
|
|
|
_("Something went wrong. Please wait a few seconds and try again."))
|
|
|
|
raise BillingError('other stripe error', BillingError.CONTACT_SUPPORT)
|
2018-01-30 21:03:59 +01:00
|
|
|
return wrapped # type: ignore # https://github.com/python/mypy/issues/1927
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-08-06 18:22:55 +02:00
|
|
|
def stripe_get_customer(stripe_customer_id: str) -> stripe.Customer:
|
2018-07-24 14:40:46 +02:00
|
|
|
stripe_customer = stripe.Customer.retrieve(stripe_customer_id, expand=["default_source"])
|
2018-03-31 04:13:44 +02:00
|
|
|
if PRINT_STRIPE_FIXTURE_DATA:
|
2018-08-06 18:18:16 +02:00
|
|
|
print(''.join(['"customer_with_subscription": ', str(stripe_customer), ','])) # nocoverage
|
2018-03-31 04:13:44 +02:00
|
|
|
return stripe_customer
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-08-06 18:23:48 +02:00
|
|
|
def stripe_get_upcoming_invoice(stripe_customer_id: str) -> stripe.Invoice:
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe_invoice = stripe.Invoice.upcoming(customer=stripe_customer_id)
|
|
|
|
if PRINT_STRIPE_FIXTURE_DATA:
|
|
|
|
print(''.join(['"upcoming_invoice": ', str(stripe_invoice), ','])) # nocoverage
|
|
|
|
return stripe_invoice
|
2018-01-30 20:49:25 +01:00
|
|
|
|
2018-07-10 10:56:21 +02:00
|
|
|
# Return type should be Optional[stripe.Subscription], which throws a mypy error.
|
|
|
|
# Will fix once we add type stubs for the Stripe API.
|
|
|
|
def extract_current_subscription(stripe_customer: stripe.Customer) -> Any:
|
|
|
|
if not stripe_customer.subscriptions:
|
|
|
|
return None
|
2018-07-24 14:35:22 +02:00
|
|
|
for stripe_subscription in stripe_customer.subscriptions:
|
2018-07-10 10:56:21 +02:00
|
|
|
if stripe_subscription.status != "canceled":
|
|
|
|
return stripe_subscription
|
|
|
|
return None
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
@catch_stripe_errors
|
2018-07-10 12:11:34 +02:00
|
|
|
def do_create_customer_with_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer:
|
2018-03-31 04:13:44 +02:00
|
|
|
realm = user.realm
|
|
|
|
stripe_customer = stripe.Customer.create(
|
|
|
|
description="%s (%s)" % (realm.string_id, realm.name),
|
2018-08-08 13:17:10 +02:00
|
|
|
email=user.email,
|
2018-03-31 04:13:44 +02:00
|
|
|
metadata={'realm_id': realm.id, 'realm_str': realm.string_id},
|
|
|
|
source=stripe_token)
|
|
|
|
if PRINT_STRIPE_FIXTURE_DATA:
|
|
|
|
print(''.join(['"create_customer": ', str(stripe_customer), ','])) # nocoverage
|
2018-06-28 00:48:51 +02:00
|
|
|
event_time = timestamp_to_datetime(stripe_customer.created)
|
|
|
|
RealmAuditLog.objects.create(
|
2018-08-10 23:23:28 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED,
|
2018-07-22 18:06:36 +02:00
|
|
|
event_time=event_time)
|
2018-06-28 00:48:51 +02:00
|
|
|
RealmAuditLog.objects.create(
|
2018-08-11 00:46:39 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_ADDED,
|
|
|
|
event_time=event_time)
|
2018-07-10 12:11:34 +02:00
|
|
|
Customer.objects.create(
|
2018-03-31 04:13:44 +02:00
|
|
|
realm=realm,
|
|
|
|
stripe_customer_id=stripe_customer.id,
|
|
|
|
billing_user=user)
|
2018-07-10 12:11:34 +02:00
|
|
|
return stripe_customer
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-07-20 17:19:19 +02:00
|
|
|
def do_subscribe_customer_to_plan(stripe_customer: stripe.Customer, stripe_plan_id: str,
|
2018-03-31 04:13:44 +02:00
|
|
|
seat_count: int, tax_percent: float) -> None:
|
2018-07-10 10:56:21 +02:00
|
|
|
if extract_current_subscription(stripe_customer) is not None:
|
2018-08-06 06:16:29 +02:00
|
|
|
# Most likely due to a race condition where two people in the org
|
|
|
|
# try to upgrade their plan at the same time
|
2018-07-10 10:56:21 +02:00
|
|
|
billing_logger.error("Stripe customer %s trying to subscribe to %s, "
|
|
|
|
"but has an active subscription" % (stripe_customer.id, stripe_plan_id))
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('subscribing with existing subscription', BillingError.TRY_RELOADING)
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe_subscription = stripe.Subscription.create(
|
2018-07-10 12:11:34 +02:00
|
|
|
customer=stripe_customer.id,
|
2018-03-31 04:13:44 +02:00
|
|
|
billing='charge_automatically',
|
|
|
|
items=[{
|
|
|
|
'plan': stripe_plan_id,
|
|
|
|
'quantity': seat_count,
|
|
|
|
}],
|
|
|
|
prorate=True,
|
|
|
|
tax_percent=tax_percent)
|
|
|
|
if PRINT_STRIPE_FIXTURE_DATA:
|
|
|
|
print(''.join(['"create_subscription": ', str(stripe_subscription), ','])) # nocoverage
|
2018-07-10 12:11:34 +02:00
|
|
|
customer = Customer.objects.get(stripe_customer_id=stripe_customer.id)
|
2018-06-28 00:48:51 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
customer.realm.has_seat_based_plan = True
|
|
|
|
customer.realm.save(update_fields=['has_seat_based_plan'])
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=customer.realm,
|
|
|
|
acting_user=customer.billing_user,
|
2018-08-11 00:48:10 +02:00
|
|
|
event_type=RealmAuditLog.STRIPE_PLAN_CHANGED,
|
2018-06-28 00:48:51 +02:00
|
|
|
event_time=timestamp_to_datetime(stripe_subscription.created),
|
|
|
|
extra_data=ujson.dumps({'plan': stripe_plan_id, 'quantity': seat_count}))
|
|
|
|
|
|
|
|
current_seat_count = get_seat_count(customer.realm)
|
|
|
|
if seat_count != current_seat_count:
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=customer.realm,
|
2018-08-11 00:51:18 +02:00
|
|
|
event_type=RealmAuditLog.STRIPE_PLAN_QUANTITY_RESET,
|
2018-06-28 00:48:51 +02:00
|
|
|
event_time=timestamp_to_datetime(stripe_subscription.created),
|
|
|
|
requires_billing_update=True,
|
|
|
|
extra_data=ujson.dumps({'quantity': current_seat_count}))
|
2018-07-27 15:37:04 +02:00
|
|
|
|
2018-08-06 06:47:15 +02:00
|
|
|
def process_initial_upgrade(user: UserProfile, plan: Plan, seat_count: int, stripe_token: str) -> None:
|
2018-07-27 15:37:04 +02:00
|
|
|
stripe_customer = do_create_customer_with_payment_source(user, stripe_token)
|
|
|
|
do_subscribe_customer_to_plan(
|
|
|
|
stripe_customer=stripe_customer,
|
2018-08-06 06:47:15 +02:00
|
|
|
stripe_plan_id=plan.stripe_plan_id,
|
2018-07-27 15:37:04 +02:00
|
|
|
seat_count=seat_count,
|
|
|
|
# TODO: billing address details are passed to us in the request;
|
|
|
|
# use that to calculate taxes.
|
|
|
|
tax_percent=0)
|
2018-07-03 21:49:55 +02:00
|
|
|
|
|
|
|
## Process RealmAuditLog
|
|
|
|
|
|
|
|
def do_set_subscription_quantity(
|
|
|
|
customer: Customer, timestamp: int, idempotency_key: str, quantity: int) -> None:
|
|
|
|
stripe_customer = stripe_get_customer(customer.stripe_customer_id)
|
|
|
|
stripe_subscription = extract_current_subscription(stripe_customer)
|
|
|
|
stripe_subscription.quantity = quantity
|
|
|
|
stripe_subscription.proration_date = timestamp
|
|
|
|
stripe_subscription.save(idempotency_key=idempotency_key)
|
|
|
|
|
|
|
|
def do_adjust_subscription_quantity(
|
|
|
|
customer: Customer, timestamp: int, idempotency_key: str, delta: int) -> None:
|
|
|
|
stripe_customer = stripe_get_customer(customer.stripe_customer_id)
|
|
|
|
stripe_subscription = extract_current_subscription(stripe_customer)
|
|
|
|
stripe_subscription.quantity = stripe_subscription.quantity + delta
|
|
|
|
stripe_subscription.proration_date = timestamp
|
|
|
|
stripe_subscription.save(idempotency_key=idempotency_key)
|
|
|
|
|
|
|
|
def increment_subscription_quantity(
|
|
|
|
customer: Customer, timestamp: int, idempotency_key: str) -> None:
|
|
|
|
return do_adjust_subscription_quantity(customer, timestamp, idempotency_key, 1)
|
|
|
|
|
|
|
|
def decrement_subscription_quantity(
|
|
|
|
customer: Customer, timestamp: int, idempotency_key: str) -> None:
|
|
|
|
return do_adjust_subscription_quantity(customer, timestamp, idempotency_key, -1)
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
|
|
|
def process_billing_log_entry(processor: BillingProcessor, log_row: RealmAuditLog) -> None:
|
|
|
|
processor.state = BillingProcessor.STARTED
|
|
|
|
processor.log_row = log_row
|
|
|
|
processor.save()
|
|
|
|
|
|
|
|
customer = Customer.objects.get(realm=log_row.realm)
|
|
|
|
timestamp = datetime_to_timestamp(log_row.event_time)
|
|
|
|
idempotency_key = 'process_billing_log_entry:%s' % (log_row.id,)
|
|
|
|
extra_args = {} # type: Dict[str, Any]
|
|
|
|
if log_row.extra_data is not None:
|
|
|
|
extra_args = ujson.loads(log_row.extra_data)
|
|
|
|
processing_functions = {
|
|
|
|
RealmAuditLog.STRIPE_PLAN_QUANTITY_RESET: do_set_subscription_quantity,
|
|
|
|
RealmAuditLog.USER_CREATED: increment_subscription_quantity,
|
|
|
|
RealmAuditLog.USER_ACTIVATED: increment_subscription_quantity,
|
|
|
|
RealmAuditLog.USER_DEACTIVATED: decrement_subscription_quantity,
|
|
|
|
RealmAuditLog.USER_REACTIVATED: increment_subscription_quantity,
|
|
|
|
} # type: Dict[str, Callable[..., None]]
|
|
|
|
processing_functions[log_row.event_type](customer, timestamp, idempotency_key, **extra_args)
|
|
|
|
|
|
|
|
processor.state = BillingProcessor.DONE
|
|
|
|
processor.save()
|
|
|
|
|
|
|
|
def get_next_billing_log_entry(processor: BillingProcessor) -> Optional[RealmAuditLog]:
|
|
|
|
if processor.state == BillingProcessor.STARTED:
|
|
|
|
return processor.log_row
|
|
|
|
assert processor.state != BillingProcessor.STALLED
|
|
|
|
if processor.state not in [BillingProcessor.DONE, BillingProcessor.SKIPPED]:
|
|
|
|
raise BillingError(
|
|
|
|
'unknown processor state',
|
|
|
|
"Check for typos, since this value is sometimes set by hand: %s" % (processor.state,))
|
|
|
|
|
|
|
|
if processor.realm is None:
|
|
|
|
realms_with_processors = BillingProcessor.objects.exclude(
|
|
|
|
realm=None).values_list('realm', flat=True)
|
|
|
|
query = RealmAuditLog.objects.exclude(realm__in=realms_with_processors)
|
|
|
|
else:
|
|
|
|
global_processor = BillingProcessor.objects.get(realm=None)
|
|
|
|
query = RealmAuditLog.objects.filter(
|
|
|
|
realm=processor.realm, id__lt=global_processor.log_row.id)
|
|
|
|
return query.filter(id__gt=processor.log_row.id,
|
|
|
|
requires_billing_update=True).order_by('id').first()
|
|
|
|
|
|
|
|
def run_billing_processor_one_step(processor: BillingProcessor) -> bool:
|
|
|
|
# Returns True if a row was processed, or if processing was attempted
|
|
|
|
log_row = get_next_billing_log_entry(processor)
|
|
|
|
if log_row is None:
|
|
|
|
if processor.realm is not None:
|
|
|
|
processor.delete()
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
process_billing_log_entry(processor, log_row)
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
billing_logger.error("Error on log_row.realm=%s, event_type=%s, log_row.id=%s, "
|
|
|
|
"processor.id=%s, processor.realm=%s" % (
|
|
|
|
processor.log_row.realm.string_id, processor.log_row.event_type,
|
|
|
|
processor.log_row.id, processor.id, processor.realm))
|
|
|
|
if isinstance(e, StripeCardError):
|
|
|
|
if processor.realm is None:
|
|
|
|
BillingProcessor.objects.create(log_row=processor.log_row,
|
|
|
|
realm=processor.log_row.realm,
|
|
|
|
state=BillingProcessor.STALLED)
|
|
|
|
processor.state = BillingProcessor.SKIPPED
|
|
|
|
else:
|
|
|
|
processor.state = BillingProcessor.STALLED
|
|
|
|
processor.save()
|
|
|
|
return True
|
|
|
|
raise
|