<?php


namespace App\Services;


class FoxPay
{
    protected $merchantOrder;
    protected $thirdUserId;
    protected $currency;
    protected $amount;


    /**
     * 充值
     * 传入数组进行HTTP POST请求
     * @param $url
     * @param array $params
     * @param int $timeout
     * @return bool|string
     */
    public function curlPost($url, $params = array(),$timeout)
    {
        $payConfigService = new PayConfig();
        $config = $payConfigService->getConfig('foxPay');
        $data_string = json_encode($params); //将数组转json格式的数据
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Authorization: Basic'.base64_encode($config['merchantID'].':'.$config['SecretKey'].''), //添加头
                'Content-Length: ' . strlen($data_string))
        );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $hdresult = curl_exec($ch);
        curl_close($ch);
        return $hdresult;
    }



    public function sign($data = [])
    {
        unset($data['amount']);
        unset($data['fee']);
        ksort($data);  //按照参数名ASCII码从小到大排序

        $payConfigService = new PayConfig();
        $config = $payConfigService->getConfig('foxPay');

        $md5key = $config['SecretKey'];  //md5密钥

        $stringA = "";

        foreach ($data as $key => $value) {
            $stringA .= $value;
        }
        return md5($stringA . $md5key);  //拼接密钥后，md5加密后转为大写

    }

    // 提现签名
    public function cashierSign($requestData,$data)
    {
        ksort($requestData);  //按照参数名ASCII码从小到大排序
        $stringA = urldecode(http_build_query($requestData));
        ksort($data);  //按照参数名ASCII码从小到大排序
        $stringB = urldecode(http_build_query($data));
        $stringC = $stringA.'&'.$stringB;

        $payConfigService = new PayConfig();
        $config = $payConfigService->getConfig('auPay');
        $md5key = $config['SecretKey'];  //md5密钥
        return strtoupper(md5($stringC.$md5key));
    }

    public function allData()
    {
        $data = get_object_vars($this);
        return array_filter($data);
    }


    /**
     * @param mixed $amount
     */
    public function setAmount($amount): void
    {
        $this->amount = $amount;
    }

    /**
     * @return mixed
     */
    public function getAmount()
    {
        return $this->amount;
    }



    /**
     * @param mixed $memberid
     */
    public function setMemberid($memberid): void
    {
        $this->thirdUserId = $memberid;
    }

    /**
     * @return mixed
     */
    public function getMemberid()
    {
        return $this->thirdUserId;
    }


    /**
     * @param mixed $orderid
     */
    public function setOrderid($orderid): void
    {
        $this->merchantOrder = $orderid;
    }

    /**
     * @return mixed
     */
    public function getOrderid()
    {
        return $this->merchantOrder;
    }



    /**
     * @param mixed $currency
     */
    public function setCurrency($currency): void
    {
        $this->currency = $currency;
    }

}
