def pairing(code, host): privkey = generate_privkey() btc_client = BTCPayClient(host=host, pem=privkey) btc_token = btc_client.pair_client(code) btc_client = BTCPayClient(host=host, pem=privkey, tokens=btc_token) client_store = BTCPayClientStore.query.first() if client_store is None: client_store = BTCPayClientStore(client=btc_client) db.session.add(client_store) else: client_store.client = btc_client db.session.commit()
def start_payment(transaction_id, amount): client = BTCPayClient( host=settings.ZENAIDA_BTCPAY_HOST, pem=settings.ZENAIDA_BTCPAY_CLIENT_PRIVATE_KEY, tokens={"merchant": settings.ZENAIDA_BTCPAY_MERCHANT}) btcpay_invoice = client.create_invoice({"price": 1, "currency": "USD"}) BTCPayInvoice.invoices.create(transaction_id=transaction_id, invoice_id=btcpay_invoice.get('id'), status=btcpay_invoice.get('status'), amount=amount) return HttpResponseRedirect(btcpay_invoice.get("url"))
def client_setup(pairing_code): client = BTCPayClient.create_client(host='http://localhost:14142', code=pairing_code) client_connector = PaymentProcessor(client=client) db.session.add(client_connector) db.session.commit() return jsonify({'code': pairing_code, 'url': 'http://localhost:14142'})
def on_model_change(self, form, model: Users, is_created: bool): if is_created: raise Exception('Can not create users') if model.btcpay_host is None: model.btcpay_client = None if model.btcpay_pairing_code is not None: model.btcpay_client = BTCPayClient.create_client( host=model.btcpay_host, code=model.btcpay_pairing_code) model.btcpay_pairing_code = None
def authbtc(): # accepts BTCPay pairing code and calls pairing fn status = login(request.cookies) if status is not None: return redirect(status) form = BTCCodeForm() url = urljoin(str(os.getenv('BTCPAY_HOST')), 'api-tokens') if form.validate_on_submit(): client = BTCPayClient.create_client( host=app.config.get('BTCPAY_HOST'), code=form.code.data, ) save('btc_client', client) save('forward_url', form.forward_url.data) flash('Pairing to BTCPay Successful') return render_template('index.html') return render_template( 'authbtc.html', title='Enter Code', form=form, url=url )
def get_btcpay_client(cfg): """Reconstruct client credentials""" try: from btcpay import BTCPayClient except ImportError: print('btcpay_not_installed') sys.exit() # extract host, private key and merchant token host = cfg.invoice.btcpay.host pem = cfg.invoice.btcpay.pem tokens = dict(merchant=cfg.invoice.btcpay.tokens.merchant) # see if private key points to a pem file # pem_filename = hydra.utils.to_absolute_path(cfg.invoice.btcpay.pem) pem_filename = os.path.abspath(cfg.invoice.btcpay.pem) if path.exists(pem_filename): with open(pem_filename) as pem_file: pem = pem_file.read() client = BTCPayClient(host=host, pem=pem, tokens=tokens) return client
def _create_new_client(token, url=None): ''' creates new client this only needs to be done once, since it will be saved in the database get the pairing code from btc pay when you pair example: Server initiated pairing code: XXXXXX TOKENS only work once, if you get a weird http /tokens not found error, generate a new one and try again python >>> from btcpay import BTCPayClient >>> url = 'http://mynode.local:49393' >>> client = BTCPayClient.create_client(host=url, code=token) ''' if url is None: url = 'http://localhost:23001' client = BTCPayClient.create_client(host=url, code=token) pickled = pickle.dumps(client) db = get_db() db.execute( '''INSERT INTO btc_pay_server_client (pickled_client) VALUES (?)''', (pickled,) ) db.commit()
API_HOST = "https://testnet.demo.btcpayserver.org" #Testnet BTCPAY KEY_FILE = "/tmp/key.priv" TOKEN_FILE = "/tmp/btctoken.priv" if os.path.isfile(KEY_FILE): f = open(KEY_FILE, 'r') key = f.read() f.close() print("Creating a bitpay client using existing private key from disk.") else: key = btcpay.crypto.generate_privkey() f = open(KEY_FILE, 'w') f.write(key) f.close() client = BTCPayClient(host=API_HOST, pem=key) def fetch_token(facade, pairingCode): if os.path.isfile(TOKEN_FILE + facade): f = open(TOKEN_FILE + facade, 'r') token = f.read() f.close() client.tokens[facade] = token else: token = client.pair_client(pairingCode) client.tokens[facade] = token f = open(TOKEN_FILE + facade, 'w') print(token[facade]) f.write(token[facade]) f.close()
def btcpay_tor_server_setup(params): return BTCPayClient.create_tor_client( host=params['host'], code=params['code'], proxy='socks5h://127.0.0.1:9050', )
def handle(self, *args, **options): client = BTCPayClient( host=settings.ZENAIDA_BTCPAY_HOST, pem=settings.ZENAIDA_BTCPAY_CLIENT_PRIVATE_KEY, tokens={"merchant": settings.ZENAIDA_BTCPAY_MERCHANT}) while True: # Check if BTCPay server is up and running. try: client.get_rate("USD") except: logger.exception( "BTCPay server connection problem while getting rates.") time.sleep(60) continue logger.debug('check payments at %r', timezone.now().strftime("%Y-%m-%d %H:%M:%S")) # Check status of all incomplete invoices. incomplete_invoices = BTCPayInvoice.invoices.filter( finished_at=None) for invoice in incomplete_invoices: try: btcpay_resp = client.get_invoice(invoice.invoice_id) except: logger.exception( "BTCPay server connection problem while checking invoice payment status." ) break # If status is new, there is not any update yet on BTCPay server, so move to the next invoice. if btcpay_resp.get('status') == 'new': logger.debug(f'active btcpay invoice: {invoice}') continue # If invoice is paid, process the payment in the database as paid. # Else, payment is not done, so decline the payment in the database. if btcpay_resp['btcPaid'] >= btcpay_resp['btcPrice']: logger.debug(f'paid btcpay invoice: {invoice}') payment_status = 'processed' btcpay_invoice_status = 'paid' else: logger.debug(f'expired btcpay invoice: {invoice}') payment_status = 'declined' btcpay_invoice_status = 'expired' if not payments.finish_payment( transaction_id=invoice.transaction_id, status=payment_status): logger.critical( f'payment failed to be completed, transaction_id={invoice.transaction_id}' ) continue invoice.status = btcpay_invoice_status invoice.finished_at = timezone.now() invoice.save() logger.info( f'payment is {payment_status} because it is {btcpay_invoice_status}, ' f'transaction_id={invoice.transaction_id}') time.sleep(60)
from btcpay import BTCPayClient from os import environ pem = open('btcpayserver.pem', 'r') privkey = pem.read() host_store = "http://raspberrypi.local" tok = open('merchant_token', 'r') token = tok.read() client = BTCPayClient( host = host_store, pem = privkey, tokens = {'merchant': token} ) new_invoice = client.create_invoice({"price": 20, "currency": "USD"}) print(new_invoice)
def client(self): from btcpay import BTCPayClient return BTCPayClient(host=self.settings.url, pem=self.settings.pem, tokens={'merchant': self.settings.token})
def initialize_btcpay(cfg): try: from btcpay import BTCPayClient except ImportError: print('btcpay_not_installed') sys.exit() btcpay = cfg.invoice.btcpay host = input("Enter your btcpay server's host name (blank to quit) :") cfg.invoice.btcpay.host = host if len(cfg.invoice.btcpay.host) == 0: sys.exit() print(btcpay_instructions.format(btcpay.host)) generate_privkey = input( "Should hourly generate your private key? (yes/n) ") if generate_privkey.lower() == 'yes': try: from btcpay import crypto except ImportError(): print(btcpay_not_installed) sys.exit() btcpay.pem = crypto.generate_privkey() save_privkey = input("Should hourly save your private key? (yes/n) ") if save_privkey == 'yes': pem_file = input( "Enter private key filename (leave blank for btcpayserver.pem):" ) if len(pem_file) == 0: pem_file = 'btcpayserver.pem' if path.exists(pem_file): print("File already exists! Exiting.") sys.exit() with open(pem_file, 'w') as pem: pem.write(btcpay.pem) print("private key written to {}".format(btcpay.pem)) print( "Do not commit {} to your repo! List it in .gitignore just to be safe" .format(btcpay.pem)) else: print("Ok, assuming your btcpay.pem has not yet been paired already") client = BTCPayClient(host=btcpay.host, pem=btcpay.pem) pairing_code = input("Paste your 7 digit pairing code here: ") if len(pairing_code) != 7: print("Pairing code is not 7 digits!") sys.exit() btcpay.tokens = client.pair_client(pairing_code) print("merchant token generated") save_configuration = input("save configuration? (yes/n) ") if save_configuration.lower() == 'yes': btcpay_filename = input( "enter configuration file name (leave blank for btcpay.yaml):") with open(btcpay_filename, 'w') as btcpay_file: btcpay_file.write(btcpay.pretty()) print("btcpay config written to {}".format(btcpay_filename)) print( "Do not commit {} to your repo! List ig in .gitignore just to be safe" .format(btcpay_filename)) print("Your btcpay configuration is given below:\n") print(btcpay.pretty())
def btcpay_server_setup(params): return BTCPayClient.create_client( host=params['host'], code=params['code'] )
from btcpay import BTCPayClient, crypto host_url = "http://raspberrypi.local" privkey = crypto.generate_privkey() with open('btcpayserver.pem', 'w') as pem: pem.write(privkey) pairing_code = "cPbc35p" client = BTCPayClient(host=host_url, pem=privkey) token = client.pair_client(pairing_code) merchant_token = token['merchant'] with open('merchant_token', 'w') as tok: tok.write(merchant_token)
from btcpay import BTCPayClient import configparser main_config = configparser.ConfigParser() main_config.read('config.ini') btcpay_config = main_config['btcpay'] ltcpay_client = BTCPayClient(host=btcpay_config['url'], pem=btcpay_config['pemltc'], tokens={'merchant': btcpay_config['ltc_token']}) btcpay_client = BTCPayClient(host=btcpay_config['url'], pem=btcpay_config['pembtc'], tokens={'merchant': btcpay_config['btc_token']}) def genInvoice(type, callbacks, amount=0.00000001, description='no description'): if type == 'btc': invoice = btcpay_client.create_invoice( { "price": amount, "currency": "BTC", "notificationURL": callbacks, "fullNotifications": True, "buyer": { "name": '123' },
bot = telegram.Bot(blazhbot_token) userbot = telegram.Bot(atomic_tipbot) dbclient = pymongo.MongoClient('localhost') mongo_db = "tipdb" mongo = dbclient[mongo_db] ### BTCPay btcpay_client = BTCPayClient( host='https://btcpayjungle.com', pem=''' -----BEGIN EC PRIVATE KEY----- pemPEMpemPEMpemPEMpemPEMpemPEMpemPEMpemPEM pemPEMpemPEMpemPEMpemPEMpemPEMpemPEMpemPEM pemPEMpemPEMpemPEMpemPEMpemPEMpemPEMpemPEM pemPEMpemPEMpemPEMpemPEMpemPEMpemPEMpemPEM -----END EC PRIVATE KEY----- ''', tokens={'merchant': '7NyxUT6ybc6ybcmM1bJmT66ybcmM1bJybcmT2U'} ) def sendTele(msg): bot.send_message(chat_id='admin_user_id', text=msg) def answerUser(userid, msg): userbot.send_message(userid, msg) def answer_newBalance(userid, msg): userbot.send_animation(userid, animation="https://myhost.org/static/duck.gif", caption=msg)
def client(self): return BTCPayClient(host=self.settings.url, pem=self.settings.pem, tokens={'merchant': self.settings.token})