Repository: internetarchive/openlibrary

Issue:
Switch donation form to use IA js/iframe
The IA folks have created a donation form iframe (triggered by a script). We currently splice HTML/JS from IA into OL to achieve the same effect. It seems like they might want to deprecate the old PHP endpoint we're using in favor of this. Priority TBD.

The new code would be:

```js
<script src="https://archive.org/includes/donate.js"></script>
```

Pros:
- Don't inject HTML from IA (brittle dev dependency)
- Don't slow server response to fetch HTML/JS from IA; done on client
- DRY up donation code on IA

Cons:
- ~The iframe based form requires users to be sent to IA to complete the transaction; previously only sent to IA for the "Thank you" page~ @jbuckner: "That’s true for both versions of the banner; We’re now always redirecting users to the donate page."
- Importing a loose js file is still a bit of a brittle dependency
- ~Won't work for non-JS users~ (donation form requires js anyways)

Risks:
- Might have some security gotchas since the iframe is from another domain, but should be possible since it's not trying to display modals and what not.

### Stakeholders
@jbuckner





Discussion:

Resolving patch:
diff --git a/openlibrary/plugins/upstream/utils.py b/openlibrary/plugins/upstream/utils.py
--- a/openlibrary/plugins/upstream/utils.py
+++ b/openlibrary/plugins/upstream/utils.py
@@ -741,32 +741,14 @@ def get_donation_include(include):
     dev_host = web_input.pop("dev_host", "")  # e.g. `www-user`
     if dev_host and re.match('^[a-zA-Z0-9-.]+$', dev_host):
         dev_host += "."   # e.g. `www-user.`
-    url_banner_source = "https://%sarchive.org/includes/donate.php" % dev_host
-    param = '?platform=ol'
+    script_src = "https://%sarchive.org/includes/donate.js" % dev_host
     if 'ymd' in web_input:
-        param += '&ymd=' + web_input.ymd
+        script_src += '?ymd=' + web_input.ymd
 
-    # Look for presence of cookie indicating banner has been closed
-    opener = urllib.request.build_opener()
-    donation_param = web.cookies().get('donation')
-    if donation_param:
-        # Append a tuple with the cookie pair (*not* extraneous parentheses!)
-        opener.addheaders.append(('Cookie', urllib.parse.urlencode({'donation': donation_param})))
-
-    html = ''
-    if include == 'true' and "dev" in web.ctx.features:
-        try:
-            html += opener.open(url_banner_source + param, timeout=3).read().decode("utf-8")
-            # Donation banner is temporarily (Jan 2020) disabled on prod, but available on dev (so that it can be used
-            # for testing). To avoid it appearing like it's working, display a warning if it loads correctly that it's
-            # disabled on prod.
-            if '<div' in html or '<iframe' in html:
-                html = """
-                <center>WARNING: Donation banner disabled on prod; see <a href="https://github.com/internetarchive/openlibrary/issues/2853">GitHub #2853</a></center>
-                """ + html
-        except urllib.error.URLError:
-            logging.getLogger("openlibrary").error('Could not load donation banner')
-            return ''
+    html = """
+    <div id="donato"></div>
+    <script src="%s" data-platform="ol"></script>
+    """ % script_src
     return html
 
 #get_donation_include = cache.memcache_memoize(get_donation_include, key_prefix="upstream.get_donation_include", timeout=60)


Test patch: