SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 06b7f7aed22a8a52ebc903bed9370d86dbf7905c
parent 5a26b3d4c4c3700ffe6e7c539fd540cd2bb4a1b0
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Tue, 15 Dec 2020 01:00:14 +1100

Bitcoind rpc, invoice and payment processing. Expanded config options.

Diffstat:
Minvoice/__init__.py | 5++++-
Amain.py | 47+++++++++++++++++++++++++++++++++++++++++++++++
Apay/__init__.py | 0
Apay/bitcoind.py | 48++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/invoice/__init__.py b/invoice/__init__.py @@ -1,5 +1,8 @@ from .price_feed import get_btc_value class invoice(): - def __init__(self, dollar_value, currency): + def __init__(self, dollar_value, currency, label): + self.dollar_value = dollar_value + self.currency = currency self.value = get_btc_value(dollar_value, currency) + self.label = label diff --git a/main.py b/main.py @@ -0,0 +1,47 @@ +import config +import invoice +from pay import bitcoind + +import time +import subprocess + +# If tunnel is required (might make things easier) +try: + if config.tunnel_host is not None: + command = ['ssh', config.tunnel_host, '-L', '{}:localhost:{}'.format(config.rpcport, config.rpcport), '-q'] + print("Opening tunnel to {}.".format(' '.join(command))) + tunnel_proc = subprocess.Popen(['ssh', config.tunnel_host, '-L', '{}:localhost:{}'.format(config.rpcport, config.rpcport)]) + else: + tunnel_proc = None +except Exception: + tunnel_proc = None + pass + + +inv = invoice.invoice(1.8, "USD", "test invoice232") +payment = bitcoind.btcd() +payment.load_invoice(inv) +payment.get_address() + +start_time = time.time() +while time.time() - start_time < config.payment_timeout: + conf_paid, unconf_paid = payment.check_payment() + print(conf_paid, unconf_paid) + + if conf_paid > payment.value: + print("Invoice {} paid! {} BTC.".format(payment.label, conf_paid)) + payment.paid = True + break + else: + time.sleep(15) +else: + print("Invoice {} expired.".format(payment.label)) + +if payment.paid: + print("Returning paid to site.") +else: + print("Reload page, etc. need to create new invoice") + +if tunnel_proc is not None: + tunnel_proc.kill() + diff --git a/pay/__init__.py b/pay/__init__.py diff --git a/pay/bitcoind.py b/pay/bitcoind.py @@ -0,0 +1,48 @@ +import config +import subprocess + +class btcd: + def __init__(self): + from bitcoinrpc.authproxy import AuthServiceProxy + + connection_str = "http://{}:{}@{}:{}".format(config.username, config.password, config.host, config.rpcport) + print("Attempting to connect to {}.".format(connection_str)) + + try: + self.rpc = AuthServiceProxy(connection_str) + info = self.rpc.getblockchaininfo() + info = self.rpc.help() + + print("Successfully contacted bitcoind.") + print("-"*10) + print(info) + print("-"*10) + + except Exception as e: + print(e) + + def load_invoice(self, invoice): + self.value = invoice.value + self.label = invoice.label + self.paid = False + return + + def check_payment(self): + self.address = "bc1qwxlwghumfmhwdc2deyn7h42syp2t496penax2y" + transactions = self.rpc.listtransactions() + relevant_txs = [tx for tx in transactions if tx['address'] == self.address] + + conf_paid = 0 + unconf_paid = 0 + for tx in relevant_txs: + if tx['confirmations'] >= config.required_confirmations: + conf_paid += tx['amount'] + else: + unconf_paid += tx['amount'] + + return conf_paid, unconf_paid + + def get_address(self): + self.address = self.rpc.getnewaddress(self.label) + return +