update pay ui
This commit is contained in:
@@ -46,9 +46,7 @@ class AlipayPay:
|
||||
self.app_private_key = self._load_private_key(app_private_key)
|
||||
self.alipay_public_key = self._load_public_key(alipay_public_key)
|
||||
|
||||
print(f"✅ 支付宝支付初始化成功")
|
||||
print(f" App ID: {app_id}")
|
||||
print(f" 网关: {gateway_url}")
|
||||
# 注意:不要在这里使用 print,会影响 subprocess 的 JSON 输出
|
||||
|
||||
def _load_private_key(self, key_str):
|
||||
"""加载RSA私钥(支持 PKCS#1 和 PKCS#8 格式)"""
|
||||
@@ -76,11 +74,13 @@ class AlipayPay:
|
||||
)
|
||||
return private_key
|
||||
except Exception as e:
|
||||
print(f"[AlipayPay] Load private key failed: {e}")
|
||||
import sys
|
||||
print(f"[AlipayPay] Load private key failed: {e}", file=sys.stderr)
|
||||
raise
|
||||
|
||||
def _load_public_key(self, key_str):
|
||||
"""加载RSA公钥"""
|
||||
import sys
|
||||
try:
|
||||
# 如果密钥不包含头尾,添加PEM格式头尾
|
||||
if '-----BEGIN' not in key_str:
|
||||
@@ -92,7 +92,7 @@ class AlipayPay:
|
||||
)
|
||||
return public_key
|
||||
except Exception as e:
|
||||
print(f"❌ 加载公钥失败: {e}")
|
||||
print(f"[AlipayPay] Load public key failed: {e}", file=sys.stderr)
|
||||
raise
|
||||
|
||||
def _sign(self, unsigned_string):
|
||||
@@ -105,6 +105,7 @@ class AlipayPay:
|
||||
Returns:
|
||||
Base64编码的签名
|
||||
"""
|
||||
import sys
|
||||
try:
|
||||
signature = self.app_private_key.sign(
|
||||
unsigned_string.encode('utf-8'),
|
||||
@@ -113,7 +114,7 @@ class AlipayPay:
|
||||
)
|
||||
return base64.b64encode(signature).decode('utf-8')
|
||||
except Exception as e:
|
||||
print(f"❌ 签名失败: {e}")
|
||||
print(f"[AlipayPay] Sign failed: {e}", file=sys.stderr)
|
||||
raise
|
||||
|
||||
def _verify(self, message, signature):
|
||||
@@ -127,6 +128,7 @@ class AlipayPay:
|
||||
Returns:
|
||||
bool: 验证是否通过
|
||||
"""
|
||||
import sys
|
||||
try:
|
||||
self.alipay_public_key.verify(
|
||||
base64.b64decode(signature),
|
||||
@@ -136,7 +138,7 @@ class AlipayPay:
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"❌ 签名验证失败: {e}")
|
||||
print(f"[AlipayPay] Verify signature failed: {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
def _get_sign_content(self, params):
|
||||
@@ -205,9 +207,9 @@ class AlipayPay:
|
||||
# 构建完整的支付URL
|
||||
pay_url = f"{self.gateway_url}?{urlencode(params)}"
|
||||
|
||||
print(f"✅ 支付宝订单创建成功: {out_trade_no}")
|
||||
print(f" 金额: {total_amount}元")
|
||||
print(f" 标题: {subject}")
|
||||
# 日志输出到 stderr,避免影响 subprocess JSON 输出
|
||||
import sys
|
||||
print(f"[AlipayPay] Order created: {out_trade_no}, amount: {total_amount}", file=sys.stderr)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
@@ -216,7 +218,8 @@ class AlipayPay:
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 创建支付宝订单失败: {e}")
|
||||
import sys
|
||||
print(f"[AlipayPay] Create order failed: {e}", file=sys.stderr)
|
||||
return {
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
@@ -266,7 +269,9 @@ class AlipayPay:
|
||||
response = requests.get(self.gateway_url, params=params, timeout=30)
|
||||
result = response.json()
|
||||
|
||||
print(f"📡 支付宝查询响应: {result}")
|
||||
# 日志输出到 stderr
|
||||
import sys
|
||||
print(f"[AlipayPay] Query response: {result}", file=sys.stderr)
|
||||
|
||||
# 解析响应
|
||||
query_response = result.get('alipay_trade_query_response', {})
|
||||
@@ -295,10 +300,12 @@ class AlipayPay:
|
||||
}
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"❌ 支付宝API请求失败: {e}")
|
||||
import sys
|
||||
print(f"[AlipayPay] API request failed: {e}", file=sys.stderr)
|
||||
return {'success': False, 'error': f'网络请求失败: {e}'}
|
||||
except Exception as e:
|
||||
print(f"❌ 查询订单异常: {e}")
|
||||
import sys
|
||||
print(f"[AlipayPay] Query order error: {e}", file=sys.stderr)
|
||||
return {'success': False, 'error': str(e)}
|
||||
|
||||
def verify_callback(self, params):
|
||||
@@ -323,21 +330,22 @@ class AlipayPay:
|
||||
sign_content = self._get_sign_content(params)
|
||||
|
||||
# 验证签名
|
||||
import sys
|
||||
if self._verify(sign_content, sign):
|
||||
print(f"✅ 支付宝回调签名验证通过")
|
||||
print(f"[AlipayPay] Callback signature verified", file=sys.stderr)
|
||||
return {
|
||||
'success': True,
|
||||
'data': params
|
||||
}
|
||||
else:
|
||||
print(f"❌ 支付宝回调签名验证失败")
|
||||
print(f"[AlipayPay] Callback signature verification failed", file=sys.stderr)
|
||||
return {
|
||||
'success': False,
|
||||
'error': '签名验证失败'
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 验证回调异常: {e}")
|
||||
print(f"[AlipayPay] Verify callback error: {e}", file=sys.stderr)
|
||||
return {'success': False, 'error': str(e)}
|
||||
|
||||
def verify_return(self, params):
|
||||
@@ -403,14 +411,14 @@ if __name__ == '__main__':
|
||||
import sys
|
||||
|
||||
print("=" * 60)
|
||||
print("支付宝支付测试")
|
||||
print("Alipay Payment Test")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
# 检查配置
|
||||
is_ready, message = check_alipay_ready()
|
||||
print(f"\n配置状态: {'✅ 就绪' if is_ready else '❌ 未就绪'}")
|
||||
print(f"详情: {message}")
|
||||
print(f"\nConfig status: {'READY' if is_ready else 'NOT READY'}")
|
||||
print(f"Details: {message}")
|
||||
|
||||
if is_ready:
|
||||
# 创建实例
|
||||
@@ -421,19 +429,19 @@ if __name__ == '__main__':
|
||||
result = alipay.create_page_pay_url(
|
||||
out_trade_no=test_order_no,
|
||||
total_amount='0.01',
|
||||
subject='测试商品',
|
||||
body='这是一个测试订单'
|
||||
subject='Test Product',
|
||||
body='This is a test order'
|
||||
)
|
||||
|
||||
print(f"\n创建订单结果:")
|
||||
print(f"\nCreate order result:")
|
||||
print(json.dumps(result, indent=2, ensure_ascii=False))
|
||||
|
||||
if result['success']:
|
||||
print(f"\n🔗 支付链接(复制到浏览器打开):")
|
||||
print(f"\nPayment URL (open in browser):")
|
||||
print(result['pay_url'][:200] + '...')
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 测试失败: {e}")
|
||||
print(f"\nTest failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user