<?php

declare(strict_types=1);

class MpayClass
{
    private $pid;
    private $key;
    private $host;
    private $check_neworder_url;
    private $submit_records_url;
    function __construct($config)
    {
        $this->pid = $config['pid'];
        $this->key = $config['key'];
        $this->host = $config['host'];
        $this->check_neworder_url = $this->host . '/order.php';
        $this->submit_records_url = $this->host . '/payHeart';
    }
    // 查询新订单
    public function orderHeart()
    {
        $url = $this->check_neworder_url . "?pid={$this->pid}&sign={$this->getSign()}";
        $res = $this->getHttpResponse($url);
        return $res;
    }
    // 提交收款明细
    public function upRecords($records, $aid)
    {
        $header = ['Content-Type: application/json;charset=UTF-8'];
        $url = $this->submit_records_url . "?pid={$this->pid}&aid={$aid}&sign={$this->getSign()}";
        $res = $this->getHttpResponse($url, $header, json_encode($records));
        return $res;
    }

    // 签名方法
    private function getSign()
    {
        return md5($this->pid . $this->key);
    }
    // 请求外部资源
    private function getHttpResponse($url, $header = [], $post = null, $timeout = 10)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        } else {
            $httpheader[] = "Accept: */*";
            $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
            $httpheader[] = "Connection: close";
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        }
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
}
