Пример #1
0
def fetch_stripe_subscription(token):
    try:
        billing_token = BillingToken(token)
        if billing_token.has_expired():
            raise BillingTokenInvalidError("Billing token expired: " +
                                           str(billing_token))
        team_id = billing_token.get_team_id()
    except BillingTokenInvalidError:
        raise

    team = Team(team_id)

    if team.is_trialing():
        # This shouldn't happen
        # (should be caught in the slash command;
        # the URL actually shouldn't have generated).
        # But if it does, exit gracefully.
        # Otherwise, the function'll get upset later because
        # the team doesn't have a self.best_subscription.
        raise BillingTokenInvalidError("Team is on free trial: " +
                                       str(team_id) + "\nBilling token: " +
                                       str(billing_token))
        # TODO: Redirect to a pretty page instead

    if team.never_expires():
        # This shouldn't happen either, but just in case!
        raise BillingTokenInvalidError("Team's 'payment' never expires: " +
                                       str(team_id) + "\nBilling token: " +
                                       str(billing_token))
        # TODO: Redirect to a pretty page instead

    # TODO: Implement a response for when I manually input a payment
    # expiration date, but the team has no Stripe subscription yet.

    stripe_subscription = team.get_best_subscription()
    return stripe_subscription
Пример #2
0
def respond_to_billing_request(params):
    user_id = params['user_id'][0]
    team_id = params['team_id'][0]
    team_domain = params['team_domain'][0]
    response_url = params['response_url'][0]
    command_text = params['text'][0]
    
    user = User(user_id)
    team = Team(team_id)
    
    try:
        token = user.get_auth_token()
    except UserAuthorizeError:
        post_and_print_info_and_confirm_success(
            response_url,
            "Sorry, you can't manage your DelaySay billing information"
            " because you haven't authorized DelaySay yet."
            "\n*Please grant DelaySay permission* to schedule your messages:"
            f"\n{api_domain}/add/?team=" + team_id)
        return
    
    billing_info = (
        "your workspace's DelaySay subscription and billing information")
    option, other_user_id, other_user = parse_option_and_user(command_text)
    if option:
        res = write_message_and_add_or_remove_billing_role(
            option, user, user_id, other_user, other_user_id, billing_info)
    elif team.is_trialing():
        if team.get_time_payment_has_been_overdue() > PAYMENT_GRACE_PERIOD:
            res = (
                "Your team's free trial has ended."
                "\nTo continue using DelaySay, *please subscribe here:*"
                f"\n{subscribe_url}/?team={team_id}"
                "\nIf you have any questions, please reach out at"
                f" {contact_page} or {support_email}"
            )
        else:
            res = (
                "Your team is currently on a *free trial* with full access"
                " to all DelaySay features."
                "\nIf you're interested in starting your DelaySay subscription"
                " early before your trial ends, please subscribe here:"
                f"\n{subscribe_url}/?team={team_id}"
                "\nAfter you subscribe, check back here to manage"
                f" {billing_info}."
                # TODO: Add the trial expiration date
            )
    elif team.never_expires():
        res = (
            "Congrats! Your team currently has free access to DelaySay."
            "\nIf you subscribe in the future, check back here to manage"
            f" {billing_info}."
        )
    elif not user.can_manage_billing():
        res = (
            f"You're not authorized to manage {billing_info}."
            "\nPlease ask a *workspace admin* to try instead."
            "\nAn admin can also decide to give you access by typing this:"
            f"\n        `{slash} billing authorize <@{user_id}>`"
        )
    elif False:
        # TODO: Implement a response for when I manually input a payment
        # expiration date, but the team has no Stripe subscription yet.
        pass
    else:
        res = write_billing_portal_message(
            user_id, team_id, team_domain, response_url)
    
    post_and_print_info_and_confirm_success(response_url, res)