From: Michael Tremer Date: Thu, 30 Dec 2021 17:38:19 +0000 (+0000) Subject: donate: Update API to create orders X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b577aabf9132f8f21d78dfa924295919bdbe246f;p=ipfire.org.git donate: Update API to create orders Signed-off-by: Michael Tremer --- diff --git a/src/web/donate.py b/src/web/donate.py index bda1ee5b..07f9138a 100644 --- a/src/web/donate.py +++ b/src/web/donate.py @@ -6,6 +6,13 @@ import tornado.web from . import auth from . import base +SKUS = { + "monthly" : "IPFIRE-DONATION-MONTHLY", + "quarterly" : "IPFIRE-DONATION-QUARTERLY", + "yearly" : "IPFIRE-DONATION-YEARLY", +} +DEFAULT_SKU = "IPFIRE-DONATION" + class DonateHandler(auth.CacheMixin, base.BaseHandler): def get(self): country = self.current_country_code @@ -69,31 +76,25 @@ class DonateHandler(auth.CacheMixin, base.BaseHandler): "/api/v1/persons/create", **donor, **address ) + # Store person number person = response.get("number") - donation = { - "person" : person, - - # $$$ - "amount" : amount, - "currency" : currency, + # Create a new order + order = await self._create_order(person=person, currency=currency) - # Is this a recurring donation? - "recurring" : frequency == "monthly", + # Add donation to the order + await self._create_donation(order, frequency, amount, currency) - # 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), - } + # Submit the order + needs_payment = await self._submit_order(order) - # Create donation - response = await self.backend.zeiterfassung.send_request( - "/api/v1/donations/create/ipfire-project", **donation, **address) + # Pay the order + if needs_payment: + redirect_url = await self._pay_order(order) + else: + redirect_url = "https://%s/donate/thank-you" % self.request.host # 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") @@ -103,6 +104,68 @@ class DonateHandler(auth.CacheMixin, base.BaseHandler): except Exception: raise + async def _create_order(self, person, currency=None): + """ + Creates a new order and returns its ID + """ + response = await self.backend.zeiterfassung.send_request( + "/api/v1/orders/create", person=person, currency=currency, + ) + + # Return the order number + return response.get("number") + + async def _create_donation(self, order, frequency, amount, currency): + """ + Creates a new donation + """ + # Select the correct product + try: + sku = SKUS[frequency] + except KeyError: + sku = DEFAULT_SKU + + # Add it to the order + await self.backend.zeiterfassung.send_request( + "/api/v1/orders/%s/products/add" % order, sku=sku, quantity=1, + ) + + # Set the price + await self.backend.zeiterfassung.send_request( + "/api/v1/orders/%s/products/%s/price" % (order, sku), + price=amount, currency=currency, + ) + + async def _submit_order(self, order): + """ + Submits the order + """ + response = await self.backend.zeiterfassung.send_request( + "/api/v1/orders/%s/submit" % order, + ) + + # Return whether this needs payment + return not response.get("is_authorized") + + async def _pay_order(self, order): + """ + Pay the order + """ + # Add URLs to redirect the user back + urls = { + "success_url" : "https://%s/donate/thank-you" % self.request.host, + "error_url" : "https://%s/donate/error" % self.request.host, + "back_url" : "https://%s/donate" % self.request.host, + } + + # Send request + response = await self.backend.zeiterfassung.send_request( + "/api/v1/orders/%s/pay" % order, **urls, + ) + + # Return redirect URL + return response.get("redirect_url", None) + class ThankYouHandler(base.BaseHandler): def get(self):