Transaction.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\ApiUser;
  4. use app\common\model\Orders;
  5. use app\common\model\OrdersRequestLog;
  6. use think\facade\Db;
  7. use think\Exception;
  8. use think\facade\Request;
  9. use think\Log;
  10. class Transaction extends Base
  11. {
  12. protected $contract_address = '0x55d398326f99059ff775485246999027b3197955';//交易币种的合约地址,默认为usdt的:0x55d398326f99059ff775485246999027b3197955,测试合约:0xcf3271b1be72f834e4dd35b9d558d583894473f1
  13. /**
  14. *
  15. *
  16. *
  17. * @return void
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public function withdraw(){
  23. // $data = [
  24. // 'apiId' => '$api_key',
  25. // 'toAddress' => '$address',
  26. // 'coinName' => 'usdt',
  27. // 'coinType' => 'bsc',
  28. // 'amount' => '$mum',//提币数量
  29. // 'orderNo' => '$order_sn',//本平台的唯一订单号
  30. // 'timeStamp' => '$order_sn',//本平台的唯一订单号
  31. // 'sign' => '$order_sn'//本平台的唯一订单号
  32. // ];
  33. //获取post参数
  34. $params = Request::post();
  35. $api_user = (new ApiUser())
  36. ->where('api_id', $params['apiId'])
  37. ->find();
  38. if(empty($api_user)){
  39. $this->error('apiId有误');
  40. }
  41. $sign = $params['sign'];
  42. // "param" => "amount=2&apiId=1001&coinName=usdt&coinType=BEP20&orderNo=T2023&timeStamp=1700584041&toAddress=ox4545454545-&apiKey=123456"
  43. if($sign != md5(getSignKey($params, $api_user['api_key']))){
  44. dump($sign);
  45. dump(md5(getSignKey($params, $api_user['api_key'])));
  46. $this->error('签名错误');
  47. }
  48. $check_order = (new Orders())
  49. ->where('api_id', $params['apiId'])
  50. ->where('user_order_no', $params['orderNo'])
  51. ->count();
  52. if($check_order){
  53. $this->error('该订单号已存在,请勿重复提交');
  54. }
  55. // 启动事务
  56. Db::startTrans();
  57. try {
  58. // 生成订单
  59. $order_id = (new Orders())
  60. ->insertGetId([
  61. 'api_id' => $params['apiId'],
  62. 'order_no' => $params['apiId'] . '-' . $params['orderNo'],
  63. 'user_order_no' => $params['orderNo'],
  64. 'to_address' => $params['toAddress'],
  65. 'amount' => $params['amount'],
  66. 'coin_type' => 'BEP20',
  67. 'coin_name' => 'usdt',
  68. 'create_time' => date('Y-m-d H:i:s')
  69. ]);
  70. $rs_id = (new OrdersRequestLog())
  71. ->save([
  72. 'order_id' => $order_id,
  73. 'ip' => $_SERVER['REMOTE_ADDR'],
  74. 'url' => $this->request->host(),
  75. 'params' => json_encode($params),
  76. ]);
  77. // 提交事务
  78. Db::commit();
  79. } catch (Exception $e) {
  80. // 回滚事务
  81. Db::rollback();
  82. $this->error('提交失败:' . $e->getMessage());
  83. }
  84. $this->success('提交成功');
  85. }
  86. public function getTxhashDetail(){
  87. $tx_hash = Request::post('tx_hash');
  88. $api_url = 'http://127.0.0.1:3389/api/txDetail';
  89. if(empty($tx_hash)){
  90. $this->error('参数有误');
  91. }
  92. $data = [
  93. 'tx_hash' => $tx_hash,
  94. ];
  95. $body = $this->doCurlPostRequest($api_url, $data);
  96. //dump($body);
  97. $info = json_decode($body, true);
  98. //dump($body);
  99. //$body = $this->getInfoByTransactionHash($txhash);
  100. if($info['code'] == 200){
  101. $this->success('', $info['data']);
  102. }
  103. $this->error($info['message']);
  104. }
  105. public function createAddress(){
  106. $tx_hash = Request::post('tx_hash');
  107. $api_url = 'http://127.0.0.1:3389/api/txDetail';
  108. if(empty($tx_hash)){
  109. $this->error('参数有误');
  110. }
  111. $data = [
  112. 'api_id' => 1,
  113. 'coin_type' => 'BEP20',
  114. 'user_no' => 1,
  115. 'time_stamp' => 1122,
  116. 'reset' => true
  117. ];
  118. $body = $this->doCurlPostRequest($api_url, $data);
  119. //dump($body);
  120. $info = json_decode($body, true);
  121. //dump($body);
  122. //$body = $this->getInfoByTransactionHash($txhash);
  123. if($info['code'] == 200){
  124. $this->success('', $info['data']);
  125. }
  126. $this->error($info['message']);
  127. }
  128. function doCurlPostRequest($url = '',Array $data = array())
  129. {
  130. $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
  131. // $data_string = $data;
  132. $curl_con = curl_init();
  133. curl_setopt($curl_con, CURLOPT_URL,$url);
  134. curl_setopt($curl_con, CURLOPT_HEADER, false);
  135. curl_setopt($curl_con, CURLOPT_POST, true);
  136. curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE);
  137. curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
  138. curl_setopt($curl_con, CURLOPT_HTTPHEADER, array(
  139. 'Content-Type: application/json',
  140. 'Content-Length: ' . strlen($data_string))
  141. );
  142. curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string);
  143. $res = curl_exec($curl_con);
  144. $status = curl_getinfo($curl_con);
  145. curl_close($curl_con);
  146. if (isset($status['http_code']) && $status['http_code'] == 200) {
  147. return $res;
  148. } else {
  149. return FALSE;
  150. }
  151. }
  152. }