Skip to content

Commit

Permalink
Merge branch 'payment'
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Jun 25, 2023
2 parents 290ee24 + 4a032c5 commit ecc5bb6
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 104 deletions.
77 changes: 77 additions & 0 deletions client/geoword/confirmation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!doctype html>
<html lang="en">

<head>
<title>QB Reader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Play geoword on qbreader.">

<link href="/apple-touch-icon.png" rel="apple-touch-icon">
<link href="/apple-touch-icon-precomposed.png" rel="apple-touch-icon-precomposed">
<link type="image/x-icon" href="/favicon.ico" rel="icon">

<link href="/bootstrap/light.css" rel="stylesheet">
<link href="/bootstrap/dark.css" rel="stylesheet" id="custom-css">
<script src="/apply-theme.js"></script>
</head>

<body>
<nav class="navbar navbar-light navbar-expand-lg bg-custom" id="navbar" style="z-index: 10">
<div class="container-fluid">
<a class="navbar-brand ms-1 py-0" id="logo" href="/">
<span class="logo-prefix">QB</span><span class="logo-suffix">Reader</span>
</a>
<button class="navbar-toggler" data-bs-target="#navbarSupportedContent" data-bs-toggle="collapse" type="button"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="/tossups">Tossups</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/bonuses">Bonuses</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/multiplayer">Multiplayer</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/db">Database</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/geoword">Geoword</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/api-docs">API</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About</a>
</li>
</ul>
<div class="d-flex">
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="/user/login" id="login-link">Log in</a>
</li>
</ul>
</div>
</div>
</div>
</nav>

<div class="container-xl mt-3 mb-5 pb-5">
<p>
Payment confirmed.
<a href="/geoword">Return to geoword</a>.
</p>
</div>

<script src="/bootstrap/bootstrap.bundle.min.js"></script>
<script src="/script.js"></script>

<script src="/geoword/script.js"></script>
</body>

</html>
2 changes: 1 addition & 1 deletion client/geoword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fetch('/api/geoword/packet-list')
const gameListSelect = document.getElementById('packet-list');
packetList.forEach(game => {
const a = document.createElement('a');
a.href = '/geoword/division/' + game.name;
a.href = '/geoword/' + (game.costInCents ? 'payment/' : 'division/') + game.name;
a.textContent = titleCase(game.name);

const li = document.createElement('li');
Expand Down
93 changes: 93 additions & 0 deletions client/geoword/payment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!doctype html>
<html lang="en">

<head>
<title>QB Reader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Play geoword on qbreader.">

<link href="/apple-touch-icon.png" rel="apple-touch-icon">
<link href="/apple-touch-icon-precomposed.png" rel="apple-touch-icon-precomposed">
<link type="image/x-icon" href="/favicon.ico" rel="icon">

<link href="/bootstrap/light.css" rel="stylesheet">
<link href="/bootstrap/dark.css" rel="stylesheet" id="custom-css">
<script src="/apply-theme.js"></script>

<script src="https://js.stripe.com/v3/"></script>
<script src="/geoword/payment.js" defer></script>
</head>

<body>
<nav class="navbar navbar-light navbar-expand-lg bg-custom" id="navbar" style="z-index: 10">
<div class="container-fluid">
<a class="navbar-brand ms-1 py-0" id="logo" href="/">
<span class="logo-prefix">QB</span><span class="logo-suffix">Reader</span>
</a>
<button class="navbar-toggler" data-bs-target="#navbarSupportedContent" data-bs-toggle="collapse" type="button"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="/tossups">Tossups</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/bonuses">Bonuses</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/multiplayer">Multiplayer</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/db">Database</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/geoword">Geoword</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/api-docs">API</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About</a>
</li>
</ul>
<div class="d-flex">
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="/user/login" id="login-link">Log in</a>
</li>
</ul>
</div>
</div>
</div>
</nav>

<div class="container-xl mt-3 mb-5 pb-5">
<p>
The cost to play the packet <b id="packet-name"></b> is <b>$<span id="cost">2.00</span></b>.
Please enter payment information below:
</p>
<form id="payment-form">
<div id="link-authentication-element">
<!--Stripe.js injects the Link Authentication Element-->
</div>
<div class="mb-3" id="payment-element">
<!--Stripe.js injects the Payment Element-->
</div>
<button class="btn btn-primary" id="submit">
<div class="spinner hidden" id="spinner"></div>
<span id="button-text">Pay now</span>
</button>
<div id="payment-message" class="hidden"></div>
</form>
</div>

<script src="/bootstrap/bootstrap.bundle.min.js"></script>
<script src="/script.js"></script>

<script src="/geoword/script.js"></script>
</body>

</html>
127 changes: 127 additions & 0 deletions client/geoword/payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const packetName = window.location.pathname.split('/').pop();
document.getElementById('packet-name').textContent = titleCase(packetName);

// This is your test publishable API key.
const STRIPE_PUBLISHABLE_KEY = 'pk_live_51NManVKG9mAb0mOpZxtFcYWRju7COWAwtirGyd01es3bEJhqSZd8SdSsOPgyj2LizN0QYjLumsWiOoB2nKadXrt100bTtyHh8m';
const stripe = Stripe(STRIPE_PUBLISHABLE_KEY);

let elements;

initialize();
checkStatus();

document.querySelector('#payment-form').addEventListener('submit', handleSubmit);

let emailAddress = '';
// Fetches a payment intent and captures the client secret
async function initialize() {
const response = await fetch('/api/geoword/create-payment-intent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ packetName: packetName }),
});
const { clientSecret } = await response.json();

const isDarkTheme = (localStorage.getItem('color-theme') === 'dark') || (!localStorage.getItem('color-theme') && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
const appearance = {
theme: isDarkTheme ? 'night' : 'stripe'
};
elements = stripe.elements({ appearance, clientSecret });

const linkAuthenticationElement = elements.create('linkAuthentication');
linkAuthenticationElement.mount('#link-authentication-element');

linkAuthenticationElement.on('change', (event) => {
emailAddress = event.value.email;
});

const paymentElementOptions = {
layout: 'tabs',
};

const paymentElement = elements.create('payment', paymentElementOptions);
paymentElement.mount('#payment-element');
}

async function handleSubmit(e) {
e.preventDefault();
setLoading(true);

const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: window.location.origin + '/geoword/confirmation',
receipt_email: emailAddress,
},
});

// This point will only be reached if there is an immediate error when
// confirming the payment. Otherwise, your customer will be redirected to
// your `return_url`. For some payment methods like iDEAL, your customer will
// be redirected to an intermediate site first to authorize the payment, then
// redirected to the `return_url`.
if (error.type === 'card_error' || error.type === 'validation_error') {
showMessage(error.message);
} else {
showMessage('An unexpected error occurred.');
}

setLoading(false);
}

// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
'payment_intent_client_secret'
);

if (!clientSecret) {
return;
}

const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);

switch (paymentIntent.status) {
case 'succeeded':
showMessage('Payment succeeded!');
break;
case 'processing':
showMessage('Your payment is processing.');
break;
case 'requires_payment_method':
showMessage('Your payment was not successful, please try again.');
break;
default:
showMessage('Something went wrong.');
break;
}
}

// ------- UI helpers -------

function showMessage(messageText) {
const messageContainer = document.querySelector('#payment-message');

messageContainer.classList.remove('hidden');
messageContainer.textContent = messageText;

setTimeout(function () {
messageContainer.classList.add('hidden');
messageContainer.textContent = '';
}, 4000);
}

// Show a spinner on payment submission
function setLoading(isLoading) {
if (isLoading) {
// Disable the button and show a spinner
document.querySelector('#submit').disabled = true;
document.querySelector('#spinner').classList.remove('hidden');
document.querySelector('#button-text').classList.add('hidden');
} else {
document.querySelector('#submit').disabled = false;
document.querySelector('#spinner').classList.add('hidden');
document.querySelector('#button-text').classList.remove('hidden');
}
}
Loading

0 comments on commit ecc5bb6

Please sign in to comment.