Pay.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. {
  789. $order = Db::name('video_share')->where('id', $id)->find();
  790. if ($order) {
  791. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  792. $contribution = $order['num'] + $user['contribution'];
  793. $up = [
  794. 'vid' => $order['vid'],
  795. 'vname' => $order['vname'],
  796. 'uid' => $user['id'],
  797. 'link_id' => $order['id'],
  798. 'num' => $order['num'],
  799. 'after' => $contribution,
  800. 'content' => '用户购买版权,获得贡献值',
  801. 'createtime' => time(),
  802. 'pm' => 0,
  803. ];
  804. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  805. $share = $user['share'] + $order['num'];
  806. // 添加版权和贡献值
  807. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
  808. }
  809. }
  810. public function shareyuepay($money, $user_id, $id)//版权余额支付
  811. {
  812. $user = $this->Usermodel::get($user_id);
  813. if ($user && $money != 0) {
  814. $data = Db::name('video_share')->where('id', $id)->find();
  815. if ($user->money < $money) {
  816. $this->error(__('余额不足'));
  817. }
  818. $before = $user->money;
  819. $after = $user->money - $money;
  820. if ($data['type'] == 1) {
  821. Db::startTrans();
  822. try {
  823. //更新会员信息
  824. $user->save(['money' => $after]);
  825. //写入日志
  826. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  827. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  828. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  829. if (empty($video_user)) { //判断用户是否购买过该视频版权
  830. $up = [
  831. 'vid' => $data['vid'],
  832. 'vname' => $data['vname'],
  833. 'uid' => $data['uid'],
  834. 'share' => $data['num'],
  835. 'contribution' => $data['num'],
  836. 'income' => 0,
  837. 'createtime' => time(),
  838. ];
  839. $id = Db::name('VideoUser')->insertGetId($up);
  840. }
  841. $this->share_record($id);//添加贡献值记录
  842. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  843. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  844. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  845. // 扣除短剧总版权
  846. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  847. $share = $shares - $data['num'];
  848. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  849. Db::commit();
  850. $this->success(__('操作成功'));
  851. } catch (Exception $e) {
  852. Db::rollback();
  853. $this->error($e->getMessage());
  854. }
  855. } else if ($data['type'] > 1) {
  856. $this->error(__('已经支付'));
  857. }
  858. } else {
  859. $this->error(__('金额不对'));
  860. }
  861. }
  862. // u币充值(展示二维码)
  863. public function recharge()
  864. {
  865. $uid = input('uid');
  866. $recharge_type = input('type');
  867. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  868. $user = Db::name('user')->where('id', $uid)->find();
  869. if (empty($user)) {
  870. $this->error(__('用户不存在'));
  871. }
  872. // $data = [
  873. // 'order_id'=> $number,
  874. // 'uid' => $uid,
  875. // 'paid' => 0,
  876. // 'add_time' => time()
  877. // ];
  878. $return = [];
  879. $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
  880. if ($recharge_type == 'trx') {
  881. // $data['recharge_type']='trx';
  882. $return['address'] = $address['trx_address'];
  883. $return['16_address'] = $address['trx_16_address'];
  884. } elseif ($recharge_type == 'bsc') {
  885. // $data['recharge_type']='bsc';
  886. $return['address'] = $address['bsc_address'];
  887. }
  888. // Db::name('user_recharge')->insert($data);
  889. Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
  890. $this->success($return);
  891. }
  892. // 查询u币转账记录
  893. public function search_recharge()
  894. {
  895. $time = strtotime("-1 hours", time());
  896. $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
  897. foreach ($user_address as $k => $v) {
  898. $user_info = Db::name('user')->where('id', $v['uid'])->find();
  899. // trx部分
  900. $service = TronService::instance('usdt');
  901. $time =bcmul($v['last_time'], 1000, 0);
  902. $info = $service->getTransfer($v['trx_address'], $time);
  903. foreach ($info['data'] as $k2 => $v2){
  904. $transaction_id = $v2['transaction_id'];
  905. $value =bcmul($v2['value'], 1000000, 0);
  906. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  907. if ($be) { //hash重复
  908. continue;
  909. }
  910. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  911. $data = [
  912. 'order_id' => $number,
  913. 'uid' => $v['uid'],
  914. 'paid' => 1,
  915. 'price' => $value,
  916. 'transactionHash' => $transaction_id,
  917. 'pay_time' => time(),
  918. 'add_time' => time()
  919. ];
  920. Db::name('user_recharge')->insert($data);
  921. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  922. $money = bcadd($user_info['money'], $value, 18);
  923. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  924. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  925. }
  926. // bsc部分
  927. $service = Web3Service::instance('bsc', 'usdt', '');
  928. $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
  929. foreach ($info as $k2 => $v2) {
  930. $value = $v2->params['value']->toString(); //金额
  931. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  932. $transactionHash = $v2->transactionHash;
  933. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  934. if ($be) { //hash重复
  935. continue;
  936. }
  937. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  938. $data = [
  939. 'order_id' => $number,
  940. 'uid' => $v['uid'],
  941. 'paid' => 1,
  942. 'price' => $value2,
  943. 'transactionHash' => $transactionHash,
  944. 'pay_time' => time(),
  945. 'add_time' => time()
  946. ];
  947. Db::name('user_recharge')->insert($data);
  948. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  949. $money = bcadd($user_info['money'], $value2, 18);
  950. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  951. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  952. }
  953. sleep(60);
  954. }
  955. }
  956. public function test()
  957. {
  958. // $service = Web3Service::instance('bsc', 'usdt', '');
  959. // $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
  960. //
  961. //
  962. //
  963. //
  964. // foreach ($info as $k => $v) {
  965. // $value = $v->params['value']->toString();
  966. // var_dump($value);
  967. // $transactionHash = $v->transactionHash;
  968. // var_dump($transactionHash);
  969. // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
  970. // var_dump($value);
  971. // }
  972. // var_dump($info);
  973. // die();
  974. // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
  975. $service = TronService::instance('usdt');
  976. $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
  977. $user_info = Db::name('user')->where('id', 1571)->find();
  978. foreach ($info['data'] as $k2 => $v2){
  979. $transaction_id = $v2['transaction_id'];
  980. $value =bcmul($v2['value'], 1000000, 0);
  981. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  982. if ($be) { //hash重复
  983. continue;
  984. }
  985. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  986. $data = [
  987. 'order_id' => $number,
  988. 'uid' => 1571,
  989. 'paid' => 1,
  990. 'price' => $value,
  991. 'transactionHash' => $transaction_id,
  992. 'pay_time' => time(),
  993. 'add_time' => time()
  994. ];
  995. Db::name('user_recharge')->insert($data);
  996. Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
  997. $money = bcadd($user_info['money'], $value, 18);
  998. Db::name('user')->where('id', 1571)->update(['money' => $money]);
  999. MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  1000. }
  1001. var_dump($info);die();
  1002. }
  1003. // function paginatedTransferQuery($service, $toAddress, $targetTimestamp) {
  1004. // $BPS = 3; // BSC每秒区块数
  1005. // $currentBlock = intval(time() * $BPS);
  1006. // $startBlock = intval($targetTimestamp * $BPS);
  1007. // $pageSize = 500;
  1008. // $results = [];
  1009. // $attempts = 0;
  1010. //
  1011. // while ($startBlock <= $currentBlock && $attempts < 50) {
  1012. // $endBlock = min($startBlock + $pageSize - 1, $currentBlock);
  1013. //
  1014. // try {
  1015. // $transfers = $service->getLastTransfer(
  1016. // [],
  1017. // [$toAddress],
  1018. // 0,
  1019. // $startBlock,
  1020. // $endBlock
  1021. // );
  1022. //
  1023. // // 只有当有结果时才处理
  1024. // if (!empty($transfers)) {
  1025. // $results = array_merge($results, $transfers);
  1026. //
  1027. // // 检查是否达到目标时间
  1028. // $newest = end($transfers);
  1029. // reset($transfers); // 重置数组指针
  1030. //
  1031. // // 确保有 timestamp 属性再检查
  1032. // if (property_exists($newest, 'timestamp') && $newest->timestamp >= time() - 60) {
  1033. // break; // 已查询到最新区块
  1034. // }
  1035. // }
  1036. //
  1037. // // 成功时增加分页大小
  1038. // $pageSize = min(5000, $pageSize * 2);
  1039. // $startBlock = $endBlock + 1;
  1040. //
  1041. // } catch (RequestException $e) {
  1042. // if (strpos($e->getMessage(), '413') !== false) {
  1043. // $pageSize = max(100, $pageSize / 2);
  1044. // $attempts++;
  1045. // continue;
  1046. // }
  1047. // throw $e;
  1048. // } catch (\Throwable $e) {
  1049. // // 捕获所有类型错误
  1050. // error_log("查询错误: " . $e->getMessage());
  1051. // $pageSize = max(100, $pageSize / 2);
  1052. // $attempts++;
  1053. // continue;
  1054. // }
  1055. //
  1056. // usleep(100000);
  1057. // $attempts++;
  1058. // }
  1059. //
  1060. // return array_filter($results, function($t) use ($targetTimestamp) {
  1061. // return property_exists($t, 'timestamp') && $t->timestamp >= $targetTimestamp;
  1062. // });
  1063. // }
  1064. }