From d429ea577fc5af028b0d5f3ed309fc96155e8240 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 8 Aug 2020 13:02:11 +0530 Subject: switch to merchant backend based payment flow --- talermerchantdemos/donations/donations.py | 77 ++++++---------------- .../donations/templates/request_payment.html | 69 ------------------- 2 files changed, 19 insertions(+), 127 deletions(-) delete mode 100644 talermerchantdemos/donations/templates/request_payment.html (limited to 'talermerchantdemos/donations') diff --git a/talermerchantdemos/donations/donations.py b/talermerchantdemos/donations/donations.py index c011189..f77bec1 100644 --- a/talermerchantdemos/donations/donations.py +++ b/talermerchantdemos/donations/donations.py @@ -22,8 +22,6 @@ import logging import flask import lxml.etree import os -import qrcode -import qrcode.image.svg import time import traceback import urllib @@ -37,7 +35,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = flask.Flask(__name__, template_folder=BASE_DIR) app.debug = True -app.secret_key = base64.b64encode(os.urandom(64)).decode('utf-8') +app.secret_key = base64.b64encode(os.urandom(64)).decode("utf-8") TC = TalerConfig.from_env() BACKEND_BASE_URL = TC["frontends"]["backend"].value_string(required=True) @@ -81,6 +79,7 @@ def backend_instanced_get(instance, endpoint, params): backend_url = urljoin(BACKEND_BASE_URL, f"instances/{instance}/") return backend_get(backend_url, endpoint, params) + ## # POST a request to the backend, and return a error # response if any error occurs. @@ -93,6 +92,7 @@ def backend_instanced_post(instance, endpoint, json): backend_url = urljoin(BACKEND_BASE_URL, f"instances/{instance}/") return backend_post(backend_url, endpoint, json) + ## # Inspect GET arguments in the look for a parameter. # @@ -114,9 +114,7 @@ def expect_parameter(name): @app.errorhandler(Exception) def internal_error(e): return flask.render_template( - "templates/error.html", - message="Internal error", - stack=traceback.format_exc() + "templates/error.html", message="Internal error", stack=traceback.format_exc() ) @@ -126,9 +124,7 @@ def internal_error(e): # @return response object of the index page. @app.route("/") def index(): - return flask.render_template( - "templates/index.html", merchant_currency=CURRENCY - ) + return flask.render_template("templates/index.html", merchant_currency=CURRENCY) ## @@ -156,7 +152,7 @@ def checkout(): donation_amount=amount, donation_receiver=donation_receiver, donation_donor=donation_donor, - merchant_currency=CURRENCY + merchant_currency=CURRENCY, ) @@ -189,47 +185,26 @@ def donate(): "fulfillment", timestamp=str(time.time()), receiver=donation_receiver, - _external=True + _external=True, ) order = dict( amount=donation_amount, extra=dict( - donor=donation_donor, - receiver=donation_receiver, - amount=donation_amount + donor=donation_donor, receiver=donation_receiver, amount=donation_amount ), fulfillment_url=fulfillment_url, summary="Donation to {}".format(donation_receiver), - refund_deadline=dict(t_ms=1000*int(time.time() + 10 * 30)), - wire_transfer_deadline=dict(t_ms=1000*int(time.time() + 15 * 30)) + refund_deadline=dict(t_ms=1000 * int(time.time() + 10 * 30)), + wire_transfer_deadline=dict(t_ms=1000 * int(time.time() + 15 * 30)), + ) + order_resp = backend_instanced_post( + donation_receiver, "private/orders", dict(order=order) ) - order_resp = backend_instanced_post(donation_receiver, "private/orders", dict(order=order)) order_id = order_resp["order_id"] return flask.redirect( - flask.url_for( - "fulfillment", receiver=donation_receiver, order_id=order_id - ) + flask.url_for("fulfillment", receiver=donation_receiver, order_id=order_id) ) -## -# This endpoint is used by the payment request page -# to check if the payment has been completed via the QR code. -@app.route("/check-status//") -def check_status(instance, order_id): - pay_params = dict(order_id=order_id) - pay_status = backend_instanced_get( - instance, - f"private/orders/{order_id}", - params=dict() - ) - return flask.jsonify(paid=pay_status["paid"]) - - -def get_qrcode_svg(data): - factory = qrcode.image.svg.SvgImage - img = qrcode.make(data, image_factory=factory) - return lxml.etree.tostring(img.get_image()).decode("utf-8") - ## # Serve the fulfillment page. @@ -244,11 +219,10 @@ def fulfillment(receiver): order_id = expect_parameter("order_id") pay_params = dict(order_id=order_id) pay_status = backend_instanced_get( - receiver, - f"private/orders/{order_id}", - params=dict() + receiver, f"private/orders/{order_id}", params=dict() ) - if pay_status.get("paid"): + order_status = pay_status.get("order_status") + if order_status == "paid": extra = pay_status["contract_terms"]["extra"] return flask.render_template( "templates/fulfillment.html", @@ -256,19 +230,6 @@ def fulfillment(receiver): donation_amount=extra["amount"], donation_donor=extra["donor"], order_id=order_id, - currency=CURRENCY + currency=CURRENCY, ) - taler_pay_uri = pay_status["taler_pay_uri"] - qrcode_svg = get_qrcode_svg(taler_pay_uri) - check_status_url_enc = urllib.parse.quote( - flask.url_for("check_status", instance=receiver, order_id=order_id) - ) - content = flask.render_template( - "templates/request_payment.html", - taler_pay_uri=taler_pay_uri, - qrcode_svg=qrcode_svg, - check_status_url_enc=check_status_url_enc - ) - headers = {"Taler": taler_pay_uri} - resp = flask.Response(content, status=402, headers=headers) - return resp + return flask.redirect(pay_status["order_status_url"]) diff --git a/talermerchantdemos/donations/templates/request_payment.html b/talermerchantdemos/donations/templates/request_payment.html deleted file mode 100644 index 6e050d0..0000000 --- a/talermerchantdemos/donations/templates/request_payment.html +++ /dev/null @@ -1,69 +0,0 @@ -{% extends "templates/base.html" %} - - -{% block meta %} - -{% endblock meta %} - - -{% block scripts %} - -{% endblock scripts %} - - -{% block main %} - -

Payment Required

- -
-

- Looks like your browser doesn't support GNU Taler payments. You can try - installing a wallet browser extension. -

-
- -
- -

- You can use this QR code to pay with your mobile wallet: -

- - {{ qrcode_svg | safe }} - -

- Click this link to open your system's Taler wallet if it exists. -

- -
- -{% endblock main %} -- cgit v1.2.3