<commit_before><?php

namespace Yansongda\Pay\Gateways\Wechat;

use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Traits\HasHttpRequest;

abstract class Wechat implements GatewayInterface
{
    use HasHttpRequest;

    /**
     * @var string
     */
    protected $gateway = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

    /**
     * @var string
     */
    protected $gateway_query = 'https://api.mch.weixin.qq.com/pay/orderquery';

    /**
     * @var string
     */
    protected $gateway_close = 'https://api.mch.weixin.qq.com/pay/closeorder';

    /**
     * @var string
     */
    protected $gateway_refund = 'https://api.mch.weixin.qq.com/secapi/pay/refund';

    /**
     * @var array
     */
    protected $config;

    /**
     * @var \Yansongda\Pay\Support\Config
     */
    protected $user_config;

    /**
     * [__construct description].
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->user_config = new Config($config);

        $this->config = [
            'appid'      => $this->user_config->get('app_id', ''),
            'mch_id'     => $this->user_config->get('mch_id', ''),
            'nonce_str'  => $this->createNonceStr(),
            'sign_type'  => 'MD5',
            'notify_url' => $this->user_config->get('notify_url', ''),
            'trade_type' => $this->getTradeType(),
        ];
    }

    /**
     * pay a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config_biz
     *
     * @return mixed
     */
    abstract public function pay(array $config_biz = []);

    /**
     * refund.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return string|bool
     */
    public function refund($config_biz = [])
    {
        $this->config = array_merge($this->config, $config_biz);
        $this->config['total_fee'] = intval($this->config['total_fee'] * 100);
        $this->config['refund_fee'] = intval($this->config['refund_fee'] * 100);
        $this->config['op_user_id'] = isset($this->config['op_user_id']) ?: $this->user_config->get('mch_id', '');

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_refund, true);
    }

    /**
     * close a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return array|bool
     */
    public function close($out_trade_no = '')
    {
        $this->config['out_trade_no'] = $out_trade_no;

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_close);
    }

    /**
     * find a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $out_trade_no
     *
     * @return array|bool
     */
    public function find($out_trade_no = '')
    {
        $this->config['out_trade_no'] = $out_trade_no;

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_query);
    }

    /**
     * verify the notify.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $data
     * @param string $sign
     * @param bool   $sync
     *
     * @return array|bool
     */
    public function verify($data, $sign = null, $sync = false)
    {
        $data = $this->fromXml($data);

        $sign = is_null($sign) ? $data['sign'] : $sign;

        return $this->getSign($data) === $sign ? $data : false;
    }

    /**
     * get trade type config.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return string
     */
    abstract protected function getTradeType();

    /**
     * pre order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config_biz
     *
     * @return array
     */
    protected function preOrder($config_biz = [])
    {
        $this->config = array_merge($this->config, $config_biz);
        $this->config['total_fee'] = intval($this->config['total_fee'] * 100);

        return $this->getResult($this->gateway);
    }

    /**
     * get api result.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $end_point
     * @param bool   $cert
     *
     * @return array
     */
    protected function getResult($end_point, $cert = false)
    {
        $this->config['sign'] = $this->getSign($this->config);

        if ($cert) {
            $data = $this->fromXml($this->post(
                $end_point,
                $this->toXml($this->config),
                [
                    'cert'    => $this->user_config->get('cert_client', ''),
                    'ssl_key' => $this->user_config->get('cert_key', ''),
                ]
            ));
        } else {
            $data = $this->fromXml($this->post($end_point, $this->toXml($this->config)));
        }

        if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
            $error = 'getResult error:'.$data['return_msg'];
            $error .= isset($data['err_code_des']) ? ' - '.$data['err_code_des'] : '';
        }

        if (!isset($error) && $this->getSign($data) !== $data['sign']) {
            $error = 'getResult error: return data sign error';
        }

        if (isset($error)) {
            throw new GatewayException(
                $error,
                20000,
                $data);
        }

        return $data;
    }

    /**
     * sign.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function getSign($data)
    {
        if (is_null($this->user_config->get('key'))) {
            throw new InvalidArgumentException('Missing Config -- [key]');
        }

        ksort($data);

        $string = md5($this->getSignContent($data).'&key='.$this->user_config->get('key'));

        return strtoupper($string);
    }

    /**
     * get sign content.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function getSignContent($data)
    {
        $buff = '';

        foreach ($data as $k => $v) {
            $buff .= ($k != 'sign' && $v != '' && !is_array($v)) ? $k.'='.$v.'&' : '';
        }

        return trim($buff, '&');
    }

    /**
     * create random string.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param int $length
     *
     * @return string
     */
    protected function createNonceStr($length = 16)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }

    /**
     * convert to xml.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function toXml($data)
    {
        if (!is_array($data) || count($data) <= 0) {
            throw new InvalidArgumentException('convert to xml error!invalid array!');
        }

        $xml = '<xml>';
        foreach ($data as $key => $val) {
            $xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
                                       '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
        }
        $xml .= '</xml>';

        return $xml;
    }

    /**
     * convert to array.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $xml
     *
     * @return array
     */
    protected function fromXml($xml)
    {
        if (!$xml) {
            throw new InvalidArgumentException('convert to array error !invalid xml');
        }

        libxml_disable_entity_loader(true);

        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
    }

    /**
     * delete trade_type and notify_url.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return bool
     */
    protected function unsetTradeTypeAndNotifyUrl()
    {
        unset($this->config['notify_url']);
        unset($this->config['trade_type']);

        return true;
    }
}
<commit_msg>不对传入金额进行*100操作<commit_after><?php

namespace Yansongda\Pay\Gateways\Wechat;

use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Traits\HasHttpRequest;

abstract class Wechat implements GatewayInterface
{
    use HasHttpRequest;

    /**
     * @var string
     */
    protected $gateway = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

    /**
     * @var string
     */
    protected $gateway_query = 'https://api.mch.weixin.qq.com/pay/orderquery';

    /**
     * @var string
     */
    protected $gateway_close = 'https://api.mch.weixin.qq.com/pay/closeorder';

    /**
     * @var string
     */
    protected $gateway_refund = 'https://api.mch.weixin.qq.com/secapi/pay/refund';

    /**
     * @var array
     */
    protected $config;

    /**
     * @var \Yansongda\Pay\Support\Config
     */
    protected $user_config;

    /**
     * [__construct description].
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->user_config = new Config($config);

        $this->config = [
            'appid'      => $this->user_config->get('app_id', ''),
            'mch_id'     => $this->user_config->get('mch_id', ''),
            'nonce_str'  => $this->createNonceStr(),
            'sign_type'  => 'MD5',
            'notify_url' => $this->user_config->get('notify_url', ''),
            'trade_type' => $this->getTradeType(),
        ];
    }

    /**
     * pay a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config_biz
     *
     * @return mixed
     */
    abstract public function pay(array $config_biz = []);

    /**
     * refund.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return string|bool
     */
    public function refund($config_biz = [])
    {
        $this->config = array_merge($this->config, $config_biz);
        $this->config['total_fee'] = $this->config['total_fee'];
        $this->config['refund_fee'] = $this->config['refund_fee'];
        $this->config['op_user_id'] = isset($this->config['op_user_id']) ?: $this->user_config->get('mch_id', '');

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_refund, true);
    }

    /**
     * close a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return array|bool
     */
    public function close($out_trade_no = '')
    {
        $this->config['out_trade_no'] = $out_trade_no;

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_close);
    }

    /**
     * find a order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $out_trade_no
     *
     * @return array|bool
     */
    public function find($out_trade_no = '')
    {
        $this->config['out_trade_no'] = $out_trade_no;

        $this->unsetTradeTypeAndNotifyUrl();

        return $this->getResult($this->gateway_query);
    }

    /**
     * verify the notify.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $data
     * @param string $sign
     * @param bool   $sync
     *
     * @return array|bool
     */
    public function verify($data, $sign = null, $sync = false)
    {
        $data = $this->fromXml($data);

        $sign = is_null($sign) ? $data['sign'] : $sign;

        return $this->getSign($data) === $sign ? $data : false;
    }

    /**
     * get trade type config.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return string
     */
    abstract protected function getTradeType();

    /**
     * pre order.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $config_biz
     *
     * @return array
     */
    protected function preOrder($config_biz = [])
    {
        $this->config = array_merge($this->config, $config_biz);
        $this->config['total_fee'] = $this->config['total_fee'];

        return $this->getResult($this->gateway);
    }

    /**
     * get api result.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $end_point
     * @param bool   $cert
     *
     * @return array
     */
    protected function getResult($end_point, $cert = false)
    {
        $this->config['sign'] = $this->getSign($this->config);

        if ($cert) {
            $data = $this->fromXml($this->post(
                $end_point,
                $this->toXml($this->config),
                [
                    'cert'    => $this->user_config->get('cert_client', ''),
                    'ssl_key' => $this->user_config->get('cert_key', ''),
                ]
            ));
        } else {
            $data = $this->fromXml($this->post($end_point, $this->toXml($this->config)));
        }

        if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
            $error = 'getResult error:'.$data['return_msg'];
            $error .= isset($data['err_code_des']) ? ' - '.$data['err_code_des'] : '';
        }

        if (!isset($error) && $this->getSign($data) !== $data['sign']) {
            $error = 'getResult error: return data sign error';
        }

        if (isset($error)) {
            throw new GatewayException(
                $error,
                20000,
                $data);
        }

        return $data;
    }

    /**
     * sign.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function getSign($data)
    {
        if (is_null($this->user_config->get('key'))) {
            throw new InvalidArgumentException('Missing Config -- [key]');
        }

        ksort($data);

        $string = md5($this->getSignContent($data).'&key='.$this->user_config->get('key'));

        return strtoupper($string);
    }

    /**
     * get sign content.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function getSignContent($data)
    {
        $buff = '';

        foreach ($data as $k => $v) {
            $buff .= ($k != 'sign' && $v != '' && !is_array($v)) ? $k.'='.$v.'&' : '';
        }

        return trim($buff, '&');
    }

    /**
     * create random string.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param int $length
     *
     * @return string
     */
    protected function createNonceStr($length = 16)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }

    /**
     * convert to xml.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param array $data
     *
     * @return string
     */
    protected function toXml($data)
    {
        if (!is_array($data) || count($data) <= 0) {
            throw new InvalidArgumentException('convert to xml error!invalid array!');
        }

        $xml = '<xml>';
        foreach ($data as $key => $val) {
            $xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
                                       '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
        }
        $xml .= '</xml>';

        return $xml;
    }

    /**
     * convert to array.
     *
     * @author yansongda <<EMAIL>>
     *
     * @param string $xml
     *
     * @return array
     */
    protected function fromXml($xml)
    {
        if (!$xml) {
            throw new InvalidArgumentException('convert to array error !invalid xml');
        }

        libxml_disable_entity_loader(true);

        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
    }

    /**
     * delete trade_type and notify_url.
     *
     * @author yansongda <<EMAIL>>
     *
     * @return bool
     */
    protected function unsetTradeTypeAndNotifyUrl()
    {
        unset($this->config['notify_url']);
        unset($this->config['trade_type']);

        return true;
    }
}

{'fraction_non_alphanumeric': 0.17066504706954144, 'fraction_numerical': 0.0036440935317339812, 'mean_word_length': 1.9066195939982349, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 221, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4798971', 'n_tokens_mistral': 5498, 'n_tokens_neox': 5313, 'n_words': 917}