taler-merchant-demos

Python-based Frontends for the Demonstration Web site
Log | Files | Refs | Submodules | README | LICENSE

commit 728c4bf11e13dc7a83d88e737b117cbbd4b97cf3
parent 06c708def001b20b6e15d02c5224d835559e4f71
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun,  6 Sep 2020 22:15:20 +0200

add logic for repurchase detection, adjusting order_id if provided

Diffstat:
Mtalermerchantdemos/blog/blog.py | 46++++++++++++++++++++++++++++++++--------------
1 file changed, 32 insertions(+), 14 deletions(-)

diff --git a/talermerchantdemos/blog/blog.py b/talermerchantdemos/blog/blog.py @@ -243,23 +243,41 @@ def article(article_name, lang=None, data=None): order_resp = backend_post(BACKEND_URL, "private/orders", dict(order=order)) order_id = order_resp["order_id"] - # Ask the backend for the status of the payment - pay_status = backend_get( - BACKEND_URL, f"private/orders/{order_id}", params=dict(session_id=session_id) - ) + # We run this code twice, 2nd time in case repurchase detection gave us + # a new order_id. + for retries in range(0,2): + # Ask the backend for the status of the payment + pay_status = backend_get( + BACKEND_URL, f"private/orders/{order_id}", params=dict(session_id=session_id) + ) - order_status = pay_status.get("order_status") + order_status = pay_status.get("order_status") - if order_status == "paid": - refunded = pay_status["refunded"] - if refunded: - return flask.render_template( - "templates/article_refunded.html", - article_name=article_name, - order_id=order_id, - ) + if order_status == "paid": + refunded = pay_status["refunded"] + if refunded: + return flask.render_template( + "templates/article_refunded.html", + article_name=article_name, + order_id=order_id, + ) + else: + response = render_article(article_name, data, order_id) + response.set_cookie("order_id", order_id, path=urllib.parse.quote(f"/essay/{article_name}")) + response.set_cookie("order_id", order_id, path=urllib.parse.quote(f"/{lang}/essay/{article_name}")) + return response else: - return render_article(article_name, data, order_id) + if "already_paid_order_id" in pay_status: + order_id = pay_status.get("already_paid_order_id") + print ("Already paid: " + order_id); + # retry (but only once) + continue + else: + print ("Not yet paid paid: " + str(pay_status)) + + + # Give up trying, order is unpaid + break # Redirect the browser to a page where the wallet can # run the payment protocol.