fzlee / alipay

Python Alipay(支付宝) SDK with SHA1/SHA256 support
Other
1.29k stars 378 forks source link

支付宝POST回调时使用alipay.verify报错 #158

Closed zsaa2 closed 8 months ago

zsaa2 commented 8 months ago

Describe the bug 在初始化时加密选择了RSA2,但是在支付宝POST回调时报下面的错误

Environment OS version: Python version: 3.10 Alipay version: python-alipay-sdk 3.3.0

To reproduce Code snippets:

#初始化
alipay = AliPay(
    appid="9021000135610284",
    app_notify_url='http://47.xxx.xxx.xxx/check_order/',  # 默认回调 url
    app_private_key_string=app_private_key_string,
    alipay_public_key_string=alipay_public_key_string,
    sign_type='RSA2',  # RSA 或者 RSA2
    debug=True,  # 默认 False
    verbose=True,  # 输出调试数据
    config=AliPayConfig(timeout=15)  # 可选,请求超时时间
)

#我的views
class CheckOrder(APIView):
    authentication_classes = []
    def post(self,request):
        data = request.data
        _mutable = data._mutable
        data._mutable = True
        signature = data.pop("sign")
        print(alipay._sign_type,data.get('sign_type'))
        #这里打印出来两个都是 RSA2
        success = alipay.verify(data, signature)
        if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
            print("trade succeed")
        return Response()
#报错信息
    success = alipay.verify(data, signature)
  File "/www/server/pyporject_evn/backend_venv/lib/python3.10/site-packages/alipay/__init__.py", line 202, in verify
    raise AliPayException(None, "Unknown sign type: {}".format(sign_type))
alipay.exceptions.AliPayException: AliPayException: code:None, message:Unknown sign type: ['RSA2']

麻烦大佬们帮我看看.

zsaa2 commented 8 months ago

django的version是3.2,drf的version是3.14

fzlee commented 8 months ago

你把request.data print出来, 可以看见, signtype 是一个数组, 我们需要的是字符串

zsaa2 commented 8 months ago

好的我去修改一下试试

zsaa2 commented 8 months ago

我将request.data重新赋值成一个dict后可以了,感谢!