From c7bcb9caa97b86d688f3deeb7f016ac431ba795f Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 2 Nov 2018 20:07:22 +0000 Subject: [PATCH] donate: Add thank you and error page Signed-off-by: Michael Tremer --- Makefile.am | 9 ++- src/templates/{ => donate}/donate.html | 2 +- src/templates/donate/error.html | 12 ++++ src/templates/donate/thank-you.html | 19 +++++ src/templates/error.html | 4 +- src/web/__init__.py | 5 +- src/web/donate.py | 97 ++++++++++++++++++++++++++ src/web/handlers.py | 88 +---------------------- 8 files changed, 145 insertions(+), 91 deletions(-) rename src/templates/{ => donate}/donate.html (99%) create mode 100644 src/templates/donate/error.html create mode 100644 src/templates/donate/thank-you.html create mode 100644 src/web/donate.py diff --git a/Makefile.am b/Makefile.am index 33ea58ae..6867394d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -78,6 +78,7 @@ web_PYTHON = \ src/web/base.py \ src/web/blog.py \ src/web/boot.py \ + src/web/donate.py \ src/web/download.py \ src/web/fireinfo.py \ src/web/handlers.py \ @@ -97,7 +98,6 @@ webdir = $(backenddir)/web templates_DATA = \ src/templates/base.html \ - src/templates/donate.html \ src/templates/error.html \ src/templates/index.html @@ -132,6 +132,13 @@ templates_blog_modules_DATA = \ templates_blog_modulesdir = $(templates_blogdir)/modules +templates_donate_DATA = \ + src/templates/donate/donate.html \ + src/templates/donate/error.html \ + src/templates/donate/thank-you.html + +templates_donatedir = $(templatesdir)/donate + templates_download_DATA = \ src/templates/download/release.html diff --git a/src/templates/donate.html b/src/templates/donate/donate.html similarity index 99% rename from src/templates/donate.html rename to src/templates/donate/donate.html index ffae8b0b..d0271b2e 100644 --- a/src/templates/donate.html +++ b/src/templates/donate/donate.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "../base.html" %} {% block title %}{{ _("Donate") }}{% end block %} diff --git a/src/templates/donate/error.html b/src/templates/donate/error.html new file mode 100644 index 00000000..f9eb4a35 --- /dev/null +++ b/src/templates/donate/error.html @@ -0,0 +1,12 @@ +{% extends "../error.html" %} + +{% block title %}{{ _("Oops!") }}{% end block %} + +{% block message %}{{ _("Error") }}{% end block %} + +{% block main %} +

+ {{ _("Unfortunately we were not able to process your donation.") }} + {{ _("Please try again later.") }} +

+{% end block %} diff --git a/src/templates/donate/thank-you.html b/src/templates/donate/thank-you.html new file mode 100644 index 00000000..2b6d9a38 --- /dev/null +++ b/src/templates/donate/thank-you.html @@ -0,0 +1,19 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Thank You!") }}{% end block %} + +{% block container %} +
+
+
+
+

{{ _("Thank You") }}

+ +

+ {{ _("Thank you very much for your donation to the IPFire Project.") }} +

+
+
+
+
+{% end block %} diff --git a/src/templates/error.html b/src/templates/error.html index 97025770..02cc4fa1 100644 --- a/src/templates/error.html +++ b/src/templates/error.html @@ -6,8 +6,10 @@
-
{{ _("Error %s") % status_code }}
+
{% block message %}{{ _("Error %s") % status_code }}{% end block %}

{{ _("oops, something went wrong") }}

+ + {% block main %}{% end block %}
diff --git a/src/web/__init__.py b/src/web/__init__.py index 75021c9f..b1aa7386 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -17,6 +17,7 @@ from .handlers import * from . import auth from . import blog from . import boot +from . import donate from . import download from . import fireinfo from . import iuse @@ -114,7 +115,9 @@ class Application(tornado.web.Application): (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler), # Donate - (r"/donate", DonateHandler), + (r"/donate", donate.DonateHandler), + (r"/donate/thank-you", donate.ThankYouHandler), + (r"/donate/error", donate.ErrorHandler), (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }), # Newsletter diff --git a/src/web/donate.py b/src/web/donate.py new file mode 100644 index 00000000..98c426a6 --- /dev/null +++ b/src/web/donate.py @@ -0,0 +1,97 @@ +#!/usr/bin/python3 + +import iso3166 +import tornado.gen +import tornado.web + +from . import base + +class DonateHandler(base.BaseHandler): + def get(self): + location = self.get_remote_location() + + if location: + country = location.country + else: + country = None + + # Get defaults + amount = self.get_argument_int("amount", None) + currency = self.get_argument("currency", None) + frequency = self.get_argument("frequency", None) + + # Set default currency + if not currency in ("EUR", "USD"): + currency = "EUR" + + # Default to USD for the US only + if country == "US": + currency = "USD" + + # Set default frequency + if not frequency in ("one-time", "monthly"): + frequency = "one-time" + + self.render("donate.html", countries=iso3166.countries, + country=country, amount=amount, currency=currency, frequency=frequency) + + @tornado.gen.coroutine + def post(self): + amount = self.get_argument("amount") + currency = self.get_argument("currency", "EUR") + frequency = self.get_argument("frequency") + + # Get form inputs + args = { + "amount" : amount, + "currency" : currency, + + # Is this a recurring donation? + "recurring" : frequency == "monthly", + + # Address + "email" : self.get_argument("email"), + "title" : self.get_argument("title"), + "first_name" : self.get_argument("first_name"), + "last_name" : self.get_argument("last_name"), + "company_name" : self.get_argument("company_name", None), + "street1" : self.get_argument("street1"), + "street2" : self.get_argument("street2", None), + "post_code" : self.get_argument("post_code"), + "city" : self.get_argument("city"), + "state" : self.get_argument("state", None), + "country_code" : self.get_argument("country_code"), + } + + # Add URLs to redirect the user back + args.update({ + "success_url" : "https://%s/donate/thank-you" % self.request.host, + "error_url" : "https://%s/donate/error" % self.request.host, + "back_url" : "https://%s/donate?amount=%s¤cy=%s&frequency=%s" % + (self.request.host, amount, currency, frequency), + }) + + # Send request to Zeiterfassung + try: + response = yield self.backend.zeiterfassung.send_request( + "/api/v1/donations/create/ipfire-project", **args) + + except Exception: + raise # XXX handle any problems when Zeiterfassung is unreachable + + # Redirect the user to the payment page + redirect_url = response.get("redirect_url") + if not redirect_url: + raise tornado.web.HTTPError(500, "Did not receive a redirect URL") + + self.redirect(redirect_url) + + +class ThankYouHandler(base.BaseHandler): + def get(self): + self.render("donate/thank-you.html") + + +class ErrorHandler(base.BaseHandler): + def get(self): + self.render("donate/error.html") diff --git a/src/web/handlers.py b/src/web/handlers.py index 4aa204c5..364f3ed6 100644 --- a/src/web/handlers.py +++ b/src/web/handlers.py @@ -1,9 +1,4 @@ -#!/usr/bin/python - -import iso3166 -import tornado.gen -import tornado.httputil -import tornado.web +#!/usr/bin/python3 from . import base @@ -50,87 +45,6 @@ class PlanetUserHandler(base.BaseHandler): self.redirect("https://blog.ipfire.org/authors/%s" % user, permanent=True) -class DonateHandler(base.BaseHandler): - def get(self): - location = self.get_remote_location() - - if location: - country = location.country - else: - country = None - - # Get defaults - amount = self.get_argument_int("amount", None) - currency = self.get_argument("currency", None) - frequency = self.get_argument("frequency", None) - - # Set default currency - if not currency in ("EUR", "USD"): - currency = "EUR" - - # Default to USD for the US only - if country == "US": - currency = "USD" - - # Set default frequency - if not frequency in ("one-time", "monthly"): - frequency = "one-time" - - self.render("donate.html", countries=iso3166.countries, - country=country, amount=amount, currency=currency, frequency=frequency) - - @tornado.gen.coroutine - def post(self): - amount = self.get_argument("amount") - currency = self.get_argument("currency", "EUR") - frequency = self.get_argument("frequency") - - # Get form inputs - args = { - "amount" : amount, - "currency" : currency, - - # Is this a recurring donation? - "recurring" : frequency == "monthly", - - # Address - "email" : self.get_argument("email"), - "title" : self.get_argument("title"), - "first_name" : self.get_argument("first_name"), - "last_name" : self.get_argument("last_name"), - "company_name" : self.get_argument("company_name", None), - "street1" : self.get_argument("street1"), - "street2" : self.get_argument("street2", None), - "post_code" : self.get_argument("post_code"), - "city" : self.get_argument("city"), - "state" : self.get_argument("state", None), - "country_code" : self.get_argument("country_code"), - } - - # Add URLs to redirect the user back - args.update({ - "success_url" : "https://%s/donate/thank-you" % self.request.host, - "error_url" : "https://%s/donate/error" % self.request.host, - "back_url" : "https://%s/donate?amount=%s¤cy=%s&frequency=%s" % - (self.request.host, amount, currency, frequency), - }) - - # Send request to Zeiterfassung - try: - response = yield self.backend.zeiterfassung.send_request( - "/api/v1/donations/create/ipfire-project", **args) - - except Exception: - raise # XXX handle any problems when Zeiterfassung is unreachable - - # Redirect the user to the payment page - redirect_url = response.get("redirect_url") - if not redirect_url: - raise tornado.web.HTTPError(500, "Did not receive a redirect URL") - - self.redirect(redirect_url) - - class StaticHandler(base.BaseHandler): def initialize(self, template): self._template = template -- 2.39.5