peertube-plugin-ncd-sell-st.../client/pages/subscription.js

161 lines
6.7 KiB
JavaScript
Raw Normal View History

2023-12-13 00:31:35 +00:00
async function showPage({ rootEl, peertubeHelpers }) {
// Redirect to login page if not auth
if (!peertubeHelpers.isLoggedIn()) return (window.location.href = "/login");
const { translate } = peertubeHelpers;
const loading = await peertubeHelpers.translate("Loading");
rootEl.innerHTML = loading + "...";
// Get settings
const settings = await peertubeHelpers.getSettings();
const currency = settings['sell-currency'];
const description = await peertubeHelpers.markdownRenderer.enhancedMarkdownToHTML(settings["sell-description"]);
const plans = [];
2024-04-02 19:43:20 +00:00
for (let i = 1; i <= 5; i++) {
2023-12-13 00:31:35 +00:00
const name = settings["plan-" + i + "-name"];
const key = settings["plan-" + i + "-key"];
const storage = settings["plan-" + i + "-storage"];
const price = settings["plan-" + i + "-price"];
plans.push({
name: name,
key: key,
storage: storage,
price: price
});
}
// Fetch session id and subscribed plan (if any)
let session_id = null, sub_plan = null;
const response = await fetch(peertubeHelpers.getBaseRouterRoute() + "/get-session-id", {
method: "GET",
headers: peertubeHelpers.getAuthHeader(),
});
const data = await response.json();
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
// If have error
if (!data || !data.status || (data.status && data.status !== "success")) {
peertubeHelpers.notifier.error(data.message || "Unknown error");
2024-04-02 19:43:20 +00:00
} else {
2023-12-13 00:31:35 +00:00
session_id = data?.data?.session_id;
sub_plan = data?.data?.sub_plan;
}
rootEl.innerHTML = `
2023-12-13 00:42:42 +00:00
<div class="ncd-content text-center">
2024-04-02 19:58:58 +00:00
<h1>${await peertubeHelpers.translate("Choose your Storage Plan")}</h1>
<h5><i>To subscribe to a storage plan with cryptocurrency (BTC, ETH, XMR), <a href="/about/contact">contact us</a>.</i></h5>
2024-04-12 13:03:15 +00:00
<h6><i>Cryptocurrency prices are 10% higher due to transaction fees.</i></h6>
2024-04-02 19:58:58 +00:00
<!-- <p>${description.length ? description : await peertubeHelpers.translate("You want tu spport us ? Or need more space ? Your in the right place!")}</p> -->
<div class="mt-5" style="max-width: 90%; margin: 0 auto;">
2023-12-13 00:31:35 +00:00
<div class="row">
${(await Promise.all(plans.map(async (plan) =>
2024-04-02 19:43:20 +00:00
`<div class="col-sm-12 col-md-6 col-lg-4" style="margin-bottom: 1rem;">
2023-12-13 00:31:35 +00:00
<div class="card">
<div class="card-body">
2023-12-13 00:42:42 +00:00
<form method="POST" action="#" class="ncdSubscriptionForm">
2023-12-13 00:31:35 +00:00
<h3 class="card-title">${plan.name}</h3>
2023-12-13 15:34:57 +00:00
<h4>${currency}${plan.price} /${await peertubeHelpers.translate("month")}</h4>
2023-12-13 00:31:35 +00:00
<p class="card-text">${plan.storage} ${await peertubeHelpers.translate("GB Storage")}</p>
<input type="hidden" name="lookup_key" value="${plan.key}">
<button class="btn btn-primary" type="submit">${await peertubeHelpers.translate("Subscribe")}</button>
</form>
</div>
</div>
</div>
`))).join("")}
</div>
</div>
2024-04-02 19:24:26 +00:00
2024-04-02 19:58:58 +00:00
${session_id ? `
2024-04-02 19:43:20 +00:00
<div class="mt-5">
<form method="POST" action="#" id="formManageSub">
<input type="hidden" id="session-id" name="session_id" value="${session_id}" />
<button id="checkout-and-portal-button" type="submit" class="btn btn-primary">${await translate("Manage my Subscription")}</button>
</form>
</div>
2024-04-02 19:58:58 +00:00
` : ""}
2024-04-02 19:24:26 +00:00
2023-12-13 00:31:35 +00:00
${sub_plan ? `
2023-12-13 15:34:57 +00:00
<p><i><b>${await translate("Your current plan")}</b>: ${sub_plan.name}, ${currency}${sub_plan.price} /${await peertubeHelpers.translate("month")}, ${sub_plan.storage} ${await peertubeHelpers.translate("GB Storage")}</i></p>
2024-04-02 20:29:40 +00:00
<div>
<a href="https://billing.stripe.com/p/login/aEUg0YcdE3ff9j2cMM" target="_blank" style="font-size: 1.25rem; background-color: #0083f5; color: white; border-radius: 0.25rem; padding: 0.5rem 0.75rem; margin-top: 1.25rem;">Manage Subcription</a>
<div>
2023-12-13 00:31:35 +00:00
` : ""}
</div>
`;
const checkForListen = () => {
2023-12-13 15:34:57 +00:00
document.querySelectorAll(".ncdSubscriptionForm").length === 5 ? listenSubmitSubscription(peertubeHelpers) : setTimeout(() => checkForListen(), 1000);
2023-12-13 00:31:35 +00:00
};
checkForListen();
}
function listenSubmitSubscription(peertubeHelpers) {
const baseUrl = peertubeHelpers.getBaseRouterRoute();
// Sub
2023-12-13 00:42:42 +00:00
document.querySelectorAll(".ncdSubscriptionForm").forEach(el => {
2023-12-13 00:31:35 +00:00
el.addEventListener("submit", (e) => {
e.preventDefault();
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
try {
const form = new URLSearchParams(new FormData(e.target));
fetch(baseUrl + "/create-checkout-session", {
method: "POST",
headers: peertubeHelpers.getAuthHeader(),
body: form,
}).then((res) => res.json()).then((data) => {
2024-04-02 19:43:20 +00:00
if (!data || !data.status || data.status !== "success") {
2023-12-13 00:31:35 +00:00
peertubeHelpers.notifier.error(data.message || "Unknown error");
return;
}
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
window.location.href = data.data.redirectUrl;
}).catch(err => {
console.error(err);
peertubeHelpers.notifier.error(err);
})
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
} catch (error) {
peertubeHelpers.notifier.error(error);
}
});
})
// Manage sub
document.getElementById("formManageSub")?.addEventListener("submit", (e) => {
e.preventDefault();
try {
const form = new URLSearchParams(new FormData(e.target));
fetch(baseUrl + "/create-portal-session", {
method: "POST",
headers: peertubeHelpers.getAuthHeader(),
body: form,
}).then((res) => res.json()).then((data) => {
2024-04-02 19:43:20 +00:00
if (!data || !data.status || data.status !== "success") {
2023-12-13 00:31:35 +00:00
peertubeHelpers.notifier.error(data.message || "Unknown error");
return;
}
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
window.location.href = data.data.redirectUrl;
}).catch(err => {
console.error(err);
peertubeHelpers.notifier.error(err);
})
2024-04-02 19:43:20 +00:00
2023-12-13 00:31:35 +00:00
} catch (error) {
peertubeHelpers.notifier.error(error);
}
});
}
export { showPage };