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)
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() pairingCode = "otMSapf" facade = "merchant" fetch_token(facade, pairingCode) print(client.tokens[facade]) client = BTCPayClient(host=API_HOST, pem=key, tokens=client.tokens) print(client) #fetch_token(facade, pairingCode) #print("Token: ", client.tokens[facade]) new_invoice = client.create_invoice({"price": 20, "currency": "EUR", "token": client.tokens[facade],"extendedNotifications": True}) print("NEW INVOICE: ", new_invoice) fetched_invoice = client.get_invoice(new_invoice['id']) print("FETCHED INVOICE: ", new_invoice['id']) curl --no-keepalive --raw --show-error --verbose --connect-timeout 10 --insecure --max-redirs 1 -H "Content-Type: application/json" -d '{"data": "{"id":"P38S4ewSvvLoRrBSoEiMTz"}"}" http://odoo-dev.zynthian.org/payment/btcpay/ipn