| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | <?php/** * Wxpay.php  UTF-8 * huosdk_v8_btdev * * @date    : 2019/12/2 20:26 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : chenbingling <cbl@huosdk.com> * @version : HUOSDK 8.5 */namespace huoIdentify\identifyDriver\driver;use huoIdentify\identifyDriver\Driver;use huolib\status\CommonStatus;use TencentCloud\Common\Credential;use TencentCloud\Faceid\V20180301\FaceidClient;use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;use think\Config;use think\Log;class Wxpay extends Driver {    /**     * 构造函数     *     * @param array $config     */    public function __construct($config = []) {        if (empty($config)) {            $config = (array)Config::get('identify_conf.weixin');        }        $_config = array(            'secretId'  => get_val($config, 'SECRET_ID', ''),            'secretKey' => get_val($config, 'SECRET_KEY', ''),            'apiFrom'   => get_val($config, 'API_FROM', 2),  //认证api 来源 1 官方 2 市场        );        parent::__construct($_config);    }    /**     * 获取认证     *     * @return array     */    public function identify() {        $_api_from = $this->config['apiFrom'];        if (1 == $_api_from) {            return $this->fromOfficial();        }        return $this->fromMarket();    }    /***     * 腾讯云官方认证接口     * https://cloud.tencent.com/document/api/1007/33188     */    public function fromOfficial() {        require_once 'wxpay/TCloudAutoLoader.php';        // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey        $cred = new Credential($this->config['secretId'], $this->config['secretKey']);        // # 实例化要请求产品(以cvm为例)的client对象        $client = new FaceidClient($cred, "ap-guangzhou");        // 实例化一个请求对象        $req = new IdCardVerificationRequest();        $req->IdCard = $this->id_card;        $req->Name = $this->real_name;        // 通过client对象调用想要访问的接口,需要传入请求对象        $resp = $client->IdCardVerification($req);        $_rs = $resp->toJsonString();        $_rs_arr = json_decode($_rs, true);        if ('0' != $_rs_arr['Result']) {            Log::write(                'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode(                    array($this->config, $this->id_card, $this->real_name)                ).'&rs='.$_rs.'[微信实名认证api]', Log::ERROR            );            $_code = CommonStatus::INNER_ERROR;            return $this->huoError($_code, $_rs_arr['Description']);        }        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));    }    /***     * 腾讯云市场认证接口     * https://market.cloud.tencent.com/products/15838     */    public function fromMarket() {        // 云市场分配的密钥Id        $secretId = $this->config['secretId'];        // 云市场分配的密钥Key        $secretKey = $this->config['secretKey'];        $source = 'market';        // 签名        $datetime = gmdate('D, d M Y H:i:s T');        $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);        $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));        $auth = sprintf(            'hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign        );        // 请求方法        $method = 'GET';        // 请求头        $headers = array(            'X-Source'      => $source,            'X-Date'        => $datetime,            'Authorization' => $auth,        );        // 查询参数        $queryParams = array(            'cardNo'   => $this->id_card,            'realName' => $this->real_name,        );        // body参数(POST方法下)        $bodyParams = array();        // url参数拼接        $url = 'https://service-d7fobmw9-1253662630.ap-shanghai.apigateway.myqcloud.com/release/VerifyIdcard';        if (count($queryParams) > 0) {            $url .= '?'.http_build_query($queryParams);        }        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_TIMEOUT, 60);        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);        curl_setopt(            $ch, CURLOPT_HTTPHEADER, array_map(                   function ($v, $k) {                       return $k.': '.$v;                   }, array_values($headers), array_keys($headers)               )        );        if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));        }        $_rs_json = curl_exec($ch);        curl_close($ch);        $_rs = json_decode($_rs_json, true);        if (!isset($_rs['error_code']) && isset($_rs['message'])) {            Log::write(                'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode(                    array($this->config, $this->id_card, $this->real_name)                ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR            );            $_code = CommonStatus::INNER_ERROR;            return $this->huoError($_code, $_rs['message']);        }        if ((isset($_rs['error_code']) && '0' != $_rs['error_code']) || empty($_rs['result'])) {            Log::write(                'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode(                    array($this->config, $this->id_card, $this->real_name)                ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR            );            $_code = CommonStatus::INNER_ERROR;            return $this->huoError($_code, CommonStatus::getMsg($_code));        }        if (!isset($_rs['result']['isok']) || false === $_rs['result']['isok']) {            Log::write(                'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'¶m='.json_encode(                    array($this->config, $this->id_card, $this->real_name)                ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR            );            $_code = CommonStatus::INNER_ERROR;            return $this->huoError($_code, CommonStatus::getMsg($_code));        }        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));    }}
 |