Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

沙箱环境下调用失败(invalid-app-id)及其解决办法 #156

Closed
PhiFever opened this issue Dec 1, 2023 · 4 comments
Closed

沙箱环境下调用失败(invalid-app-id)及其解决办法 #156

PhiFever opened this issue Dec 1, 2023 · 4 comments
Labels

Comments

@PhiFever
Copy link

PhiFever commented Dec 1, 2023

Describe the problem
使用支付宝沙箱环境创建网页支付接口得到的order_string无法正常使用,点击后报错invalid-app-id
折腾好久之后发现问题出在网关支付url的拼接上,网上查到的教程基本上都是过时陈旧的。事实上,在2023年12月的环境下支付页面应该是https://openapi-sandbox.dl.alipaydev.com/gateway.do? + order_string。查询issue后发现迄今没有人提到过这个问题,故提出此issue以记录。

Environment
OS version:Win10家庭中文版(内部版本19045)
Python version:3.11
Alipay version:3.3.0

To reproduce
以下是沙箱环境下可用的测试代码(注意填写自己的APP_ID):
settings.py

import os

# 应用私钥
APP_PRIVATE_KEY_STRING = open(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app_private_key.pem')).read()

# 支付宝公钥
ALIPAY_PUBLIC_KEY_STRING = open(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), 'alipay_public_key.pem')).read()
# 应用ID
APP_ID = ''

# 加密方式
SIGN = 'RSA2'

# 是否是支付宝测试环境(沙箱环境),如果采用真实支付宝环境,配置False
DEBUG = True

# 支付网关
GATEWAY = 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?' if DEBUG else 'https://openapi.alipay.com/gateway.do?'

aliPayTest.py


import settings

alipay = AliPay(
    appid=settings.APP_ID,
    app_notify_url=None,  # 默认回调 url
    app_private_key_string=settings.APP_PRIVATE_KEY_STRING,
    # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
    alipay_public_key_string=settings.ALIPAY_PUBLIC_KEY_STRING,
    sign_type=settings.SIGN,  # RSA 或者 RSA2
    debug=settings.DEBUG,  # 默认 False
)

if __name__ == '__main__':
    subject = "测试订单"
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no="201615435112",
        total_amount=0.01,
        subject=subject,
        return_url="https://example.com",
        notify_url="https://example.com/notify"  # 可选,不填则使用默认 notify url
    )
    url = settings.GATEWAY + order_string
    print(url)

**Expected behavior **

  1. 3,0的api貌似在沙箱环境下工作异常,错误码为INVALID_PARAMETER
  2. 我注意到在https://github.com/fzlee/alipay/blob/master/docs/init.zh-hans.mdalipay_public_key_string变量有着支付宝默认导出证书文件的后缀名,我不知道这是不是作者的一个笔误,还请斧正。
@fzlee
Copy link
Owner

fzlee commented Dec 5, 2023

你好, 这边的debug网管应该是修改到了 https://openapi-sandbox.dl.alipaydev.com/gateway.do
你可以查看源代码alipay/alipay/__init__.py 第85行左右

@fzlee
Copy link
Owner

fzlee commented Dec 5, 2023

@Gungnir762 第二个问题我不是很明白。”变量有着支付宝默认导出证书文件的后缀名“, 不太明白你的意思, 你可以再详细描述一下问题

@PhiFever
Copy link
Author

PhiFever commented Dec 6, 2023

@Gungnir762 第二个问题我不是很明白。”变量有着支付宝默认导出证书文件的后缀名“, 不太明白你的意思, 你可以再详细描述一下问题

已经看到修改了,感谢。

第二个问题我确实没说清楚,那天改的头昏脑涨的。我的意思是,alipay_public_key_string这个变量应该是从一个RSA/RSA2公钥文件(后缀一般为.pem)中读取字符串,但是文档中打开的文件却有着.cert的后缀名(一般为证书文件)。查看tests/certs/ali/ali_public_key.pem的文档后发现其格式也是公钥文件格式,并且按照此文件的格式加上头尾进行读取后我的程序可以正常执行,所以我斗胆猜测此处可能是一个笔误。
image

@fzlee
Copy link
Owner

fzlee commented Dec 6, 2023

明白了, crt和pem严格来讲尽管都是一堆base64的东西, 但是意义是有些不一样的。
我修改一下

@fzlee fzlee closed this as completed Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants