119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
#!/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://api.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):
|
|
"""从文件读取密钥内容"""
|
|
import sys
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
return f.read().strip()
|
|
except FileNotFoundError:
|
|
print(f"[AlipayConfig] Key file not found: {file_path}", file=sys.stderr)
|
|
return None
|
|
except Exception as e:
|
|
print(f"[AlipayConfig] Read key file failed: {e}", file=sys.stderr)
|
|
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 not configured")
|
|
|
|
# 检查密钥文件
|
|
if not os.path.exists(ALIPAY_CONFIG['app_private_key_path']):
|
|
issues.append(f"Private key file not found: {ALIPAY_CONFIG['app_private_key_path']}")
|
|
else:
|
|
key = get_app_private_key()
|
|
if not key or len(key) < 100:
|
|
issues.append("Private key content invalid")
|
|
|
|
if not os.path.exists(ALIPAY_CONFIG['alipay_public_key_path']):
|
|
issues.append(f"Alipay public key file not found: {ALIPAY_CONFIG['alipay_public_key_path']}")
|
|
else:
|
|
key = get_alipay_public_key()
|
|
if not key or len(key) < 100:
|
|
issues.append("Alipay public key content invalid")
|
|
|
|
return len(issues) == 0, issues
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Alipay Payment Config Validation")
|
|
print("=" * 50)
|
|
|
|
is_valid, issues = validate_config()
|
|
|
|
if is_valid:
|
|
print("[OK] Config validation passed!")
|
|
print(f" App ID: {ALIPAY_CONFIG['app_id']}")
|
|
print(f" Gateway: {ALIPAY_CONFIG['gateway_url']}")
|
|
print(f" Notify URL: {ALIPAY_CONFIG['notify_url']}")
|
|
print(f" Return URL: {ALIPAY_CONFIG['return_url']}")
|
|
else:
|
|
print("[ERROR] Config has issues:")
|
|
for issue in issues:
|
|
print(f" - {issue}")
|
|
|
|
print("\nSetup steps:")
|
|
print("1. Login to Alipay Open Platform (open.alipay.com)")
|
|
print("2. Create app and get App ID")
|
|
print("3. Configure RSA2 keys in development settings")
|
|
print("4. Put private key and Alipay public key in ./alipay/ folder")
|
|
print("5. Update config in this file")
|
|
|
|
print("=" * 50)
|