payment modifications

This commit is contained in:
2021-04-05 19:46:35 +02:00
parent 459295043c
commit f58e1bc40b
6 changed files with 148 additions and 32 deletions
+78 -28
View File
@@ -1,3 +1,6 @@
// dictionary: deveui as key, count as value
var abos_to_buy = {}
function validate(what, text) {
if (what == 'alias') {
var re = /^[a-zA-Z0-9 ]{1,25}$/;
@@ -5,7 +8,44 @@ function validate(what, text) {
return re.test(text);
}
function HandleAbocostResponse(data) {
var ordered = Object.keys(data).sort().reduce(
(obj, key) => {
obj[key] = data[key];
return obj;
},
{}
);
counter = 0;
total_amount = 0;
charge_data = '';
console.log("Response: ");
console.log(data);
abo_table = '<table class="table is-bordered is-fullwidth" >';
abo_table += '<tr><th class="is-2">Alias</th><th>verl&auml;ngern bis</th><th class="has-text-right">Betrag</th></tr>';
jQuery.each(ordered, function (index, val) {
counter += 1;
abo_table += '<tr><td>' + val[3] + '</td><td>' + moment(val[2], 'DD.MM.YYYY').add('years', val[0]).format('DD.MM.YYYY') + '</td><td class="has-text-right">' + (val[1] / 100).toFixed(2) + '</td></tr>';
total_amount += (val[1] / 100);
if (charge_data == '') {
charge_data = index + ":" + val[0] + ":" + val[1];
} else {
charge_data += "," + index + ":" + val[0] + ":" + val[1];
}
});
abo_table += '<tr><td>Total CHF</td><td></td><td class="has-text-right">' + (total_amount).toFixed(2) + '</td></tr>';
abo_table += "</table>";
if (counter > 0) {
$("#abos_verlaengern").html(abo_table);
console.log("charge_data: " + charge_data);
$("#charge_data").html(charge_data);
$("#cart").addClass("is-active");
}
}
$(document).ready(function () {
abos_to_buy = {};
$(".show-modal").click(function () {
$("#alias_exclamation").hide();
var alias = $(this).prev().html();
@@ -26,11 +66,18 @@ $(document).ready(function () {
});
$(".abo_plus").click(function () {
var deveui = $(this).prev().attr('id').replace("abo_add_years_", "");
console.log(deveui);
el = $(this).parent().find(".abo_add_years");
el_text = $(this).parent().parent().find(".abo_add_years_text");
counter = Number(el.html());
if (counter < 3) {
counter = counter + 1;
if (deveui in abos_to_buy) {
abos_to_buy[deveui] += 1;
} else {
abos_to_buy[deveui] = 1;
}
el.html(counter);
if (counter == 1) {
el_text.html("+" + counter + " Jahr");
@@ -38,15 +85,23 @@ $(document).ready(function () {
el_text.html("+" + counter + " Jahre");
}
}
console.log("abos_to_buy: %o", abos_to_buy);
});
$(".abo_minus").click(function () {
var deveui = $(this).prev().attr('id').replace("abo_", "");
el = $(this).parent().find(".abo_add_years");
el_text = $(this).parent().parent().find(".abo_add_years_text");
counter = Number(el.html());
if (counter > 0) {
counter = counter - 1;
el.html(counter);
if (deveui in abos_to_buy) {
abos_to_buy[deveui] -= 1;
if (abos_to_buy[deveui] == 0) {
delete abos_to_buy[deveui];
}
}
if (counter == 0) {
el_text.html("&nbsp;");
} else if (counter == 1) {
@@ -55,6 +110,8 @@ $(document).ready(function () {
el_text.html("+" + counter + " Jahre");
}
}
console.log("deveui: %s", deveui);
console.log("abos_to_buy: %o", abos_to_buy);
});
function add_years(dt, n) {
@@ -62,36 +119,29 @@ $(document).ready(function () {
}
$(".abo_pay").click(function () {
console.log(Object.keys(abos_to_buy).length);
if (Object.keys(abos_to_buy).length < 1) {
return;
}
loadStripeLibrary();
counter = 0;
charge_data = '';
abo_table = '<table class="table is-bordered is-fullwidth" >';
abo_table += '<tr><th class="is-2">Alias</th><th>verl&auml;ngern bis</th><th class="has-text-right">Betrag</th></tr>';
$(".tracker").each(function (index) {
this_count = Number($(this).find(".abo_add_years").html());
if (this_count > 0) {
counter += this_count;
paid_until = $(this).find(".paid_until").html();
if (moment(paid_until, 'DD.MM.YYYY') < moment()) {
this_date = moment().format('DD.MM.YYYY');
} else {
this_date = paid_until;
}
abo_table += '<tr><td>' + $(this).find('.alias').html() + '</td><td>' + moment(this_date, 'DD.MM.YYYY').add('years', this_count).format('DD.MM.YYYY') + '</td><td class="has-text-right">' + (this_count * 60).toFixed(2) + '</td></tr>';
if (charge_data == '') {
charge_data = $(this).find("div").first().attr('id') + ":" + this_count;
} else {
charge_data += "," + $(this).find("div").first().attr('id') + ":" + this_count;
}
$.ajax({
url: "abocost",
type: "POST", //send it through post method
dataType: "json",
data: JSON.stringify(abos_to_buy),
success: function(response) {
console.log("SUCCESS");
console.log(response);
HandleAbocostResponse(response);
},
error: function(xhr) {
console.log("ERROR");
console.log(xhr);
}
});
abo_table += '<tr><td>Total CHF</td><td></td><td class="has-text-right">' + (counter * 60).toFixed(2) + '</td></tr>';
abo_table += "</table>";
if (counter > 0) {
$("#abos_verlaengern").html(abo_table);
$("#charge_data").html(charge_data);
$("#cart").addClass("is-active");
}
});
$("#modal-close").click(function () {
@@ -157,7 +207,7 @@ function loadStripeLibrary() {
function SetupStripe() {
// Set your publishable key: remember to change this to your live publishable key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
stripe = Stripe('pk_live_51Icq29K2XyHQRTs2oNQMPT75m7J96ajcKDADEE8FCSA3WWD98je6IZgN0E7Q2t5Vccdu1ar5kQZUIjbPhRluTkS500rgdMr0dF');
stripe = Stripe('pk_test_51Icq29K2XyHQRTs2DeltUIWrbFb5evfJVGaQzMv4r50g8Q7HAUkSFr7BBGv0aP1damXIhM6fZ4Yf9Wz0qW6TpTVi00dWcW7J1O');
elements = stripe.elements({ locale: "de" });
// Set up Stripe.js and Elements to use in checkout form