<?php
/**
 * 支付宝SDK配置
 * 从数据库读取配置
 */
require_once dirname(__FILE__) . '/../core/db/db.php';
$db = \Core\Db\DB::getInstance();

$alipayConfigs = $db->fetchAll("SELECT config_key, config_value FROM {$db->table('config')} WHERE config_key LIKE 'alipay_%' OR config_key LIKE 'site_%'");
$configs = [];
foreach ($alipayConfigs as $c) { $configs[$c['config_key']] = $c['config_value']; }

// 构建支付宝配置
$config = [
    'gatewayUrl'      => 'https://openapi.alipay.com/gateway.do',
    'app_id'          => $configs['alipay_appid'] ?? '',
    'merchant_private_key' => $configs['alipay_private_key'] ?? '',
    'alipay_public_key'   => $configs['alipay_public_key'] ?? '',
    'charset'         => 'UTF-8',
    'sign_type'       => 'RSA2',
    'notify_url'      => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? '') . '/api/payment.php?action=alipay_notify',
    'return_url'      => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? '') . '/api/payment.php?action=alipay_return',
];

return $config;
