Wxpay.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Wxpay.php UTF-8
  4. * huosdk_v8_btdev
  5. *
  6. * @date : 2019/12/2 20:26
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoIdentify\identifyDriver\driver;
  13. use huoIdentify\identifyDriver\Driver;
  14. use huolib\status\CommonStatus;
  15. use TencentCloud\Common\Credential;
  16. use TencentCloud\Faceid\V20180301\FaceidClient;
  17. use TencentCloud\Faceid\V20180301\Models\IdCardVerificationRequest;
  18. use think\Config;
  19. use think\Log;
  20. class Wxpay extends Driver {
  21. /**
  22. * 构造函数
  23. *
  24. * @param array $config
  25. */
  26. public function __construct($config = []) {
  27. if (empty($config)) {
  28. $config = (array)Config::get('identify_conf.weixin');
  29. }
  30. $_config = array(
  31. 'secretId' => get_val($config, 'SECRET_ID', ''),
  32. 'secretKey' => get_val($config, 'SECRET_KEY', ''),
  33. 'apiFrom' => get_val($config, 'API_FROM', 2), //认证api 来源 1 官方 2 市场
  34. );
  35. parent::__construct($_config);
  36. }
  37. /**
  38. * 获取认证
  39. *
  40. * @return array
  41. */
  42. public function identify() {
  43. $_api_from = $this->config['apiFrom'];
  44. if (1 == $_api_from) {
  45. return $this->fromOfficial();
  46. }
  47. return $this->fromMarket();
  48. }
  49. /***
  50. * 腾讯云官方认证接口
  51. * https://cloud.tencent.com/document/api/1007/33188
  52. */
  53. public function fromOfficial() {
  54. require_once 'wxpay/TCloudAutoLoader.php';
  55. // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
  56. $cred = new Credential($this->config['secretId'], $this->config['secretKey']);
  57. // # 实例化要请求产品(以cvm为例)的client对象
  58. $client = new FaceidClient($cred, "ap-guangzhou");
  59. // 实例化一个请求对象
  60. $req = new IdCardVerificationRequest();
  61. $req->IdCard = $this->id_card;
  62. $req->Name = $this->real_name;
  63. // 通过client对象调用想要访问的接口,需要传入请求对象
  64. $resp = $client->IdCardVerification($req);
  65. $_rs = $resp->toJsonString();
  66. $_rs_arr = json_decode($_rs, true);
  67. if ('0' != $_rs_arr['Result']) {
  68. Log::write(
  69. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  70. array($this->config, $this->id_card, $this->real_name)
  71. ).'&rs='.$_rs.'[微信实名认证api]', Log::ERROR
  72. );
  73. $_code = CommonStatus::INNER_ERROR;
  74. return $this->huoError($_code, $_rs_arr['Description']);
  75. }
  76. $_code = CommonStatus::NO_ERROR;
  77. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  78. }
  79. /***
  80. * 腾讯云市场认证接口
  81. * https://market.cloud.tencent.com/products/15838
  82. */
  83. public function fromMarket() {
  84. // 云市场分配的密钥Id
  85. $secretId = $this->config['secretId'];
  86. // 云市场分配的密钥Key
  87. $secretKey = $this->config['secretKey'];
  88. $source = 'market';
  89. // 签名
  90. $datetime = gmdate('D, d M Y H:i:s T');
  91. $signStr = sprintf("x-date: %s\nx-source: %s", $datetime, $source);
  92. $sign = base64_encode(hash_hmac('sha1', $signStr, $secretKey, true));
  93. $auth = sprintf(
  94. 'hmac id="%s", algorithm="hmac-sha1", headers="x-date x-source", signature="%s"', $secretId, $sign
  95. );
  96. // 请求方法
  97. $method = 'GET';
  98. // 请求头
  99. $headers = array(
  100. 'X-Source' => $source,
  101. 'X-Date' => $datetime,
  102. 'Authorization' => $auth,
  103. );
  104. // 查询参数
  105. $queryParams = array(
  106. 'cardNo' => $this->id_card,
  107. 'realName' => $this->real_name,
  108. );
  109. // body参数(POST方法下)
  110. $bodyParams = array();
  111. // url参数拼接
  112. $url = 'https://service-d7fobmw9-1253662630.ap-shanghai.apigateway.myqcloud.com/release/VerifyIdcard';
  113. if (count($queryParams) > 0) {
  114. $url .= '?'.http_build_query($queryParams);
  115. }
  116. $ch = curl_init();
  117. curl_setopt($ch, CURLOPT_URL, $url);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  119. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  120. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  121. curl_setopt(
  122. $ch, CURLOPT_HTTPHEADER, array_map(
  123. function ($v, $k) {
  124. return $k.': '.$v;
  125. }, array_values($headers), array_keys($headers)
  126. )
  127. );
  128. if (in_array($method, array('POST', 'PUT', 'PATCH'), true)) {
  129. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($bodyParams));
  130. }
  131. $_rs_json = curl_exec($ch);
  132. curl_close($ch);
  133. $_rs = json_decode($_rs_json, true);
  134. if (!isset($_rs['error_code']) && isset($_rs['message'])) {
  135. Log::write(
  136. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  137. array($this->config, $this->id_card, $this->real_name)
  138. ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR
  139. );
  140. $_code = CommonStatus::INNER_ERROR;
  141. return $this->huoError($_code, $_rs['message']);
  142. }
  143. if ((isset($_rs['error_code']) && '0' != $_rs['error_code']) || empty($_rs['result'])) {
  144. Log::write(
  145. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  146. array($this->config, $this->id_card, $this->real_name)
  147. ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR
  148. );
  149. $_code = CommonStatus::INNER_ERROR;
  150. return $this->huoError($_code, CommonStatus::getMsg($_code));
  151. }
  152. if (!isset($_rs['result']['isok']) || false === $_rs['result']['isok']) {
  153. Log::write(
  154. 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode(
  155. array($this->config, $this->id_card, $this->real_name)
  156. ).'&rs='.$_rs_json.'[微信实名认证api]', Log::ERROR
  157. );
  158. $_code = CommonStatus::INNER_ERROR;
  159. return $this->huoError($_code, CommonStatus::getMsg($_code));
  160. }
  161. $_code = CommonStatus::NO_ERROR;
  162. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  163. }
  164. }