Commit bfd77343 authored by Aral Balkan's avatar Aral Balkan

Multiple fixes, improvements

parent 0f1ba234
Pipeline #874 canceled with stages
const stripe = require('stripe')('sk_test_GT9DfvDjhljTdIe3rKyEHtyU00RVQ0FCNP')
stripe.setTelemetryEnabled(false)
// Converts a Stripe (Unix) timestamp string to a JavaScript date object.
// (JS expects the timestamp in miliseconds so we pad three decimal places).
function timestampToDate(timestamp) {
return new Date(parseInt(`${timestamp}000`))
}
module.exports = async (request, response) => {
const sessionId = request.query.session_id
// Failed.
if (request.query.status === 'failed') {
response.type('html').status(200).end(`
<h1>Payment failed</h1>
<p>Sorry, your payment failed. <a href='/fund-us'>Please try again.</a></p>
`)
return
}
const sessionId = request.query.id
const session = await stripe.checkout.sessions.retrieve(sessionId)
const subscription = await stripe.subscriptions.retrieve(session.subscription)
const subscriptionId = subscription.id
const subscriptionStatus = subscription.status
const patronSince = Date(subscription.billing_cycle_anchor)
const currentPeriodStart = Date(subscription.current_period_start)
const currentPeriodEnd = Date(subscription.current_period_end)
const patronSince = timestampToDate(subscription.billing_cycle_anchor)
const currentPeriodStart = timestampToDate(subscription.current_period_start)
const currentPeriodEnd = timestampToDate(subscription.current_period_end)
const patronageAmount = subscription.quantity
console.log(subscription.billing_cycle_anchor)
// Newly-created?
let newPatronMessage = ''
// TODO: Base the display of this message on whether it is 1+ day after the subscription was created or not.
// if (request.query.action === 'created') {
// newPatronMessage = `
// <h2>Thank you for becoming a patron!</h2>
// <p><strong>Please bookmark this page now.</strong> You can return to this page at any time to update your patronage settings or to cancel your patronage. Please do not lose this address and do not share it with anyone else.</p>
// <p>(It’s not the end of the world if you do lose it – just shoot us an email at <a href='mailto:support@small-tech.org'>support@small-tech.org</a> and we can help you out.)</p>
// `
// }
const html = `<!DOCTYPE html>
<html lang="en">
<head>
......@@ -31,13 +62,17 @@ module.exports = async (request, response) => {
<body>
<h1>Your patronage</h1>
${newPatronMessage}
<h2>Details</h2>
<ul>
<li>Patron since: ${patronSince}</li>
<li>Amount: €${patronageAmount}/month</li>
<li>Status: ${subscriptionStatus}</li>
</ul>
<p>Current period started ${currentPeriodStart} and will end on ${currentPeriodEnd}.</p>
<p>Your last payment was on ${currentPeriodStart}. Your next payment is scheduled for ${currentPeriodEnd}.</p>
<h2>Update your patronage</h2>
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Stripe client test</title>
<style>
html {
color: black;
background-color: white;
}
</style>
</head>
<body>
<h1>Fund us</h1>
<button id='checkout'>€100/month patronage</button>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_mLQRpGuO7qq3XMfSgwmt4n8U00FSZOIY1h')
document.querySelector('#checkout').addEventListener('click', (event) => {
stripe.redirectToCheckout({
items: [
{plan: 'plan_FSsO2vwva5oEOP', quantity: 100}
],
successUrl: 'https://aral.hypha.dev/patronage/?id={CHECKOUT_SESSION_ID}',
cancelUrl: 'https://aral.hypha.dev/patronage/?status=failed',
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
alert(result.error.message)
})
})
</script>
</body>
</html>
\ No newline at end of file
......@@ -4,36 +4,10 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Stripe client test</title>
<style>
html {
color: black;
background-color: white;
}
</style>
<title>Small Technology Foundation</title>
</head>
<body>
<button id='checkout'>Checkout!</button>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe('pk_test_mLQRpGuO7qq3XMfSgwmt4n8U00FSZOIY1h')
document.querySelector('#checkout').addEventListener('click', (event) => {
stripe.redirectToCheckout({
items: [
{plan: 'plan_FSsO2vwva5oEOP', quantity: 100}
],
successUrl: 'https://aral.hypha.dev/success?session_id={CHECKOUT_SESSION_ID}',
cancelUrl: 'https://aral.hypha.dev/cancel',
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
alert(result.error.message)
})
})
</script>
<h1>Small Technology Foundation</h1>
<p><a href="/fund-us">Fund us!</a></p>
</body>
</html>
\ No newline at end of file
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment