diff options
author | MS <ms@taler.net> | 2020-07-28 17:04:50 +0200 |
---|---|---|
committer | MS <ms@taler.net> | 2020-07-28 17:04:50 +0200 |
commit | 32fdf9a36ec3550131b2090b6de9f0206cd1a8a1 (patch) | |
tree | f18eae44c1b68ebd24b60fccb2c9f59ce43b6932 | |
parent | a10d1b6354d0ebd97b296943b273164da4d8b783 (diff) | |
download | bank-32fdf9a36ec3550131b2090b6de9f0206cd1a8a1.tar.gz bank-32fdf9a36ec3550131b2090b6de9f0206cd1a8a1.tar.bz2 bank-32fdf9a36ec3550131b2090b6de9f0206cd1a8a1.zip |
Extracting subject value from payto URI.
-rw-r--r-- | talerbank/app/management/commands/wire_transfer_payto.py | 102 | ||||
-rw-r--r-- | talerbank/app/views.py | 23 |
2 files changed, 124 insertions, 1 deletions
diff --git a/talerbank/app/management/commands/wire_transfer_payto.py b/talerbank/app/management/commands/wire_transfer_payto.py new file mode 100644 index 0000000..9eab068 --- /dev/null +++ b/talerbank/app/management/commands/wire_transfer_payto.py @@ -0,0 +1,102 @@ +## +# This file is part of TALER +# (C) 2014, 2015, 2016 Taler Systems SA +# +# TALER is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 3, 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with TALER; see the file COPYING. If not, see +# <http://www.gnu.org/licenses/> +# +# @author Marcello Stanisci +# @brief CLI utility that issues a wire transfer. + +import sys +import logging +import json +from django.core.management.base import BaseCommand +from django.contrib.auth import authenticate +from taler.util.amount import Amount +from ...views import wire_transfer, User, PaytoParse +from ...models import BankAccount, BankTransaction + +LOGGER = logging.getLogger(__name__) + + +## +# Django-specific definition to register the CLI utility. +class Command(BaseCommand): + help = "Wire transfer money and return the transaction id." + + ## + # Register the command line options of this command. + # + # @param self this object. + # @param parser API used to actually register the option. + def add_arguments(self, parser): + parser.add_argument( + "user", + type=str, + metavar="USERNAME", + help="Which user is performing the wire transfer", + ) + parser.add_argument( + "password", type=str, metavar="PASSWORD", help="Performing user's password." + ) + parser.add_argument( + "payto-credit-account-with-subject", + type=str, + metavar="PAYTO-CREDIT-ACCOUNT-WITH-SUBJECT", + help="Which account will receive money, in the form 'payto://x-taler-bank/[bank-host/]CreditAccountName?subject=PaymentSubject'.", + ) + parser.add_argument( + "amount", + type=str, + metavar="AMOUNT", + help="Wire transfer's amount, given in the " "CURRENCY:X.Y form.", + ) + + ## + # This callable gets invoked when the user invokes the + # CLI utility; it is responsible of making the wire transfer + # effective. + # + # @param self this object. + # @param args arguments list -- currently unused. + # @param options options given by the user at the command line. + def handle(self, *args, **options): + user = authenticate(username=options["user"], password=options["password"]) + if not user: + LOGGER.error("Wrong user/password.") + sys.exit(1) + try: + amount = Amount.parse(options["amount"]) + except BadFormatAmount: + LOGGER.error("Amount's format is wrong: respect C:X.Y.") + sys.exit(1) + try: + parsed_payto = PaytoParse(options["payto-credit-account-with-subject"]) + credit_account_user = User.objects.get(username=parsed_payto.account) + credit_account = credit_account_user.bankaccount + except BankAccount.DoesNotExist: + LOGGER.error("Credit account does not exist.") + sys.exit(1) + if not parsed_payto.subject: + print("Please provide 'subject' parameter along the payto URI") + sys.exit(1) + try: + transaction = wire_transfer( + amount, user.bankaccount, credit_account, parsed_payto.subject + ) + print("Transaction id: " + str(transaction.id)) + except Exception as exc: + LOGGER.error(exc) + sys.exit(1) diff --git a/talerbank/app/views.py b/talerbank/app/views.py index be2a38b..a6687a8 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -28,7 +28,7 @@ import re import time import base64 import uuid -from urllib.parse import urlparse +from urllib.parse import urlparse, parse_qs import django.contrib.auth import django.contrib.auth.views import django.contrib.auth.forms @@ -69,6 +69,18 @@ LOGGER = logging.getLogger(__name__) # can handle (because of the wallet). UINT64_MAX = (2 ** 64) - 1 +class PaytoParse: + def __init__(self, payto_uri): + parsed_payto = urlparse(payto_uri) + if parsed_payto.scheme != "payto": + raise Exception("Bad Payto URI: '%s'" % payto_uri) + path_as_list = parsed_payto.path.split("/") + if (len(path_as_list) == 0): + raise Exception("No account/user name found: '%s'" % payto_uri) + self.account = path_as_list[-1] + params = parse_qs(parsed_payto.query) + self.subject = params.get("subject") + ## # Exception raised upon failing login. # @@ -383,6 +395,15 @@ def get_acct_from_payto(uri_str: str) -> str: raise Exception("Bad Payto URI: '%s'" % uri_str) return wire_uri.path.split("/")[-1] +def get_subject_from_payto(uri_str: str) -> str: + wire_uri = urlparse(uri_str) + if wire_uri.scheme != "payto": + raise Exception("Bad Payto URI: '%s'" % uri_str) + params = parse_qs(wire_uri.query) + subject = params.get("subject") + if not subject: + raise Exception("Subject not found in Payto URI: '%s'" % uri_str) + return subject ## # Class representing the registration form. |