电子发票 | 数电发票 Python SDK | 发票接口 | 开票api |开票、验真、红冲一站式集成 电子发票/数电发票 Python SDK | 开票、验真、红冲一站式集成发票 Python SDK专为电子发票、数电发票全电发票场景设计支持开票、红冲、版式文件下载等核心功能快速对接税务平台API。关键词: 电子发票SDK,数电票Python,开票接口,发票api,发票开具,发票红冲,全电发票集成 核心功能基础认证✅获取授权- 快速接入税务平台身份认证✅人脸二维码登录- 支持数电发票平台扫码登录✅认证状态查询- 实时获取纳税人身份状态发票开具数电蓝票开具- 支持增值税普通/专用电子发票版式文件下载- 自动获取销项发票PDF/OFD/XML文件发票红冲红冲前蓝票查询- 精确检索待红冲的电子发票红字信息表申请- 生成红冲凭证负数发票开具- 自动化红冲流程 快速安装pipinstalltax-invoice 查看pypi地址 查看中文文档 为什么选择此SDK精准覆盖中国数电发票标准- 严格遵循国家最新接口规范开箱即用- 无需处理XML/签名等底层细节专注业务逻辑企业级验证- 已在生产环境处理超100万张电子发票 支持的开票类型发票类型状态数电发票普通发票✅ 支持数电发票增值税专用发票✅ 支持数电发票铁路电子客票✅ 支持数电发票航空运输电子客票行程单✅ 支持数电票二手车销售统一发票✅ 支持数电纸质发票增值税专用发票✅ 支持数电纸质发票普通发票✅ 支持数电纸质发票机动车发票✅ 支持数电纸质发票二手车发票✅ 支持from tax.invoiceimportInvoiceClientimporttimeimporttraceback# 新增导入importjsonimportredisimportqrcodeimportioimportbase64 from PILimportImageimporttimefrom inputimeoutimportinputimeout, TimeoutOccurred# pip install inputimeoutfrom datetimeimportdatetime, timedelta APP_KEYAPP_SECRET# 配置信息nsrsbh# 统一社会信用代码title# 名称营业执照username# 手机号码电子税务局password# 个人用户密码电子税务局type6#6基础7标准 xhdwdzdh重庆市渝北区龙溪街道丽园路2号XXXX 1325580XXXX# 地址和电话 空格隔开xhdwyhzh工商银行XXXX 15451211XXXX#开户行和银行账号 空格隔开tokendebugTrue# 连接Redis 安装pip install redisrredis.Redis(host127.0.0.1,port6379,passwordtest123456,# 如果有密码填写密码db0,# 使用第0个数据库decode_responsesTrue# 自动将Redis返回的字节转为字符串)# 初始化客户端clientInvoiceClient(app_keyAPP_KEY,app_secretAPP_SECRET,base_urlhttps://api.fa-piao.com,debugdebug)#字符串转二维码图片base64def str_to_qr_base64(data: str)-str:# 创建二维码对象qrqrcode.QRCode(version1,error_correctionqrcode.constants.ERROR_CORRECT_L,box_size10,border4,)qr.add_data(data)qr.make(fitTrue)# 生成图像PIL Imageimgqr.make_image(fill_colorblack,back_colorwhite)# 将图像保存到内存中的字节流bufferio.BytesIO()img.save(buffer,formatPNG)# 获取 Base64 编码img_base64base64.b64encode(buffer.getvalue()).decode(utf-8)returnimg_base64#字符串转二维码图片 在命令行打印def print_qr_code(data):# 创建二维码对象qrqrcode.QRCode(version1,error_correctionqrcode.constants.ERROR_CORRECT_L,# 低容错率生成的图案更稀疏box_size1,# 字符画中通常设为1border1,# 边框宽度)qr.add_data(data)qr.make(fitTrue)# 使用终端输出模式# qrcode 库支持将二维码输出为 ANSI 字符qr.print_ascii(invertTrue)# invertTrue 会让背景变黑码变白适合深色背景终端def blue_invoice():# 开票税额计算demoinvoice_params{fpqqlsh:APP_KEY str(int(time.time()*1000)),# 建议用你的订单号fplxdm:82,kplx:0,xhdwsbh:nsrsbh,username:username,xhdwmc:title,xhdwdzdh:xhdwdzdh,xhdwyhzh:xhdwyhzh,ghdwmc:个人,# ghdwsbh: 91330100563015xxxx,zsfs:0,bz:,# 添加商品明细fyxm:[{fphxz:0,spmc:*研发和技术服务*技术服务费,je:114.68,sl:0.01,se:1.14,hsbz:1,spbm:3040105000000000000}],# 合计金额hjje:113.54,hjse:1.14,jshj:114.68}# 数电蓝票开具接口 文档returnclient.invoice.blue_invoice(**invoice_params)def get_pdf_ofd_xml(fphm): pdf_params{downflag:4,nsrsbh:nsrsbh,username:username,fphm:fphm}# 获取销项数电版式文件 文档 PDF/OFD/XMLpdf_responseclient.invoice.get_pdf_ofd_xml(**pdf_params)ifpdf_response.get(code)200: print(pdf_response.get(data))def get_token(force_updateFalse): keynsrsbh username TOKENifforce_update:# 获取授权Token文档auth_responseclient.auth.get_authorization(nsrsbh,type)# auth_response client.auth.get_authorization(nsrsbh,type,username,password)ifauth_response.get(code)200: print(f授权成功Token: {auth_response.get(data, {}).get(token)})tokenauth_response.get(data,{}).get(token)r.set(key, token,ex3600*24*30)# 过期时间30天client.auth.set_token(token)else: tokenr.get(key)iftoken: client.auth.set_token(token)else: get_token(force_updateTrue)try:# 一 获取授权token 可从缓存redis中获取Tokenget_token()# 前端模拟数电发票/电子发票开具 (蓝字发票)# 二 开具蓝票invoice_responseblue_invoice()match invoice_response.get(code):case200: print(获取pdf/ofd/xml)print(fdata: {invoice_response.get(data)})# 三 获取pdf/ofd/xmlget_pdf_ofd_xml(invoice_response.get(data,{}).get(Fphm))case420: print(登录(短信认证))# 登录数电发票平台 短信# 1 发短信验证码login_responseclient.auth.login_dppt(nsrsbhnsrsbh,usernameusername,passwordpassword)iflogin_response.get(code)200: deadline(datetime.now() timedelta(seconds300)).strftime(%Y-%m-%d %H:%M:%S)try:# 设置 timeout300 表示 300 秒smsinputimeout(promptf请在300秒内({deadline}前)输入验证码: ,timeout300)print(f\n你输入了: {sms})# 2 输入验证码login_response2client.auth.login_dppt(nsrsbhnsrsbh,usernameusername,passwordpassword,smssms)iflogin_response2.get(code)200: print(login_response2.get(data))print(\n短信验证成功)print(\n再次调用blue_invoice)invoice_responseblue_invoice()get_pdf_ofd_xml(invoice_response.get(data,{}).get(Fphm))except TimeoutOccurred: print(\n短信认证输入超时!)case430: print(人脸认证)# 1 获取人脸二维码qr_code_responseclient.face.get_face_img(nsrsbhnsrsbh,usernameusername,type1)ewmlyqr_code_response.get(data,{}).get(ewmly)ifewmlyswj:print(\n请使用电子税务局app扫码)print(\n成功做完人脸认证,请输入数字 1)# ewm自己生成二维码 安装pip install qrcode[pil]ifqr_code_response.get(data,{}).get(ewm)is not None and len(qr_code_response.get(data,{}).get(ewm))500:# ewm qr_code_response.get(data, {}).get(ewm)# qr_base64 str_to_qr_base64(ewm)# qr_code_response.get(data, {}).update({ewm: qr_base64})# print(qr_base64)# 前端使用示例: base64Uri data:image/png;base64, qr_base64print_qr_code(qr_code_response.get(data,{}).get(ewm))deadline(datetime.now() timedelta(seconds300)).strftime(%Y-%m-%d %H:%M:%S)try:# 设置 timeout300 表示 300 秒input_numinputimeout(promptf请在300秒内({deadline}前)输入: ,timeout300)print(f\n你输入了: {input_num})# 2 认证完成后 获取人脸二维码认证状态rzidqr_code_response.get(data,{}).get(rzid)face_status_responseclient.face.get_face_state(nsrsbhnsrsbh,rzidrzid,usernameusername,type1)print(code: str(face_status_response.get(code)))print(data: str(face_status_response.get(data)))ifface_status_response.get(data)is not None: slztface_status_response.get(data,{}).get(slzt)ifslzt1:print(\n人脸未认证)elifslzt2:print(\n人脸认证成功)print(\n请再次调用blue_invoice)invoice_responseblue_invoice()get_pdf_ofd_xml(invoice_response.get(data,{}).get(Fphm))elifslzt3:print(\n人脸认证二维码过期--重新获取人脸二维码)except TimeoutOccurred: print(\n人脸认证输入超时!)elifewmlygrsds:print(个人所得税app扫码)# ewm是二维码的base64case401:# 重新授权print(f{invoice_response.get(code)}授权失败:{invoice_response.get(msg)})print(重新授权 获取token 缓存到redis)get_token(True)invoice_responseblue_invoice()get_pdf_ofd_xml(invoice_response.get(data,{}).get(Fphm))case_: print(f{invoice_response.get(code)}异常: {invoice_response.get(msg)})except Exception as e:# 打印完整堆栈信息包含行号traceback.print_exc()# 新增堆栈跟踪# 添加上下文信息可选print(f\n错误发生时参数状态)print(f当前时间戳: {int(time.time())})

本周精选

本月热点