易宝支付服务商的接入代码展示

// header("Content-type: text/html; charset=gb2312");
class Yeepay
{

    protected $app;
    protected $key;
    protected $host;
    protected $basePath;
    protected $headers;
    protected $debug;
    protected $delimiter;

    protected $logistics_info;

    public function __construct($debug = false)
    {
        $this->app = '';
        $this->key = '';
        $this->host = '';
        $this->basePath = '/open-platform/dtpay';
        $this->debug = $debug;
        $this->delimiter = uniqid();

        $expires_time = session('expires_time');
        if($expires_time < time()){
            self::oauth();
        }
        $this->headers = session('oauth');
    }

    // 获取 TOKEN
    public function oauth()
    {
        $request_url = $this->host . "/open-platform/blade-auth/oauth/token";
        $postData = [
            "grant_type" => "client_credentials",
        ];
        $headers = [
            'Authorization' => "Basic " . base64_encode($this->app .':' . $this->key),
            'Content-Type' => "application/x-www-form-urlencoded",
            'charset' => "UTF-8",
        ];
        $result =  Requests::post($request_url, $headers, $postData);
        $oauth = json_decode($result->body,true);
        $time = time();
        $oauth['Blade-Auth'] = $oauth['access_token'];
        unset($oauth['access_token']);
        // 先缓存
        session('oauth', $oauth);
        session('expires_time', $time + $oauth['expires_in']);
        Session::save(); // 这个很重要
        return true;
    }

    /**
     * 统一下单 - 【交易模块】
     * @param $postData
     * @return mixed
     */
    public function prePay($postData)
    {
        // 在读取
        $request_url = $this->host . $this->basePath . "/doc/v1/trade/pre-pay";
        $headers = array_merge([
            'Content-Type' => "application/json",
        ], $this->headers);
        $result =  Requests::post($request_url, $headers, $this->aes_encrypt($postData));
        return json_decode($result->body,true);
    }

    /**
     * 申请现金退款 - 【交易模块】
     * @param $postData
     * @return mixed
     */
    public function refund($postData)
    {
        // 在读取
        $request_url = $this->host . $this->basePath . "/doc/v1/trade/refund";
        $headers = array_merge([
            'Content-Type' => "application/json",
        ], $this->headers);
        $result =  Requests::post($request_url, $headers, $this->aes_encrypt($postData));
        return json_decode($result->body,true);
    }

    /**
     * 查询订单 - 【交易模块】
     * @param $postData
     * @return mixed
     */
    public function payQuery($postData)
    {
        // 在读取
        $request['url'] = $this->host . $this->basePath . "/doc/v1/trade/order/query";
        $request['type'] = 'POST';
        $data = ['data' => $this->aes_encrypt($postData)];
        $request['data'] =  self::buildData($data);
        $request['headers'] = array_merge([
            "Content-Type" => "multipart/form-data; boundary=" . $this->delimiter,
            "Content-Length" => strlen($request['data'])
        ], $this->headers);
        // $result =  Requests::post($request['url'], $request['headers'], $data);
        $result =  Requests::request_multiple([$request]);
        $result = $result[0];
        return json_decode($result->body,true);
    }

    /**
     * 解密
     * @param $postData
     * @return mixed
     */
    public function aesDecrypt($postData)
    {
        return json_decode($this->aes_decrypt($postData), true);
    }

    /**【手动填充】
     * 对称解密使用的算法为 AES-256-CBC
     * @param string $data 需要加密的json
     * @return string base64后的补齐明文字符串
     */
    private function aes_encrypt(string $data){
        return base64_encode(openssl_encrypt(self::padZero($data, 16),'aes-256-cbc', $this->key,OPENSSL_NO_PADDING, substr($this->key, 0, 16)));
    }

    /**
     * 对密文进行解密
     * @param string $data 需要解密的密文
     * @return string 解密得到的明文
     */
    private function aes_decrypt(string $data){
        return self::subZero(openssl_decrypt(base64_decode($data),'aes-256-cbc', $this->key,OPENSSL_NO_PADDING, substr($this->key , 0 , 16)));
    }

    /**【手动填充】**
     * 对需要加密的明文进行填充补位
     * @param $data      string 需要进行填充补位操作的明文
     * @param $blockSize int 块大小为16个字节
     * @return string 补齐明文字符串
     */
    private function padZero(string $data, int $blockSize){
        $pad = $blockSize - (strlen($data) % $blockSize);
        return $data . str_repeat("\0", $pad);
    }

    /**【去除填充】**
     * 对解密后的明文进行补位删除
     * @param $text false|string 解密后的明文
     * @return false|string 删除填充补位后的明文
     */
    private function subZero($text)
    {
        $pad = ord(substr($text, -1));
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($text, 0, (strlen($text) - $pad));
    }
    private function buildData($param){
        $data = '';
        $eol = "\r\n";
        // $upload = $param['upload'];
        // unset($param['upload']);

        foreach ($param as $name => $content) {
            $data .= "--" . $this->delimiter . "\r\n"
                . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
                . $content . "\r\n";
        }
        // 拼接文件流
        // $data .= "--" . $this->delimiter . $eol
        //     . 'Content-Disposition: form-data; name="upload"; filename="' . $param['filename'] . '"' . "\r\n"
        //     . 'Content-Type:application/octet-stream'."\r\n\r\n";

        // $data .= $upload . "\r\n";
        $data .= "--" . $this->delimiter . "--\r\n";
        return $data;
    }
}

本文为原创文章,转载请注明出处!