Skip to content

Commit

Permalink
billing: Use form.serializeArray in create_ajax_request.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerkid authored and rishig committed Dec 30, 2018
1 parent b655cfe commit 9384383
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
37 changes: 27 additions & 10 deletions static/js/billing/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,42 @@ $(function () {
return val;
}

function is_in_array(value, array) {
return array.indexOf(value) > -1;
}

function create_ajax_request(url, form_name, stripe_token = null) {
var form = $("#" + form_name + "-form");
var form_loading_indicator = "#" + form_name + "_loading_indicator";
var form_input_section = "#" + form_name + "-input-section";
var form_success = "#" + form_name + "-success";
var form_error = "#" + form_name + "-error";
var form_loading = "#" + form_name + "-loading";

var numeric_inputs = ["licenses"];

loading.make_indicator($(form_loading_indicator),
{text: 'Processing ...', abs_positioned: true});
$(form_input_section).hide();
$(form_error).hide();
$(form_loading).show();

var license_management = get_form_input(form_name, "license_management", false);
var data = {};
if (stripe_token) {
data.stripe_token = JSON.stringify(stripe_token.id);
}

form.serializeArray().forEach(function (item) {
if (is_in_array(item.name, numeric_inputs)) {
data[item.name] = item.value;
} else {
data[item.name] = JSON.stringify(item.value);
}
});

$.post({
url: url,
data: {
stripe_token: JSON.stringify(stripe_token.id),
signed_seat_count: get_form_input(form_name, "signed_seat_count"),
salt: get_form_input(form_name, "salt"),
schedule: get_form_input(form_name, "schedule"),
license_management: JSON.stringify(license_management),
licenses: $("#" + license_management + "_license_count").val(),
billing_modality: get_form_input(form_name, "billing_modality"),
},
data: data,
success: function () {
$(form_loading).hide();
$(form_error).hide();
Expand Down Expand Up @@ -204,8 +215,14 @@ $(function () {
$("#license-manual-section").hide();
$("#license-mix-section").hide();

$("#automatic_license_count").prop('disabled', true);
$("#manual_license_count").prop('disabled', true);
$("#mix_license_count").prop('disabled', true);

var section_id = "#license-" + license + "-section";
$(section_id).show();
var input_id = "#" + license + "_license_count";
$(input_id).prop("disabled", false);
}

$('input[type=radio][name=license_management]').change(function () {
Expand Down
6 changes: 3 additions & 3 deletions templates/corporate/upgrade.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h3>{{ _("License management") }}</h3>
are added, and remove licenses not in use at the end of each billing
period.
</p>
<input type="hidden" id="automatic_license_count" value="{{ seat_count }}">
<input type="hidden" name="licenses" id="automatic_license_count" value="{{ seat_count }}">
</div>

<div id="license-manual-section">
Expand All @@ -116,7 +116,7 @@ <h3>{{ _("License management") }}</h3>
</p>

<h4>Number of licenses (minimum {{ seat_count }})</h4>
<input type="number" min="{{ seat_count }}" autocomplete="off" id="manual_license_count" required/><br>
<input type="number" name="licenses" min="{{ seat_count }}" autocomplete="off" id="manual_license_count" required/><br>
</div>

<div id="license-mix-section">
Expand All @@ -127,7 +127,7 @@ <h4>Number of licenses (minimum {{ seat_count }})</h4>
period.
</p>
<h4>Number of licenses (minimum {{ seat_count }})</h4>
<input type="number" min="{{ seat_count }}"autocomplete="off" id="mix_license_count" required/><br>
<input type="number" name="licenses" min="{{ seat_count }}"autocomplete="off" id="mix_license_count" required/><br>
</div>

<button id="add-card-button" class="stripe-button-el">
Expand Down

0 comments on commit 9384383

Please sign in to comment.