You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中打印BTCPay生成的发票的URL及比特币地址?

提取BTCPay发票的指定属性

client.create_invoice()返回的new_invoice是可通过键(或属性)访问的结构,直接定位目标字段即可实现需求:

提取支付页面URL

直接通过键名获取:

new_invoice = client.create_invoice({"price": 20, "currency": "USD"})
# 打印支付页面URL
print(new_invoice['url'])

如果返回的是对象而非字典,改用点语法:print(new_invoice.url)

提取BIP21对应的比特币地址

BIP21信息通常嵌套在paymentMethods下的BTC配置里,逐层访问并增加判断避免报错:

new_invoice = client.create_invoice({"price": 20, "currency": "USD"})
# 提取BIP21地址
if 'paymentMethods' in new_invoice and 'BTC' in new_invoice['paymentMethods']:
    bip21_addr = new_invoice['paymentMethods']['BTC']['bip21']
    print(bip21_addr)
else:
    print("未找到BTC支付方式的BIP21信息")

若返回的是对象,对应写法:

if hasattr(new_invoice, 'paymentMethods') and 'BTC' in new_invoice.paymentMethods:
    print(new_invoice.paymentMethods.BTC.bip21)

内容的提问来源于stack exchange,提问作者Ace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:39:20