test = window.nice.url('test');
jQuery(function($) {
subscription_on_page = $('.try_subscription').length;
subscription = window.nice.url('subscription');
len = $(`.price_template[num="${subscription}"]`).length;
if(subscription_on_page && subscription) {
th = $(`.try_subscription[p='${subscription}']:eq(0)`);
if(!th.length) {
$('body').append(`
`);
th = $(`.try_subscription[p='${subscription}']:eq(0)`);
}
try_subscription(th);
}
});
var style = {
base: {
color: "#32325d",
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#aab7c4"
}
},
invalid: {
color: "#fa755a",
iconColor: "#fa755a"
}
}
window.stripe_style = style;
cardElement = '';
function mount_card() {
var elements = stripe.elements();
var cardElement = elements.create("card", { style: window.stripe_style });
cardElement.mount("#card-element");
cardElement.on('change', showCardError);
}
len = $('#card-element').length;
if(len) mount_card();
function showCardError(event) {
loader_remove();
let displayError = document.getElementById('card-errors');
if(event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
}
function showCardError2(event) {
loader_remove();
hide_errors();
if(event.error) {
error = event.error.message;
$('.wan_cheng').closest('.button_block').before(""+error+"
");
}
}
function check_cards_table() {
len = $('.card_table').length;
//console.log(len);
if(len) {
tr = $('.card_table .card_row').length;
//console.log(tr);
if(tr) {
$('.no_cards_message').hide();
$('.card_table').show();
}
else {
$('.card_table').hide();
$('.no_cards_message').show();
}
}
}
function try_subscription(th, info = '') {
test = window.nice.url('test');
p = th.attr('p');
no_esc = th.attr('no_esc');
next = th.attr('next');
next = myvar(next);
if(next == 'this') {
//console.log(next);
next = window.location.pathname;
}
else if(next) {
first_litera = next.substring(0, 1);
if(first_litera == '/') {
next2 = next;
next2 = next.substring(1);
next2 = encodeURIComponent(next2);
next = '/' + next2;
}
}
if(next && window.nice.url('next')) {
next = window.nice.url('next')
window.next_link = next
}
if(!info) {
s = th.attr('s');
s = myvar(s);
info = {s};
attributes = '';
if(typeof th.get(0) != 'undefined')
attributes = th.get(0).attributes;
if(attributes)
$.each(attributes, function(i, attrib){
name = attrib.name;
value = attrib.value;
info[name] = value;
});
if(next) {
info.next = next;
window.next_link = next;
}
inp = $('.donate_input input');
if(inp.length) info.amount = inp.val();
}
//console.log(info);
inf = info;
$.post(window.homepage + '/ajax/subscription/sf', {p, s, test, inf}).done(function(data) {
loader_remove();
if(getpos(data,'error') && getpos(data,'{') && getpos(data, "error:")) {
data = JSON.parse(data);
error = data.error;
showbox('',error);
return;
}
classes = ''
if(no_esc) classes += 'no_esc'
//console.log('classes', classes)
showbox('', data, 'fullwidth no_headline payment_window ' + classes);
if(no_esc) {
$('.steps_menu .post_tab[type="step_1"]').removeClass('post_tab')
}
chosen_p = $('.chosen_p').text();
if(chosen_p) {
$('.post_tab[num="${chosen_p}"]').trigger('click');
}
p = $('.p_type.active').attr('num');
loading = 0;
len = $('#card-element').length;
if(typeof stripe == 'undefined') return;
if(!len) return;
var elements = stripe.elements();
var cardElement = elements.create("card", {style: window.stripe_style});
cardElement.mount("#card-element");
cardElement.on('change', showCardError2);
$(document).on('click', '.wan_cheng', function(event){
$('#payment-form button').trigger('click');
});
form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
if(loading) return;
loading = 1;
//$(document).on('click', '.wan_cheng', function(event){
th = $('.wan_cheng');
th.append(" " + fa_spinner);
o = $('.step_3').attr('o');
hide_errors();
existing = $('.existing_input').val();
card = $('select[name="cards"]').val();
//console.log(existing);
//console.log(card);
if(!existing) {
//console.log('init stripe');
stripe.createToken(cardElement).then(function(result) {
error = result.error;
//console.log('try to pay with a new card');
try_to_pay(error, result.token);
}).catch((error) => {
//console.log(error);
loader_remove(); loading = 0;
});
}
else {
//console.log('pay with existing method');
try_to_pay('','');
}
});
});
}
$(document).on('click', '.wallet_pay', function(){
th = $(this);
len = th.find('.fa-spin').length;
if(len) return;
hide_errors();
th.append(" " + fa_spinner);
info = $('.th_info').text();
o = $('.step_3').attr('o');
p = $('.step_3').attr('p');
a = $('.to_pay[num="'+ p + '"]').text();
postto('/api/wallet/pay', {o,a}, 'wallet_pay_');
});
window.wallet_pay_ = function(data){
//loader_remove();
$('.wallet_pay').before(data);
}
$(document).on('click', '.post_tab[type="step_1"]', function(){
chosen_p = $('.chosen_p').length;
no_esc = $('.my_box.no_esc').length
//console.log(no_esc)
if(no_esc) return;
if(chosen_p) del_mybox();
});
$(document).on('click', '.paypal_scheduled', function(){
th = $(this);
p = th.attr('p');
d = th.attr('d');
canceling = th.attr('canceling');
next = th.attr('next');
gift_sub = th.attr('gift_sub');
if(!def(gift_sub)) gift_sub = '';
if(!def(canceling)) canceling = '';
if(gift_sub == '0') gift_sub = '';
plan = th.attr('plan');
th.append("");
th.removeClass('paypal_scheduled');
cancel = $("meta[property='og:url']").attr('content');
return_url = '';
test = window.nice.url('test');
len = $('.return_url').length;
if(len) return_url = $('.return_url').text();
if(next) return_url = next
if(!return_url && window.next_link) return_url = window.next_link;
if(return_url && !getpos(return_url, 'https')) return_url = window.homepage + return_url;
len = $('.cancel_scheduled').length;
if(len) cancel = $('.cancel_scheduled').last().text();
o = $('.step_3').attr('o');
prodvar = $('.step_3').attr('prodvar');
email = $('.my_box [name="friend_email"]').val();
pass = $('.my_box [name="friend_password"]').val();
//console.log(return_url);
//console.log(cancel);
//if(test) return;
if(gift_sub) {
email_check = check_mail2(email);
val = '';
ps_length = pass.length;
if(!email || !pass) val = 'guest_email_pass';
else if(!email_check) val = 'email_incorrect';
else if(ps_length < 7) val = 'ps_length';
if(val) {
$.post(window.homepage + "/do/any/tran", {val}).done(function(data) {
show_msg(data);
loader_remove();
$('.scheduled_button').addClass('paypal_scheduled');
});
return;
}
}
$.post(window.homepage + '/do/any/paypal_scheduled', {o, p, d, prodvar, plan, cancel, return_url, test, email, pass, canceling}).done(function(data) {
data = JSON.parse(data);
error = data.error;
result = data.result;
logs = data.logs;
if(error) {
show_msg(error);
loader_remove();
$('.scheduled_button').addClass('paypal_scheduled');
}
else if(result) {
$('body').append(result);
}
});
});
$(document).on('click', '.you_already_subscribed', function(){
postto('view/already_subscribed');
});
$(document).on('click', '.remove_payment_method', function(){
th = $(this);
i = th.attr('i');
th.append(' ' + fa_spinner);
$.post(window.homepage + '/ajax/stripe/rem_card', {i}).done(function(data){
data = JSON.parse(data);
error = data.error;
table = data.table;
success_message = data.success_message;
error_message = data.error_message;
if(!error) {
short_mes(success_message);
$('.card_table tbody').html(table);
}
else {
showbox(error_message, error);
}
});
});
$(document).on('click', '.make_card_default', function(){
th = $(this);
i = th.attr('i');
th.append(' ' + fa_spinner);
$.post(window.homepage + '/ajax/stripe/make_def', {i}).done(function(data) {
data = JSON.parse(data);
error = data.error;
table = data.table;
error_message = data.error_message;
success_message = data.success_message;
if(!error) {
short_mes(success_message);
$('.card_table tbody').html(table);
}
else {
showbox(error_message, error);
}
});
});
$(document).on('click', '.existing_card', function(){
th = $(this);
th.hide();
$('.card_container').hide();
$('.another_card').show();
$('.existing_cards').show();
$('.existing_input').val('1');
hide_errors();
});
$(document).on('click', '.another_card', function(){
th = $(this);
th.hide();
hide_errors();
$('.card_container').show();
$('.existing_card').show();
$('.existing_cards').hide();
$('.existing_input').val('');
});
$(document).on('click', '.add_pay_method', function(){
$.post(window.homepage + '/ajax/stripe/add_pay', {}).done(function(data) {
if(getpos(data,'error') && !getpos(data,'card-errors')) {
data = JSON.parse(data);
error = data.error;
showbox('',error);
return;
}
showme(data);
elements = stripe.elements();
cardElement = elements.create("card", { style: window.stripe_style });
cardElement.mount("#card-element");
cardElement.on('change', showCardError);
cardElement.on('ready', function(){
$('.button_block').show(300);
loader_remove();
});
working = '';
form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
$('.save_card_but').append(" " + fa_spinner);
if(working) {
loader_remove();
return;
}
working = 1;
stripe.createToken(cardElement).then(function(result) {
working = 0;
if (result.error) {
errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
loader_remove();
} else {
make_default = $('input[name="make_default"]').val();
$.post(window.homepage + '/ajax/stripe/savec', {make_default: make_default, info: result.token}).done(function(data) {
data = JSON.parse(data);
error = data.error;
message = data.message;
mybox = data.mybox;
success_message = data.success_message;
loader_remove();
if(error) {
$('#card-errors').html(error);
}
else {
table = data.table;
$('.card_table tbody').html(table);
short_mes(success_message);
del_mybox();
}
});
}
});
});
});
});
$(document).on('click', '.to_step_3', function() {
th = $(this);
o = $('.step_3').attr('o');
p = th.attr('num');
if(p) {
$('.p_type[num="'+p+'"]').addClass('active');
}
if(typeof o == 'undefined') {
cancel_url = '';
len = $('.cancel_scheduled').length;
if(len) cancel_url = $('.cancel_scheduled').last().text();
info = $('.th_info').text();
$.post(window.homepage + '/ajax/invoice/c_i', {test, o, p, cancel_url, info}).done(function(data) {
data = JSON.parse(data);
o = data.o;
payp = data.paypal;
info = data.info;
error = data.error;
success = data.success;
if(success) {
$('.my_box').html(success);
return;
}
if(error) showbox('', error);
else {
only_step_2 = $('.step_3').attr('only_step_2');
if(!def(only_step_2)) only_step_2 = '';
if(only_step_2) {
current_link = window.location.href;
if(window.next_link) current_link = window.next_link;
if(getpos(current_link, '?')) current_link += '&'; else current_link += '?';
current_link += 'signed_up=1';
window.location.href = current_link;
}
}
//console.log(payp);
if(payp) {
$('.my_custom_form .paypal_form').html(payp);
}
if(o) $('.step_3').attr('o', o);
loader_remove();
});
}
});
function change_number_of_items() {
inp = $('input[name="number_of_items"]');
myinp = $('.my_box input[name="number_of_items"]');
if(myinp.length) inp = myinp;
number_of_items = inp.val();
val = number_of_items;
info = $('.th_info').text();
val = parseInt(val);
o = $('.step_3').attr('o');
p = $('.number_of_items_element').attr('pp');
window.p = p;
$.post('/ajax/invoice/number', {o, val, p, info}, function(data){
data = JSON.parse(data);
total = data.total;
error = data.error;
val = data.val;
table = data.table;
next_payment_on = data.next_payment_on;
if(error) show_msg(error);
if(total) {
$('.total_price_number').text(total);
$('.next_payment_on_text').text(next_payment_on);
len = $('.my_box .number_of_items_element').length;
if(len && table) {
$('.number_of_items_element').html(table);
}
p = window.p;
if(p) {
$(`.try_subscription[p="${p}"]`).attr('numofi', val);
}
}
//showme(data);
});
//total = total.toFixed(2)
}
$(document).on('click', '.number_of_items_field .select_list_option', function(){
setTimeout(() => {
change_number_of_items();
}, 100);
});
$(document).on('click', '.try_subscription', function(event){
th = $(this);
try_subscription(th);
});
function try_to_pay(error, token) {
o = $('.step_3').attr('o');
existing = $('.existing_input').val();
card = $('select[name="cards"]').val();
if(existing && card == '-') {
select_a_card_phrase = $('.select_a_card_phrase').text();
error = select_a_card_phrase;
}
//console.log(error);
//console.log(existing, card);
if(typeof error == 'object') error = error.message ? error.message : '';
nice = new nice_dd89();
test = nice.url('test');
if(error) {
loading = 0;
loader_remove();
hide_errors();
$('.wan_cheng').closest('.button_block').before(""+error+"
");
//errorElement = document.getElementById('card-errors');
//errorElement.textContent = error;
}
//return;
if(!error) {
go = window.homepage + '/api/stripe/new_s';
$.post(go, {f: 'new_s', card: card, existing: existing, t: token, o: o, test:test}).done(function(data) {
loader_remove();
loading = 0;
data = JSON.parse(data);
error = data.error;
subscription = data.val;
value = data.value;
email = data.email;
query = data.query;
window.subscription = subscription;
//console.log(data);
if(!error) {
ev = '/sub_charge_' + subscription + '_new_subscription_' + value + '_USD';
next = '';
next_link = window.next_link;
if (next_link) {
next_link = encodeURIComponent(next_link);
next = '&next=' + next_link;
}
thankyou_link = window.homepage + ev + '?' + query + next;
window.location.href = thankyou_link;
return;
}
else {
$('.wan_cheng').closest('.button_block').before(error);
}
});
}
}