<commit_before><?php

namespace hiqdev\php\merchant;

use Closure;
use ReflectionClass;

abstract class Merchant
{
    protected $_error;
    protected $_secret;
    protected $_secret2;
    protected $_config;
    protected static $_defaults = [
        'basePage'    => '/merchant/pay/',
        'proto'       => 'https',
        'fee'         => 0,
        'quantity'    => 1,
        'method'      => 'POST',
        'currency'    => 'usd',
        'username'    => '',
        'isCart'      => true,
        'confirmText' => 'OK',
    ];

    public function __construct($config = [])
    {
        $config = array_merge((array)self::$_defaults, (array)static::$_defaults, (array)$config);
        $this->_secret  = $config['secret'];
        $this->_secret2 = $config['secret2'];
        unset($config['secret'], $config['secret2']);
        $this->_config = $config;
    }

    public static function create($config)
    {
        $reflection = new ReflectionClass(static::guessClass($config));
        return $reflection->newInstanceArgs([$config]);
    }

    public static function guessClass($config)
    {
        if ($config['class']) {
            return $config['class'];
        }
        $name = $config['name'] ?: $config['id'];
        if (!$name) {
            throw new SystemException('No merchant class given!');
        }
        return "hiqdev\\php\\merchant\\$name\\Merchant";
    }

    abstract public function getInputs();

    abstract public function validateConfirmation($data);

    public function get($name, $default = null)
    {
        $res = $this->_config[$name];
        $res = is_null($res) ? $default : $res;
        return $res instanceof Closure ? call_user_func($res, $this) : $res;
    }

    public function set($name, $value)
    {
        $this->_config[$name] = $value;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->_config)) {
            return $this->get($name);
        }
        $getter = 'get' . $name;
        if (!method_exists($this, $getter)) {
            throw new SystemException('Getting unknown property: ' . get_class($this) . '::' . $name);
        }
        return $this->$getter();
    }

    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } else {
            $this->set($name, $value);
        }
    }

    public function mset($values)
    {
        foreach ($values as $k => $v) {
            $this->set($k, $v);
        }
    }
    public function error($name)
    {
        $this->_error = $name;
        return false;
    }

    public function getError()
    {
        return $this->_error;
    }

    public function getInfopath()
    {
        return '/' . join('/', [$this->name, $this->currency, $this->username]);
    }

    public function getConfirmUrl()
    {
        return $this->getReturnUrl('confirm');
    }

    public function getSuccessUrl()
    {
        return $this->getReturnUrl('success');
    }

    public function getFailureUrl()
    {
        return $this->getReturnUrl('failure');
    }

    public function getReturnUrl($return)
    {
        return $this->siteUrl . $this->getReturnPage($return) . $this->infopath;
    }

    public function getSite()
    {
        return $_SERVER['HTTP_HOST'];
    }

    public function getSiteUrl()
    {
        return $this->proto . '://' . $this->site;
    }

    public function getReturnPage($return)
    {
        return $this->get($return.'Page', $this->basePage . $return);
    }

    public function getDescription()
    {
        return $this->site . ': deposit ' . $this->username;
    }

    public function getTotal()
    {
        return $this->formatMoney($this->convertTo($this->sum + $this->fee));
    }

    public function getCents()
    {
        return $this->formatCents($this->total);
    }

    public function getInvoiceId()
    {
        return implode('_', [$this->uniqId, $this->username, $this->cents]);
    }

    public function getFormId()
    {
        return implode('_', ['merchant', $this->name, $this->currency]);
    }

    protected $_time;

    public function getTime()
    {
        if ($this->_time === null) {
            $this->_time = date('c');
        }
        return $this->_time;
    }

    protected $_uniqId;

    public function getUniqId()
    {
        if ($this->_uniqId === null) {
            $this->_uniqId = uniqid();
        }
        return $this->_uniqId;
    }

    public function checkMoney($sum)
    {
        return preg_match('/^\d+(\.\d{1,2)?$/', $sum) ? $this->formatMoney($sum) : null;
    }

    public function formatMoney($sum)
    {
        return number_format($sum, 2, '.', '');<commit_msg>changed: redone to use system <- name<commit_after><?php

namespace hiqdev\php\merchant;

use Closure;
use ReflectionClass;

abstract class Merchant
{
    protected $_error;
    protected $_secret;
    protected $_secret2;
    protected $_config;
    protected static $_defaults = [
        'basePage'    => '/merchant/pay/',
        'proto'       => 'https',
        'fee'         => 0,
        'quantity'    => 1,
        'method'      => 'POST',
        'currency'    => 'usd',
        'username'    => '',
        'isCart'      => true,
        'confirmText' => 'OK',
    ];

    public function __construct($config = [])
    {
        $config = array_merge((array)self::$_defaults, (array)static::$_defaults, (array)$config);
        $this->_secret  = $config['secret'];
        $this->_secret2 = $config['secret2'];
        unset($config['secret'], $config['secret2']);
        $this->_config = $config;
    }

    public static function create($config)
    {
        $reflection = new ReflectionClass(static::guessClass($config));
        return $reflection->newInstanceArgs([$config]);
    }

    public static function guessClass($config)
    {
        if ($config['class']) {
            return $config['class'];
        }
        $system = $config['system'] ?: $config['name'];
        if (!$system) {
            throw new SystemException('No merchant class or system given!');
        }
        return "hiqdev\\php\\merchant\\$system\\Merchant";
    }

    abstract public function getInputs();

    abstract public function validateConfirmation($data);

    public function get($name, $default = null)
    {
        $res = $this->_config[$name];
        $res = is_null($res) ? $default : $res;
        return $res instanceof Closure ? call_user_func($res, $this) : $res;
    }

    public function set($name, $value)
    {
        $this->_config[$name] = $value;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->_config)) {
            return $this->get($name);
        }
        $getter = 'get' . $name;
        if (!method_exists($this, $getter)) {
            throw new SystemException('Getting unknown property: ' . get_class($this) . '::' . $name);
        }
        return $this->$getter();
    }

    public function __set($name, $value)
    {
        $setter = 'set' . $name;
        if (method_exists($this, $setter)) {
            $this->$setter($value);
        } else {
            $this->set($name, $value);
        }
    }

    public function mset($values)
    {
        foreach ($values as $k => $v) {
            $this->set($k, $v);
        }
    }
    public function error($name)
    {
        $this->_error = $name;
        return false;
    }

    public function getError()
    {
        return $this->_error;
    }

    public function getInfopath()
    {
        return '/' . join('/', [$this->system, $this->currency, $this->username]);
    }

    public function getConfirmUrl()
    {
        return $this->getReturnUrl('confirm');
    }

    public function getSuccessUrl()
    {
        return $this->getReturnUrl('success');
    }

    public function getFailureUrl()
    {
        return $this->getReturnUrl('failure');
    }

    public function getReturnUrl($return)
    {
        return $this->siteUrl . $this->getReturnPage($return) . $this->infopath;
    }

    public function getSite()
    {
        return $_SERVER['HTTP_HOST'];
    }

    public function getSiteUrl()
    {
        return $this->proto . '://' . $this->site;
    }

    public function getReturnPage($return)
    {
        return $this->get($return.'Page', $this->basePage . $return);
    }

    public function getDescription()
    {
        return $this->site . ': deposit ' . $this->username;
    }

    public function getTotal()
    {
        return $this->formatMoney($this->convertTo($this->sum + $this->fee));
    }

    public function getCents()
    {
        return $this->formatCents($this->total);
    }

    public function getInvoiceId()
    {
        return implode('_', [$this->uniqId, $this->username, $this->cents]);
    }

    public function getFormId()
    {
        return implode('_', ['merchant', $this->system, $this->currency]);
    }

    protected $_time;

    public function getTime()
    {
        if ($this->_time === null) {
            $this->_time = date('c');
        }
        return $this->_time;
    }

    protected $_uniqId;

    public function getUniqId()
    {
        if ($this->_uniqId === null) {
            $this->_uniqId = uniqid();
        }
        return $this->_uniqId;
    }

    public function checkMoney($sum)
    {
        return preg_match('/^\d+(\.\d{1,2)?$/', $sum) ? $this->formatMoney($sum) : null;
    }

    public function formatMoney($sum)
    {
        return number_format($sum, 2, '.', '');
{'fraction_non_alphanumeric': 0.16270585782780905, 'fraction_numerical': 0.001876172607879925, 'mean_word_length': 2.0931656995486785, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '459929', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2866, 'n_words': 595}