import jingrow

@jingrow.whitelist()
def create_payment_link(payment_method, order_id, amount, subject, user_id=None):
    """创建支付链接
    
    Args:
        payment_method: 支付方式 (alipay/wechatpay)
        order_id: 订单ID
        amount: 金额
        subject: 订单主题
        user_id: 用户ID (可选)
    
    Returns:
        支付链接或二维码链接
    """
    if not user_id:
        user_id = jingrow.session.user
    
    try:
        if payment_method == "alipay":
            from jcloud.api.payment.alipay import AlipayAPI
            api = AlipayAPI()
            return api.generate_payment_url(order_id, amount, subject, user_id)
            
        elif payment_method == "wechatpay":
            from jcloud.api.payment.wechatpay import WeChatPayAPI
            api = WeChatPayAPI()
            return api.generate_payment_url(order_id, amount, subject, user_id)
            
        else:
            jingrow.throw(f"不支持的支付方式: {payment_method}")
    except Exception as e:
        jingrow.log_error(f"创建支付链接失败: {str(e)}", f"{payment_method} Payment Error")
        jingrow.throw(f"创建支付链接失败: {str(e)}")
