Commit ed44ea6a authored by Aral Balkan's avatar Aral Balkan

Display dates better

parent c83d5a2e
Pipeline #876 canceled with stages
const moment = require('moment')
const stripe = require('stripe')('sk_test_GT9DfvDjhljTdIe3rKyEHtyU00RVQ0FCNP')
stripe.setTelemetryEnabled(false)
......@@ -7,16 +9,12 @@ function timestampToDate(timestamp) {
return new Date(parseInt(`${timestamp}000`))
}
module.exports = async (request, response) => {
function lowercaseFirstLetter (s) {
if (typeof s !== 'string') return ''
return s.charAt(0).toLowerCase() + s.slice(1)
}
// 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
}
module.exports = async (request, response) => {
const sessionId = request.query.id
......@@ -25,9 +23,22 @@ module.exports = async (request, response) => {
const subscriptionId = subscription.id
const subscriptionStatus = subscription.status
const patronSince = timestampToDate(subscription.billing_cycle_anchor)
const patronSinceMoment = moment(patronSince)
const patronSinceRelative = patronSinceMoment.fromNow()
const patronSinceAbsolute = lowercaseFirstLetter(patronSinceMoment.calendar())
const currentPeriodStart = timestampToDate(subscription.current_period_start)
const currentPeriodStartMoment = moment(currentPeriodStart)
const currentPeriodStartRelative = currentPeriodStartMoment.fromNow()
const currentPeriodStartAbsolute = lowercaseFirstLetter(currentPeriodStartMoment.calendar())
const currentPeriodEnd = timestampToDate(subscription.current_period_end)
const currentPeriodEndMoment = moment(currentPeriodEnd)
const currentPeriodEndRelative = currentPeriodEndMoment.fromNow()
const currentPeriodEndAbsolute = lowercaseFirstLetter(currentPeriodEndMoment.calendar())
const patronageAmount = subscription.quantity
console.log(subscription.billing_cycle_anchor)
......@@ -56,6 +67,7 @@ module.exports = async (request, response) => {
html {
color: black;
background-color: white;
font-family: sans-serif;
}
</style>
</head>
......@@ -64,16 +76,15 @@ module.exports = async (request, response) => {
${newPatronMessage}
<h2>Details</h2>
<h2>At a glance</h2>
<ul>
<li>Patron since: ${patronSince}</li>
<li>Amount: €${patronageAmount}/month</li>
<li>Status: ${subscriptionStatus}</li>
<li><strong>Patron since:</strong> ${patronSinceRelative} (${patronSinceAbsolute})</strong>
<li><strong>Amount:</strong> €${patronageAmount}/month</li>
<li><strong>Last payment:</strong> ${currentPeriodStartRelative} (${currentPeriodStartAbsolute})</li>
<li><strong>Next payment:</strong> ${currentPeriodEndRelative} (${currentPeriodEndAbsolute})</li>
</ul>
<p>Your last payment was on ${currentPeriodStart}. Your next payment is scheduled for ${currentPeriodEnd}.</p>
<h2>Update your patronage</h2>
<p>This will update your patronage amount effective immediately.</p>
......
......@@ -9,6 +9,11 @@
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
},
"moment": {
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
},
"qs": {
"version": "6.7.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
......
......@@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"moment": "^2.24.0",
"stripe": "^7.4.0"
}
}
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