python-alipay-sdk中的api_alipay_trade_query
接口是对支付宝原生的alipay.trade.query接口的二次封装。支付宝原生的的alipay.trade.query
作用是:统一收单线下交易查询。但是今天我在使用时查询订单的状态时,PyCharm控制台报了如下错误。
报错内容如下
Internal Server Error: /order/check
Traceback (most recent call last):
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/zhangbin/Desktop/bj15/dailyfresh/apps/order/views.py", line 444, in post
response = alipay.api_alipay_trade_query(out_trade_no=order_id)
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/alipay/__init__.py", line 297, in api_alipay_trade_query
return self._verify_and_return_sync_response(raw_string, "alipay_trade_query_response")
File "/Users/zhangbin/py_envs/CoderZB_First/lib/python3.7/site-packages/alipay/__init__.py", line 557, in _verify_and_return_sync_response
raise AliPayValidationError
alipay.exceptions.AliPayValidationError
翻了一下python-alipay-sdk的源码,发现了错误所在。如果满足如下红色方框的逻辑,就会报如上的错误。但是由于开发需要,要拿到具体的状态码,上面的报错阻断了获取状态码,所以我就把红色方框的代码注释掉了。这样的好处是,如果支付过程中有什么问题,直接会将状态码
包在json里面返回给调用者。
后台页面获取状态码code
response = alipay.api_alipay_trade_query(out_trade_no=order_id)
print(response)
code = response.get('code')