paivana

HTTP paywall reverse proxy
Log | Files | Refs | Submodules | README | LICENSE

commit 5e5bb88b508ffe4f9068d2d2b9988099e021c15c
parent f015ab179e7af073fd3e694626aed03e7282ae0d
Author: Florian Dold <dold@taler.net>
Date:   Thu, 19 Nov 2026 16:14:05 +0100

paywall: keep the payment link usable when a status poll fails

Diffstat:
Msrc/frontend/global.d.ts | 2+-
Msrc/frontend/paywall.en.must.j2 | 11+++++------
Msrc/frontend/paywall.js | 16++++++++++++----
Msrc/tests/test_paywall.sh | 11+++++++++--
Asrc/tests/test_paywall_poll.py | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 97 insertions(+), 13 deletions(-)

diff --git a/src/frontend/global.d.ts b/src/frontend/global.d.ts @@ -20,7 +20,7 @@ declare const I18N_PAYMENT_CONFIRMED_LOADING: string; declare const I18N_PAYMENT_CONFIRMED_PROBLEM: string; declare const I18N_PAYMENT_CONFIRMED_ERROR: string; declare const I18N_PAYMENT_CONFIRMED_NO_ORDER: string; -declare const I18N_PAYMENT_NETWORK_PROBLEM: string; +declare const I18N_PAYMENT_STATUS_ERROR: string; declare const MERCHANT_BACKEND: string; declare const MERCHANT_TEMPLATE_ID: string; declare const MAX_PICKUP_DELAY: number; diff --git a/src/frontend/paywall.en.must.j2 b/src/frontend/paywall.en.must.j2 @@ -13,7 +13,7 @@ const I18N_PAYMENT_CONFIRMED_PROBLEM = 'Payment confirmed, but something went wrong when we tried to obtain the access token. This is a bug, please contact the operator if the issue persists.'; const I18N_PAYMENT_CONFIRMED_ERROR = 'Could not reach the server!'; const I18N_PAYMENT_CONFIRMED_NO_ORDER = 'Unexpected 200 OK response without an order_id. Trying again.'; - const I18N_PAYMENT_NETWORK_PROBLEM = 'Network error! Retrying...'; + const I18N_PAYMENT_STATUS_ERROR = 'Could not check payment status. Retrying...'; // The _js values arrive as complete JavaScript string literals, // quotes included, so they are interpolated unescaped: mustache's // HTML escaping is the wrong escaping inside a <script> element @@ -297,7 +297,7 @@ border: 1px black solid; } .footer { - position: fixed; + position: sticky; bottom: 0; width: 100%; padding: 8px; @@ -309,11 +309,10 @@ flex-direction: column; } .footer p { + text-align: center; + line-height: 1.5; text-wrap-mode: wrap; } - body { - margin-bottom: 60px; - } svg.lock-icon { fill: var(--primary-container); stroke: var(--primary); @@ -474,7 +473,7 @@ var QRCode;!function(){function a(a){this.mode=c.MODE_8BIT_BYTE,this.data=a,this </div> <div class="footer"> - <p id="error-message"> </p> + <p id="error-message" role="status"></p> <div class="button-cta"> <a target="_blank" id="taler-link" href="" class="cta">Pay now</a> </div> diff --git a/src/frontend/paywall.js b/src/frontend/paywall.js @@ -243,6 +243,13 @@ async function main() { const start = performance.now(); try { const res = await fetch(PAIVANA_POLL_URL, { cache: "no-store" }); + // 404 is normal until the wallet creates an order. 202 means it is unpaid. + if (![200, 202, 404].includes(res.status)) { + throw new Error(`Unexpected session status response: HTTP ${res.status}`); + } + if (errorMessageLabel) { + errorMessageLabel.textContent = ""; + } if (res.status === 200) { let info = null; try { @@ -270,10 +277,11 @@ async function main() { } } catch (e) { console.warn("[paivana] poll error:", e); - if (talerLink) { - // @ts-ignore - talerLink.href = "#"; - talerLink.textContent = I18N_PAYMENT_NETWORK_PROBLEM; + if ( + errorMessageLabel && + errorMessageLabel.textContent !== I18N_PAYMENT_STATUS_ERROR + ) { + errorMessageLabel.textContent = I18N_PAYMENT_STATUS_ERROR; } } const remMs = Math.round(POLL_WAIT_MS - (performance.now() - start)); diff --git a/src/tests/test_paywall.sh b/src/tests/test_paywall.sh @@ -30,8 +30,9 @@ # the same way merchant and anastasis test their own payment paths), buys # access with taler-wallet-cli, and checks what the daemon does with it. # -# The client half is written out by hand rather than driven through a -# browser: the paywall page's JavaScript computes a payment identifier from +# The wallet payment uses a hand-written client. When Chromium and Playwright +# are available, a separate browser check exercises the page's polling UI. +# The paywall page's JavaScript computes a payment identifier from # (nonce, website, expiration) and the daemon computes the same identifier # independently, so the two agreeing IS the protocol -- it is never sent # from one to the other. paivana_id.py re-derives it from the definition @@ -424,6 +425,12 @@ else fail "the paywall page has no restrictive CSP" fi +if python3 "$SRCDIR/test_paywall_poll.py" "${BASE_URL}hello"; then + ok "the browser keeps payment and support available while retrying" +elif [ "$?" -ne 77 ]; then + fail "paywall polling in the browser" +fi + # -------------------------------------------------------------------- # The whitelist, which only exists on this side of the paywall. # diff --git a/src/tests/test_paywall_poll.py b/src/tests/test_paywall_poll.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Exercise the served paywall with a local Chromium and simulated polls.""" + +import shutil +import sys +from pathlib import Path + +try: + from playwright.sync_api import expect, sync_playwright +except ModuleNotFoundError: + print("SKIP: paywall browser test requires Python Playwright") + sys.exit(77) + + +def check_polling(page, url): + status_code = None + + def answer_poll(route): + if status_code is None: + route.abort("connectionreset") + else: + body = {"order_id": "test-order"} if status_code in (200, 202) else {} + route.fulfill(status=status_code, json=body) + + page.route("**/sessions/**", answer_poll) + page.clock.install() + page.goto(url) + retry = page.get_by_role("status") + pay = page.get_by_role("link", name="Pay now") + expect(retry).to_contain_text("Retrying") + uri = page.locator("#qrcode").get_attribute("title") + assert uri.startswith(("taler://pay-template/", "taler+http://pay-template/")) + expect(pay).to_have_attribute("href", uri) + + # At large font sizes the retry message can wrap onto several lines. + # Both links must still accept a click without the footer covering them. + pay.click(trial=True) + page.get_by_role("link", name="Contact support").click(trial=True) + + interval = page.evaluate("POLL_WAIT_MS") + for status_code in (404, 503, 202, 500): + page.clock.fast_forward(interval) + if status_code in (404, 202): + expect(retry).to_be_empty() + else: + expect(retry).to_contain_text("Retrying") + expect(pay).to_have_attribute("href", uri) + + status_code = 200 + page.route("**/.well-known/paivana", lambda route: route.fulfill(status=204)) + page.route(url, lambda route: route.fulfill(body="Paid article")) + page.clock.fast_forward(interval) + expect(page.locator("body")).to_have_text("Paid article") + + +if __name__ == "__main__": + with sync_playwright() as playwright: + chromium = ( + shutil.which("chromium") + or shutil.which("chromium-browser") + or playwright.chromium.executable_path + ) + if not Path(chromium).is_file(): + print("SKIP: paywall browser test requires Chromium") + sys.exit(77) + with playwright.chromium.launch(executable_path=chromium) as browser: + page = browser.new_page(viewport={"width": 375, "height": 667}) + devtools = page.context.new_cdp_session(page) + devtools.send("Page.setFontSizes", {"fontSizes": {"standard": 32}}) + check_polling(page, sys.argv[1])