Created
November 8, 2019 20:44
-
-
Save fsjuhl/86e26fcc7445345bdb9623aafac61529 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main() { | |
const scriptLoader = url => new Promise(resolve => { | |
const script = document.createElement("script") | |
script.onload = resolve | |
script.src = url | |
document.body.append(script) | |
}) | |
const scripts = [ | |
"https://code.jquery.com/jquery-3.4.1.min.js", | |
"https://checkout.stripe.com/v2/checkout.js" | |
] | |
await Promise.all(scripts.map(scriptLoader)) | |
const config = { | |
stripeApiKey: "pk_live_dxtcgv9FQOiOuxscZutQVFjj009kpbRwRA", | |
inStockText: "Purchase Now", | |
soldOutText: "Sold Out", | |
// baseURL: "http://localhost:8080", | |
baseURL: "https://dashboard.dropoclock.io", | |
price: 3000, | |
currency: "usd", | |
name: "Drop O'Clock" | |
} | |
const handler = StripeCheckout.configure({ | |
key: config.stripeApiKey, | |
locale: "auto", | |
token: async (token) => { | |
window.authcordSubmitting = true | |
purchaseButton.addClass("processing") | |
purchaseButton.text("Processing...") | |
purchaseButton.attr("disabled", true) | |
const request = await fetch(config.baseURL + "/api/dropin/purchase", { | |
method: "POST", | |
headers: { | |
"content-type": "application/json" | |
}, | |
body: JSON.stringify({ | |
email: token.email, | |
token: token.id, | |
password: search.get("password") || "landingpage" | |
}) | |
}).then(result => result.json()) | |
window.authcordSubmitting = false | |
purchaseButton.removeClass("processing") | |
purchaseButton.addClass("soldOut") | |
purchaseButton.text("Sold out") | |
purchaseButton.attr("disabled", false) | |
if (request.success) { | |
document.location.href = config.baseURL + `/success?license_key=${request.license_key}&name=${request.name}` | |
} else { | |
document.location.href = config.baseURL + "/sorry" | |
} | |
} | |
}) | |
const purchaseButton = $(".authcordPurchaseButton") | |
const search = new URLSearchParams(document.location.search) | |
const stock = await fetch(config.baseURL + `/api/dropin/stock?password=${search.get("password") || "landingpage"}`) | |
.then(result => result.json()) | |
if (stock.success) { | |
purchaseButton.addClass( stock.inStock ? "inStock" : "soldOut" ) | |
purchaseButton.text( stock.inStock ? config.inStockText : config.soldOutText ) | |
purchaseButton.attr("disabled", !stock.inStock) | |
} else { | |
// Display error | |
purchaseButton.addClass("soldOut") | |
purchaseButton.text(config.soldOutText) | |
purchaseButton.attr("disabled", true) | |
} | |
purchaseButton.on("click", async () => { | |
if (window.authcordSubmitting) return | |
handler.open({ | |
name: config.name, | |
amount: config.price, | |
currency: config.currency, | |
description: config.name + " Monthly Subscription" | |
}) | |
}) | |
window.addEventListener("popstate", handler.close) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment