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
"/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")
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):