]>
Commit | Line | Data |
---|---|---|
c7bcb9ca MT |
1 | #!/usr/bin/python3 |
2 | ||
3 | import iso3166 | |
4 | import tornado.gen | |
5 | import tornado.web | |
6 | ||
7 | from . import base | |
8 | ||
9 | class DonateHandler(base.BaseHandler): | |
52d4e79b MT |
10 | @base.blacklisted |
11 | def prepare(self): | |
12 | # Makes sure that we call blacklist for everything | |
13 | pass | |
14 | ||
c7bcb9ca MT |
15 | def get(self): |
16 | location = self.get_remote_location() | |
17 | ||
18 | if location: | |
19 | country = location.country | |
20 | else: | |
21 | country = None | |
22 | ||
23 | # Get defaults | |
24 | amount = self.get_argument_int("amount", None) | |
25 | currency = self.get_argument("currency", None) | |
26 | frequency = self.get_argument("frequency", None) | |
27 | ||
28 | # Set default currency | |
29 | if not currency in ("EUR", "USD"): | |
30 | currency = "EUR" | |
31 | ||
32 | # Default to USD for the US only | |
33 | if country == "US": | |
34 | currency = "USD" | |
35 | ||
36 | # Set default frequency | |
37 | if not frequency in ("one-time", "monthly"): | |
38 | frequency = "one-time" | |
39 | ||
40 | self.render("donate.html", countries=iso3166.countries, | |
41 | country=country, amount=amount, currency=currency, frequency=frequency) | |
42 | ||
43 | @tornado.gen.coroutine | |
44 | def post(self): | |
45 | amount = self.get_argument("amount") | |
46 | currency = self.get_argument("currency", "EUR") | |
47 | frequency = self.get_argument("frequency") | |
48 | ||
49 | # Get form inputs | |
50 | args = { | |
51 | "amount" : amount, | |
52 | "currency" : currency, | |
53 | ||
54 | # Is this a recurring donation? | |
55 | "recurring" : frequency == "monthly", | |
56 | ||
57 | # Address | |
58 | "email" : self.get_argument("email"), | |
59 | "title" : self.get_argument("title"), | |
60 | "first_name" : self.get_argument("first_name"), | |
61 | "last_name" : self.get_argument("last_name"), | |
62 | "company_name" : self.get_argument("company_name", None), | |
63 | "street1" : self.get_argument("street1"), | |
64 | "street2" : self.get_argument("street2", None), | |
65 | "post_code" : self.get_argument("post_code"), | |
66 | "city" : self.get_argument("city"), | |
67 | "state" : self.get_argument("state", None), | |
68 | "country_code" : self.get_argument("country_code"), | |
69 | } | |
70 | ||
71 | # Add URLs to redirect the user back | |
72 | args.update({ | |
73 | "success_url" : "https://%s/donate/thank-you" % self.request.host, | |
74 | "error_url" : "https://%s/donate/error" % self.request.host, | |
75 | "back_url" : "https://%s/donate?amount=%s¤cy=%s&frequency=%s" % | |
76 | (self.request.host, amount, currency, frequency), | |
77 | }) | |
78 | ||
79 | # Send request to Zeiterfassung | |
80 | try: | |
81 | response = yield self.backend.zeiterfassung.send_request( | |
82 | "/api/v1/donations/create/ipfire-project", **args) | |
83 | ||
84 | except Exception: | |
85 | raise # XXX handle any problems when Zeiterfassung is unreachable | |
86 | ||
87 | # Redirect the user to the payment page | |
88 | redirect_url = response.get("redirect_url") | |
89 | if not redirect_url: | |
90 | raise tornado.web.HTTPError(500, "Did not receive a redirect URL") | |
91 | ||
92 | self.redirect(redirect_url) | |
93 | ||
94 | ||
95 | class ThankYouHandler(base.BaseHandler): | |
96 | def get(self): | |
97 | self.render("donate/thank-you.html") | |
98 | ||
99 | ||
100 | class ErrorHandler(base.BaseHandler): | |
101 | def get(self): | |
102 | self.render("donate/error.html") |