def principal_to_user_profile(agent: UserProfile, principal: Union[str, int]) -> UserProfile: try: if isinstance(principal, str): return get_active_user(principal, agent.realm) else: return get_active_user_profile_by_id_in_realm(principal, agent.realm) except UserProfile.DoesNotExist: # We have to make sure we don't leak information about which users # are registered for Zulip in a different realm. We could do # something a little more clever and check the domain part of the # principal to maybe give a better error message raise PrincipalError(principal)
def handle_checkout_session_completed_event( stripe_session: stripe.checkout.Session, session: Session) -> None: session.status = Session.COMPLETED session.save() stripe_setup_intent = stripe.SetupIntent.retrieve( stripe_session.setup_intent) assert session.customer.realm is not None user_id = stripe_session.metadata.get("user_id") assert user_id is not None user = get_active_user_profile_by_id_in_realm(user_id, session.customer.realm) payment_method = stripe_setup_intent.payment_method if session.type in [ Session.UPGRADE_FROM_BILLING_PAGE, Session.RETRY_UPGRADE_WITH_ANOTHER_PAYMENT_METHOD, ]: ensure_realm_does_not_have_active_plan(user.realm) update_or_create_stripe_customer(user, payment_method) assert session.payment_intent is not None session.payment_intent.status = PaymentIntent.PROCESSING session.payment_intent.last_payment_error = () session.payment_intent.save( update_fields=["status", "last_payment_error"]) try: stripe.PaymentIntent.confirm( session.payment_intent.stripe_payment_intent_id, payment_method=payment_method, off_session=True, ) except stripe.error.CardError: pass elif session.type in [ Session.FREE_TRIAL_UPGRADE_FROM_BILLING_PAGE, Session.FREE_TRIAL_UPGRADE_FROM_ONBOARDING_PAGE, ]: ensure_realm_does_not_have_active_plan(user.realm) update_or_create_stripe_customer(user, payment_method) process_initial_upgrade( user, int(stripe_setup_intent.metadata["licenses"]), stripe_setup_intent.metadata["license_management"] == "automatic", int(stripe_setup_intent.metadata["billing_schedule"]), charge_automatically=True, free_trial=True, ) elif session.type in [Session.CARD_UPDATE_FROM_BILLING_PAGE]: update_or_create_stripe_customer(user, payment_method)
def handle_payment_intent_succeeded_event( stripe_payment_intent: stripe.PaymentIntent, payment_intent: PaymentIntent) -> None: payment_intent.status = PaymentIntent.SUCCEEDED payment_intent.save() metadata: Dict[str, Any] = stripe_payment_intent.metadata assert payment_intent.customer.realm is not None user_id = metadata.get("user_id") assert user_id is not None user = get_active_user_profile_by_id_in_realm( user_id, payment_intent.customer.realm) description = "" for charge in stripe_payment_intent.charges: description = f"Payment (Card ending in {charge.payment_method_details.card.last4})" break stripe.InvoiceItem.create( amount=stripe_payment_intent.amount * -1, currency="usd", customer=stripe_payment_intent.customer, description=description, discountable=False, ) try: ensure_realm_does_not_have_active_plan(user.realm) except UpgradeWithExistingPlanError as e: stripe_invoice = stripe.Invoice.create( auto_advance=True, collection_method="charge_automatically", customer=stripe_payment_intent.customer, days_until_due=None, statement_descriptor="Zulip Cloud Standard Credit", ) stripe.Invoice.finalize_invoice(stripe_invoice) raise e process_initial_upgrade( user, int(metadata["licenses"]), metadata["license_management"] == "automatic", int(metadata["billing_schedule"]), True, False, )
def get_presence_backend(request: HttpRequest, user_profile: UserProfile, user_id_or_email: str) -> HttpResponse: # This isn't used by the webapp; it's available for API use by # bots and other clients. We may want to add slim_presence # support for it (or just migrate its API wholesale) later. try: try: user_id = int(user_id_or_email) target = get_active_user_profile_by_id_in_realm( user_id, user_profile.realm) except ValueError: email = user_id_or_email target = get_active_user(email, user_profile.realm) except UserProfile.DoesNotExist: return json_error(_("No such user")) if target.is_bot: return json_error(_("Presence is not supported for bot users.")) presence_dict = get_presence_for_user(target.id) if len(presence_dict) == 0: return json_error( _("No presence data for {user_id_or_email}").format( user_id_or_email=user_id_or_email)) # For initial version, we just include the status and timestamp keys result = dict(presence=presence_dict[target.email]) aggregated_info = result["presence"]["aggregated"] aggr_status_duration = datetime_to_timestamp( timezone_now()) - aggregated_info["timestamp"] if aggr_status_duration > settings.OFFLINE_THRESHOLD_SECS: aggregated_info["status"] = "offline" for val in result["presence"].values(): val.pop("client", None) val.pop("pushable", None) return json_success(result)