Skip to content

Instantly share code, notes, and snippets.

@ser
Created March 11, 2019 09:03
Show Gist options
  • Save ser/c194fea52a2903587d2b4f7841c09915 to your computer and use it in GitHub Desktop.
Save ser/c194fea52a2903587d2b4f7841c09915 to your computer and use it in GitHub Desktop.
soon it will be on github
@blueprint.route('/admin/pair', methods=['GET', 'POST'])
@roles_accepted('admin')
def btcpaypair():
"""Payment processor pairing."""
btcpaypem_location = current_app.config.get('APP_DIR')+'/data/btcpayserver.key'
btcpaytoken_location = current_app.config.get('APP_DIR')+'/data/btcpayserver.token'
# Recognise if pairing is done and valid
try:
btcpayuri = current_app.config.get('BTCPAY_URI')
btcpaycode = current_app.config.get('BTCPAY_CODE')
fiat = current_app.config.get('FIAT')
with open(btcpaypem_location, 'r') as file:
btcpaypem = file.read()
with open(btcpaytoken_location, 'r') as file:
s = file.read()
btcpaytoken = ast.literal_eval(s)
btcpayclient = btcpay.BTCPayClient(
host=btcpayuri,
pem=btcpaypem,
tokens=btcpaytoken,
)
# try if it works
rates = btcpayclient.get_rates()
log.debug(pprint.pformat(rates, depth=5))
state = True
log.debug("We found a working processor.")
except Exception as e:
log.debug(e)
log.debug("We are unable to bind to the processor.")
state = False
# Form to update/pair
form = AddPairingTextForm()
if request.method == 'POST':
if form.validate_on_submit():
try:
pem = btcpay.crypto.generate_privkey()
fo = open(btcpaypem_location, "w")
fo.write(pem)
fo.close()
btcpayclient = btcpay.BTCPayClient(
host=btcpayuri,
pem=pem
)
btcpaytoken = btcpayclient.pair_client(
form.pairing_code.data
)
fo = open(btcpaytoken_location, "w")
fo.write(str(btcpaytoken))
fo.close()
flash('Your payment processor was paired successfully.', 'success')
state = True
except Exception as e:
log.debug("Exception")
log.exception(e)
flash('The pairing process failed. Check logs or repeat.', 'error')
state = False
else:
flash_errors(form)
return render_template(
'users/pair.html',
state=state,
form=form
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment