2019-09-06 11:07:46 +03:00
|
|
|
Array.prototype.forEach.call(document.querySelectorAll('form[data-members-form]'), function (form){
|
2019-09-03 07:39:00 +03:00
|
|
|
form.addEventListener('submit', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
form.classList.remove('success', 'invalid', 'error');
|
2019-09-06 11:07:46 +03:00
|
|
|
var input = event.target.querySelector('input[data-members-email]');
|
|
|
|
var email = input.value;
|
2019-09-03 07:39:00 +03:00
|
|
|
|
|
|
|
if (!email.includes('@')) {
|
|
|
|
form.classList.add('invalid')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:29:56 +03:00
|
|
|
form.classList.add('loading');
|
2019-09-06 10:14:21 +03:00
|
|
|
fetch('{{admin-url}}/api/canary/members/send-magic-link/', {
|
2019-09-03 07:39:00 +03:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2019-09-06 11:07:46 +03:00
|
|
|
email: email
|
2019-09-03 07:39:00 +03:00
|
|
|
})
|
|
|
|
}).then(function (res) {
|
2019-09-09 07:29:56 +03:00
|
|
|
form.classList.remove('loading');
|
2019-09-03 07:39:00 +03:00
|
|
|
if (res.ok) {
|
|
|
|
form.classList.add('success')
|
|
|
|
} else {
|
|
|
|
form.classList.add('error')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
|
|
|
|
2019-09-06 11:07:46 +03:00
|
|
|
Array.prototype.forEach.call(document.querySelectorAll('[data-members-subscription]'), function (button) {
|
2019-09-06 10:14:21 +03:00
|
|
|
button.addEventListener('click', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2019-09-06 11:07:46 +03:00
|
|
|
var plan = event.target.dataset.membersSubscriptionPlan;
|
2019-09-06 10:14:21 +03:00
|
|
|
|
2019-09-09 07:29:56 +03:00
|
|
|
button.classList.add('loading');
|
2019-09-06 10:14:21 +03:00
|
|
|
fetch('{{blog-url}}/members/ssr', {
|
|
|
|
credentials: 'same-origin'
|
|
|
|
}).then(function (res) {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error('Could not get identity token');
|
|
|
|
}
|
|
|
|
return res.text();
|
|
|
|
}).then(function (identity) {
|
|
|
|
return fetch('{{admin-url}}/api/canary/members/create-stripe-checkout-session/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2019-09-06 11:07:46 +03:00
|
|
|
plan: plan,
|
|
|
|
identity: identity
|
2019-09-06 10:14:21 +03:00
|
|
|
})
|
|
|
|
}).then(function (res) {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error('Could not create stripe checkout session');
|
|
|
|
}
|
|
|
|
return res.json();
|
|
|
|
});
|
2019-09-06 11:07:46 +03:00
|
|
|
}).then(function (result) {
|
|
|
|
var stripe = Stripe(result.publicKey);
|
2019-09-06 10:14:21 +03:00
|
|
|
return stripe.redirectToCheckout({
|
2019-09-06 11:07:46 +03:00
|
|
|
sessionId: result.sessionId
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
2019-09-09 07:29:56 +03:00
|
|
|
}, function (_err) {
|
|
|
|
console.error(_err);
|
|
|
|
button.classList.remove('loading');
|
|
|
|
button.classList.add('error');
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-09-03 07:39:00 +03:00
|
|
|
|
2019-09-06 11:07:46 +03:00
|
|
|
var magicLinkRegEx = /token=([a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+)/;
|
|
|
|
var match = location.search.match(magicLinkRegEx);
|
|
|
|
var isMagicLink = !!match
|
|
|
|
var token = match && match[1];
|
2019-09-03 07:39:00 +03:00
|
|
|
|
|
|
|
if (isMagicLink) {
|
|
|
|
fetch('{{blog-url}}/members/ssr', {
|
|
|
|
method: 'POST',
|
|
|
|
body: token
|
|
|
|
}).then(function (res) {
|
|
|
|
if (res.ok) {
|
|
|
|
window.location.search = window.location.search.replace(magicLinkRegEx, '');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|