1
This commit is contained in:
@@ -235,11 +235,23 @@ const AdminPaymentConfig: React.FC = () => {
|
||||
<Form.Item name="alipay_app_id" label="AppID">
|
||||
<Input placeholder="支付宝应用AppID" size="large" disabled={!alipayEnabled} />
|
||||
</Form.Item>
|
||||
<Form.Item name="alipay_private_key" label="应用私钥">
|
||||
<Input.TextArea rows={3} placeholder="支付宝应用私钥 (PKCS8格式)" disabled={!alipayEnabled} />
|
||||
<Form.Item
|
||||
name="alipay_private_key"
|
||||
label="应用私钥"
|
||||
extra="支付宝应用私钥(PKCS8格式),不需要添加头部和尾部标记,直接粘贴私钥内容"
|
||||
>
|
||||
<Input.TextArea rows={4} placeholder="
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC...
|
||||
" disabled={!alipayEnabled} />
|
||||
</Form.Item>
|
||||
<Form.Item name="alipay_public_key" label="支付宝公钥">
|
||||
<Input.TextArea rows={3} placeholder="支付宝公钥" disabled={!alipayEnabled} />
|
||||
<Form.Item
|
||||
name="alipay_public_key"
|
||||
label="支付宝公钥"
|
||||
extra="从支付宝开放平台获取的支付宝公钥,不需要添加头部和尾部标记"
|
||||
>
|
||||
<Input.TextArea rows={4} placeholder="
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...
|
||||
" disabled={!alipayEnabled} />
|
||||
</Form.Item>
|
||||
<Form.Item name="alipay_gateway" label="网关地址" extra="正式环境: https://openapi.alipay.com/gateway.do 沙箱环境: https://openapi-sandbox.dl.alipaydev.com/gateway.do">
|
||||
<Input placeholder="https://openapi.alipay.com/gateway.do" size="large" disabled={!alipayEnabled} />
|
||||
|
||||
@@ -211,6 +211,28 @@ _alipay_client = None
|
||||
_alipay_client_app_id = None
|
||||
|
||||
|
||||
def _format_alipay_key(key: str) -> str:
|
||||
"""
|
||||
格式化支付宝密钥,移除PEM头部和尾部,并清理所有空白字符。
|
||||
"""
|
||||
if not key:
|
||||
return ""
|
||||
# 移除首尾空白
|
||||
key = key.strip()
|
||||
# 移除 PEM 头部和尾部
|
||||
key = key.replace("-----BEGIN RSA PRIVATE KEY-----", "")
|
||||
key = key.replace("-----BEGIN PRIVATE KEY-----", "")
|
||||
key = key.replace("-----END RSA PRIVATE KEY-----", "")
|
||||
key = key.replace("-----END PRIVATE KEY-----", "")
|
||||
key = key.replace("-----BEGIN PUBLIC KEY-----", "")
|
||||
key = key.replace("-----END PUBLIC KEY-----", "")
|
||||
key = key.replace("-----BEGIN ALIPAY PUBLIC KEY-----", "")
|
||||
key = key.replace("-----END ALIPAY PUBLIC KEY-----", "")
|
||||
# 移除所有空白字符(换行、空格、制表符等)
|
||||
key = "".join(key.split())
|
||||
return key
|
||||
|
||||
|
||||
def _get_alipay_client(app_id: str, private_key: str, public_key: str, gateway: str = ""):
|
||||
"""Get or create an Alipay client. Recreated if app_id changes."""
|
||||
global _alipay_client, _alipay_client_app_id
|
||||
@@ -228,18 +250,23 @@ def _get_alipay_client(app_id: str, private_key: str, public_key: str, gateway:
|
||||
)
|
||||
return None
|
||||
|
||||
# 格式化私钥和公钥
|
||||
formatted_private_key = _format_alipay_key(private_key)
|
||||
formatted_public_key = _format_alipay_key(public_key)
|
||||
|
||||
config = AlipayClientConfig()
|
||||
config.server_url = gateway or "https://openapi.alipay.com/gateway.do"
|
||||
config.app_id = app_id
|
||||
config.app_private_key = private_key
|
||||
config.alipay_public_key = public_key
|
||||
config.app_private_key = formatted_private_key
|
||||
config.alipay_public_key = formatted_public_key
|
||||
config.sign_type = "RSA2"
|
||||
config.charset = "utf-8"
|
||||
|
||||
try:
|
||||
_alipay_client = DefaultAlipayClient(config, logger)
|
||||
_alipay_client_app_id = app_id
|
||||
except Exception:
|
||||
logger.info("Alipay client initialized successfully")
|
||||
except Exception as e:
|
||||
logger.exception("Failed to initialize Alipay client")
|
||||
_alipay_client = None
|
||||
_alipay_client_app_id = None
|
||||
@@ -505,7 +532,7 @@ async def _close_wechat_order(db: AsyncSession, order: PaymentOrder, db_configs:
|
||||
|
||||
try:
|
||||
code, result = client.close(out_trade_no=order.order_no)
|
||||
if code == 200:
|
||||
if code == 204:
|
||||
logger.info(f"WeChat order closed: order_no={order.order_no}")
|
||||
return True
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user