Pay.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use blockchain\TronService;
  5. use blockchain\Web3Service;
  6. use fast\Random;
  7. use GuzzleHttp\Exception\RequestException;
  8. use think\Exception;
  9. use think\Validate;
  10. use think\Db;
  11. use app\common\model\MoneyLog;
  12. use Tron\Address;
  13. /**
  14. * 会员接口
  15. */
  16. class Pay extends Api
  17. {
  18. protected $noNeedLogin = ['notify', 'notifycz', 'notifydsf'];
  19. protected $noNeedRight = '*';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->Usermodel = model('User');
  24. }
  25. public function index()
  26. {
  27. // // 启动事务
  28. // Db::startTrans();
  29. // try {
  30. if (input('id', 0) > 0) {
  31. $id = input('id');
  32. $data = Db::name('task')->where('id', $id)->find();
  33. if ($data) {
  34. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  35. Db::name('task')->where('id', $id)->update(['out_trade_no' => $number, 'buytype' => input('buytype')]);
  36. $data = Db::name('task')->where('id', $id)->find();
  37. } else {
  38. $this->error(__('订单数据不对'));
  39. }
  40. } else {
  41. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  42. if (!input('price')) {
  43. $this->error(__('金额不对'));
  44. }
  45. $datapost = input('param.');//订单数据
  46. $datapost['buzs'] = htmlspecialchars_decode($datapost['buzs']);
  47. if (input('endtime') == '数量完成自动结束') {
  48. $endtimesjc = 100000000000;
  49. } else {
  50. $endtimesjc = strtotime(input('endtime'));
  51. }
  52. $up = [
  53. 'type' => input('type'),
  54. 'uid' => input('uid'),
  55. 'name' => input('name'),
  56. 'rwurl' => input('rwurl'),
  57. 'buz' => $datapost['buzs'],
  58. 'price' => input('price'),
  59. 'sum' => input('sum'),
  60. 'sumsy' => input('sum'),
  61. 'endtime' => input('endtime'),
  62. 'subtime' => input('subtime'),
  63. 'shetime' => input('shetime'),
  64. 'endtimesjc' => $endtimesjc,
  65. 'buytype' => input('buytype'),
  66. 'zprice' => input('price') * input('sum'),
  67. 'out_trade_no' => $number,
  68. 'createtime' => time(),
  69. ];
  70. $id = Db::name('task')->insertGetId($up);
  71. $data = $up;
  72. }
  73. $site = config('site');
  74. $amount = $data['zprice'];
  75. if (!$amount) {
  76. $this->error(__('金额不对'));
  77. }
  78. // 异步通知地址
  79. $notify_url = $site['zfb']['notifyurl'];
  80. // 订单标题
  81. $subject = 'wool订单';
  82. // 订单详情
  83. $body = 'wool致力于打造最好的移动服务平台';
  84. // 订单号,示例代码使用时间值作为唯一的订单ID号
  85. if ($data['buytype'] == '支付宝') {
  86. $this->allpay($amount, $subject, $body, $number, $notify_url);
  87. } else if ($data['buytype'] == '余额') {
  88. $this->yuepay($amount, input('uid'), $id);
  89. }
  90. // // 提交事务
  91. // Db::commit();
  92. // } catch (\Exception $e) {
  93. // // 回滚事务
  94. // var_dump($e);
  95. // Db::rollback();
  96. // }
  97. }
  98. public function videopay()
  99. {
  100. $datavideo_order = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 2])->find();
  101. $datavideo_order1 = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 1])->find();
  102. $sad = 1;
  103. $lx = input('lx');
  104. if ($datavideo_order1) {
  105. if ($datavideo_order1['type'] == 2) {
  106. $sad = 2;
  107. $this->error(__('已经全集购买'));
  108. }
  109. }
  110. if ($datavideo_order and $lx == 2) {
  111. if ($datavideo_order['type'] == 2) {
  112. $this->error(__('已经单集购买'));
  113. }
  114. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  115. $price = input('priceq');
  116. $pricebuy = input('pricebuy');
  117. if (input('lx') == 2) {
  118. $price = input('priced');
  119. $pricebuy = input('pricebuy');
  120. }
  121. $up = [
  122. 'type' => 1,
  123. 'lx' => $lx,//1 全章购买 2单集购买
  124. 'uid' => input('uid'),
  125. 'name' => input('namet'),
  126. 'img' => input('img'),
  127. 'price' => $price,
  128. 'sum' => input('sum'),
  129. 'sid' => input('pid'),
  130. 'smid' => input('smid'),
  131. 'paytype' => input('buytype'),
  132. 'zprice' => $pricebuy * input('sum'),
  133. 'out_trade_no' => $number,
  134. ];
  135. Db::name('video_order')->where(['id' => $datavideo_order['id']])->update($up);
  136. $id = $datavideo_order['id'];
  137. $data = $up;
  138. } else {
  139. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  140. if (!input('pricebuy')) {
  141. $this->error(__('金额不对'));
  142. }
  143. $price = input('priceq');
  144. $pricebuy = input('pricebuy');
  145. if (input('lx') == 2) {
  146. $price = input('priced');
  147. $pricebuy = input('pricebuy');
  148. }
  149. $up = [
  150. 'type' => 1,
  151. 'lx' => $lx,//1 全章购买 2单集购买
  152. 'uid' => input('uid'),
  153. 'name' => input('namet'),
  154. 'img' => input('img'),
  155. 'price' => $price,
  156. 'sum' => input('sum'),
  157. 'sid' => input('pid'),
  158. 'smid' => input('smid'),
  159. 'paytype' => input('buytype'),
  160. 'zprice' => $pricebuy * input('sum'),
  161. 'out_trade_no' => $number,
  162. 'createtime' => time(),
  163. 'updatetime' => time(),
  164. ];
  165. //$this->error($up);
  166. $id = Db::name('video_order')->insertGetId($up);
  167. $data = $up;
  168. }
  169. $site = config('site');
  170. $amount = $data['zprice'];
  171. if (!$amount) {
  172. $this->error(__('金额不对'));
  173. }
  174. // 异步通知地址
  175. $notify_url = $site['zfb']['notifyurl'];
  176. // 订单标题
  177. $subject = 'wool订单';
  178. // 订单详情
  179. $body = 'wool致力于打造最好的移动服务平台';
  180. // 订单号,示例代码使用时间值作为唯一的订单ID号
  181. //var_dump($data['paytype']);
  182. if ($data['paytype'] == '支付宝') {
  183. $this->allpay($amount, $subject, $body, $number, $notify_url);
  184. } else if ($data['paytype'] == '余额') {
  185. $this->videoyuepay($amount, input('uid'), $id);
  186. } else if ($data['paytype'] == '微信') {
  187. $wxlx = input('wxlx');
  188. if ($wxlx == 'wxxcx') {
  189. $openid = input('openid');
  190. if (!$openid) {
  191. $this->error(__('openid不对'));
  192. }
  193. $this->paywxxcx($id, $number, $amount, $openid);
  194. } else if ($wxlx == 'wxh5') {
  195. $buytype = '微信h5';
  196. $this->paywxh5($id, $number, $amount, $buytype);
  197. } else if ($wxlx == 'wxgzh') {
  198. $buytype = '微信公众号';
  199. $this->paywxh5($id, $number, $amount, $buytype);
  200. }
  201. } else if ($data['paytype'] == '汇聚') {
  202. $paid_price = 0;
  203. // if ($recharId) {
  204. // $data = SystemGroupData::getDateValue($recharId);
  205. // if ($data === false) {
  206. // return app('json')->fail('您选择的充值方式已下架!');
  207. // } else {
  208. // $paid_price = $data['give_money'] ?? 0;
  209. // }
  210. // }
  211. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  212. $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'video_pay');
  213. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  214. }
  215. }
  216. public function paywxh5($oid, $number, $amount, $buytype)
  217. {
  218. $site = config('site');
  219. $user = $this->auth->getUser();
  220. if (!$user) {
  221. $this->error(__('user不对'));
  222. }
  223. if ($amount <= 0) {
  224. $this->error(__('金额不对'));
  225. }
  226. $up = [
  227. 'type' => 1,
  228. 'uid' => $user['id'],
  229. 'tdnum' => input('tdnum'),//支付通道
  230. 'amount' => $amount,
  231. 'out_trade_no' => $number,
  232. 'createtime' => time(),
  233. 'buytype' => $buytype,
  234. 'oid' => $oid,
  235. ];
  236. $id = Db::name('paylog')->insertGetId($up);
  237. $urls = $site['imgurl'] . '/index/paydsf/wxh5?out_trade_no=' . $number;
  238. if ($buytype == '微信公众号') {
  239. $urls = $site['imgurl'] . '/index/index/pay?id=' . $id;
  240. }
  241. $this->success('订单提交成功 正在跳转支付', $urls);
  242. }
  243. public function paywxxcx($oid, $number, $amount, $openid)
  244. {
  245. $site = config('site');
  246. $user = $this->auth->getUser();
  247. if (!$user) {
  248. $this->error(__('user不对'));
  249. }
  250. if ($amount <= 0) {
  251. $this->error(__('金额不对'));
  252. }
  253. $up = [
  254. 'type' => 1,
  255. 'uid' => $user['id'],
  256. 'amount' => $amount,
  257. 'out_trade_no' => $number,
  258. 'createtime' => time(),
  259. 'buytype' => '微信小程序',
  260. 'oid' => $oid,
  261. ];
  262. $id = Db::name('paylog')->insertGetId($up);
  263. $modelxcx = new \app\api\controller\Paywx;
  264. $trs = $modelxcx->wxxcx($number, $amount, $openid);
  265. $this->success('提交成功', $trs);
  266. }
  267. public function videoyuepay($money, $user_id, $id)//余额支付
  268. {
  269. $user = $this->Usermodel::get($user_id);
  270. if ($user && $money != 0) {
  271. $data = Db::name('video_order')->where('id', $id)->find();
  272. if ($user->money < $money) {
  273. $this->error(__('余额不足'));
  274. }
  275. $before = $user->money;
  276. $after = $user->money - $money;
  277. if ($data['type'] == 1) {
  278. Db::startTrans();
  279. try {
  280. //更新会员信息
  281. $user->save(['money' => $after]);
  282. //写入日志
  283. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  284. Db::name('video_order')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'ftime' => time()]);
  285. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  286. $this->dailiyongjin($id, $data['sid'], $data['smid'], $user_id, $money, '佣金结算');//后台代理佣金结算
  287. Db::commit();
  288. $this->success(__('操作成功'));
  289. } catch (Exception $e) {
  290. Db::rollback();
  291. $this->error($e->getMessage());
  292. }
  293. } else if ($data['type'] > 1) {
  294. $this->error(__('已经支付'));
  295. }
  296. } else {
  297. $this->error(__('金额不对'));
  298. }
  299. }
  300. public function yuepay($money, $user_id, $id)//余额支付
  301. {
  302. $user = $this->Usermodel::get($user_id);
  303. if ($user && $money != 0) {
  304. $data = Db::name('task')->where('id', $id)->find();
  305. if ($user->money < $money) {
  306. $this->error(__('余额不足'));
  307. }
  308. $before = $user->money;
  309. $after = $user->money - $money;
  310. //更新会员信息
  311. $user->save(['money' => $after]);
  312. //写入日志
  313. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  314. if ($data['paytype'] == 1) {
  315. $up = [
  316. 'paytype' => 2,
  317. 'buytype' => '余额',
  318. 'ftime' => time()
  319. ];
  320. if (Db::name('task')->where('id', $id)->update($up)) {
  321. $this->success(__('操作成功'));
  322. }
  323. } else if ($data['paytype'] > 1) {
  324. $this->error(__('已经支付'));
  325. }
  326. } else {
  327. $this->error(__('金额不对'));
  328. }
  329. }
  330. public function czpay()
  331. {
  332. $site = config('site');
  333. $user = $this->auth->getUser();
  334. if (!$user) {
  335. $this->error(__('user不对'));
  336. }
  337. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  338. $amount = input('total', 0);
  339. $notify_url = $site['zfb']['notifyurlcz'];
  340. // 订单标题
  341. $subject = 'wool充值';
  342. // 订单详情
  343. $body = 'wool致力于打造最好的移动服务平台';
  344. if ($amount <= 0) {
  345. $this->error(__('金额不对'));
  346. }
  347. $up = [
  348. 'type' => 1,
  349. 'uid' => $user['id'],
  350. 'amount' => input('total'),
  351. 'out_trade_no' => $number,
  352. 'createtime' => time(),
  353. ];
  354. $id = Db::name('paylog')->insertGetId($up);
  355. if (input('type', 'allpay') == 'iap') {
  356. $this->success('请求成功', $number);
  357. } else {
  358. $this->allpay($amount, $subject, $body, $number, $notify_url);
  359. }
  360. }
  361. public function dsfczpay()
  362. {
  363. $site = config('site');
  364. $user = $this->auth->getUser();
  365. if (!$user) {
  366. $this->error(__('user不对'));
  367. }
  368. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  369. $amount = input('total', 0);
  370. if ($amount <= 0) {
  371. $this->error(__('金额不对'));
  372. }
  373. $up = [
  374. 'type' => 1,
  375. 'uid' => $user['id'],
  376. 'tdnum' => input('tdnum'),
  377. 'amount' => $amount,
  378. 'out_trade_no' => $number,
  379. 'createtime' => time(),
  380. 'buytype' => isset($site['dsfzf']['zfname']) ? $site['dsfzf']['zfname'] : '',
  381. ];
  382. $id = Db::name('paylog')->insertGetId($up);
  383. $urls = $site['imgurl'] . '/index/paydsf?out_trade_no=' . $number;
  384. $this->success('订单提交成功 正在跳转支付', $urls);
  385. }
  386. public function czpayiaptz()
  387. {
  388. $out_trade_no = input('out_trade_no');
  389. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  390. //$this->dingding_log($order);
  391. if ($order['type'] == 1) {
  392. $up = [
  393. 'type' => 2,
  394. 'payment' => null,
  395. 'trade_no' => null,
  396. 'buyer' => null,
  397. 'buytype' => '苹果',
  398. 'ftime' => time()
  399. ];
  400. Db::startTrans();
  401. try {
  402. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  403. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  404. Db::commit();
  405. $this->success('支付成功');
  406. } catch (Exception $e) {
  407. Db::rollback();
  408. $this->error($e->getMessage());
  409. }
  410. } else {
  411. $this->success('已经支付');
  412. }
  413. }
  414. public function allpay($amount, $subject, $body, $out_trade_no, $notify_url)//支付宝支付
  415. {
  416. $total = floatval($amount);
  417. if (!$total) {
  418. $this->error(__('金额不对'));
  419. }
  420. //require_once('/vendor/alipayrsa2/AopSdk.php');
  421. //Vendor('AopSdk.alipayrsa2');
  422. // require_once './vendor/alipayrsa2/aop/AopClient.php';
  423. // require_once './vendor/alipayrsa2/aop/request/AlipayTradeAppPayRequest.php';
  424. vendor('alipayrsa2.AopSdk');
  425. $site = config('site');
  426. $aop = new \AopClient;
  427. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  428. $aop->appId = $site['zfb']['appId'];
  429. //开发者私钥去头去尾去回车,一行字符串
  430. $aop->rsaPrivateKey = $site['zfb']['rsaPrivateKey'];
  431. $aop->format = "json";
  432. $aop->charset = "UTF-8";
  433. $aop->signType = "RSA2";
  434. //请填写支付宝公钥,一行字符串
  435. $aop->alipayrsaPublicKey = $site['zfb']['alipayrsaPublicKey'];
  436. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  437. $request = new \AlipayTradeAppPayRequest();
  438. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  439. $bizcontent = "{\"body\":\"" . $body . "\","
  440. . "\"subject\": \"" . $subject . "\","
  441. . "\"out_trade_no\": \"" . $out_trade_no . "\","
  442. . "\"timeout_express\": \"30m\","
  443. . "\"total_amount\": \"" . $total . "\","
  444. . "\"product_code\":\"QUICK_MSECURITY_PAY\""
  445. . "}";
  446. $request->setNotifyUrl($notify_url);
  447. $request->setBizContent($bizcontent);
  448. //这里和普通的接口调用不同,使用的是sdkExecute
  449. $response = $aop->sdkExecute($request);
  450. // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
  451. //$this->success(__('获取成功'),$response);
  452. echo $response;
  453. }
  454. //支付宝回调
  455. public function notify()
  456. {
  457. if ($_POST) {
  458. //Db::transaction(function () {
  459. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  460. $out_trade_no = $_POST['out_trade_no'];//交易号
  461. $trade_no = $_POST['trade_no'];//订单支付时间
  462. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  463. $order = Db::name('task')->where('out_trade_no', $out_trade_no)->find();
  464. if ($order['paytype'] == 1) {
  465. $up = [
  466. 'paytype' => 2,
  467. 'payment' => $gmt_payment,
  468. 'trade_no' => $trade_no,
  469. 'buyer' => $_POST['buyer_logon_id'],
  470. 'buytype' => '支付宝',
  471. 'ftime' => time()
  472. ];
  473. if (Db::name('task')->where('out_trade_no', $out_trade_no)->update($up)) {
  474. echo 'success';
  475. }
  476. } else {
  477. echo 'success';
  478. }
  479. }
  480. //});
  481. }
  482. }
  483. public function notifycz()
  484. {
  485. if ($_POST) {
  486. //$this->dingding_log($_POST);
  487. //Db::transaction(function () {
  488. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  489. $out_trade_no = $_POST['out_trade_no'];//交易号
  490. $trade_no = $_POST['trade_no'];//订单支付时间
  491. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  492. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  493. //$this->dingding_log($order);
  494. if ($order['type'] == 1) {
  495. $up = [
  496. 'type' => 2,
  497. 'payment' => $gmt_payment,
  498. 'trade_no' => $trade_no,
  499. 'buyer' => $_POST['buyer_logon_id'],
  500. 'buytype' => '支付宝',
  501. 'ftime' => time()
  502. ];
  503. Db::startTrans();
  504. try {
  505. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  506. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  507. Db::commit();
  508. echo 'success';
  509. } catch (Exception $e) {
  510. Db::rollback();
  511. $this->error($e->getMessage());
  512. }
  513. } else {
  514. echo 'success';
  515. }
  516. }
  517. //});
  518. }
  519. }
  520. public function notifydsf()//第三方充值回调
  521. {
  522. $this->dingding_log($_REQUEST);
  523. //Db::transaction(function () {
  524. $returncode = isset($_REQUEST["returncode"]) ? $_REQUEST["returncode"] : 1;
  525. if ($returncode == "00") {//处理交易完成或者支付成功的通知 //获取订单号
  526. $out_trade_no = isset($_REQUEST["orderid"]) ? $_REQUEST["orderid"] : '';
  527. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  528. $this->dingding_log($order);
  529. if ($order['type'] == 1) {
  530. $up = [
  531. 'type' => 2,
  532. 'ftime' => time()
  533. ];
  534. Db::startTrans();
  535. try {
  536. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  537. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  538. Db::commit();
  539. echo 'OK';
  540. } catch (Exception $e) {
  541. Db::rollback();
  542. $this->error($e->getMessage());
  543. }
  544. } else {
  545. echo 'OK';
  546. }
  547. }
  548. //});
  549. }
  550. public function money($money, $user_id, $id, $sxf = 0)
  551. {
  552. $user = $this->Usermodel::get($user_id);
  553. if ($user && $money != 0) {
  554. $before = $user->money;
  555. $after = $user->money + $money;
  556. //更新会员信息
  557. $user->save(['money' => $after]);
  558. //写入日志
  559. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => $id, 'sxf' => $sxf, 'memo' => '用户充值']);
  560. } else {
  561. $this->error(__('金额不对'));
  562. }
  563. }
  564. public function dingding_log($content)
  565. {
  566. $r = $_SERVER['DOCUMENT_ROOT'] . '/api_log/' . date('Y-m-d_H-i-s', time()) . '.txt';
  567. $fp = fopen($r, 'w+');
  568. fwrite($fp, "执行日期:" . date('Y-m-d H:i:s', time()) . ' ' . var_export($content, true));
  569. fclose($fp);
  570. }
  571. public function FromXml($xml)
  572. {
  573. //将XML转为array
  574. //禁止引用外部xml实体
  575. libxml_disable_entity_loader(true);
  576. $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  577. return $values;
  578. }
  579. public function sharepay()
  580. {
  581. // $datavideo_order=Db::name('video_share')->where(['vid'=>input('id'),'uid'=>input('uid')])->find();
  582. // $datavideo_order1=Db::name('video_share')->where(['sid'=>input('id'),'uid'=>input('uid'),'lx'=>1])->find();
  583. // $lx=input('lx');
  584. $num = input('num'); //购买的版权数量
  585. $uid = input('uid');
  586. $vid = input('vid');
  587. $user = Db::name('user')->where('id', $uid)->find();
  588. if (empty($user)){
  589. $this->error(__('用户不存在'));
  590. }
  591. if (empty($num)){
  592. $this->error(__('请输入数量'));
  593. }
  594. // 验证数量
  595. $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
  596. if ($user_num) {
  597. $cs_num = $user_num['share'] + $num;
  598. if ($cs_num > 10 || $num > 10) {
  599. $this->error(__('单剧集购买版权不能超过10份'));
  600. }
  601. }
  602. $user_share = Db::name('videolist')->where('id', $vid)->value('share');
  603. if (empty($user_share)){
  604. $this->error(__('短剧不存在'));
  605. }
  606. $user_shares = bcsub($user_share , $num, 0);
  607. if ($user_shares < 0) {
  608. $this->error(__('该局总版权只剩下' . $user_share . '份'));
  609. }
  610. // if($datavideo_order){
  611. // if($datavideo_order['type']==2){
  612. // $this->error(__('该订单已支付'));
  613. // }
  614. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  615. // $price=input('priceq');
  616. // $pricebuy=input('pricebuy');
  617. // if(input('lx')==2){
  618. // $price=input('priced');
  619. // $pricebuy=input('pricebuy');
  620. // }
  621. // $up=[
  622. // 'type'=>1,
  623. // 'lx'=>$lx,//1 全章购买 2单集购买
  624. // 'uid'=>input('uid'),
  625. // 'name'=>input('namet'),
  626. // 'img'=>input('img'),
  627. // 'price'=>$price,
  628. // 'sum'=>input('sum'),
  629. // 'sid'=>input('pid'),
  630. // 'smid'=>input('smid'),
  631. // 'paytype'=>input('buytype'),
  632. // 'zprice'=>$pricebuy*input('sum'),
  633. // 'out_trade_no'=>$number,
  634. // ];
  635. // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
  636. // $id=$datavideo_order['id'];
  637. // $data=$up;
  638. // }else{
  639. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  640. // if (!input('pricebuy')) {
  641. // $this->error(__('金额不对'));
  642. // }
  643. // $price = input('price');
  644. $price = Db::name('videolist')->where('id', input('vid'))->value('sharesprice');;
  645. // $pricebuy = input('pricebuy');
  646. // if(input('lx')==2){
  647. // $price=input('priced');
  648. // $pricebuy=input('pricebuy');
  649. // }
  650. // 获取当前时间的时间戳
  651. $now = time();
  652. // 计算三年后的时间戳
  653. $threeYearsLater = strtotime('+3 years', $now);
  654. $vname = Db::name('videolist')->where('id', input('vid'))->value('name');
  655. $total_price = $price * $num;
  656. $up = [
  657. 'type' => 1,
  658. 'uid' => input('uid'),
  659. 'vid' => input('vid'),
  660. 'vname' => $vname,
  661. 'num' => input('num'),
  662. 'price' => $price,
  663. 'total_price' => $total_price,
  664. 'paytype' => input('buytype'),
  665. 'out_trade_no' => $number,
  666. 'createtime' => time(),
  667. 'endtime' => $threeYearsLater,
  668. 'updatetime' => time(),
  669. ];
  670. //$this->error($up);
  671. $id = Db::name('video_share')->insertGetId($up);
  672. $data = $up;
  673. // }
  674. $site = config('site');
  675. $amount = $price;
  676. // $amount = $data['zprice'];
  677. // if (!$amount) {
  678. // $this->error(__('金额不对'));
  679. // }
  680. // 异步通知地址
  681. $notify_url = $site['zfb']['notifyurl'];
  682. // 订单标题
  683. $subject = 'wool订单';
  684. // 订单详情
  685. $body = 'wool致力于打造最好的移动服务平台';
  686. // 订单号,示例代码使用时间值作为唯一的订单ID号
  687. //var_dump($data['paytype']);
  688. if ($data['paytype'] == '支付宝') {
  689. $this->allpay($amount, $subject, $body, $number, $notify_url);
  690. } else if ($data['paytype'] == '余额') {
  691. $this->shareyuepay($amount, input('uid'), $id);
  692. } else if ($data['paytype'] == '微信') {
  693. $wxlx = input('wxlx');
  694. if ($wxlx == 'wxxcx') {
  695. $openid = input('openid');
  696. if (!$openid) {
  697. $this->error(__('openid不对'));
  698. }
  699. $this->paywxxcx($id, $number, $amount, $openid);
  700. } else if ($wxlx == 'wxh5') {
  701. $buytype = '微信h5';
  702. $this->paywxh5($id, $number, $amount, $buytype);
  703. } else if ($wxlx == 'wxgzh') {
  704. $buytype = '微信公众号';
  705. $this->paywxh5($id, $number, $amount, $buytype);
  706. } else if ($data['paytype'] == '汇聚') {
  707. $paid_price = 0;
  708. // if ($recharId) {
  709. // $data = SystemGroupData::getDateValue($recharId);
  710. // if ($data === false) {
  711. // return app('json')->fail('您选择的充值方式已下架!');
  712. // } else {
  713. // $paid_price = $data['give_money'] ?? 0;
  714. // }
  715. // }
  716. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  717. $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'share_pay');
  718. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  719. }
  720. }
  721. }
  722. // 判断订单的用户是否有上级以及判断推荐等级
  723. public function grade_sell($out_trade_no)
  724. {
  725. $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
  726. $site = config('site');
  727. if ($order) {
  728. $user = Db::name('user')->where('id', $order['uid'])->find();
  729. while ($user['pid'] != 1) { //默认账号pid为1
  730. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  731. $grade = $user['grade'];
  732. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  733. if (empty($video_user)) { //如果上级没有购买过该视频版权
  734. $up = [
  735. 'vid' => $order['vid'],
  736. 'vname' => $order['vname'],
  737. 'uid' => $user['id'],
  738. 'share' => 0,
  739. 'contribution' => 0,
  740. 'income' => 0,
  741. 'createtime' => time(),
  742. ];
  743. $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
  744. }
  745. $contribution = 0;
  746. $check = 0;
  747. // $sell = $video_user['sell'] + $order['num'];
  748. $sell = $user['sell'] + $order['num'];
  749. // switch ($video_user['grade']) {
  750. switch ($user['grade']) {
  751. case 0:
  752. $contribution = $site['gxz']['普通会员'] * $order['num'];
  753. if ($sell >= 30) {
  754. $check = 1;
  755. $sell = $sell - 30;
  756. }
  757. break;
  758. case 1:
  759. $contribution = $site['gxz']['初级合伙人'] * $order['num'];
  760. if ($sell >= 300) {
  761. $check = 1;
  762. $sell = $sell - 300;
  763. }
  764. break;
  765. case 2:
  766. $contribution = $site['gxz']['高级合伙人'] * $order['num'];
  767. if ($sell >= 3000) {
  768. $check = 1;
  769. $sell = $sell - 3000;
  770. }
  771. break;
  772. case 3:
  773. $contribution = $site['gxz']['董事'] * $order['num'];
  774. default:
  775. }
  776. if ($check == 1) {
  777. // $grade = $video_user['grade'] + 1;
  778. $grade = $user['grade'] + 1;
  779. }
  780. $after = $video_user['contribution'] + $contribution;
  781. // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  782. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
  783. $res2 = Db::name('user')->where('id', $user['id'])->update(['grade' => $grade, 'sell' => $sell]);
  784. $up = [
  785. 'vid' => $order['vid'],
  786. 'vname' => $order['vname'],
  787. 'uid' => $user['id'],
  788. 'link_id' => $order['id'],
  789. 'num' => $contribution,
  790. 'after' => $contribution,
  791. 'content' => '下级用户购买版权,获得贡献值',
  792. 'createtime' => time(),
  793. 'pm' => 0,
  794. ];
  795. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  796. }
  797. }
  798. }
  799. // 添加贡献值记录 订单id
  800. public function share_record($id)
  801. {
  802. $order = Db::name('video_share')->where('id', $id)->find();
  803. if ($order) {
  804. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  805. $contribution = $order['num'] + $user['contribution'];
  806. $up = [
  807. 'vid' => $order['vid'],
  808. 'vname' => $order['vname'],
  809. 'uid' => $user['id'],
  810. 'link_id' => $order['id'],
  811. 'num' => $order['num'],
  812. 'after' => $contribution,
  813. 'content' => '用户购买版权,获得贡献值',
  814. 'createtime' => time(),
  815. 'pm' => 0,
  816. ];
  817. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  818. $share = $user['share'] + $order['num'];
  819. // 添加版权和贡献值
  820. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
  821. }
  822. }
  823. public function shareyuepay($money, $user_id, $id)//版权余额支付
  824. {
  825. $user = $this->Usermodel::get($user_id);
  826. if ($user) {
  827. $data = Db::name('video_share')->where('id', $id)->find();
  828. if ($user->money < $money) {
  829. $this->error(__('余额不足'));
  830. }
  831. $before = $user->money;
  832. $after = $user->money - $money;
  833. if ($data['type'] == 1) {
  834. Db::startTrans();
  835. try {
  836. //更新会员信息
  837. $user->save(['money' => $after]);
  838. //写入日志
  839. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  840. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  841. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  842. if (empty($video_user)) { //判断用户是否购买过该视频版权
  843. $up = [
  844. 'vid' => $data['vid'],
  845. 'vname' => $data['vname'],
  846. 'uid' => $data['uid'],
  847. 'share' => $data['num'],
  848. 'contribution' => $data['num'],
  849. 'income' => 0,
  850. 'createtime' => time(),
  851. ];
  852. $id = Db::name('VideoUser')->insertGetId($up);
  853. }
  854. $this->share_record($id);//添加贡献值记录
  855. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  856. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  857. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  858. // 扣除短剧总版权
  859. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  860. $share = $shares - $data['num'];
  861. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  862. Db::commit();
  863. $this->success(__('操作成功'));
  864. } catch (Exception $e) {
  865. Db::rollback();
  866. $this->error($e->getMessage());
  867. }
  868. } else if ($data['type'] > 1) {
  869. $this->error(__('已经支付'));
  870. }
  871. } else {
  872. $this->error(__('用户不存在'));
  873. }
  874. }
  875. // u币充值(展示二维码)
  876. public function recharge()
  877. {
  878. $uid = input('uid');
  879. $recharge_type = input('type');
  880. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  881. $user = Db::name('user')->where('id', $uid)->find();
  882. if (empty($user)) {
  883. $this->error(__('用户不存在'));
  884. }
  885. // $data = [
  886. // 'order_id'=> $number,
  887. // 'uid' => $uid,
  888. // 'paid' => 0,
  889. // 'add_time' => time()
  890. // ];
  891. $return = [];
  892. $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
  893. if ($recharge_type == 'trx') {
  894. // $data['recharge_type']='trx';
  895. $return['address'] = $address['trx_address'];
  896. $return['16_address'] = $address['trx_16_address'];
  897. } elseif ($recharge_type == 'bsc') {
  898. // $data['recharge_type']='bsc';
  899. $return['address'] = $address['bsc_address'];
  900. }
  901. // Db::name('user_recharge')->insert($data);
  902. Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
  903. $this->success($return);
  904. }
  905. // 查询u币转账记录
  906. public function search_recharge()
  907. {
  908. $time = strtotime("-1 hours", time());
  909. $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
  910. foreach ($user_address as $k => $v) {
  911. $user_info = Db::name('user')->where('id', $v['uid'])->find();
  912. // trx部分
  913. $service = TronService::instance('usdt');
  914. $time =bcmul($v['last_time'], 1000, 0);
  915. $info = $service->getTransfer($v['trx_address'], $time);
  916. @file_put_contents("usdt.txt",json_encode($info). "-trx转账记录\r\n", 8);
  917. foreach ($info['data'] as $k2 => $v2){
  918. $transaction_id = $v2['transaction_id'];
  919. $value =bcdiv($v2['value'], 1000000, 18);
  920. // $value3 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  921. // @file_put_contents("usdt.txt",$value3. "-另一种转换方式\r\n", 8);
  922. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  923. if ($be) { //hash重复
  924. continue;
  925. }
  926. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  927. $data = [
  928. 'order_id' => $number,
  929. 'uid' => $v['uid'],
  930. 'paid' => 1,
  931. 'price' => $value,
  932. 'hash' => $transaction_id,
  933. 'recharge_type' => 'trx',
  934. 'pay_time' => time(),
  935. 'add_time' => time()
  936. ];
  937. Db::name('user_recharge')->insert($data);
  938. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  939. $money = bcadd($user_info['money'], $value, 18);
  940. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  941. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  942. }
  943. // bsc部分
  944. $service = Web3Service::instance('bsc', 'usdt', '');
  945. $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
  946. @file_put_contents("usdt.txt",json_encode($info). "-bsc转账记录\r\n", 8);
  947. foreach ($info as $k2 => $v2) {
  948. $value = $v2->params['value']->toString(); //金额
  949. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  950. $transactionHash = $v2->transactionHash;
  951. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  952. if ($be) { //hash重复
  953. continue;
  954. }
  955. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  956. $data = [
  957. 'order_id' => $number,
  958. 'uid' => $v['uid'],
  959. 'paid' => 1,
  960. 'price' => $value2,
  961. 'hash' => $transactionHash,
  962. 'recharge_type' => 'bsc',
  963. 'pay_time' => time(),
  964. 'add_time' => time()
  965. ];
  966. Db::name('user_recharge')->insert($data);
  967. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  968. $money = bcadd($user_info['money'], $value2, 18);
  969. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  970. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  971. }
  972. }
  973. $this->success('查询完成');
  974. }
  975. public function withdrawal(){
  976. $uid = input('uid');
  977. $address = input('address');
  978. $type = input('type');
  979. $img = input('img');
  980. $price = input('price');
  981. if (empty($address)&&empty($img)){
  982. $this->error(__('请上传地址或上传收款码'));
  983. }
  984. Db::name('txjl')->insert(['uid' => $uid, 'address' => $address, 'type' => $type, 'img' => $img,'money'=> $price,'iscl'=>1]);
  985. $this->success('提交成功');
  986. }
  987. public function test()
  988. {
  989. // $a= self::base58ToHex('TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu');
  990. // $b = self::base58ToHex('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX');
  991. // var_dump($a);
  992. // var_dump($b);die();
  993. $service = TronService::instance('usdt');
  994. $trxBalance = $service->balance('TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu'); //trx主币查询
  995. var_dump($trxBalance);
  996. $trcGasAddress = new Address(
  997. 'TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu',
  998. '0xed6a4d8cf092bee064b8fdd828980bdaf16700c483c5abfd6affb300c09a5423',
  999. '4199f98810123e30a27100af641ccdf03e37bca669'
  1000. );
  1001. $balance = $service->tokenBalance($trcGasAddress); //trx代币查询
  1002. var_dump($balance);
  1003. $address = new Address(
  1004. 'TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX',
  1005. '0xf322ad257024b72be5a7bf09cbb58aec0bd89ee6de8bdd9858d4bcbdc0c84427',
  1006. '413be12e20eb06a9110999b6566437f90da137bebb'
  1007. );
  1008. $balance = $service->tokenBalance($address); //trx代币查询
  1009. var_dump($balance);
  1010. $res = $service->tokenTransfer($trcGasAddress, $address, 0.0002);
  1011. var_dump($res);die();
  1012. $service = Web3Service::instance('bsc', 'usdt','ba46d817b91ee27b701e1a02d2f2540ab8641d262650b6aaafe8caf2fa7b6d15');
  1013. $balance = $service->getTokenBalance('0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'); //bsc代币余额
  1014. var_dump($balance);
  1015. $gas_balance =$service->getBalance('0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'); //bsc主币余额
  1016. var_dump($gas_balance);
  1017. $res = $service->transferToken('0x83105BDFAc1a15DF3C22D019Ac2F2937AC1Ef098', 2);
  1018. var_dump($res);
  1019. // $site = config('site');
  1020. // var_dump($site['gxz']['普通会员']);
  1021. // var_dump($site['gxz']['初级合伙人']);
  1022. // var_dump($site['gxz']['高级合伙人']);
  1023. // var_dump($site['gxz']['董事']);
  1024. die();
  1025. $service = Web3Service::instance('bsc', 'usdt', '');
  1026. $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
  1027. $user_info = Db::name('user')->where('id', 1571)->find();
  1028. foreach ($info as $k2 => $v2) {
  1029. $value = $v2->params['value']->toString(); //金额
  1030. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12);
  1031. $transactionHash = $v2->transactionHash;
  1032. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  1033. if ($be) { //hash重复
  1034. continue;
  1035. }
  1036. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1037. $data = [
  1038. 'order_id' => $number,
  1039. 'uid' => $user_info['id'],
  1040. 'paid' => 1,
  1041. 'price' => $value2,
  1042. 'hash' => $transactionHash,
  1043. 'recharge_type' => 'bsc',
  1044. 'pay_time' => time(),
  1045. 'add_time' => time()
  1046. ];
  1047. Db::name('user_recharge')->insert($data);
  1048. Db::name('user_usdt_address')->where('uid', $user_info['id'])->update(['last_time' => time()]);
  1049. $money = bcadd($user_info['money'], $value2, 18);
  1050. Db::name('user')->where('id', $user_info['id'])->update(['money' => $money]);
  1051. MoneyLog::create(['user_id' => $user_info['id'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  1052. }
  1053. // foreach ($info as $k => $v) {
  1054. // $value = $v->params['value']->toString();
  1055. // var_dump($value);
  1056. // $transactionHash = $v->transactionHash;
  1057. // var_dump($transactionHash);
  1058. // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
  1059. // var_dump($value);
  1060. // }
  1061. var_dump($info);
  1062. die();
  1063. // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
  1064. // $service = TronService::instance('usdt');
  1065. //
  1066. // $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
  1067. // $user_info = Db::name('user')->where('id', 1571)->find();
  1068. // foreach ($info['data'] as $k2 => $v2){
  1069. // $transaction_id = $v2['transaction_id'];
  1070. // $value =bcdiv($v2['value'], 1000000, 0);
  1071. // $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  1072. // if ($be) { //hash重复
  1073. // continue;
  1074. // }
  1075. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1076. // $data = [
  1077. // 'order_id' => $number,
  1078. // 'uid' => 1571,
  1079. // 'paid' => 1,
  1080. // 'price' => $value,
  1081. // 'hash' => $transaction_id,
  1082. // 'type' => 'trx',
  1083. // 'pay_time' => time(),
  1084. // 'add_time' => time()
  1085. // ];
  1086. // Db::name('user_recharge')->insert($data);
  1087. // Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
  1088. // $money = bcadd($user_info['money'], $value, 18);
  1089. // Db::name('user')->where('id', 1571)->update(['money' => $money]);
  1090. // MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  1091. //
  1092. // }
  1093. // var_dump($info);die();
  1094. }
  1095. function base58ToHex($base58) {
  1096. // TRON 专用 Base58 字符集
  1097. $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  1098. // 1. 将 Base58 转换为十进制大数
  1099. $num = '0';
  1100. for ($i = 0; $i < strlen($base58); $i++) {
  1101. $char = $base58[$i];
  1102. $pos = strpos($alphabet, $char);
  1103. if ($pos === false) {
  1104. throw new Exception("无效的 Base58 字符: $char");
  1105. }
  1106. $num = bcmul($num, '58');
  1107. $num = bcadd($num, (string)$pos);
  1108. }
  1109. // 2. 将十进制大数转换为十六进制
  1110. $hex = '';
  1111. while ($num !== '0') {
  1112. $remainder = bcmod($num, '16');
  1113. $hex = dechex((int)$remainder) . $hex;
  1114. $num = bcdiv($num, '16', 0);
  1115. }
  1116. // 3. 添加前导零(每个前导'1'对应一个00)
  1117. $leadingZeros = '';
  1118. for ($i = 0; $i < strlen($base58) && $base58[$i] === '1'; $i++) {
  1119. $leadingZeros .= '00';
  1120. }
  1121. // 4. 组合并确保长度为50字符(25字节)
  1122. $hex = $leadingZeros . $hex;
  1123. $hex = str_pad($hex, 50, '0', STR_PAD_LEFT);
  1124. // 5. 提取地址部分并添加 TRON 前缀
  1125. // 跳过版本字节(0x41)和校验码(最后4字节)
  1126. $addressPart = substr($hex, 2, 40); // 取20字节地址
  1127. return '0x41' . $addressPart; // TRON 地址以0x41开头
  1128. }
  1129. }