summaryrefslogtreecommitdiff
path: root/talermerchantdemos/donations/donations.py
blob: 19fc235ee7311abe97b8f0d9c0465113481ade86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
##
# This file is part of GNU TALER.
# Copyright (C) 2014-2016, 2020 Taler Systems SA
#
# TALER is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 2.1, or (at your option) any later version.
#
# TALER is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# GNU TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
#
# @author Florian Dold
# @author Marcello Stanisci
# @brief Implementation of a donations site.

import base64
import logging
import flask
from flask import request
from flask_babel import Babel
from flask_babel import refresh
from flask_babel import force_locale
from flask_babel import gettext
import os
import time
import traceback
import urllib
from taler.util.talerconfig import TalerConfig, ConfigurationError
from urllib.parse import urljoin
from ..httpcommon import backend_post, backend_get, fallback_404, self_localized
import sys

if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
    print("Python 3.6 or higher is required.")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
    sys.exit(1)

LOGGER = logging.getLogger(__name__)

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")

TC = TalerConfig.from_env()
try:
    BACKEND_BASE_URL = TC["frontends"]["backend"].value_string(required=True)
    CURRENCY = TC["taler"]["currency"].value_string(required=True)
    APIKEY = TC["frontends"]["backend_apikey"].value_string(required=True)
except ConfigurationError as ce:
    print(ce)
    exit(1)

app.config.from_object(__name__)
babel = Babel(app)


LOGGER.info("Using translations from:" + ':'.join(list(babel.translation_directories)))
translations = [str(translation) for translation in babel.list_translations()]
if not 'en' in translations:
    translations.append('en')
LOGGER.info("Operating with the following translations available: " + ' '.join(translations))

app.jinja_env.globals.update(self_localized=self_localized)


@babel.localeselector
def get_locale():
    parts = request.path.split('/', 2)
    if (2 >= len(parts)):
        # Totally unexpected path format, do not localize
        return "en"
    lang = parts[1]
    if lang in translations:
        return lang
    return "en"


##
# Extend the templating language with a function
# that fetches values from the environment.
#
# @return the environment-reading function.
@app.context_processor
def utility_processor():
    def env(name, default=None):
        return os.environ.get(name, default)

    return dict(env=env)


##
# Return a error response to the client.
#
# @param abort_status_code status code to return along the response.
# @param params _kw_ arguments to passed verbatim to the templating engine.
def err_abort(abort_status_code, **params):
    t = flask.render_template(
        "templates/error.html.j2",
        lang=get_locale(),
        **params)
    flask.abort(flask.make_response(t, abort_status_code))


##
# Issue a GET request to the backend.
#
# @param endpoint the backend endpoint where to issue the request.
# @param params (dict type of) URL parameters to append to the request.
# @return the JSON response from the backend, or a error response
#         if something unexpected happens.
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.
#
# @param endpoint the backend endpoint where to POST
#        this request.
# @param json the POST's body.
# @return the backend response (JSON format).
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.
#
# @param name the parameter name to lookup.
# @return the parameter value, or a error page if not found.
def expect_parameter(name):
    val = flask.request.args.get(name)
    if not val:
        return err_abort(400, message=gettext("parameter '{}' required").format(name))
    return val


##
# "Fallback" exception handler to capture all the unmanaged errors.
#
# @param e the Exception object, currently unused.
# @return flask-native response object carrying the error message
#         (and execution stack!).
@app.errorhandler(Exception)
def internal_error(e):
    return flask.render_template(
        "templates/error.html.j2",
        message=gettext("Internal error"),
        lang=get_locale(),
        stack=traceback.format_exc()
    )

##
# Serve the /favicon.ico requests.
#
# @return the favicon.ico file.
@app.route("/favicon.ico")
def favicon():
    LOGGER.info("will look into: " + os.path.join(app.root_path, 'static'))
    return flask.send_from_directory(
        os.path.join(app.root_path, 'static'),
        "favicon.ico",
        mimetype="image/vnd.microsoft.ico"
    )

##
# Serve the main index page, redirecting to /<lang>/
#
# @return response object of the index page.
@app.route("/")
def index():
    default = 'en'
    target = flask.request.accept_languages.best_match(translations, default)
    return flask.redirect("/" + target + "/", code=302)


##
# Serve the main index page.
#
# @return response object of the index page.
@app.route("/<lang>/")
def start(lang):
    return flask.render_template(
        "templates/index.html.j2",
        lang=lang,
        merchant_currency=CURRENCY
    )


##
# Serve the "/javascript" page.
#
# @return response object for the /javascript page.
@app.route("/javascript")
def javascript_licensing():
    return flask.render_template("templates/javascript.html")


##
# Serve the "/checkout" page.  This page lets the
# user pick the payment method they want to use,
# and finally confirm the donation.
#
# @return response object for the /checkout page.
@app.route("/<lang>/checkout", methods=["GET"])
def checkout(lang):
    amount = expect_parameter("donation_amount")
    donation_receiver = expect_parameter("donation_receiver")
    donation_donor = expect_parameter("donation_donor")
    return flask.render_template(
        "templates/checkout.html.j2",
        donation_amount=amount,
        donation_receiver=donation_receiver,
        donation_donor=donation_donor,
        lang=lang,
        merchant_currency=CURRENCY,
    )


##
# Serve the page advising the user about the impossibility
# of further processing the payment method they chose.
#
# @return response object about the mentioned impossibility.
@app.route("/<lang>/provider-not-supported")
def provider_not_supported():
    return flask.render_template(
        "templates/provider-not-supported.html.j2",
        lang=lang
    )


##
# POST the donation request to the backend.  In particular,
# it uses the "POST /order" API.
#
# @return response object that will redirect the browser to
#         the fulfillment URL, where all the pay-logic will
#         happen.
@app.route("/<lang>/donate")
def donate(lang):
    donation_receiver = expect_parameter("donation_receiver")
    donation_amount = expect_parameter("donation_amount")
    donation_donor = expect_parameter("donation_donor")
    payment_system = expect_parameter("payment_system")
    if payment_system != "taler":
        return flask.redirect(flask.url_for("provider_not_supported",
                                            lang=lang))
    fulfillment_url = flask.url_for(
        "fulfillment",
        timestamp=str(time.time()),
        lang=lang,
        receiver=donation_receiver,
        _external=True,
    )
    fulfillment_url = fulfillment_url + "&order_id=${ORDER_ID}"
    order = dict(
        amount=donation_amount,
        extra=dict(
            donor=donation_donor,
            receiver=donation_receiver,
            amount=donation_amount
        ),
        fulfillment_url=fulfillment_url,
        summary="Donation to {}".format(donation_receiver),
        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_id = order_resp["order_id"]
    return flask.redirect(
        flask.url_for("fulfillment",
                      receiver=donation_receiver,
                      lang=lang,
                      order_id=order_id)
    )


##
# Serve the fulfillment page.
#
# @param receiver the donation receiver name, that should
#        correspond to a merchant instance.
# @return after the wallet sent the payment, the final HTML "congrats"
#         page is returned; otherwise, the browser will be redirected
#         to a page that accepts the payment.
@app.route("/<lang>/donation/<receiver>")
def fulfillment(lang,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()
    )
    order_status = pay_status.get("order_status")
    if order_status == "paid":
        extra = pay_status["contract_terms"]["extra"]
        return flask.render_template(
            "templates/fulfillment.html",
            donation_receiver=extra["receiver"],
            donation_amount=extra["amount"],
            donation_donor=extra["donor"],
            order_id=order_id,
            lang=lang,
            currency=CURRENCY,
        )
    return flask.redirect(pay_status["order_status_url"])

@app.errorhandler(404)
def handler(e):
    return flask.render_template(
        "templates/error.html.j2",
        lang=get_locale(),
        message=gettext("Page not found")
    )