from django.db import transaction from django.utils.translation import ugettext as _ from django.utils.timezone import now as timezone_now from django.core.signing import Signer import stripe from zerver.lib.logging_util import log_to_file from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.utils import generate_random_token from zerver.models import Realm, UserProfile, RealmAuditLog from corporate.models import Customer, CustomerPlan, LicenseLedger, \ get_current_plan_by_customer, get_customer_by_realm, \ get_current_plan_by_realm from zproject.config import get_secret STRIPE_PUBLISHABLE_KEY = get_secret('stripe_publishable_key') stripe.api_key = get_secret('stripe_secret_key') BILLING_LOG_PATH = os.path.join( '/var/log/zulip' if not settings.DEVELOPMENT else settings.DEVELOPMENT_LOG_DIRECTORY, 'billing.log') billing_logger = logging.getLogger('corporate.stripe') log_to_file(billing_logger, BILLING_LOG_PATH) log_to_file(logging.getLogger('stripe'), BILLING_LOG_PATH) CallableT = TypeVar('CallableT', bound=Callable[..., Any]) MIN_INVOICED_LICENSES = 30 MAX_INVOICED_LICENSES = 1000 DEFAULT_INVOICE_DAYS_UNTIL_DUE = 30
import configparser import os import sys ZULIP_PATH = os.getcwd() # Thumbor doesn’t set __file__ when loading this sys.path.append(ZULIP_PATH) from zproject.config import get_secret os.environ["AWS_ACCESS_KEY_ID"] = get_secret("s3_key", "") os.environ["AWS_SECRET_ACCESS_KEY"] = get_secret("s3_secret_key", "") config_file = configparser.RawConfigParser() config_file.read("/etc/zulip/zulip.conf") # Whether this instance of Zulip is running in a production environment. PRODUCTION = config_file.has_option("machine", "deploy_type") if PRODUCTION: try: from zproject.prod_settings import LOCAL_UPLOADS_DIR except ImportError: LOCAL_UPLOADS_DIR = None else: from zproject.dev_settings import LOCAL_UPLOADS_DIR IS_LOCAL_STORAGE = bool(LOCAL_UPLOADS_DIR) ################################# File Loader ################################## ## The root path where the File Loader will try to find images if IS_LOCAL_STORAGE:
def redirect_to_upbook_api() -> HttpResponse: response = redirect( get_secret('upbook_api_url') + '/api/v1/auth/token/code/zulip?access_token=' + awsToken) return response
import configparser import os import sys ZULIP_PATH = os.getcwd() # Thumbor doesn’t set __file__ when loading this sys.path.append(ZULIP_PATH) from zproject.config import get_secret os.environ['AWS_ACCESS_KEY_ID'] = get_secret('s3_key', '') os.environ['AWS_SECRET_ACCESS_KEY'] = get_secret('s3_secret_key', '') config_file = configparser.RawConfigParser() config_file.read("/etc/zulip/zulip.conf") # Whether this instance of Zulip is running in a production environment. PRODUCTION = config_file.has_option('machine', 'deploy_type') if PRODUCTION: try: from zproject.prod_settings import LOCAL_UPLOADS_DIR except ImportError: LOCAL_UPLOADS_DIR = None else: from zproject.dev_settings import LOCAL_UPLOADS_DIR IS_LOCAL_STORAGE = bool(LOCAL_UPLOADS_DIR) ################################# File Loader ################################## ## The root path where the File Loader will try to find images if IS_LOCAL_STORAGE:
CustomerPlan, LicenseLedger, get_current_plan_by_customer, get_current_plan_by_realm, get_customer_by_realm, ) from zerver.lib.exceptions import JsonableError from zerver.lib.logging_util import log_to_file from zerver.lib.send_email import FromAddress, send_email_to_billing_admins_and_realm_owners from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.utils import assert_is_not_none from zerver.models import Realm, RealmAuditLog, UserProfile, get_system_bot from zilencer.models import RemoteZulipServer, RemoteZulipServerAuditLog from zproject.config import get_secret stripe.api_key = get_secret("stripe_secret_key") BILLING_LOG_PATH = os.path.join( "/var/log/zulip" if not settings.DEVELOPMENT else settings.DEVELOPMENT_LOG_DIRECTORY, "billing.log", ) billing_logger = logging.getLogger("corporate.stripe") log_to_file(billing_logger, BILLING_LOG_PATH) log_to_file(logging.getLogger("stripe"), BILLING_LOG_PATH) CallableT = TypeVar("CallableT", bound=Callable[..., object]) MIN_INVOICED_LICENSES = 30 MAX_INVOICED_LICENSES = 1000 DEFAULT_INVOICE_DAYS_UNTIL_DUE = 30
def stripe_webhook(request: HttpRequest) -> HttpResponse: stripe_webhook_endpoint_secret = get_secret( "stripe_webhook_endpoint_secret", "") if ( stripe_webhook_endpoint_secret and not settings.TEST_SUITE ): # nocoverage: We can't verify the signature in test suite since we fetch the events # from Stripe events API and manually post to the webhook endpoint. try: stripe_event = stripe.Webhook.construct_event( request.body, request.META.get("HTTP_STRIPE_SIGNATURE"), stripe_webhook_endpoint_secret, ) except ValueError: return HttpResponse(status=400) except stripe.error.SignatureVerificationError: return HttpResponse(status=400) else: assert not settings.PRODUCTION try: stripe_event = stripe.Event.construct_from( json.loads(request.body), stripe.api_key) except Exception: return HttpResponse(status=400) if stripe_event.api_version != STRIPE_API_VERSION: error_message = f"Mismatch between billing system Stripe API version({STRIPE_API_VERSION}) and Stripe webhook event API version({stripe_event.api_version})." billing_logger.error(error_message) return HttpResponse(status=400) if stripe_event.type not in [ "checkout.session.completed", "payment_intent.succeeded", "payment_intent.payment_failed", ]: return HttpResponse(status=200) if Event.objects.filter(stripe_event_id=stripe_event.id).exists(): return HttpResponse(status=200) event = Event(stripe_event_id=stripe_event.id, type=stripe_event.type) if stripe_event.type == "checkout.session.completed": stripe_session = stripe_event.data.object assert isinstance(stripe_session, stripe.checkout.Session) try: session = Session.objects.get(stripe_session_id=stripe_session.id) except Session.DoesNotExist: return HttpResponse(status=200) event.content_type = ContentType.objects.get_for_model(Session) event.object_id = session.id event.save() handle_checkout_session_completed_event(stripe_session, event) elif stripe_event.type == "payment_intent.succeeded": stripe_payment_intent = stripe_event.data.object assert isinstance(stripe_payment_intent, stripe.PaymentIntent) try: payment_intent = PaymentIntent.objects.get( stripe_payment_intent_id=stripe_payment_intent.id) except PaymentIntent.DoesNotExist: # PaymentIntent that was not manually created from the billing system. # Could be an Invoice getting paid which is is not an event we are interested in. return HttpResponse(status=200) event.content_type = ContentType.objects.get_for_model(PaymentIntent) event.object_id = payment_intent.id event.save() handle_payment_intent_succeeded_event(stripe_payment_intent, event) elif stripe_event.type == "payment_intent.payment_failed": stripe_payment_intent = stripe_event.data.object try: assert isinstance(stripe_payment_intent, stripe.PaymentIntent) payment_intent = PaymentIntent.objects.get( stripe_payment_intent_id=stripe_payment_intent.id) except PaymentIntent.DoesNotExist: # PaymentIntent that was not manually created from the billing system. # Could be an Invoice getting paid which is is not an event we are interested in. return HttpResponse(status=200) event.content_type = ContentType.objects.get_for_model(PaymentIntent) event.object_id = payment_intent.id event.save() handle_payment_intent_payment_failed_event(stripe_payment_intent, event) return HttpResponse(status=200)