Pay.php 41 KB

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