]>
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 | |
b00cc400 MT |
24 | first_name = self.get_argument("first_name", None) |
25 | last_name = self.get_argument("last_name", None) | |
c7bcb9ca MT |
26 | amount = self.get_argument_int("amount", None) |
27 | currency = self.get_argument("currency", None) | |
28 | frequency = self.get_argument("frequency", None) | |
29 | ||
30 | # Set default currency | |
31 | if not currency in ("EUR", "USD"): | |
32 | currency = "EUR" | |
33 | ||
34 | # Default to USD for the US only | |
35 | if country == "US": | |
36 | currency = "USD" | |
37 | ||
38 | # Set default frequency | |
39 | if not frequency in ("one-time", "monthly"): | |
40 | frequency = "one-time" | |
41 | ||
0ba47b0f | 42 | self.render("donate/donate.html", countries=iso3166.countries, |
b00cc400 MT |
43 | country=country, first_name=first_name, last_name=last_name, |
44 | amount=amount, currency=currency, frequency=frequency) | |
c7bcb9ca MT |
45 | |
46 | @tornado.gen.coroutine | |
47 | def post(self): | |
48 | amount = self.get_argument("amount") | |
49 | currency = self.get_argument("currency", "EUR") | |
50 | frequency = self.get_argument("frequency") | |
51 | ||
52 | # Get form inputs | |
53 | args = { | |
54 | "amount" : amount, | |
55 | "currency" : currency, | |
56 | ||
57 | # Is this a recurring donation? | |
58 | "recurring" : frequency == "monthly", | |
59 | ||
60 | # Address | |
61 | "email" : self.get_argument("email"), | |
62 | "title" : self.get_argument("title"), | |
63 | "first_name" : self.get_argument("first_name"), | |
64 | "last_name" : self.get_argument("last_name"), | |
65 | "company_name" : self.get_argument("company_name", None), | |
66 | "street1" : self.get_argument("street1"), | |
67 | "street2" : self.get_argument("street2", None), | |
68 | "post_code" : self.get_argument("post_code"), | |
69 | "city" : self.get_argument("city"), | |
70 | "state" : self.get_argument("state", None), | |
71 | "country_code" : self.get_argument("country_code"), | |
72 | } | |
73 | ||
74 | # Add URLs to redirect the user back | |
75 | args.update({ | |
76 | "success_url" : "https://%s/donate/thank-you" % self.request.host, | |
77 | "error_url" : "https://%s/donate/error" % self.request.host, | |
78 | "back_url" : "https://%s/donate?amount=%s¤cy=%s&frequency=%s" % | |
79 | (self.request.host, amount, currency, frequency), | |
80 | }) | |
81 | ||
82 | # Send request to Zeiterfassung | |
83 | try: | |
84 | response = yield self.backend.zeiterfassung.send_request( | |
85 | "/api/v1/donations/create/ipfire-project", **args) | |
86 | ||
87 | except Exception: | |
88 | raise # XXX handle any problems when Zeiterfassung is unreachable | |
89 | ||
90 | # Redirect the user to the payment page | |
91 | redirect_url = response.get("redirect_url") | |
92 | if not redirect_url: | |
93 | raise tornado.web.HTTPError(500, "Did not receive a redirect URL") | |
94 | ||
95 | self.redirect(redirect_url) | |
96 | ||
97 | ||
98 | class ThankYouHandler(base.BaseHandler): | |
99 | def get(self): | |
100 | self.render("donate/thank-you.html") | |
101 | ||
102 | ||
103 | class ErrorHandler(base.BaseHandler): | |
104 | def get(self): | |
105 | self.render("donate/error.html") |