Commit Graph

1056 Commits

Author SHA1 Message Date
Prakhar Pratyush 14415dfa90 invoice_paid_event: Verify invoice before activating fixed-price plan.
Earlier, we were not verifying that the invoice which got paid is
for the fixed-price plan.

That could result in a bug where another support invoice with
collection_method = "send_invoice" got paid while a fixed-price
plam is already configured. The fixed-price plan would be falsely
activated.

This commit verifies the invoice before activating the fixed-price
plan.
2024-04-16 09:49:04 -07:00
Mateusz Mandera 4a2a9176c2 realms: Add caching to the get_seat_count calculation for upload limit.
For simiplicty's sake, we can avoid trying to do cache invalidation in
the variety of events that can cause the seat count to change - since
having an up to 1 day delay between users being added and the upload
limit going up is quite reasonable.
2024-04-15 15:08:56 -07:00
Mateusz Mandera 4eacd25ad5 stripe: Rename CustomerPlan.is_paid.
This might not be the most meaningful change of phrasing, but .is_paid()
sounds like it's a check for whether the customer has already paid their
invoice. is_a_paid_plan() reflects better the meaning that it's whether
it's a plan of a "paid" type.
2024-04-15 15:08:56 -07:00
Lauryn Menard 4e5c62527c activity: Update installation total row for moving referrer column.
Updates the total row so that the ARR and user counts align with
the column updates in commit 971123565f.
2024-04-15 10:52:41 -07:00
Aman Agrawal 8a807949df stripe: Disable free trial for customer with any plan in the past. 2024-04-11 21:57:33 -07:00
Aman Agrawal ed2de77895 stripe: Send invoice to customer at the start of free trial.
We send customer an invoice at the start of free trial, if customer
pays we upgrade them to the active plan at the end of free trial,
else we downgrade them and show a custom message on the upgrade
page regarding the current status.
2024-04-11 21:57:33 -07:00
Aman Agrawal 8715ead8bc stripe: Regenerate stripe fixtures with fixed tests.
Tests were broken since #29221 and #28875 didn't account for
other tests failing due to changed stripe data. Also, there
was a bug where we were not fetching the correct setup intent
and stripe session for the current test, it was fixed by narrowing
the fetch to the current customer.

Also, we now run `invoice_plans` in a `while` loop until
`next_invoice_date` is greater than the provided event_time. It
makes sense to generate all the invoices for a customer that
needs to paid by them when `invoice_plans_as_needed` is called
for a `event_time`.
2024-04-11 21:57:33 -07:00
Aman Agrawal a2e0c84a7e stripe: Remove unused parameter. 2024-04-11 21:57:33 -07:00
Anders Kaseorg e5fd761562 test_stripe: Replace deprecated stripe.Event.to_dict_recursive.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-04-04 11:23:11 -07:00
Anders Kaseorg 040e8aa7f1 test_stripe: Remove unused stripe.util import.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-04-04 11:20:26 -07:00
Prakhar Pratyush e3f8c62e34 registration: Send a group DM to admins when no spare licenses left.
Earlier, when adding a new user failed due to no spare licenses
available, a message was sent to the "New user announcements"
stream.

We plan to disable the stream by default as a part of improving
onboarding experience.

Now, we send a group DM to admins when adding a new user fails
due to no spare licenses available. It makes it independent of
the "New user announcements" setting. These warning messages
are important and shouldn't be missed.
2024-04-03 12:28:05 -07:00
Anders Kaseorg ccb95e76dc ruff: Fix RUF007 Prefer `itertools.pairwise()` over `zip()`.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-04-01 18:32:52 -07:00
Anders Kaseorg ae47de36c9 python: Add missing TypeAlias annotations.
See commit c2c96eb0cf (#26405).

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-04-01 16:19:56 -07:00
Aman Agrawal d21f5c9b75 registration: Ask user how they found Zulip. 2024-04-01 12:44:12 -07:00
Aman Agrawal b86e37a979 stripe: Add period information for generated invoices.
Invoices need to have period to avoid charges being marked as
point-in-time revenue rather than monthly revenue.
2024-03-30 14:56:12 -07:00
Mateusz Mandera 374178903e remote_billing: Move stripe_customer_id from server to realm if needed.
As explained in the comment, when we're moving the server plan to the
remote realm's Customer object, the realm Customer may not have
stripe_customer_id set and therefore that value needs to get moved from
the server Customer.
2024-03-29 16:27:03 -07:00
Prakhar Pratyush a0cec2c378 stripe: Skip invoice overdue mail for free plans with no next plan.
Earlier, if a free plan (say legacy plan) with no next plan scheduled
was invoiced, we used to send an invoice overdue email if the last
audit log update is stale.

Actually, we don't need this data as the invoice step is just going
to downgrade the current plan. We should not wait for customer to
start uploading data in this case. Skip the email sending step and
invoice the plan to downgrade.
2024-03-28 09:29:23 -07:00
Mateusz Mandera c3caa3ecc8 stripe: Rename @error_handler decorator.
This decorator, among other things, transforms the "event" argument
passed when calling the decorated functions into actually passing
event.content_object.

So e.g. despite having a (before the decorator is applied) signature:

```
def handle_invoice_paid_event(stripe_invoice: stripe.Invoice, invoice: Invoice) -> None:
```

these are called passing an `Event` in the second arg when calling
`handle_invoice_paid_event`:

```
handle_invoice_paid_event(stripe_invoice, event)
```

I found that kind of confusing because the @error_handler decorator
didn't sound like something that would intervene in the arguments like
that. So it feels helpful to rename it something with a less modest
name, that makes it sound like it does more than just pure
error-handling.
2024-03-28 09:10:09 -07:00
Anders Kaseorg a1a341f0ae ruff: Fix UP032 Use f-string instead of `format` call.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-03-21 08:59:17 -07:00
Lauryn Menard 373671080d remote-support: Include billing entity name in internal billing notice.
Adds a line to the top of the internal_billing_notice email with
the billing entity's display name.

Makes sure all internal_billng_notice email subjects also include
the billing entity's display name.

Makes small updates to the notice text for some cases.
2024-03-20 11:58:15 -07:00
Prakhar Pratyush c9d25b2a95 stripe: Redirect to /plans if plans downgraded on /billing page visit.
When a customer with plan's status 'DOWNGRADE_AT_END_OF_FREE_TRIAL'
visits /billing page on the free-trial end date before the invoice
cron runs, the 'make_end_of_cycle_updates_if_needed' downgrades the
plan.

Earlier, when such a customer visited /billing page in this time window
it resulted in an assertion error.

This commit fixes the incorrect behaviour by redirecting to '/plans'
page in such cases.
2024-03-19 09:25:07 -07:00
Prakhar Pratyush 421400c651 corporate: Fix 'get_next_billing_cycle' for scheduled fixed-price plan.
No renewal ledger entry exists for a scheduled fixed-price plan.

Earlier, when a customer with scheduled fixed-price plan
visited the billing page, the 'get_next_billing_cycle' function
raised an assertion error as no renewal ledger entry exists.

We don't need the ledger entry to determine the next billing
cycle in this case, so we move the code block which determines
the last_renewal time within the else conditional i.e. where it
is needed.

The scheduled plan has NEVER_STARTED status and the
next_invoice_date set to end date of the current plan.
We use this info to determine the next billing cycle.
2024-03-15 12:31:57 -07:00
Aman Agrawal 3b4abc94ee stripe: Revive an old test case for payment method. 2024-03-15 08:39:46 -07:00
Aman Agrawal 26a92f5d66 stripe: Add more context to some comments. 2024-03-15 08:39:46 -07:00
Aman Agrawal bddc271e84 stripe: Keep remote realm and server type_of_plan_tier_change in sync.
Ideally they should never be different here.
2024-03-15 08:38:46 -07:00
Aman Agrawal e10f81197f upgrade: Fix allowed free trails for ended legacy plans on server.
If the remote realm registered after the legacy plan on server
ENDED, we never migrate the plan to the remote realm. So, we need
to check the server of remote realm whenever we are check remote
realm for legacy plan.
2024-03-15 08:38:46 -07:00
Aman Agrawal b26326815a billing: Adjust strings on billing page for invoice payment method. 2024-03-14 16:43:52 -07:00
Aman Agrawal caba57fe1e stripe: Bill manually managed licenses monthly for additional licenses.
Regardless of plan renewal schedule, we try to invoice all plans
monthly with some exceptions like free trial and fixed price plans,
which help us charge users for additional licenses used during
the previous month.
2024-03-14 16:43:52 -07:00
Aman Agrawal c84b9cbc97 upgrade: Allow payment by invoice. 2024-03-14 16:43:52 -07:00
Aman Agrawal b1ea8f3c1c stripe: Change plan type immediately after creating a live plan.
Minor refactor that makes sense to do.
2024-03-14 16:43:52 -07:00
Aman Agrawal 2788ed893a stripe: Remove completed TODO. 2024-03-14 16:43:52 -07:00
Aman Agrawal 4b93b08674 stripe: Remove unused parameter. 2024-03-14 16:43:52 -07:00
Lauryn Menard 8f5b8dbf1a stripe: Remove unused do_change_remote_server_plan_type.
This was implemented instead as part of the BillingSession class
with the do_change_plan_type method.
2024-03-12 11:19:29 -07:00
Alex Vandiver d460743705 realm_activity: Correct the "messages sent" column. 2024-03-11 14:39:03 -07:00
Lauryn Menard 4d470a52f8 remote-support: Highlight if audit log data is stale or current.
Includes has_stale_audit_log boolean in the remote support view
data so that we can style the last audit log data for the remote
server to visually highlight if it is stale or current.
2024-03-08 09:40:43 -08:00
Lauryn Menard 4e1e490b40 cloud-support: Display most recent sponsorship request information.
Refactors the Cloud support view to pass in any sponsorship or
discount information about the Customer object for the realm,
which allows us to display any information submitted in a
sponsorship request.
2024-03-08 09:40:43 -08:00
Lauryn Menard c9caad20d0 support: Have distinct labels for remote vs Cloud support views. 2024-03-08 09:40:43 -08:00
Lauryn Menard ef11619e73 support: Clean up options for realm plan_type and org_type.
Removes adding get_org_type_display_name to support context as
its use was removed in commit f25230c7d4.
2024-03-08 09:40:43 -08:00
Prakhar Pratyush 7e9212a1a0 corporate: Use 'next_invoice_date' as the event_time in invoice_plan.
Earlier, we were using 'timezone_now' (the time cron job runs)
as the event_time while invoicing plans in 'invoice_plan'.
This is not accurate as it will lead to invoicing ledger entries
created after 'next_invoice_date' and before 'timezone_now'.

We should only invoice the ledger entries created till
next_invoice_date. It should be independent of the time
at which cron job runs.

This commit updates the logic to use next_invoice_date
as the event_time while invoicing via cron.
2024-03-08 09:25:46 -08:00
Prakhar Pratyush 31a0aa2a83 test_stripe: Fix time travel to incorrect datetime.
Earlier, at few places in test_stripe we were doing
incorrect time travel. For example:
A plan was invoiced till self.next_year & while checking
the billing page, we were using the self.now datetime.
To correctly check the billing page state after plan is
invoiced, we should use a datetime greater than or equal
to the time at which plan was invoiced.

This commit fixes such logically incorrect time-travel.
2024-03-08 09:25:46 -08:00
Prakhar Pratyush 72d9a4a074 test_support_views: Fix event_time of LicenseLedger entry created.
While creating a LicenseLedger entry in 'create_customer_and_plan',
we should set the 'event_time' to the same time at which the plan
is created.

Earlier, the 'event_time' for ledger entry & 'billing_cycle_anchor'
of the plan were set to different values, which is not the
correct behavior.
2024-03-08 09:25:46 -08:00
Lauryn Menard f3906f4a2f remote-support: Add ability to deactivate and reactivate remote servers. 2024-03-07 11:51:00 -08:00
Prakhar Pratyush bd969de208 stripe: Don't invoice fixed-price plans for additional licenses.
Earlier, the code block to calculate additional licenses charge
raised assertion error when processing a fixed-price plan.

The code block shouldn't be executed for fixed-price plans as
we don't charge customers on fixed-price plans for additional
licenses.
2024-03-07 09:54:34 -08:00
Prakhar Pratyush 5085d58bc7 corporate: Fix to check if the plan we are processing is paid or not.
Earlier, the 'self.on_paid_plan()' check was verifying if the
billing_session/customer is on paid plan and not the plan we
are processing.

This resulted in a bug. While processing a legacy plan, a customer
switches from legacy plan to a paid plan resulting in the
'self.on_paid_plan()' check returning True.
It leads to invoicing legacy plan which shouldn't happen.

The fix is to check if the plan we are processing is paid or not
instead of the remote_realm/remote_server plan_type.
2024-03-07 09:54:34 -08:00
Prakhar Pratyush 01516ef512 invoice_plan: Don't test an Optional value using implicit truthiness. 2024-03-07 09:54:34 -08:00
Mateusz Mandera e952c3b627 remote_billing: Tweak /self-hosted-billing/ endpoints access model.
It's best for these to just be consistent. Therefore:
1. The .../not-configured/ error page endpoint should be restricted to
   .has_billing_access users only.
2. For consistency, self_hosting_auth_view_common is tweaked to also do
   the .has_billing_access check as the first thing, to avoid revealing
   configuration information via its redirect/error-handling behavior.

The revealed configuration information seems super harmless, but it's
simpler to not have to worry about it and just be consistent.
2024-03-05 11:53:51 -08:00
Mateusz Mandera e39f400f94 remote_billing: Make "plan management" always available.
Just shows a config error page if the bouncer is not enabled. Uses a new
endpoint for this so that it can work nicely for both browser and
desktop app clients.
It's necessary, because the desktop app expects to get a json response
with either an error or billing_access_url to redirect to. Showing a
nice config error page can't be done via the json error mechanism, so
instead we just serve a redirect to the new error page, which the app
will open in the browser in a new window or tab.
2024-03-05 11:53:51 -08:00
Lauryn Menard 666041e480 remote-support: Show deactivated servers in search results.
The remote support view now returns results for deactivated remote
servers with those results sorted to the end and formatted to
visually stand out.

Forms to change sponsorship and discount fields on the customer
for the remote server or realm are not shown, but the data stored
on the customer object is shown, including any sponsorship request
information (if the customer had a sponsorship request pending when
it was deactivated).

Forms to schedule a plan are also not shown for deactivated servers
and their associated remote realms.

Forms and information for any current plan or scheduled plan, for
either the deactivated remote server or its associated remote
realms, are shown so that support staff can update those plans if
necessary.
2024-03-01 14:11:19 -08:00
Prakhar Pratyush a513489f38 corporate: Fix invoicing of plans on free-trial with changed schedule.
Earlier, the 'next_invoice_date', 'invoiced_through', and
'invoicing_status' fields in 'do_change_schedule_after_free_trial'
were not set correctly.

It resulted in invoicing the ENDED plan and the ledger we create
in this function.

Multiple invoices were created depending on the number of times the
billing frequency was changed for customers who started free-trial
& changed their billing frequency.

This commit sets those fields correctly leading to create only one
invoice.
2024-03-01 10:47:55 -08:00
Lauryn Menard 26e305e27d remote-support: Show remote realm matches if deactivated.
As a follow-up to commit d66b7ad853, where we send internal emails
when an active paid plan is on a locally deleted remote realm, we
need search queries in the remote support view to return results
for these deactivated remote realms, instead of excluding them.
2024-03-01 09:30:31 -08:00
Anders Kaseorg 82a9fd927b ruff: Fix E226 Missing whitespace around arithmetic operator.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-03-01 09:30:04 -08:00
Anders Kaseorg 570f3dd447 python: Reformat with Ruff formatter.
https://docs.astral.sh/ruff/formatter/

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-02-29 17:07:16 -08:00
Prakhar Pratyush 5d58a39087 stripe: Use a common email template for internal billing notices.
This is a prep commit which replaces the 'invoice_overdue'
and 'reminder_to_review_plan' email templates with
'internal_billing_notice'.

This will help us to use the same template as we plan to
send an email to sales when a remote realm with paid plan
attached is locally deleted.
2024-02-29 12:50:23 -08:00
Lauryn Menard 8d60ca548b remote-support: Use remote server or realm UUID in support URL.
Because the remote support page now supports searching by UUID,
the support URL for remote billing entities, which is used for
sponsorship request emails and overdue invoice emails, can now
use the remote server or realm UUID.

Adds the remote realm UUID to the remote support view information.
2024-02-29 11:21:00 -08:00
Prakhar Pratyush 24f902f3c6 corporate: Fix next payment info on billing page for fixed-price plans.
Earlier, for fixed-price plans we were showing the generic
next payment info on billing page which stated that plan
will automatically renew on end_date. It is no longer correct
for fixed-price plans.

This commit fixes the next payment info for fixed-price plans.

When the next_billing_cycle is the end_date, we inform the customer
that their plan will end on end_date and zulip sales will contact
them a couple of month ago before the end_date for renewal.
2024-02-28 08:48:18 -08:00
Prakhar Pratyush 18d1924823 corporate: Fix 'renewal_amount' always a truthy value on billing page.
Earlier, the 'renewal_amount' context variable passed to the billing
template was always a non-empty string which is a truthy value.

The expected behavior is that 'renewal_amount' should be falsy value
if the renewal_cents is 0. This commit fixes the incorrect behavior
and returns None in this case.
2024-02-28 08:48:18 -08:00
Lauryn Menard 1914b881d9 remote-support: Add ability to search by billing user email. 2024-02-23 13:21:54 -08:00
Lauryn Menard 1f72ab5133 remote-support: Show active billing user emails. 2024-02-23 13:21:54 -08:00
Lauryn Menard e4e65074df remote-support: Add push notification status information.
Adds the information returned by get_push_status_for_remote_request
for remote billing users to the support page. Note that getting
the current push status data will result in some duplicate database
queries (getting customer, plan, current billed users, next billing
cycle) when generating the remote support view.
2024-02-23 09:08:21 -08:00
Lauryn Menard 423af9916a remote-support: Add specific class for support section headers. 2024-02-23 09:08:21 -08:00
Alya Abbott fce68e23a2 portico: Add Semsee case study. 2024-02-22 10:33:31 -08:00
Aman Agrawal bddb44eebf stripe: Disable free trial for legacy customer with ended plan.
This ensures that customer who had legacy plan any time in the past
cannot avail free trial.
2024-02-21 21:40:47 -08:00
Tim Abbott d0c276d863 corporate: Fix billing_session variable reuse confusion.
The previous logic incorrectly used the server-level number of users
even when a (presumably smaller) realm-level count was available.

Fixes a bug introduced in 2e1ed4431a.
2024-02-21 17:51:30 -08:00
Lauryn Menard 161125c9be remote-support: Get user data once from the database.
Instead of querying the database twice for the user counts and
for the currently used licenses, we know pass that information
to the function where we populate the plan data for the remote
server or realm.
2024-02-21 12:40:34 -08:00
Lauryn Menard 907c209296 remote-support: Add search results for remote server/realm uuid. 2024-02-21 12:40:34 -08:00
Lauryn Menard 1eca9b3f6e remote-support: Return server results for realms with host match.
Checks for remote realm hosts that match the search query. Adds a
database query to getting the initial search results.
2024-02-21 12:40:34 -08:00
Lauryn Menard aec02d18af remote-support: Improve getting remote realm/server data.
Only get the customer information once from the database.

Extract logic for any next plan (either scheduled or an offer) data
into a separate function and do not hit database twice when getting
the number of licenses at the next plan renewal.
2024-02-21 12:40:34 -08:00
Lauryn Menard 8189b94a70 remote-support: Improve search results in view code.
Removes prefetch of remote realms on the remote server query
because, by excluding the system bot realm when this is referenced,
we end up hitting the database again.

Strips whitespace from query string and makes a few small updates
for readability.
2024-02-21 12:40:34 -08:00
Mateusz Mandera ea863bab5b remote_billing: Remove code migrating Legacy plan from server to realms.
We no longer want to migrate Legacy plans from server to realms, since
Legacy plans are not really a thing in the original sense anymore, since
February 15th.

Now they're just a tool to give temporary extensions of access to the
push notification service for users, when needed. And as such, it makes
no sense to migrate like that.

The remaining code in this function is for migrating (any) plan from the
server object to the realm object, if the server has just a single
realm.
2024-02-21 11:10:45 -08:00
Prakhar Pratyush 2f8e4a71d6 corporate: Send a reminder email 2 months before the end date.
For fixed-price plans, we send a reminder email to
sales@zulip.com, 2 months before the end date, to review
the pricing and configure a new fixed-price plan accordingly.
2024-02-21 10:43:23 -08:00
Prakhar Pratyush 06e48fd553 corporate: Add end_date to fixed-price plans.
Earlier, we were not configuring the end_date while creating
a fixed-price plan which would result in the automatic renewal
of the plan at the end of billing cycle.

Our plan is to re-evaluate the pricing when we are close to the
end date, then:
1. Schedule a new fixed-price plan with the updated or same price.
2. or Don't do anything - the plan will end on the end_date.

Now, we add an end_date of 1 year from the start date when
creating a fixed-price plan.
2024-02-21 10:43:23 -08:00
Prakhar Pratyush 648d0286bc stripe: Extract repetitive write_to_audit_log code into a function.
This is a prep commit which extracts the write_to_audit_log
code block in `update_end_date_of_current_plan` into a
function named 'write_to_audit_log_plan_property_changed'.

This would be helpful to avoid repetition as we plan to
include another such block when the plan's property
reminder_to_review_plan_email_sent is changed.
2024-02-21 10:43:23 -08:00
Aman Agrawal ca96868b0e billing: Return login URL for AJAX request session timeout for servers. 2024-02-21 10:30:06 -08:00
Mateusz Mandera 834dbd552b remote_billing: Make handle_customer_migration_... more robust.
The logic in the case where there's only one realm and the function
tries to migrate the server's plan to it, had two main unhandled edge
cases that would throw exceptions:
1.
```
        remote_realm = RemoteRealm.objects.get(
            uuid=realm_uuids[0], plan_type=RemoteRealm.PLAN_TYPE_SELF_MANAGED
        )
```

This could throw an exception if the RemoteRealm exists, but has an
active e.g. Legacy plan. Then there'd be no object matching the
plan_type in the query, raising RemoteRealm.DoesNotExist.

2. If the RemoteRealm had e.g. a Legacy plan in the past, that's now
   expired, then it'd have a Customer object. Meaning that the attempt
   to move the server's customer to the realm:
   `server_plan.customer = remote_realm_customer`
   would trigger an IntegrityError since a RemoteRealm can't have two
   Customer objects.

In simple cases the situation in (2) can still be easily migrated, by
moving the plan from the server's customer to the realm's customer.
2024-02-20 16:02:03 -08:00
Mateusz Mandera 5e6f4faad2 test_remote_billing: Rename server_on_active_plan_error helper arg.
This is kind of too specific, allowing testing for only one single error
when accessing signed_auth_url. Instead, this should use a general
pattern, which will allow other tests to use this to assert other kinds
of error responses that may be returned.
2024-02-20 16:02:03 -08:00
Mateusz Mandera 1c61bb2fad remote_billing: Fix two tests that shouldn't be asserting in a loop.
It doesn't make sense to run a loop over "all" query results, when those
results are just two, and each of them has its own distinct asserts.

That for loop is there probably due to copying the structure of the
earlier test_transfer_legacy_plan_from_server_to_all_realms test, for
which the loop does make sense.
2024-02-20 16:02:03 -08:00
Lauryn Menard 68f02da82e installation-activity: Fix ARR value in total row.
In commit ff8552269d, when we moved the links to the first column
of the activity charts, we missed updating the column for the ARR
total.
2024-02-20 15:57:06 -08:00
Lauryn Menard b6d50c158d support: Add CSS grid for confirmation objects in query results. 2024-02-20 15:46:55 -08:00
Prakhar Pratyush a20ce627d9 stripe: Add a check to create invoices only for paid plans.
In 'invoice_plan()', the primary way to not create
an invoice for a plan is to not have any new ledger entry.

This commit adds a 'self.on_paid_plan()' check which is
an extra layer of defense to avoid creating any invoices
for customers not on paid plan. It saves a DB query too.
2024-02-20 08:25:49 -08:00
Mateusz Mandera be03dabf76 zilencer: Implement do_reactivate_remote_server utility function.
The inverse of do_deactivate_remote_server. It's just flipping the
.deactivated flag, but we also should have an AuditLog for these events.
2024-02-19 20:26:47 -08:00
Tim Abbott 2e1ed4431a corporate: Fix plan precedence issues with expired plans.
RemoteRealm customer takes precedence over RemoteServer
in general. But if an inactive plan is associated with
RemoteRealm and an active plan with RemoteServer, the
ACTIVE plan takes precendence.

Co-authored-by: Prakhar Pratyush <prakhar@zulip.com>
2024-02-19 17:58:49 -08:00
Anders Kaseorg a4938d3760 page_params: Parse page_params and state_data with Zod.
This establishes a runtime check that their types continue to reflect
reality going forward.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-02-17 00:02:38 -08:00
Alya Abbott 4f1659fe8f billing: Make push notifications error message more clear. 2024-02-16 16:01:53 -08:00
Aman Agrawal 176c15f74b billing: Redirect user to login page if session expires.
If user makes an AJAX request but their session is expired, we
redirect user to the login page.
2024-02-16 14:56:26 -08:00
Aman Agrawal 98868b7f4a billing: Take user to default page if next_page is invalid.
Here invalid is more or less uncommon since there are a lot of
next_page that are valid for a logged in user.
2024-02-16 14:56:26 -08:00
Lauryn Menard 0c17b90fc5 remote-support: Fix last push notification sent field.
Instead of displaying the end of the day interval for the latest
count stat update for push notifications forwarded on the bouncer,
we display the start of the day interval and format it as a date
instead of a date and time.
2024-02-16 14:49:34 -08:00
Lauryn Menard 05b5295f18 remote-activity: Fix broken support links.
In commit 230294c, the logic for creating the support links in the
remote activity chart were broken. Updates the constants so that
the links have the correct server information for the search query.
2024-02-16 14:49:34 -08:00
Prakhar Pratyush d05a3f0cb0 corporate: Send invoice overdue mail also when data never uploaded.
Earlier, during invoicing we used to send a mail to
sales@zulip.com when the last_audit_log_update was
at least one day ago.

There was an assertion that last_audit_log_update
can't be None which is incorrect as such customers exist.

This commit extends the behaviour to send an invoice
overdue email even if the data was never uploaded.
2024-02-16 14:48:31 -08:00
Lauryn Menard 9c7d0c6e5a remote-support: Note if emails were sent for remote server sponsorship.
In cases where a support admin approves a sponsorship for a remote
server without any billing users having been created, note that no
emails were sent in the success message on the support page.

We already do this for remote realms and should be an infrequent
case as ideally most remote servers will submit official
sponsorship requests.
2024-02-16 14:46:59 -08:00
Prakhar Pratyush 9648256c3c corporate: Fix next_invoice_date not updated on end_date extension.
Earlier, on extending end_date for legacy plans, next_invoice_date
was not extended which resulted in invoice_plan() run for such
legacy plans before the end_date.

The intended behaviour is that the legacy plan should be invoiced
only once on the end_date to downgrade or switch to a new tier.

This commit adds logic to update next_invoice_date when end_date
is extended via /support.
2024-02-16 08:30:17 -08:00
Lauryn Menard 230294cfb3 remote-activity: Add column for remote server or realm creation date.
Expands the main query for remote servers to get the audit log
event datetime for when the server was created/registered.

The remote realm object has a field for when the remote realm
was created on the remote server.
2024-02-15 10:29:12 -08:00
Alex Vandiver 7f46773ef1 tests: Clear in-memory Client caches before testing query counts.
This makes counts more apples-to-apples comparable when run
back-to-back.
2024-02-14 12:27:03 -08:00
Lauryn Menard b275e9c4d6 stripe: Add billing portal for customer name and address.
Adds a link on the upgrade and billing pages that opens a stripe
billing portal for the customer to update their name and address
that will appear on invoices and receipts.

On the billing page, updating the credit card information will
no longer update the customer billing address, since they can
now do this directly through the billing portal. To be consistent
with the credit card form on the upgrade page, we still require
inputting a billing address for the card.

Note that, once an invoice is paid/complete, then changes to the
customer's name and address will not be applied to those invoices.
2024-02-13 14:18:38 -08:00
Lauryn Menard 29e8a63a39 stripe: Add name to stripe customer with payment method.
Set the name on the stripe customer object to the name on the
credit card when one is initially attached to a customer.

Prep commit to adding a stripe billing portal so that billing
admins will be able to update both the name and address that
appears on their stripe generated invoices and receipts.
2024-02-13 14:18:38 -08:00
Aman Agrawal 8cba101e05 support: Add button to delete configured fixed next plan.
This will help us modify the configured plan if we need to.
2024-02-13 09:40:53 -08:00
Aman Agrawal 7b33d660eb billing: Rename self-managed to free. 2024-02-12 21:25:19 -08:00
Aman Agrawal 7ca85bed71 stripe: Fix $0 invoices being generated on upgrade.
Instead of charging the customer using the attached payment
method and then creating the invoice, we create an invoice and
force an immediate payment for the invoice via the attached
payment method.
2024-02-12 15:15:56 -08:00
Prakhar Pratyush a764f9ce25 corporate: Fix 'free_trial=True` for legacy customers during upgrade.
Earlier, we were not setting `free_trial` = False for legacy
customers in `do_upgrade`.

This lead to a bug where customers were upgraded even before the
payment was complete. 'process_initial_upgrade' was always getting
called instead of 'setup_upgrade_payment_intent_and_charge'.

This commit fixes the incorrect behaviour.
2024-02-12 09:06:06 -08:00
Prakhar Pratyush 2055dfa83e support: Add support to configure fixed-price plan with pay-by-invoice.
* Manually create & send invoice
* Configure a fixed-price plan with sent invoice-id.
* When customer pays, upgrade them to concerned plan.
2024-02-06 18:43:23 -08:00
Lauryn Menard 6818a09123 remote-support: Add server mobile users without a remote realm.
Adds a count for mobile users registered for the remote server
with a RemotePushDeviceToken that does not have an associated
remote realm, which was a recently added field.

If the remote server is pre-8.0 and does not have remote realms,
then only the total mobile user count is displayed, as the count
for uncatagorized mobile users would be equal to the total mobile
user count.
2024-02-06 18:09:45 -08:00
Lauryn Menard 4f04f9f1f0 remote-support: Show datetime of last mobile push forwarded count.
This is useful for support admin to know if push notifications for
the remote server or realm have recently been logged/counted.
2024-02-06 18:09:45 -08:00
Lauryn Menard 5479053a9f remote-support: Add server data for last audit log update time.
This is useful in the support view in case the audit log data is
stale and user counts are not updated for billing.

Also, renames formatting function for optional datetimes that is
used in the support and activity views/charts. And instead of
showing these datetime strings in the eastern US timezone, we
now show and label them as UTC.
2024-02-06 18:09:45 -08:00
Lauryn Menard cbb6d962d5 stripe: Add billing address collection to checkout session.
This will require customers to include an address when setting
up, or updating, the credit card information for their account.
The billing address for the card will also be saved as the
billing address for the stripe customer object.

The customer object billing address appears on the invoices
that are generated by stripe.
2024-02-05 10:16:04 -08:00
Aman Agrawal de33aa4b7b stripe: Add page to show list of past customer invoices. 2024-02-04 17:38:24 -08:00
Aman Agrawal 130aecbf9e stripe: Extract method to get org_name. 2024-02-01 09:29:08 -08:00
Lauryn Menard ff8552269d activity: Move links columns to be first in chart.
Moves links in installation and remote server activity charts
to be the first column.
2024-02-01 09:17:26 -08:00
Lauryn Menard afe0161bc2 remote-activity: Add column for rate paid on displayed plan.
Adds a column with the percentage rate that the remote server
or realm is paying on the displayed plan.

We display 0% for community plans that are 100% sponsored.
For legacy plans or plans with a scheduled downgrade, we
display a placeholder, "---". Otherwise, the value is
calculated from the CustomerPlan discount field.
2024-01-31 16:08:15 -08:00
Prakhar Pratyush 82da6abb24 corporate: Restrict free-trial when fixed-price plan is configured.
When fixed-price plan is configured:
* Don't show 'free-trial for 30 days' button on plans page.
* Remove free-trial related texts from /upgrade page.
2024-01-31 12:25:31 -08:00
Prakhar Pratyush c4b6cfe142 support: Fix next plan info missing for customers with no current plan.
Earlier, when a fixed-price plan for a customer with
no current plan was configured via /support, the next plan
info was missing on support page.

It was because we were considering next plan only if the
customer had a current plan.

This commit fixes the incorrect behaviour.
2024-01-31 12:25:31 -08:00
Alya Abbott ce7cf883e3 portico: Add GUT contact case study. 2024-01-30 13:46:47 -08:00
Prakhar Pratyush 26536a20ed support: Always show the option to update end_date on /support page.
If a plan is already on "basic" or "business" plan and wants to
switch to a fixed-price "basic" / "business" plan, then it is
necessary that the current plan should have an end date configured.

Earlier, we could update the end_date only if the current plan
already had an end_date set.

This commit makes it possible to always show the option to
set or update end_date.
2024-01-30 10:09:52 -08:00
Prakhar Pratyush 928de1a74b stripe: Set fixed price plan status within if statement.
While configuring fixed price offer via /support, the
status for fixed price plan & offer are not the same.

Updates the code to not set it initially & then overwrite,
instead set it while creating the plan / offer.
2024-01-30 10:09:16 -08:00
Lauryn Menard 12ec133224 templates: Move billing templates to /templates/corporate/billing. 2024-01-30 10:06:48 -08:00
Lauryn Menard 916892e437 templates: Move support templates to /templates/corporate/support.
Also, makes small updates to `next_plan_forms_support.html`.
Removes unneeded "btn" and "btn-default" classes, and updates
the placeholder text for the input as not marked for translation.
2024-01-30 10:06:48 -08:00
Lauryn Menard efd0d689fc templates: Move activity templates to /templates/corporate/activity.
Also, renames `ad_hoc_query.html` to `activity_table.html`,
`realm_summary_table.html` to `installation_activity_table.html`,
and `activity_details_template.html` to `activity.html`.

Removes the style attribute in the installation activity template
and uses a CSS class, "installation-activity-header", to center the
h3 and p tags instead. This removes an exception from the custom
lint check.
2024-01-30 10:06:48 -08:00
Lauryn Menard df2f4b6469 corporate: Move support and activity views to /corporate.
View functions in `analytics/views/support.py` are moved to
`corporate/views/support.py`.

Shared activity functions in `analytics/views/activity_common.py`
are moved to `corporate/lib/activity.py`, which was also renamed
from `corporate/lib/analytics.py`.
2024-01-30 10:06:48 -08:00
Tim Abbott ae52adb4ee lint: Fix errors caused by rebasing past black upgrade. 2024-01-29 11:31:04 -08:00
Prakhar Pratyush edec29e0b6 support: Add support to configure fixed_price plan. 2024-01-29 11:23:20 -08:00
Anders Kaseorg 93198a19ed requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-01-29 10:41:54 -08:00
Lauryn Menard bfd9eec4b3 activity: Add totals row as sticky footer to activity charts.
Updates the total row for the installation and remote activity
charts to be in the table footer. Makes the footer class sticky
to the bottom of the view so that it is always visible on the
chart.

Also, updates the installation activity column for revenue to
be formatted as a dollar string, since this formatting was being
applied in the updated total row.
2024-01-26 09:04:39 -08:00
Lauryn Menard dcae35196c remote-activity: Prefetch LicenseLedger entry for current plan.
To estimate the annual recurring revenue for remote server and
remote realm CustomerPlans, we prefetch the current license
ledger as part of the CustomerPlan query.

Also adds a select related to the remote realm audit log query
so that we don't go to the database for the remote realm ID.

With the test added in the previous commit, the query count for
the remote activity view goes from 27 to 11, as we are no longer
hitting the database multiple times for every current plan or
for every remote realm with audit log data.

Refactors get_customer_plan_renewal_amount so that a license
ledger is always passed and make_end_of_cycle_updates_if_needed
does not need to be called.
2024-01-25 15:35:44 -08:00
Lauryn Menard 897cf4a610 corporate: Update Customer model string method for remote realm case. 2024-01-25 12:50:28 -08:00
Prakhar Pratyush 3a6c98f6a9 stripe: Fix invoicing for new plans scheduled to upgrade.
Earlier, the next_invoicing_date and invoicing_status
for new plan weren't set correctly, resulting in the
scheduled switching of legacy plan to a new plan not
working as expected.

This commit fixes the incorrect behaviour.
2024-01-25 10:57:29 -08:00
Prakhar Pratyush 026eb37c28 stripe: Fix legacy plans not being invoiced.
Earlier, in 'migrate_customer_to_legacy_plan`, we set
'next_invoice_date' to None for legacy plans.
This will result in legacy plans not getting invoiced.
We need to invoice legacy plans on their end date to
either downgrade them or switch to a new plan.

We set next_invoice_date for legacy plans to end_date.
2024-01-25 10:57:29 -08:00
Tim Abbott cbb83b214f billing: Remove buggy i18n check. 2024-01-23 10:00:28 -08:00
Mateusz Mandera 4ccddda074 remote_billing: Pass language argument to send_email calls.
This was an oversight in the original implementation.
2024-01-22 18:31:06 -08:00
Lauryn Menard fca9ff1ae7 support: Show date for start of next billing cycle for current plan.
Instead of showing the next invoice date for the plan, show the
date for the next billing cycle start (e.g. the next plan renewal
charge date), except for plans currently on a free trial.

For plans on a free trial, the next plan renewal date will be when
the free trial ends, which is stored as the next invoice date on
the plan.
2024-01-22 10:09:00 -08:00
Lauryn Menard 76b26612a0 remote-activity: Get user counts for all servers and realms.
Instead of querying the database for every remote server and realm
in the remote activity chart, we now get the server and realm data
for the installation in two queries.
2024-01-19 11:46:13 -08:00
Lauryn Menard 536aef854c remote-activity: Display rows for remote realms.
Adds columns for remote realm ID, name and organization type. If
a remote server has remote realms attached that are not marked
as deactivated by the remote server, then there will be a row in
the chart for each remote realm (which duplicates some remote
server data).

Updates the plan data, revenue and user counts to be for the realm
if present and otherwise for the server.

Updates the user counts to be total users and guest users, instead
of non guest and guest users.

The total row for mobile data (users and pushes forwarded) sums
each remote server's data once, so while the column duplicates
data, the total row should be an accurate total for the installation.

Adds 5 queries to the remote activity page test. One is for the
additional query for the remote realm plans. The other four are
getting the remote realm object and then the user count data for
the two remote realms in the test.
2024-01-19 11:46:13 -08:00
Aman Agrawal 5a97a4d8dc stripe: End legacy plan on end_date. 2024-01-19 09:34:36 -08:00
Mateusz Mandera 649b4885e8 remote_billing: Add rate-limiting for confirmation email sending.
These should be rate-limited by both IP using our regular
sends_email_by_ip bucket as well as by server, using a new bucket
dedicated to this.
2024-01-18 12:06:16 -08:00
Alya Abbott 57b5be6c91 billing: Rename button to apply for sponsorship or discount.
It was not clear that the button could be used for discounts.
2024-01-18 08:33:08 -08:00
Alya Abbott 91ee0bf676 emails: Remove organization type from sponsorship request emails.
This makes the subject nicer for replying to these emails.
2024-01-17 16:44:06 -08:00
Aman Agrawal 884c44d3cb stripe: Avoid redirects from `event_status` to `event_status/`. 2024-01-17 12:46:44 -08:00
Mateusz Mandera fc247cba3f remote_billing: Fix /self-hosted-billing/ handling for desktop app.
When you click "Plan management", the desktop app opens
/self-hosted-billing/ in your browser immediately. So that works badly
if you're already logged into another account in the browser, since that
session will be used and it may be for a different user account than in
the desktop app, causing unintended behavior.

The solution is to replace the on click behavior for "Plan management"
in the desktop app case, to instead make a request to a new endpoint
/json/self-hosted-billing, which provides the billing access url in a
json response. The desktop app takes that URL and window.open()s it (in
the browser). And so a remote billing session for the intended user will
be obtained.
2024-01-15 16:50:48 -08:00
Lauryn Menard 7e1d0adb6e support-tests: Add coverage for discount actions when upgrade scheduled. 2024-01-15 03:10:04 -08:00
Lauryn Menard a5f2883a75 remote-support: Add ability to set a required plan tier for customer. 2024-01-15 03:10:04 -08:00
Lauryn Menard fbe7145231 corporate: Check plan tier for new plan discount calculations.
Now that a customer discount may require a particular plan tier to
be applied, update the billing code to check the plan tier when
getting the customer default_discount field/information for a new
plan.

For billing schedule changes and displaying billing information for
current plans, we explicitly use the discount set on the current,
active plan and do not check the customer object for these actions.
2024-01-15 03:10:04 -08:00
Lauryn Menard 7542a676c7 corporate: Add required_plan_tier field to Customer model.
This will be used to set a required plan tier value to be used with
the default discount that is set on the Customer object or with a
fixed price set on a CustomerPlan object.
2024-01-15 03:10:04 -08:00
Lauryn Menard f27b6d896a audit-logs: Add generic property changed for Customer and CustomerPlan.
Change audit logs for changing a Customer or CustomerPlan property
to use a generic event type and specify the property name in the
extra data.
2024-01-15 03:10:04 -08:00
Prakhar Pratyush 89f9e097db stripe: Add cron-based plan invoicing to remote server billing system. 2024-01-14 15:47:55 -08:00
Prakhar Pratyush 11908c4c2e stripe: Add cron-based plan invoicing to remote realm billing system. 2024-01-14 15:47:55 -08:00
Prakhar Pratyush 6088186223 stripe: Add flat discount while invoicing self-hosted realm/server.
This prep commit adds logic to calculate discount based
on flat_discount and flat_discounted_months. Creates
a stripe invoice item for the discount.

This will be used by remote realm/server billing system
while invoicing via cron job.
2024-01-14 15:47:55 -08:00
Prakhar Pratyush 4c47b9ef2b test_stripe: Rename 'realm_user_count' to 'server_user_count'.
This commit renames the variable 'realm_user_count' to
'server_user_count' in 'test_upgrade_user_to_monthly_basic_plan'.

The variable was incorrectly named earlier as it stores
the user count of the whole server.
2024-01-14 15:47:55 -08:00
Mateusz Mandera 3a12e41c35 remote_billing: Fix handle_customer_migration_from_server_to_realms.
This was a bug from 4715a058b0 where this
was just incorrectly called. get_realms_info_for_push_bouncer() is a
function meant to be called on a self-hosted server - and this
handle_... call happens on the bouncer. Therefore this returns all
zulipchat realms in product.

With the way, handle_... is being called right now, there's no reason
for it to have an argument for passing a list of realms. It should just
fetch the relevant RemoteRealm entries  by itself, given the server arg.
2024-01-12 15:28:41 -08:00
Aman Agrawal 02a4b3ce62 stripe: Add some test for self hosted free trial.
Cannot test invoice until it is implemented.
2024-01-12 08:50:51 -08:00
Aman Agrawal f285de39e9 populate_realms: Allow billing page access without stripe_api_key. 2024-01-12 08:47:43 -08:00
Mateusz Mandera a5538636f0 remote_billing: Remove stale comment about ToS in the server flow.
That comment is stale, now we just do a proper tos_consent check, based
on the last. version the user consented to.
2024-01-12 08:39:48 -08:00
Mateusz Mandera 63254f18ec remote_billing: Handle two confirmation links for same user correctly.
The bug was that a user could do the first part of the flow twice,
receiving two confirmation links, before finishing signup. Then they
could use the first link, followed by the second, which would case an
IntegrityError due to trying to create the RemoteRealmBillingUser
for the second time.

When the second link gets clicked, we should just transparently redirect
the user further into the flow so that they can proceed.
2024-01-12 08:39:48 -08:00
Prakhar Pratyush 976354dbda stripe: Fix the invoice item description for flat discounts.
Earlier, in process_initial_upgrade, the flat_discount value
wasn't converted into dollars when specified in the invoice
description, resulting in showing the incorrect value of $2000
as a discount.

This commit converts the value in cents to dollars and adds tests
to verify the invoice generated.
2024-01-11 12:35:58 -08:00