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
+ # Collect donor information
+ donor = {
"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),
+ }
+
+ # Collect company information
+ company_name = self.get_argument("company_name", None)
+
+ # Collect address information
+ address = {
"street1" : self.get_argument("street1"),
"street2" : self.get_argument("street2", None),
"post_code" : self.get_argument("post_code"),
"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
+ # Send everything to Zeiterfassung
try:
+ # Search for person or create a new one
response = await self.backend.zeiterfassung.send_request(
- "/api/v1/donations/create/ipfire-project", **args)
+ "/api/v1/persons/search", **donor
+ )
- except Exception:
- raise # XXX handle any problems when Zeiterfassung is unreachable
+ if not response:
+ response = await self.backend.zeiterfassung.send_request(
+ "/api/v1/persons/create", **donor, **address
+ )
- # 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")
+ person = response.get("number")
- self.redirect(redirect_url)
+ # Fetch properties of this person
+ response = await self.backend.zeiterfassung.send_request(
+ "/api/v1/persons/%s/properties" % person,
+ )
+
+ # Does this person already have an organization?
+ organization = response.get("organization", None)
+
+ # Search for organization or create a new one
+ if not organization and company_name:
+ response = await self.backend.zeiterfassung.send_request(
+ "/api/v1/organizations/search", name=name,
+ )
+
+ # Create a new one if we could not find anything
+ if not response:
+ response = await self.backend.zeiterfassung.send_request(
+ "/api/v1/organizations/create", name=name, **address,
+ )
+
+ organization = response.get("number")
+
+ # Connect organization and person
+ await self.backend.zeiterfassung.send_request(
+ "/api/v1/organizations/%s/persons/add", person=person,
+ )
+
+ donation = {
+ "organization" : organization,
+ "person" : person,
+
+ # $$$
+ "amount" : amount,
+ "currency" : currency,
+
+ # Is this a recurring donation?
+ "recurring" : frequency == "monthly",
+
+ # Add URLs to redirect the user back
+ "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),
+ }
+
+ # Create donation
+ response = await self.backend.zeiterfassung.send_request(
+ "/api/v1/donations/create/ipfire-project", **donation)
+
+ # 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)
+
+ # XXX handle any problems when Zeiterfassung is unreachable
+ except Exception:
+ raise
class ThankYouHandler(base.BaseHandler):