OfflineApi.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\common\logic;
  3. use think\Env;
  4. use think\Log;
  5. /**
  6. * KKK的出款钱包API
  7. *
  8. * API文档:http://8.222.240.131:8079/doc.html
  9. */
  10. class OfflineApi
  11. {
  12. protected $urls = null;
  13. public function __construct() {
  14. // 初始化逻辑代码
  15. $this->urls = Env::get('rental.usdt_api_url');
  16. }
  17. /*
  18. * 获取BNB余额
  19. */
  20. public function getBalance(string $address)
  21. {
  22. $api_url = $this->urls . 'coinBalance/getBalance?';
  23. $api_url .= 'address=' . $address;
  24. $api_url .= '&coinType=bsc';
  25. $get_data = $this->curlGetData($api_url);
  26. if(empty($get_data)){
  27. return _error('API返回结果为空');
  28. }
  29. $get_data = json_decode($get_data, true);
  30. //返回数据结构
  31. //{
  32. // "code": 200,
  33. // "msg": "操作成功",
  34. // "data": 0.166243248
  35. //}
  36. if($get_data['code'] == 200){
  37. return _success($get_data['data']);
  38. }
  39. return _error($get_data['msg']);
  40. }
  41. /*
  42. * 获取代币合约余额,默认为USDT
  43. *
  44. * $contract 合约地址
  45. * $decimal 合约地址精度
  46. */
  47. public function getTokenBalance(string $address, string $contract = '0x55d398326f99059fF775485246999027B3197955', int $decimal = 18)
  48. {
  49. $api_url = $this->urls . 'coinBalance/getTokenBalance?';
  50. $api_url .= 'address=' . $address;
  51. $api_url .= '&coinType=bsc';
  52. $api_url .= '&contract=' . $contract;
  53. $api_url .= '&decimal=' . $decimal;
  54. $get_data = $this->curlGetData($api_url);
  55. if(empty($get_data)){
  56. return _error('API返回结果为空');
  57. }
  58. $get_data = json_decode($get_data, true);
  59. //返回数据结构
  60. //{
  61. // "code": 200,
  62. // "msg": "操作成功",
  63. // "data": 0.166243248
  64. //}
  65. if($get_data['code'] == 200){
  66. return _success($get_data['data']);
  67. }
  68. return _error($get_data['msg']);
  69. }
  70. /**
  71. * 新增提现
  72. */
  73. public function withdraw(string $address, float $mum, string $order_sn, string $api_key, string $api_pwd)
  74. {
  75. $data = [
  76. 'apiKey' => $api_key,
  77. 'toAddress' => $address,
  78. 'coinName' => 'usdt',
  79. 'coinType' => 'bsc',
  80. 'mum' => $mum,//提币数量
  81. 'companyWithdrawId' => $order_sn//本平台的唯一订单号
  82. ];
  83. $data['sign'] = md5($this->getSignKey($data, $api_pwd));
  84. $api_url = $this->urls . 'coinWithdraw/withdraw';
  85. $get_data = $this->curlPostData($api_url, $data);
  86. if(empty($get_data)){
  87. return _error('API返回结果为空');
  88. }
  89. $get_data = json_decode($get_data, true);
  90. //返回数据结构
  91. //{
  92. // "code": 200,
  93. // "msg": "操作成功",
  94. // "data": 0.166243248
  95. //}
  96. if($get_data['code'] == 200){
  97. return _success($get_data['data']);
  98. }
  99. return _error($get_data['msg']);
  100. }
  101. /**
  102. * 提现自动打款回调
  103. *
  104. * 接口回调信息格式:
  105. * companyWithdrawId=126&sign=8e3c6aee53e3ea4ff974c1d80f4e8beb&status=1&txId=0x39ce05a0698ff2b7459ca707703fd48937dd958422d98ebade6b4f5188b70995
  106. */
  107. public function withdrawCallback()
  108. {
  109. // $body1 = file_get_contents("php://input");
  110. // Log::write('提现自动打款回调1:' . $body1, 'info');
  111. //
  112. // $body = $this->request->post();
  113. $body = file_get_contents("php://input");
  114. //$body = 'companyWithdrawId=126&sign=8e3c6aee53e3ea4ff974c1d80f4e8beb&status=1&txId=0x39ce05a0698ff2b7459ca707703fd48937dd958422d98ebade6b4f5188b70995';
  115. Log::write('提现自动打款回调:' . $body, 'info');
  116. if(empty($body)){
  117. return _error('回调内容为空');
  118. }
  119. $parems = explode('&', $body);
  120. $req_arr = [];
  121. foreach ($parems as $item){
  122. $temp = explode('=', $item);
  123. $req_arr[$temp[0]] = $temp[1];
  124. }
  125. Log::write('提现自动打款回调参数:','info');
  126. Log::info(json_encode($req_arr));
  127. return _success($req_arr);
  128. }
  129. /**
  130. * 设置回调url
  131. *
  132. * 设置 api_key对应的回调地址
  133. *
  134. * url_type 通知地址类型 1充值 2提现
  135. */
  136. public function configNotice(string $api_key, string $api_pwd, string $url, int $url_type = 1)
  137. {
  138. $data = [
  139. 'apiKey' => $api_key,
  140. 'noticeUrl' => $url,
  141. 'urlType' => $url_type,
  142. ];
  143. $data['sign'] = md5($this->getSignKey($data, $api_pwd));
  144. $api_url = $this->urls . 'walletCollectTask/configNotice';
  145. dump($data);
  146. $get_data = $this->curlPostData($api_url, $data);
  147. if(empty($get_data)){
  148. return _error('API返回结果为空');
  149. }
  150. $get_data = json_decode($get_data, true);
  151. //返回数据结构
  152. //{
  153. // "code": 200,
  154. // "msg": "操作成功",
  155. // "data": null
  156. //}
  157. if($get_data['code'] == 200){
  158. return _success($get_data['data']);
  159. }
  160. return _error($get_data['msg']);
  161. }
  162. /**
  163. * 获取签名-带连接符号
  164. * @return string
  165. */
  166. protected function getSignKey(array $arr, string $secretKey)//: string
  167. {
  168. ksort($arr);
  169. $arr['apiSecret'] = $secretKey;
  170. $string = '';
  171. foreach ($arr as $key => $value)
  172. $string .= '&'.$key .'='.$value;
  173. return ltrim($string, '&');
  174. }
  175. /*
  176. * 封装Get请求
  177. */
  178. private function curlGetData($url){
  179. Log::info('查询余额');
  180. Log::info($url);
  181. # 初始化一个curl会话
  182. $ch = curl_init();
  183. # 判断是否是https
  184. if (stripos($url, "https://") !== false) {
  185. # 禁用后cURL将终止从服务端进行验证
  186. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  187. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  188. # 使用的SSL版本(2 或 3)
  189. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  190. }
  191. # 设置请求地址
  192. curl_setopt($ch, CURLOPT_URL, $url);
  193. # 在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
  194. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  195. # 执行这个请求
  196. $output = curl_exec($ch);
  197. # 关闭这个请求
  198. curl_close($ch);
  199. return $output;
  200. }
  201. /*
  202. * 封装Post请求
  203. */
  204. private function curlPostData($url , $data=array())
  205. {
  206. $headers = array("Content-type:application/x-www-form-urlencoded;charset=UTF-8");
  207. $ch = curl_init();
  208. curl_setopt($ch, CURLOPT_URL, $url);
  209. curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  211. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  212. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  213. // POST数据
  214. curl_setopt($ch, CURLOPT_POST, 1);
  215. // 把post的变量加上
  216. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  217. $output = curl_exec($ch);
  218. curl_close($ch);
  219. return $output;
  220. }
  221. }