summaryrefslogtreecommitdiff
path: root/talerdonations/donations/donations.py
blob: 56a8c1e277955fb0209ca9795a5473d4f5b10179 (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
# This file is part of GNU TALER.
# Copyright (C) 2014-2016 INRIA
#
# 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

from urllib.parse import urljoin, parse_qsl
import logging
import os
import base64
import random
from datetime import datetime
import requests
import flask
from talerdonations.talerconfig import TalerConfig
from talerdonations.helpers import (make_url, \
    expect_parameter, amount_from_float, \
    amount_to_float, join_urlparts, get_query_string, \
    MissingParameterException, backend_error)

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()
BACKEND_URL = TC["frontends"]["backend"].value_string(required=True)
CURRENCY = TC["taler"]["currency"].value_string(required=True)
MAX_FEE = dict(value=3, fraction=0, currency=CURRENCY)

app.config.from_object(__name__)

@app.context_processor
def utility_processor():
    def url(my_url):
        return join_urlparts(flask.request.script_root, my_url)
    def env(name, default=None):
        return os.environ.get(name, default)
    return dict(url=url, env=env)


@app.route("/")
def index():
    return flask.render_template("templates/index.html", merchant_currency=CURRENCY)

@app.route("/javascript")
def javascript_licensing():
    return flask.render_template("templates/javascript.html")


@app.route("/checkout", methods=["GET"])
def checkout():
    amount_str = expect_parameter("donation_amount")
    donation_receiver = expect_parameter("donation_receiver")
    try:
        float(amount_str)
    except ValueError:
        LOGGER.warning("Invalid amount ('%s')", amount_str)
        return flask.make_response(
            flask.jsonify(error="invalid amount"),
            400)
    display_alert = flask.request.args.get("display_alert", None)
    return flask.render_template(
        "templates/checkout.html",
        donation_amount=amount_str,
        donation_receiver=donation_receiver,
        merchant_currency=CURRENCY,
        display_alert=display_alert)


@app.route("/generate-contract", methods=["GET"])
def generate_contract():
    try:
        donation_receiver = expect_parameter("donation_receiver")
        donation_amount = expect_parameter("donation_amount")
    except MissingParameterException as exc:
        return flask.jsonify(
            dict(error="Missing parameter '%s'" % exc)), 400
    amount = amount_from_float(float(donation_amount))
    order_id = "donation-%s-%X-%s" % \
               (donation_receiver,
                random.randint(0, 0xFFFFFFFF),
                datetime.today().strftime('%H_%M_%S'))
    order = dict(
        summary="Donation!",
        nonce=flask.request.args.get("nonce"),
        amount=amount,
        max_fee=dict(value=1, fraction=0, currency=CURRENCY),
        order_id=order_id,
        products=[
            dict(
                description="Donation to %s" % (donation_receiver,),
                quantity=1,
                product_id=0,
                price=amount,
            ),
        ],
        fulfillment_url=make_url("/fulfillment", ("order_id", order_id)),
        pay_url=make_url("/pay"),
        merchant=dict(
            instance=donation_receiver,
            address="nowhere",
            name="Kudos Inc.",
            jurisdiction="none",
        ),
    )
    resp = requests.post(urljoin(BACKEND_URL, 'proposal'),
                         json=dict(order=order))
    if resp.status_code != 200:
        # It is important to use 'backend_error()', as it handles
        # the case where the backend gives NO JSON as response.
        # For example, if it dies, or nginx hijacks somehow the
        # response.
        return backend_error(resp)
    return flask.jsonify(resp.json()), resp.status_code

@app.route("/donate")
def donate():
    donation_receiver = expect_parameter("donation_receiver")
    donation_amount = expect_parameter("donation_amount")
    payment_system = expect_parameter("payment_system")
    if payment_system != "taler":
        return flask.redirect(make_url("checkout",
                                       ("donation_receiver", donation_receiver),
                                       ("donation_amount", donation_amount),
                                       ("display_alert", True)))
    response = flask.make_response(flask.render_template('templates/fallback.html'), 402)
    response.headers["X-Taler-Contract-Url"] = \
    make_url("/generate-contract",
             ("donation_receiver", donation_receiver),
             ("donation_amount", donation_amount))
    return response


@app.route("/fulfillment")
def fulfillment():
    order_id = expect_parameter("order_id")
    payed_order_ids = flask.session.get("payed_order_ids", [])
    print("order_id:", repr(order_id))
    print("session:", repr(flask.session))
    if order_id in payed_order_ids:
        data = payed_order_ids[order_id]
        return flask.render_template(
            "templates/fulfillment.html",
            donation_receiver=data["donation_receiver"],
            donation_amount=data["donation_amount"],
            order_id=order_id,
            currency=CURRENCY)

    response = flask.make_response(flask.render_template("templates/fallback.html"), 402)
    response.headers["X-Taler-Contract-Query"] = "fulfillment_url"
    response.headers["X-Taler-Offer-Url"] = make_url("/")
    return response


@app.route("/pay", methods=["POST"])
def pay():
    deposit_permission = flask.request.get_json()
    if deposit_permission is None:
        return flask.jsonify(error="no json in body"), 400
    resp = requests.post(urljoin(BACKEND_URL, "pay"),
                         json=deposit_permission)
    if resp.status_code != 200:
        return backend_error(resp)
    proposal_data = resp.json()["contract_terms"]
    order_id = proposal_data["order_id"]
    payed_order_ids = flask.session["payed_order_ids"] \
        = flask.session.get("payed_order_ids", {})
    payed_order_ids[order_id] = dict(
        donation_receiver=proposal_data["merchant"]["instance"],
        donation_amount=amount_to_float(proposal_data["amount"])
    )
    print("received payment for", order_id)
    return flask.jsonify(resp.json()), resp.status_code

@app.route("/backoffice")
def track():
    response = flask.make_response(flask.render_template("templates/backoffice.html"))
    return response


@app.route("/history")
def history():
    qs = get_query_string().decode("utf-8")
    url = urljoin(BACKEND_URL, "history")
    resp = requests.get(url, params=dict(parse_qsl(qs)))
    if resp.status_code != 200:
        return backend_error(resp)
    return flask.jsonify(resp.json()), resp.status_code


@app.route("/track/order")
def track_order():
    instance = expect_parameter("instance")
    order_id = expect_parameter("order_id")
    url = urljoin(BACKEND_URL, "track/transaction")
    resp = requests.get(url, params=dict(order_id=order_id, instance=instance))
    if resp.status_code != 200:
        return backend_error(resp)
    return flask.jsonify(resp.json()), resp.status_code