Exemplo n.º 1
0
def billing_home(request: HttpRequest) -> HttpResponse:
    user = request.user
    customer = Customer.objects.filter(realm=user.realm).first()
    if customer is None:
        return HttpResponseRedirect(reverse('corporate.views.initial_upgrade'))
    if not CustomerPlan.objects.filter(customer=customer).exists():
        return HttpResponseRedirect(reverse('corporate.views.initial_upgrade'))

    if not user.is_realm_admin and not user.is_billing_admin:
        context = {'admin_access': False}  # type: Dict[str, Any]
        return render(request, 'corporate/billing.html', context=context)
    context = {'admin_access': True}

    stripe_customer = stripe_get_customer(customer.stripe_customer_id)
    plan = get_active_plan(customer)
    if plan is not None:
        plan_name = {
            CustomerPlan.STANDARD: 'Zulip Standard',
            CustomerPlan.PLUS: 'Zulip Plus',
        }[plan.tier]
        now = timezone_now()
        last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(
            plan, now)
        licenses = last_ledger_entry.licenses
        licenses_used = get_seat_count(user.realm)
        # Should do this in javascript, using the user's timezone
        renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(
            dt=next_renewal_date(plan, now))
        renewal_cents = renewal_amount(plan, now)
        # TODO: this is the case where the plan doesn't automatically renew
        if renewal_cents is None:  # nocoverage
            renewal_cents = 0
        charge_automatically = plan.charge_automatically
        if charge_automatically:
            payment_method = payment_method_string(stripe_customer)
        else:
            payment_method = 'Billed by invoice'
    # 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
        plan_name = "Zulip Free"
        licenses = 0
        renewal_date = ''
        renewal_cents = 0
        payment_method = ''
        charge_automatically = False

    context.update({
        'plan_name': plan_name,
        'licenses': licenses,
        'licenses_used': licenses_used,
        'renewal_date': renewal_date,
        'renewal_amount': '{:,.2f}'.format(renewal_cents / 100.),
        'payment_method': payment_method,
        'charge_automatically': charge_automatically,
        'publishable_key': STRIPE_PUBLISHABLE_KEY,
        'stripe_email': stripe_customer.email,
    })
    return render(request, 'corporate/billing.html', context=context)
Exemplo n.º 2
0
def billing_home(request: HttpRequest) -> HttpResponse:
    user = request.user
    customer = Customer.objects.filter(realm=user.realm).first()
    if customer is None:
        return HttpResponseRedirect(reverse('corporate.views.initial_upgrade'))
    if not CustomerPlan.objects.filter(customer=customer).exists():
        return HttpResponseRedirect(reverse('corporate.views.initial_upgrade'))

    if not user.is_realm_admin and not user.is_billing_admin:
        context = {'admin_access': False}  # type: Dict[str, Any]
        return render(request, 'corporate/billing.html', context=context)
    context = {'admin_access': True}

    stripe_customer = stripe_get_customer(customer.stripe_customer_id)
    plan = get_active_plan(customer)
    if plan is not None:
        plan_name = {
            CustomerPlan.STANDARD: 'Zulip Standard',
            CustomerPlan.PLUS: 'Zulip Plus',
        }[plan.tier]
        now = timezone_now()
        last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, now)
        licenses = last_ledger_entry.licenses
        licenses_used = get_seat_count(user.realm)
        # Should do this in javascript, using the user's timezone
        renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format(dt=next_renewal_date(plan, now))
        renewal_cents = renewal_amount(plan, now)
        # TODO: this is the case where the plan doesn't automatically renew
        if renewal_cents is None:  # nocoverage
            renewal_cents = 0
        charge_automatically = plan.charge_automatically
        if charge_automatically:
            payment_method = payment_method_string(stripe_customer)
        else:
            payment_method = 'Billed by invoice'
    # 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
        plan_name = "Zulip Free"
        licenses = 0
        renewal_date = ''
        renewal_cents = 0
        payment_method = ''
        charge_automatically = False

    context.update({
        'plan_name': plan_name,
        'licenses': licenses,
        'licenses_used': licenses_used,
        'renewal_date': renewal_date,
        'renewal_amount': '{:,.2f}'.format(renewal_cents / 100.),
        'payment_method': payment_method,
        'charge_automatically': charge_automatically,
        'publishable_key': STRIPE_PUBLISHABLE_KEY,
        'stripe_email': stripe_customer.email,
    })
    return render(request, 'corporate/billing.html', context=context)