fzlee/alipay

调试错误,请回到请求来源地,重新发起请求。 错误代码 invalid-signature 错误原因: 验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配

819262791 opened this issue · 2 comments

from rest_framework.views import APIView
from alipay import AliPay
from django.conf import settings
from rest_framework.response import Response
from order.models import Order
from rest_framework import status
from alipay.utils import AliPayConfig

import logging
log = logging.getLogger("django")

class AlipayAPIView(APIView):
def get(self, request):
"""获取支付宝的支付地址"""
# 获取订单号
order_number = request.query_params.get("order_number")
# 判断订单是否存在
# 判断订单是否存在
try:
order = Order.objects.get(order_number=order_number)
except Order.DoesNotExist:
return Response({"message": "对不起,订单不存在!"}, status=status.HTTP_400_BAD_REQUEST)
with open(r"C:\Users\Xing\Desktop\luffy\luffyapi\apps\payments\keys\app_private_key.pem", 'r') as file:
app_private_key_string = file.read()
with open(r"C:\Users\Xing\Desktop\luffy\luffyapi\apps\payments\keys\alipay_public_key.pem", 'r') as file:
alipay_public_key_string = file.read()

    alipay = AliPay(
        appid=settings.ALIAPY_CONFIG["appid"],
        app_notify_url=settings.ALIAPY_CONFIG["app_notify_url"],  # 默认回调url
        app_private_key_string=app_private_key_string,
        alipay_public_key_string=alipay_public_key_string,
        sign_type=settings.ALIAPY_CONFIG["sign_type"],
        debug=settings.ALIAPY_CONFIG["debug"],  # 默认False
        verbose=False,  # 输出调试数据
        config=AliPayConfig(timeout=15)  # 可选,请求超时时间
    )

    # 调用接口
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=order.order_number,
        total_amount=float(order.real_price),
        product_code="FAST_INSTANT_TRADE_PAY",
        subject=order.order_title,
        return_url=settings.ALIAPY_CONFIG["return_url"],
        notify_url=settings.ALIAPY_CONFIG["notify_url"],  # 可选, 不填则使用默认notify url
    )

    url = settings.ALIAPY_CONFIG["gateway_url"] + order_string
    print(url)
    return Response(url)

下面是配置信息

支付宝配置信息

ALIAPY_CONFIG = {
# "gateway_url": "https://openapi.alipay.com/gateway.do?", # 真实支付宝网关地址
"gateway_url": "https://openapi-sandbox.dl.alipaydev.com/gateway.do?", # 沙箱支付宝网关地址
"appid": "9021000138662797",
"app_notify_url": None,
"sign_type": "RSA2",
"debug": False,
"return_url": "http://www.luffycity.cn:8080/payments/result", # 同步回调地址
"notify_url": "http://api.luffycity.cn:8000/payments/result", # 异步结果通知
}

调用接口打印出的地址:
https://openapi-sandbox.dl.alipaydev.com/gateway.do?app_id=9021000138662797&biz_content=%7B%22subject%22%3A%22%5Cu8def%5Cu98de%5Cu5b66%5Cu57ce%5Cu8bfe%5Cu7a0b%5Cu8d2d%5Cu4e70%22%2C%22out_trade_no%22%3A%2220240621181339000001000011%22%2C%22total_amount%22%3A109.2%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%7D&charset=utf-8&method=alipay.trade.page.pay&notify_url=http%3A%2F%2Fapi.luffycity.cn%3A8000%2Fpayments%2Fresult&return_url=http%3A%2F%2Fwww.luffycity.cn%3A8080%2Fpayments%2Fresult&sign_type=RSA2&timestamp=2024-06-28+15%3A34%3A05&version=1.0&sign=HMZkNMM5GvzS9OuG4mPVef33J9Tt6e%2B6e0OTlLtM0eW3v8%2B%2B5zxUi3s58WTji1qjqIs3E1vEOdm28YQ0P3Y8aN2Ax81qVg5xmRg161Wtzv%2FzEYynBpjNJ5Sv4fjiDyddF7nKMX6vwqFsR%2BVy0%2BoRMsW%2FXvxHXAJhLYGn77XcH0f2RK%2BhvJRSHkduTYTy6T9KXLLH%2FZLGlsOXMo2Dt75M3EpwT%2BxOEpjx%2BJW8cSVGyQSvCf4APWji%2F47Ozk66xr9L41hZPoGkZe1EnENbAWc6O1282NESkWaP9%2FqgI7PAkKvkqQZWluWNPhe2O2wm0CsbRUrYO3if6ZkYCLHa29NZ3g%3D%3D

下面是访问页面的报错信息:
调试错误,请回到请求来源地,重新发起请求。
错误代码 invalid-signature 错误原因: 验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配,网关生成的验签字符串为:app_id=9021000138662797&biz_content={"subject":"\u8def\u98de\u5b66\u57ce\u8bfe\u7a0b\u8d2d\u4e70","out_trade_no":"20240621181339000001000011","total_amount":109.2,"product_code":"FAST_INSTANT_TRADE_PAY"}&charset=utf-8&method=alipay.trade.page.pay&notify_url=http://api.luffycity.cn:8000/payments/result&return_url=http://www.luffycity.cn:8080/payments/result&sign_type=RSA2&timestamp=2024-06-28 15:34:05&version=1.0
跟之前的朋友遇到过的几乎相似,但是我的url串是正常的吧,并没有什么{}之类的符号,求大佬帮看看!!

你好, 如果你是在沙箱环境测试支付的话, 建议将debug设置true, 这样不会导致一些问题。
但即使debug字段有问题也不应该导致这个签名一致的问题。

从你的代码里面我找不出毛病, 你可以尝试重新上传你的公私钥, 使用这里的例子,看看结果https://github.com/fzlee/alipay/tree/master/tests/certs/web