在Django中用Zeep对接支付网关遇AttributeError错误求助
Django对接Zeep支付网关报错:AttributeError at /callback/ module 'zeep.client' has no attribute 'service'
错误信息
AttributeError at /callback/ module 'zeep.client' has no attribute 'service'
相关代码(shop应用views.py回调函数)
# Callback function def callback(request): if request.GET.get('Status') == 'NOK': authority = request.GET.get('authority') invoice = get_object_or_404(models.Invoice, authority=authority) amount = 0 order = invoice.order order_items = models.OrderItem.objects.filter(order=order) for item in order_items: amount += item.product_cost result = client.service.PaymentVerification(MERCHANT, authority, amount) if result.Status == 100: return render(request, 'callback.html', {'invoice': invoice}) else: return HttpResponse('error ' + str(result.Status)) else: return HttpResponse('error ')
解决方法
正确初始化Zeep客户端:
service属性属于实例化后的Client对象,而非zeep.client模块。需先创建Client实例:# 在views.py顶部添加 from zeep import Client # 替换为支付网关提供的WSDL地址 PAYMENT_WSDL = "https://your-payment-provider.wsdl" client = Client(wsdl=PAYMENT_WSDL)确保后续代码中使用的
client是这个实例,而非导入的zeep.client模块。检查变量作用域与命名冲突:确认
callback函数中引用的client是你初始化的实例,避免因变量名重复(比如误将zeep.client模块导入为client)导致的错误。验证Zeep版本:通过
pip show zeep查看当前版本,若版本过旧或过新,参考对应版本的官方文档调整客户端初始化方式,确保API调用符合版本要求。
内容的提问来源于stack exchange,提问作者Hamed 3dart
相关产品推荐
相关产品推荐

