-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainPage.kt
393 lines (348 loc) · 19.8 KB
/
MainPage.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package xyz.derekbriggs
import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.firestore.FirestoreOptions
import com.stripe.Stripe
import com.stripe.model.Customer
import com.stripe.model.PaymentIntent
import homePage
import io.ktor.features.*
import io.ktor.util.date.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.JsonPrimitive
import kweb.*
import kweb.plugins.fomanticUI.fomantic
import kweb.plugins.fomanticUI.fomanticUIPlugin
import kweb.plugins.staticFiles.ResourceFolder
import kweb.plugins.staticFiles.StaticFilesPlugin
import kweb.state.KVal
import kweb.state.KVar
import kweb.state.render
import kweb.util.json
const val usernameTableName = "reservedUsernames"
const val timeToReserveName = 60 * 1000 * 15//15 minutes
//TODO Google Authentication can fail in the first few seconds of a pod existing. Need to add check to make sure this succeeds, and call it again on fail
val firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()
.setProjectId("freenet-353920")
.setCredentials(GoogleCredentials.getApplicationDefault())
.build()
val db = firestoreOptions.service
val presetDonationValues = KVar(arrayOf("10", "20", "40"))
sealed class InputStatus {
object None : InputStatus()
class Available(val minDonationAmount : Long) : InputStatus()
object NotAvailable : InputStatus()
object Invalid : InputStatus()
}
enum class EmailStatus {
Empty, Valid, Invalid
}
fun main() {
Kweb(port = 8080, debug = false, plugins = listOf(fomanticUIPlugin, StripeRoutePlugin(),
StaticFilesPlugin(ResourceFolder("static"), "/static/stripeCheckout"))) {
doc.head {
element("meta").setAttribute("content", "width=device-width, initial-scale=1").setAttribute("name", "viewport")
element("link").setAttribute("rel", "stylesheet").setAttribute("href", "/static/stripeCheckout/checkout.css")
element("link").setAttribute("rel", "stylesheet").setAttribute("href", "/static/stripeCheckout/homepage.css")
element("script").setAttribute("src", "https://js.stripe.com/v3/")
element("script").setAttribute("src", "/static/stripeCheckout/checkout.js")
}
doc.body {
route {
path("/success") {
val payIntent = PaymentIntent.retrieve(browser.httpRequestInfo.request.call.parameters["payment_intent"])
val customerEmail = saveCustomer(payIntent.charges.data[0].customer)
saveStripePaymentDetails(payIntent.toString(), customerEmail, payIntent.metadata["username"]!!, payIntent.amount, payIntent.charges.data[0].id)
div(fomantic.ui.container.center.aligned) {
val successCopy = "Thank you for confirming the username ${payIntent.metadata["username"]!!}"
val successCopy2 = "You will receive payment receipt at $customerEmail"
val successCopy3 = "Follow Freenet on Twitter"
p().text(successCopy)
p().text(successCopy2)
p().text(successCopy3)
}
}
path("/cancel") {
h1().text("Payment cancelled")
}
path("") {
homePage()
}
path("/names") {
val ipAddress = browser.httpRequestInfo.request.call.request.origin.remoteHost
div(fomantic.ui.text.center.aligned.container) {
h1(fomantic.ui).setAttribute("style", """font-size: 4em;""").text("Locutus")
}
div(fomantic.ui.text.container) {
val copyString = "The Internet has become increasingly centralized over the past 25 years, such that a handful of companies now effectively control the Internet infrastructure. This poses a threat to freedom of speech and democracy, as the public square is privately owned." +
"Locutus is a software platform that makes it easy to create decentralized alternatives to today's centralized tech companies. These decentralized apps will be easy to use, scalable, and secured through cryptography."
val copyString2 = "Trust and identity are critical in any decentralized system. By donating to its development, you can reserve your name on Freenet and in future may be granted \"trust tokens\" and other rewards for our early supporters. Names will be fully transferrable, similar to domain names today." +
"So please donate now to help us create a more decentralized future!"
p().text(copyString).classes("headerText")
p().text(copyString2).classes("headerText")
br()
}
val usernameInputStatus: KVar<InputStatus> = KVar(InputStatus.None)
val emailInputStatus : KVar<EmailStatus> = KVar(EmailStatus.Empty)
val minimumDonationAmount : KVar<Long> = KVar(0L)
lateinit var username : KVal<String>
lateinit var email : KVal<String>
var selectedDonationAmount : KVar<String> = KVar("")
lateinit var donationInput : InputElement
div(fomantic.ui.grid.center.aligned) {
form(fomantic.ui.form) {
div(fomantic.field) {
label().text("Username")
div(fomantic.ui.icon.input.huge) {
val usernameInput = input(type = InputType.text, placeholder = "Username", attributes = mapOf("id" to "usernameInput".json))
username = usernameInput.value
username.addListener { _, new ->
GlobalScope.launch {
if (isUsernameValid(new)) {
if (isUsernameAvailable(new)) {
usernameInputStatus.value = InputStatus.Available(
minDonationAmount = getMinimumDonationAmount(new)
)
minimumDonationAmount.value = getMinimumDonationAmount(new)
presetDonationValues.value = arrayOf(minimumDonationAmount.value.toString(), "${minimumDonationAmount.value * 2}", "${minimumDonationAmount.value * 3}")
} else {
usernameInputStatus.value = InputStatus.NotAvailable
}
} else {
usernameInputStatus.value = InputStatus.Invalid
}
}
}
render(usernameInputStatus) {
when(it) {
InputStatus.None -> {}
is InputStatus.Available -> i().classes("ui green checkmark icon")
InputStatus.NotAvailable, InputStatus.Invalid -> i().classes("ui red x icon")
}
}
}
val usernameInvalidString = "Username invalid. May include numbers, letters, underscores, and hyphens"
render(usernameInputStatus) { inputStatus ->
when(inputStatus) {
InputStatus.None -> {}
InputStatus.NotAvailable -> {
p().text("Username Not Available")
}
InputStatus.Invalid -> p().text(usernameInvalidString)
is InputStatus.Available -> {
val usernameAvailableString = "Username Available. Minimum Donation Amount: ${inputStatus.minDonationAmount}"
p().text(usernameAvailableString )
}
}
}
}
div(fomantic.field) {
label().text("Email")
div(fomantic.ui.icon.input.huge) {
val emailInput = input(type = InputType.email, placeholder = "Email", attributes = mapOf("id" to "emailInput".json))
email = emailInput.value
email.addListener { _, new ->
GlobalScope.launch {
if (new.isEmpty()) {
emailInputStatus.value = EmailStatus.Empty
} else {
if (isValidEmail(new)) {
emailInputStatus.value = EmailStatus.Valid
} else {
emailInputStatus.value = EmailStatus.Invalid
}
}
}
}
render(emailInputStatus) {
when(it) {
EmailStatus.Valid -> i().classes("ui green checkmark icon")
EmailStatus.Invalid -> i().classes("ui red x icon")
EmailStatus.Empty ->{}
}
}
}
}
div(fomantic.field) {
div(fomantic.ui.grouped.fields) {
label().text("Pay What You Can")
div(fomantic.ui.center.aligned.stackable.grid) {
render(presetDonationValues) {
for (preset in presetDonationValues.value) {
div(fomantic.field.two.wide.column) {
div(fomantic.ui.radio.checkbox) {
val radioButton = input().setAttribute("type", "radio")
.setAttribute("name", "donationPresetRadio")
.setAttribute("value", preset)
.setAttribute("tabindex", "0")
radioButton.on.click {
selectedDonationAmount.value = preset
}
label().text("\$$preset")
}
}
}
}
div(fomantic.field.six.wide.column.left.aligned) {
div(fomantic.ui.radio.checkbox) {
val customDonationField = input(attributes = mapOf("id" to "donationRadioCustomField".json,
"class" to "ui input".json)).setAttribute("type", "radio")
.setAttribute("name", "donationPresetRadio")
.setAttribute("tabindex", "0")
customDonationField.on.click {
println("selectedDonationAmount on customClick: ${selectedDonationAmount.value}")
selectedDonationAmount = donationInput.value
println("selectedDonationAmount on customClick again: ${selectedDonationAmount.value}")
}
label {
donationInput = input(type= InputType.text, placeholder = "Custom",
attributes = mapOf("id" to "donationInput".json))
donationInput.setAttribute("class", "fluid").setAttribute("width", "6em")
selectedDonationAmount = donationInput.value
donationInput.on.input {
}
}
}
}
}
}
}
button(fomantic.ui.primary.button).text("Reserve Username").on.click {
when(usernameInputStatus.value) {
is InputStatus.Available -> { it
if(emailInputStatus.value == EmailStatus.Valid) {
tempReserveName(username.value, ipAddress, browser.httpRequestInfo.request.headers["Referer"])
val modal = div(fomantic.ui.modal) {
val reservationConfirmationString = "You are reserving the username \"${username.value}\" for \$${selectedDonationAmount.value}.00"
renderCheckout(reservationConfirmationString)
}
browser.callJsFunction("$(\'#\' + {}).modal(\'show\');", modal.id.json)
} else {
}
}
InputStatus.NotAvailable -> {
}
InputStatus.Invalid, InputStatus.None -> {
}
}
}
}
}
}
}
}
}
}
/*
fun isDonationAmountHighEnough(userDonationInput: String, minDonationAmount: Double) : Boolean{
return userDonationInput.toDouble() >= minDonationAmount
}
*/
fun ElementCreator<*>.renderCheckout(confirmationText : String) {
browser.callJsFunction("initialize()")
lateinit var paymentForm : FormElement
div(fomantic.ui.segment.center.aligned) {
p().text(confirmationText)
}
div(fomantic.ui.grid.center.aligned) {
paymentForm = form(mapOf("id" to JsonPrimitive("payment-form"))) {
div(mapOf("id" to JsonPrimitive("payment-element")))
button(mapOf("id" to JsonPrimitive("submit"))) {
div(mapOf("id" to JsonPrimitive("spinner"))).classes("spinner hidden")
span(mapOf("id" to JsonPrimitive("button-text"))).text("Pay Now")
}
div(mapOf("id" to JsonPrimitive("payment-message"))).classes("hidden")
}
}
div(fomantic.ui.container.center.aligned) {
val secureTransactionNoticeString = "Some text explaining that this is a secure transaction through stripe and not to worry about their credit card details"
p().text(secureTransactionNoticeString)
}
div(fomantic.ui.actions) {
val cancelButton = button(fomantic.ui.button).text("cancel").on.click {
browser.callJsFunction("$(\'.ui.modal\').modal(\'close\');")
paymentForm.deleteIfExists()
}
cancelButton.classes("ui cancel button")
}
}
fun getMinimumDonationAmount(username: String) : Long{
return when(username.length) {
2 -> 200
3 -> 150
4 -> 100
5 -> 80
6 -> 60
7 -> 45
else -> 20
}
}
fun tempReserveName(username: String, ipAddress: String, referer: String? = null) {
val docRef = db.collection(usernameTableName).document(username)
val data = HashMap<String, Any>()
data["lowercaseUsername"] = username.lowercase()
data["timeReserved"] = getTimeMillis()
data["ipAddress"] = ""
referer?.let {
data["referer"] = it
}
docRef.set(data)
}
fun isUsernameValid(username: String) : Boolean {
//allows a 2-30 character alphanumeric username that may contain an underscore or period
val usernameRegex = "^[A-Za-z][A-Za-z0-9_.]{1,29}\$"
val isValid = username.matches(usernameRegex.toRegex())
return isValid
}
fun isValidEmail(email : String) : Boolean {
val emailRegex ="""^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9 \-.]+)\.([a-zA-Z]{2,5})${'$'}"""
val isValid = email.matches(emailRegex.toRegex())
return isValid
}
fun isUsernameAvailable(username: String) : Boolean {
val usernameCollection = db.collection(usernameTableName)
val query = usernameCollection.whereEqualTo("lowercaseUsername", username.lowercase())
val querySnapshot = query.get()
for (document in querySnapshot.get().documents) {
//if the username in this document was reserved more than 'timeToReserveName' ago, and has a null transactionId, it is available
println(document.toString())
return ((document.get("Stripe_transaction_id") == null) && getTimeMillis() - document.get("timeReserved") as Long > timeToReserveName)
}
//the usernameToReserve does not exist in the database, and is available
return true
}
//Called to finalize reserving a username, via a Stripe payment
fun saveStripePaymentDetails(stripePayIntentJson: String, email: String, username: String, donationAmount: Long, transactionId: String) {
val docRef = db.collection(usernameTableName).document(username)
val data = HashMap<String, Any>()
data["stripePayIntent"] = stripePayIntentJson
data["email"] = email
data["donationAmount"] = donationAmount
data["Stripe_transaction_id"] = transactionId
data["paymentMethod"] = "Stripe"
val result = docRef.update(data)
}
fun saveCustomer(stripeCustomerId: String) : String{
val customer = Customer.retrieve(stripeCustomerId)
val docRef = db.collection("Customers").document(customer.email)
val data = HashMap<String, Any>()
data["stripeCustomerId"] = stripeCustomerId
docRef.set(data)
return customer.email
}
fun showToast(webBrowser: WebBrowser, message: String) {
webBrowser.callJsFunction("""
Toastify({
text: {},
duration: 5000,
close: false,
gravity: "bottom",
position: "center",
stopOnFocus: false,
style: {
color: "#ff0033",
background: "white"
}
}).showToast();
""".trimIndent(), message.json)
}