调用支付宝接口一直报错TypeError: 'tuple' object cannot be interpreted as an integer
后端代码:
def order_pay(request):
if request.method == "POST":
order_id = request.POST.get('order_id')
mount = request.POST.get('total_amount')
# 初始化
app_private_key_string = open(
os.path.join(settings.BASE_DIR, 'templates/agriculture/app_private_key.pem')).read(),
alipay_public_key_string = open(
os.path.join(settings.BASE_DIR, 'templates/agriculture/alipay_public_key.pem')).read(),
alipay = AliPay(
appid="2021000117639272", # 支付宝沙箱的app id
app_notify_url=None, # 默认回调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,沙箱改为True
)
# 调用支付接口
order_string = alipay.api_alipay_trade_page_pay(
out_trade_no=order_id, # 订单id
total_amount=str(mount), # 支付总金额
subject="小家惠农在线支付",
return_url=None,
)
# 返回应答
pay_url = "https://openapi.alipaydev.com/gateway.do" + '?' + order_string
return HttpResponse(json.dumps({'pay_url': pay_url}))
前端代码:
$(".buy_btn").click(function () {
$.ajax({
headers: {"X-CSRFToken": getCookie("csrftoken")},
url: 'http://127.0.0.1:8000/order_pay',
type: 'post',
data:{
order_id : 20211314520, //订单id
total_amount : $('.total').children('em').text()
},
success:function (response) {
var res = JSON.parse(response);
location.href = res.pay_url
}
});
})
报错:
[25/Apr/2021 18:08:59] "GET /detail/ HTTP/1.1" 200 4441
Internal Server Error: /order_pay
Traceback (most recent call last):
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\graduation_project\weather\views.py", line 242, in order_pay
debug=True # 默认False,沙箱改为True
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\alipay\__init__.py", line 94, in __init__
self._load_key()
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\alipay\__init__.py", line 99, in _load_key
self._app_private_key = RSA.importKey(content)
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\Cryptodome\PublicKey\RSA.py", line 748, in import_key
extern_key = tobytes(extern_key)
File "C:\Users\py\AppData\Local\Programs\Python\Python37\lib\site-packages\Cryptodome\Util\py3compat.py", line 129, in tobytes
return bytes([s])
TypeError: 'tuple' object cannot be interpreted as an integer