2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
import * as loading from "../loading";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "../page_params";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
export function create_ajax_request(
|
|
|
|
url,
|
|
|
|
form_name,
|
|
|
|
stripe_token = null,
|
2020-12-30 19:00:49 +01:00
|
|
|
ignored_inputs = [],
|
2020-12-10 18:15:09 +01:00
|
|
|
redirect_to = "/billing",
|
|
|
|
type = "POST",
|
|
|
|
) {
|
2021-02-03 23:23:32 +01:00
|
|
|
const form = $(`#${CSS.escape(form_name)}-form`);
|
|
|
|
const form_loading_indicator = `#${CSS.escape(form_name)}_loading_indicator`;
|
|
|
|
const form_input_section = `#${CSS.escape(form_name)}-input-section`;
|
|
|
|
const form_success = `#${CSS.escape(form_name)}-success`;
|
|
|
|
const form_error = `#${CSS.escape(form_name)}-error`;
|
|
|
|
const form_loading = `#${CSS.escape(form_name)}-loading`;
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
const zulip_limited_section = "#zulip-limited-section";
|
|
|
|
const free_trial_alert_message = "#free-trial-alert-message";
|
2020-05-22 15:42:46 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
loading.make_indicator($(form_loading_indicator), {
|
|
|
|
text: "Processing ...",
|
|
|
|
abs_positioned: true,
|
|
|
|
});
|
2019-01-02 09:12:19 +01:00
|
|
|
$(form_input_section).hide();
|
|
|
|
$(form_error).hide();
|
|
|
|
$(form_loading).show();
|
2020-05-22 15:42:46 +02:00
|
|
|
$(zulip_limited_section).hide();
|
|
|
|
$(free_trial_alert_message).hide();
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {};
|
2019-01-02 09:12:19 +01:00
|
|
|
if (stripe_token) {
|
2021-04-09 12:43:44 +02:00
|
|
|
data.stripe_token = stripe_token.id;
|
2019-01-02 09:12:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const item of form.serializeArray()) {
|
2020-12-30 19:00:49 +01:00
|
|
|
if (ignored_inputs.includes(item.name)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-09 12:43:44 +02:00
|
|
|
data[item.name] = item.value;
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2020-12-10 18:15:09 +01:00
|
|
|
$.ajax({
|
|
|
|
type,
|
2020-07-20 22:18:43 +02:00
|
|
|
url,
|
|
|
|
data,
|
|
|
|
success() {
|
2019-01-02 09:12:19 +01:00
|
|
|
$(form_loading).hide();
|
|
|
|
$(form_error).hide();
|
|
|
|
$(form_success).show();
|
2020-02-08 04:04:36 +01:00
|
|
|
if (["autopay", "invoice"].includes(form_name)) {
|
2019-01-09 09:26:15 +01:00
|
|
|
if ("pushState" in history) {
|
|
|
|
history.pushState("", document.title, location.pathname + location.search);
|
|
|
|
} else {
|
|
|
|
location.hash = "";
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 12:24:32 +02:00
|
|
|
window.location.replace(redirect_to);
|
2019-01-02 09:12:19 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2019-01-02 09:12:19 +01:00
|
|
|
$(form_loading).hide();
|
|
|
|
$(form_error).show().text(JSON.parse(xhr.responseText).msg);
|
|
|
|
$(form_input_section).show();
|
2020-05-22 15:42:46 +02:00
|
|
|
$(zulip_limited_section).show();
|
|
|
|
$(free_trial_alert_message).show();
|
2019-01-02 09:12:19 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function format_money(cents) {
|
2019-01-02 09:12:19 +01:00
|
|
|
// allow for small floating point errors
|
|
|
|
cents = Math.ceil(cents - 0.001);
|
2019-11-02 00:06:25 +01:00
|
|
|
let precision;
|
2019-01-02 09:12:19 +01:00
|
|
|
if (cents % 100 === 0) {
|
|
|
|
precision = 0;
|
|
|
|
} else {
|
|
|
|
precision = 2;
|
|
|
|
}
|
|
|
|
// TODO: Add commas for thousands, millions, etc.
|
|
|
|
return (cents / 100).toFixed(precision);
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function update_charged_amount(prices, schedule) {
|
|
|
|
$("#charged_amount").text(format_money(page_params.seat_count * prices[schedule]));
|
|
|
|
}
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function update_discount_details(organization_type) {
|
2020-06-09 12:24:32 +02:00
|
|
|
const discount_details = {
|
|
|
|
open_source: "Open source projects are eligible for fully sponsored (free) Zulip Standard.",
|
2020-07-15 00:34:28 +02:00
|
|
|
research:
|
|
|
|
"Academic research organizations are eligible for fully sponsored (free) Zulip Standard.",
|
2020-06-09 12:24:32 +02:00
|
|
|
non_profit: "Nonprofits are eligible for an 85%-100% discount.",
|
|
|
|
event: "Events are eligible for fully sponsored (free) Zulip Standard.",
|
|
|
|
education: "Education use is eligible for an 85%-100% discount.",
|
|
|
|
other: "Your organization might be eligible for a discount or sponsorship.",
|
|
|
|
};
|
|
|
|
$("#sponsorship-discount-details").text(discount_details[organization_type]);
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|
2020-06-09 12:24:32 +02:00
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function show_license_section(license) {
|
2019-01-02 09:12:19 +01:00
|
|
|
$("#license-automatic-section").hide();
|
|
|
|
$("#license-manual-section").hide();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#automatic_license_count").prop("disabled", true);
|
|
|
|
$("#manual_license_count").prop("disabled", true);
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
const section_id = `#license-${CSS.escape(license)}-section`;
|
2019-01-02 09:12:19 +01:00
|
|
|
$(section_id).show();
|
2021-02-03 23:23:32 +01:00
|
|
|
const input_id = `#${CSS.escape(license)}_license_count`;
|
2019-01-02 09:12:19 +01:00
|
|
|
$(input_id).prop("disabled", false);
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|
2019-01-02 09:12:19 +01:00
|
|
|
|
2020-10-07 11:47:20 +02:00
|
|
|
let current_page;
|
|
|
|
|
|
|
|
function handle_hashchange() {
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(current_page)}-tabs.nav a[href="${CSS.escape(location.hash)}"]`).tab("show");
|
2020-10-07 11:47:20 +02:00
|
|
|
$("html").scrollTop(0);
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function set_tab(page) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = location.hash;
|
2019-01-02 09:44:45 +01:00
|
|
|
if (hash) {
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(page)}-tabs.nav a[href="${CSS.escape(hash)}"]`).tab("show");
|
2020-07-15 01:29:15 +02:00
|
|
|
$("html").scrollTop(0);
|
2019-01-02 09:44:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(page)}-tabs.nav-tabs a`).on("click", function () {
|
2019-02-26 19:28:56 +01:00
|
|
|
location.hash = this.hash;
|
2019-01-09 11:09:15 +01:00
|
|
|
});
|
|
|
|
|
2020-10-07 11:47:20 +02:00
|
|
|
current_page = page;
|
|
|
|
window.addEventListener("hashchange", handle_hashchange);
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|
2019-01-02 09:44:45 +01:00
|
|
|
|
2021-02-28 01:28:31 +01:00
|
|
|
export function is_valid_input(elem) {
|
2019-01-29 18:28:50 +01:00
|
|
|
return elem[0].checkValidity();
|
2021-02-28 01:28:31 +01:00
|
|
|
}
|