summaryrefslogtreecommitdiff
path: root/talermerchantdemos/blog/blog.py
diff options
context:
space:
mode:
Diffstat (limited to 'talermerchantdemos/blog/blog.py')
-rw-r--r--talermerchantdemos/blog/blog.py43
1 files changed, 37 insertions, 6 deletions
diff --git a/talermerchantdemos/blog/blog.py b/talermerchantdemos/blog/blog.py
index e5068ee..a1db789 100644
--- a/talermerchantdemos/blog/blog.py
+++ b/talermerchantdemos/blog/blog.py
@@ -31,6 +31,24 @@ from taler.util.talerconfig import TalerConfig, ConfigurationError
from ..blog.content import ARTICLES, get_article_file, get_image_file
from talermerchantdemos.httpcommon import backend_get, backend_post, fallback_404
+class Deadline:
+ def __init__(self, value):
+ self.value = value
+ def isExpired(self):
+ if self.value == "never":
+ return False
+ now = int(round(time.time()) * 1000)
+ return now > self.value
+
+def refundable(pay_status):
+ refunded = pay_status.get("refunded")
+ refund_deadline = pay_status.get("contract_terms", {}).get("refund_deadline")
+ assert(refunded != None and refund_deadline)
+ rd = Deadline(refund_deadline)
+ if not refunded and not rd.isExpired()
+ return True
+ return False
+
if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
print("Python 3.6 or higher is required.")
print(
@@ -40,7 +58,6 @@ if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
)
sys.exit(1)
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = flask.Flask(
__name__, template_folder=BASE_DIR, static_folder=BASE_DIR + "/../static/"
@@ -122,6 +139,11 @@ def confirm_refund(order_id):
400, message="can't refund unpaid article",
)
article_name = pay_status["contract_terms"]["extra"]["article_name"]
+
+ ## FIXME: here goes #refund-checks.
+ """
+ if not refunded and < deadline, allow statement below.
+ """
return flask.render_template(
"templates/confirm_refund.html", article_name=article_name, order_id=order_id
)
@@ -148,10 +170,15 @@ def refund(order_id):
BACKEND_URL, f"private/orders/{order_id}", params=dict(session_id=session_id)
)
order_status = pay_status.get("order_status")
+
if order_status != "paid":
err_abort(
402, message="You did not pay for this article (nice try!)", json=pay_status
)
+ if not refundable(pay_status):
+ err_abort(
+ 403, message="Item not refundable (anymore)", json=pay_status
+ )
refund_spec = dict(reason="Demo reimbursement", refund=ARTICLE_AMOUNT)
resp = backend_post(BACKEND_URL, f"private/orders/{order_id}/refund", refund_spec)
return flask.redirect(pay_status["order_status_url"])
@@ -170,7 +197,7 @@ def refund(order_id):
# - 404: supplemental @a data not found.
# In the successful case, a response object carrying the
# article in it will be returned.
-def render_article(article_name, data, order_id):
+def render_article(article_name, data, order_id, refundable):
article_info = ARTICLES.get(article_name)
if article_info is None:
m = "Internal error: Files for article ({}) not found.".format(article_name)
@@ -188,9 +215,9 @@ def render_article(article_name, data, order_id):
article_file=get_article_file(article_info),
article_name=article_name,
order_id=order_id,
+ refundable=refundable
)
-
##
# Trigger a article purchase. The logic follows the main steps:
#
@@ -241,9 +268,8 @@ def article(article_name, data=None):
pay_status = backend_get(
BACKEND_URL, f"private/orders/{order_id}", params=dict(session_id=session_id)
)
-
+ # not refundable =>! not viewable!
order_status = pay_status.get("order_status")
-
if order_status == "paid":
refunded = pay_status["refunded"]
if refunded:
@@ -252,7 +278,7 @@ def article(article_name, data=None):
article_name=article_name,
order_id=order_id,
)
- return render_article(article_name, data, order_id)
+ return render_article(article_name, data, order_id, refundable(pay_status))
# Check if the customer came on this page via the
# re-purchase detection mechanism
@@ -273,6 +299,11 @@ def article(article_name, data=None):
)
return response
+@app.errorhandler(500)
+def handler(e):
+ return flask.render_template(
+ "templates/error.html", message="Internal server error")
+
@app.errorhandler(404)
def handler(e):
return flask.render_template(