From db7747cda040e55eba7e345b6ac01562be16d5ba Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 27 Oct 2020 20:58:21 +0100 Subject: [PATCH 2/2] misc: add cli tool for testing (#48064) --- README.txt | 42 +++++++++++++++++++++++ eopayment/__main__.py | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 eopayment/__main__.py diff --git a/README.txt b/README.txt index e82cdba..2b85848 100644 --- a/README.txt +++ b/README.txt @@ -69,3 +69,45 @@ You can test your PayFiP regie web-service connection with an integrated CLI uti Options: --help Show this message and exit. + + +Generic CLI Tool +================ + +You can put some configuration in ~/.config/eopayment.init :: + + [misc] + debug=yes + + [systempayv2] + # same name as passed in the options argument to Payment.__init__() + vads_site_id=12345678 + secret_test=xyzabcdefgh + vads_ctx_mode=TEST + + $ python3 -m eopayment --option vads_site_id=56781234 request 10.0 --param transaction_id=1234 --param email=john.doe@example.com + Transaction ID: 1234 +
+ + + + + + + + + + + + + + + + + + + + + +
+ [ Local browser is automatically opened with this form which is auto-submitted ] diff --git a/eopayment/__main__.py b/eopayment/__main__.py new file mode 100644 index 0000000..6accd55 --- /dev/null +++ b/eopayment/__main__.py @@ -0,0 +1,78 @@ +# eopayment - online payment library +# Copyright (C) 2011-2020 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from __future__ import print_function + +import configparser +import decimal +import logging +import os.path +import subprocess +import tempfile + +import click + +from . import Payment, FORM, URL + + +def option(value): + try: + name, value = value.split('=', 1) + except Exception: + raise ValueError('invalid option %s' % value) + return (name, value) + + +@click.group() +@click.argument('backend', type=str) +@click.option('--option', type=option, multiple=True) +@click.option('--debug/--no-debug', default=None) +@click.pass_context +def main(ctx, backend, debug, option): + config_file = os.path.expanduser('~/.config/eopayment.ini') + option = list(option) + if os.path.exists(config_file): + parser = configparser.ConfigParser(interpolation=None) + with open(config_file) as fd: + parser.read_file(fd) + if debug is None: + debug = parser.getboolean('misc', 'debug', fallback=False) + if parser.has_section(backend): + option.extend(parser.items(section=backend)) + if debug: + logging.basicConfig(level=logging.DEBUG) + ctx.obj = Payment(backend, dict(option)) + + +@main.command() +@click.argument('amount', type=decimal.Decimal) +@click.option('--param', type=option, multiple=True) +@click.pass_obj +def request(backend, amount, param): + transaction_id, kind, what = backend.request(amount, **dict(param)) + print('Transaction ID:', transaction_id) + if kind == FORM: + print(what) + with tempfile.NamedTemporaryFile(mode='w', delete=False) as fd: + print(what, file=fd) + print('''''', file=fd) + subprocess.call(['gnome-www-browser', fd.name]) + elif kind == URL: + print('Please click on URL:', what) + subprocess.call(['gnome-www-browser', what]) + + +main() -- 2.28.0