<?php
require 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;

if ($settings = sql_fetch_assoc(sql_select($config['table_prefix'] . "settings", "name = 'cbext.clientbase.merchant_tinkoff'"))){ //проверяем существует ли таблица
    if (!empty($settings['value'])){
    
    $setting = Yaml::parse($settings['value']); //конвертируем YAML в массив
    run_query("UPDATE " . $config['table_prefix'] . "cron SET `command` = '".'$extensionManager = \\\CB\\\Core::get(\\\CB\\\Core::EXTENSION_MANAGER);
    $checkPayment = $extensionManager->getService("cbext.clientbase.merchant_tinkoff", "CheckPayment");
    $checkPayment->updatePaymentTable();
    echo "Done";'."' WHERE id = ". $setting['cron_id']);
    foreach($setting['calcs'] as $key => $calc_id){
        run_query("UPDATE " . $config['table_prefix'] . "calc SET `calculate` = '".'$line["f'. $setting['link_fields'][$key].'"] = $config["site_url"] ."/api/dev/cbext/clientbase/merchant_tinkoff/p?id=". base64_encode($table_id . "|" . $line_id);'."' WHERE id =". $calc_id);
    }
    foreach($setting['buttons'] as $key => $button){
        run_query("UPDATE " . $config['table_prefix'] . "calc SET `calculate` = '".'$line["f'. $setting['link_fields'][$key].'"] = $config["site_url"] ."/api/dev/cbext/clientbase/merchant_tinkoff/p?id=". base64_encode($table_id . "|" . $line_id);'."' WHERE name = 'Button ". $button. "'");
    }
    if(file_exists(\CB\Core::get(\CB\Core::COMMON_CONTEXT)->config('site_path') . '/tinkoff.php')) {
        $extFileCode = <<<'EOC'
<?php
header('Location: api/dev/cbext/clientbase/merchant_tinkoff/p?id='.$_REQUEST['id']);
exit;
EOC;
    file_put_contents(\CB\Core::get(\CB\Core::COMMON_CONTEXT)->config('site_path') . '/tinkoff.php', $extFileCode);
    }
}
}  