From: Michael Tremer Date: Wed, 25 Mar 2020 16:59:38 +0000 (+0000) Subject: donations: Split API call into many small things X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bd9cc41db01ff9a61378df06812d5d8552f00662;p=ipfire.org.git donations: Split API call into many small things Signed-off-by: Michael Tremer --- diff --git a/src/web/donate.py b/src/web/donate.py index 8812794b..3f54cbe8 100644 --- a/src/web/donate.py +++ b/src/web/donate.py @@ -43,20 +43,19 @@ class DonateHandler(base.BaseHandler): 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"), @@ -65,28 +64,79 @@ class DonateHandler(base.BaseHandler): "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):