Files
vf_react/alipay_config.py
2025-12-12 13:30:55 +08:00

118 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
支付宝支付配置文件
电脑网站支付 (alipay.trade.page.pay)
"""
import os
# 获取当前文件所在目录
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# 支付宝支付配置
ALIPAY_CONFIG = {
# 应用ID - 从支付宝开放平台获取
'app_id': '2021005183650009',
# 支付宝网关 - 正式环境
'gateway_url': 'https://openapi.alipay.com/gateway.do',
# 沙箱环境网关(测试用)
# 'gateway_url': 'https://openapi-sandbox.dl.alipaydev.com/gateway.do',
# 签名方式 - 必须使用RSA2
'sign_type': 'RSA2',
# 编码格式
'charset': 'utf-8',
# 返回格式
'format': 'json',
# 密钥文件路径
'app_private_key_path': os.path.join(_BASE_DIR, 'alipay', '应用私钥.txt'),
'alipay_public_key_path': os.path.join(_BASE_DIR, 'alipay', '支付宝公钥.txt'),
# 回调地址 - 替换为你的实际域名
'notify_url': 'https://valuefrontier.cn/api/payment/alipay/callback', # 异步通知地址(后端)
'return_url': 'https://valuefrontier.cn/pricing?payment_return=alipay', # 同步跳转地址(前端页面)
# 产品码 - 电脑网站支付固定为此值
'product_code': 'FAST_INSTANT_TRADE_PAY',
}
def load_key_from_file(file_path):
"""从文件读取密钥内容"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read().strip()
except FileNotFoundError:
print(f"❌ 密钥文件不存在: {file_path}")
return None
except Exception as e:
print(f"❌ 读取密钥文件失败: {e}")
return None
def get_app_private_key():
"""获取应用私钥"""
return load_key_from_file(ALIPAY_CONFIG['app_private_key_path'])
def get_alipay_public_key():
"""获取支付宝公钥"""
return load_key_from_file(ALIPAY_CONFIG['alipay_public_key_path'])
def validate_config():
"""验证配置是否完整"""
issues = []
# 检查 app_id
if not ALIPAY_CONFIG.get('app_id') or ALIPAY_CONFIG['app_id'].startswith('your_'):
issues.append("❌ app_id 未配置请替换为真实的支付宝应用ID")
# 检查密钥文件
if not os.path.exists(ALIPAY_CONFIG['app_private_key_path']):
issues.append(f"❌ 应用私钥文件不存在: {ALIPAY_CONFIG['app_private_key_path']}")
else:
key = get_app_private_key()
if not key or len(key) < 100:
issues.append("❌ 应用私钥内容异常,请检查文件格式")
if not os.path.exists(ALIPAY_CONFIG['alipay_public_key_path']):
issues.append(f"❌ 支付宝公钥文件不存在: {ALIPAY_CONFIG['alipay_public_key_path']}")
else:
key = get_alipay_public_key()
if not key or len(key) < 100:
issues.append("❌ 支付宝公钥内容异常,请检查文件格式")
return len(issues) == 0, issues
if __name__ == "__main__":
print("支付宝支付配置验证")
print("=" * 50)
is_valid, issues = validate_config()
if is_valid:
print("✅ 配置验证通过!")
print(f" App ID: {ALIPAY_CONFIG['app_id']}")
print(f" 网关: {ALIPAY_CONFIG['gateway_url']}")
print(f" 回调地址: {ALIPAY_CONFIG['notify_url']}")
print(f" 同步跳转: {ALIPAY_CONFIG['return_url']}")
else:
print("⚠️ 配置存在问题:")
for issue in issues:
print(f" {issue}")
print("\n📋 配置步骤:")
print("1. 登录支付宝开放平台 (open.alipay.com)")
print("2. 创建应用并获取 App ID")
print("3. 在开发设置中配置RSA2密钥")
print("4. 将应用私钥、支付宝公钥放到 ./alipay/ 文件夹")
print("5. 更新本文件中的配置信息")
print("=" * 50)