';
if (customerInfo.coupon)
message += '
Active coupon: ' + customerInfo.coupon + '
';
$('#subscription-status').html(message + '
Your next invoice is for $'
+ nextBillingAmount + ' on ' + periodEnd + '.
');
$('#payment-form').hide();
$('#unsubscribe-form').show();
$('#subscribe-form').hide();
// $('#faq-link').hide();
} else {
if (customerInfo.isActive) {
$('#subscription-status').html('Your subscription will expire on: '
+ periodEnd);
$('.scans-remaining').hide();
} else {
$('#subscription-status').html('You are not subscribed.');
if (onDevice())
$('.scans-remaining').show();
}
$('.plan-info-row').show();
$('#payment-form').hide();
$('#unsubscribe-form').hide();
$('#subscribe-form').show();
$('#faq-link').show();
}
}
}
$(function() {
const functions = getFunctions(app, "us-central1");
// connectFunctionsEmulator(functions, "127.0.0.1", 5001);
$('#payment-form').submit(function(e) {
showLoader();
var form = $(this);
var plan = $('#pb-plan-option').is(':checked') ? 'pb' : 'npm';
var coupon = $('#coupon-code').val();
// form.find('button').prop('disabled', true);
Stripe.card.createToken(form, function (status, token) {
if (token.error) {
hideLoader();
alert(token.error.message);
return;
}
// Use Firebase Functions to call the Cloud Function
const subscribeNewCustomer = httpsCallable(functions, 'subscribeNewCustomer');
console.log('Subscribing new customer with token:', token.id, 'plan:', plan, 'coupon:', coupon);
subscribeNewCustomer({
tokenId: token.id,
plan: plan,
coupon: coupon
}).then(function(response) {
hideLoader();
customerInfo = response.data;
updateView(customerInfo);
}).catch(function(error) {
hideLoader();
alert(error.message);
});
});
clearCardFormAfterSubmit();
return false;
});
$('#unsubscribe-form').submit(function(e) {
e.preventDefault();
showLoader();
const setSubscription = httpsCallable(functions, 'setSubscription');
setSubscription({ subscribed: false }).then(
function(result) {
hideLoader();
customerInfo = result.data;
updateView(customerInfo);
},
function(error) {
hideLoader();
alert(error.message);
}
);
return false;
});
$('#subscribe-form').submit(function(e) {
e.preventDefault();
showLoader();
const plan = $('#pb-plan-option').is(':checked') ? 'pb' : 'npm';
const setSubscription = httpsCallable(functions, 'setSubscription');
setSubscription({ subscribed: true, plan: plan }).then(
function(result) {
hideLoader();
customerInfo = result.data;
updateView(customerInfo);
},
function(error) {
hideLoader();
alert(error.message);
}
);
return false;
});
$('#update-card-form').submit(function(e) {
e.preventDefault();
showLoader();
var form = $(this);
// form.find('button').prop('disabled', true);
Stripe.card.createToken(form, function(status, token) {
if (token.error) {
hideLoader();
alert(token.error.message);
return;
}
const updateCustomer = httpsCallable(functions, 'updateCustomer');
updateCustomer({ tokenId: token.id }).then(
function(result) {
hideLoader();
customerInfo = result.data;
updateView(customerInfo);
},
function(error) {
hideLoader();
alert(error.message);
}
);
});
clearCardFormAfterSubmit();
return false;
});
$('.logout').click(function() {
auth.signOut().then(function() {
// Sign-out successful.
window.location.replace('login.html');
}).catch(function(error) {
// An error happened.
alert(error.message);
});
return false;
});
});
';
if (customerInfo.coupon)
message += 'Your next invoice is for $'
+ nextBillingAmount + ' on ' + periodEnd + '.
');
$('#payment-form').hide();
$('#unsubscribe-form').show();
$('#subscribe-form').hide();
// $('#faq-link').hide();
} else {
if (customerInfo.isActive) {
$('#subscription-status').html('Your subscription will expire on: '
+ periodEnd);
$('.scans-remaining').hide();
} else {
$('#subscription-status').html('You are not subscribed.');
if (onDevice())
$('.scans-remaining').show();
}
$('.plan-info-row').show();
$('#payment-form').hide();
$('#unsubscribe-form').hide();
$('#subscribe-form').show();
$('#faq-link').show();
}
}
}
async function updateForLoggedInUser() {
const user = auth.currentUser;
if (!user) {
document.querySelector('.error').innerHTML = 'User not logged in';
return;
}
try {
const userDoc = await getDoc(doc(db, 'users', user.uid));
if (!userDoc.exists()) {
signOut(auth).then(function() {
// Sign-out successful.
// window.location.replace('login.html');
}).catch(function(error) {
// An error happened.
alert(error.message);
});
} else {
const getPlans = httpsCallable(functions, 'getPlans');
const plansResult = await getPlans();
const getCustomerInfo = httpsCallable(functions, 'getCustomerInfo');
const customerInfoResult = await getCustomerInfo();
const customerInfo = customerInfoResult.data;
getIsNotProMerchant(user.uid).then(isNotProMerchant => {
const userData = userDoc.data();
document.querySelector('.user-info').innerHTML = 'You are ' + userData.email + '.';
const plans = plansResult.data;
document.querySelector('.npm-plan-label .cost').innerHTML = '$' + (plans.npm.amount / 100) + ' per month';
document.querySelector('.pb-plan-label .cost').innerHTML = '$' + (plans.pb.amount / 100) + ' per month';
if (typeof isNotProMerchant !== 'undefined' && isNotProMerchant !== null) {
if (isNotProMerchant) {
document.querySelector('.npm-plan').style.display = 'block';
document.querySelector('.pb-plan').style.display = 'none';
document.getElementById('pb-plan-option').checked = false;
document.getElementById('npm-plan-option').checked = true;
document.getElementById('pb-plan-option').style.display = 'none';
document.getElementById('npm-plan-option').style.display = 'none';
} else {
document.querySelector('.npm-plan').style.display = 'none';
document.querySelector('.pb-plan').style.display = 'block';
document.getElementById('pb-plan-option').checked = true;
document.getElementById('npm-plan-option').checked = false;
document.getElementById('pb-plan-option').style.display = 'none';
document.getElementById('npm-plan-option').style.display = 'none';
}
} else {
document.querySelector('.npm-plan').style.display = 'block';
document.querySelector('.pb-plan').style.display = 'block';
document.getElementById('pb-plan-option').checked = true;
document.getElementById('npm-plan-option').checked = false;
document.getElementById('pb-plan-option').style.display = 'block';
document.getElementById('npm-plan-option').style.display = 'block';
}
document.querySelector('.plan-info').style.display = 'block';
updateView(customerInfo);
});
}
} catch (error) {
document.querySelector('.error').innerHTML = error.message;
alert('Error: ' + error.message);
}
}
function onDevice() {
return (window.location.hash === '#ios' || window.location.hash === '#ios_ps' || window.location.hash === '#android');
}
function updateScansRemaining() {
var total = freeScans;
if (customerInfo && customerInfo.unconsumedScans)
total += customerInfo.unconsumedScans;
document.querySelector('.scans-remaining').innerHTML = 'Free scans remaining: ' + total;
}
function setScansRemaining(scans) {
freeScans = scans;
updateScansRemaining();
}
if (!onDevice()) {
getAuth().onAuthStateChanged((user) => {
if (user) {
document.querySelector('.logout').style.display = 'block';
updateForLoggedInUser();
} else {
window.location.replace('login.html');
}
});
}
if (onDevice()) {
// this will hide the subscribe button on the FAQ page (probably no longer works)
document.getElementById('faq-link').href = document.getElementById('faq-link').href + '#APP';
}