Pay.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. var_dump(1124);die();
  42. $this->error(__('金额不对'));
  43. }
  44. $datapost = input('param.');//订单数据
  45. $datapost['buzs'] = htmlspecialchars_decode($datapost['buzs']);
  46. if (input('endtime') == '数量完成自动结束') {
  47. $endtimesjc = 100000000000;
  48. } else {
  49. $endtimesjc = strtotime(input('endtime'));
  50. }
  51. $up = [
  52. 'type' => input('type'),
  53. 'uid' => input('uid'),
  54. 'name' => input('name'),
  55. 'rwurl' => input('rwurl'),
  56. 'buz' => $datapost['buzs'],
  57. 'price' => input('price'),
  58. 'sum' => input('sum'),
  59. 'sumsy' => input('sum'),
  60. 'endtime' => input('endtime'),
  61. 'subtime' => input('subtime'),
  62. 'shetime' => input('shetime'),
  63. 'endtimesjc' => $endtimesjc,
  64. 'buytype' => input('buytype'),
  65. 'zprice' => input('price') * input('sum'),
  66. 'out_trade_no' => $number,
  67. 'createtime' => time(),
  68. ];
  69. $id = Db::name('task')->insertGetId($up);
  70. $data = $up;
  71. }
  72. $site = config('site');
  73. $amount = $data['zprice'];
  74. if (!$amount) {
  75. var_dump(5514);die();
  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. if ($order) {
  727. $user = Db::name('user')->where('id', $order['uid'])->find();
  728. while ($user['pid'] != 1) { //默认账号pid为1
  729. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  730. $grade = $user['grade'];
  731. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  732. if (empty($video_user)) { //如果上级没有购买过该视频版权
  733. $up = [
  734. 'vid' => $order['vid'],
  735. 'vname' => $order['vname'],
  736. 'uid' => $user['id'],
  737. 'share' => 0,
  738. 'contribution' => 0,
  739. 'income' => 0,
  740. 'createtime' => time(),
  741. ];
  742. $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
  743. }
  744. $contribution = 0;
  745. $check = 0;
  746. // $sell = $video_user['sell'] + $order['num'];
  747. $sell = $user['sell'] + $order['num'];
  748. // switch ($video_user['grade']) {
  749. switch ($user['grade']) {
  750. case 0:
  751. $contribution = 0.5 * $order['num'];
  752. if ($sell >= 30) {
  753. $check = 1;
  754. $sell = $sell - 30;
  755. }
  756. break;
  757. case 1:
  758. $contribution = 0.2 * $order['num'];
  759. if ($sell >= 300) {
  760. $check = 1;
  761. $sell = $sell - 300;
  762. }
  763. break;
  764. case 2:
  765. $contribution = 0.1 * $order['num'];
  766. if ($sell >= 3000) {
  767. $check = 1;
  768. $sell = $sell - 3000;
  769. }
  770. break;
  771. case 3:
  772. $contribution = 0.05 * $order['num'];
  773. default:
  774. }
  775. if ($check == 1) {
  776. // $grade = $video_user['grade'] + 1;
  777. $grade = $user['grade'] + 1;
  778. }
  779. $after = $video_user['contribution'] + $contribution;
  780. // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  781. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
  782. $res2 = Db::name('user')->where('id', $user['id'])->update(['grade' => $grade, 'sell' => $sell]);
  783. $up = [
  784. 'vid' => $order['vid'],
  785. 'vname' => $order['vname'],
  786. 'uid' => $user['id'],
  787. 'link_id' => $order['id'],
  788. 'num' => $contribution,
  789. 'after' => $contribution,
  790. 'content' => '下级用户购买版权,获得贡献值',
  791. 'createtime' => time(),
  792. 'pm' => 0,
  793. ];
  794. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  795. }
  796. }
  797. }
  798. // 添加贡献值记录 订单id
  799. public function share_record($id)
  800. {
  801. $order = Db::name('video_share')->where('id', $id)->find();
  802. if ($order) {
  803. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  804. $contribution = $order['num'] + $user['contribution'];
  805. $up = [
  806. 'vid' => $order['vid'],
  807. 'vname' => $order['vname'],
  808. 'uid' => $user['id'],
  809. 'link_id' => $order['id'],
  810. 'num' => $order['num'],
  811. 'after' => $contribution,
  812. 'content' => '用户购买版权,获得贡献值',
  813. 'createtime' => time(),
  814. 'pm' => 0,
  815. ];
  816. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  817. $share = $user['share'] + $order['num'];
  818. // 添加版权和贡献值
  819. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
  820. }
  821. }
  822. public function shareyuepay($money, $user_id, $id)//版权余额支付
  823. {
  824. $user = $this->Usermodel::get($user_id);
  825. if ($user && $money != 0) {
  826. $data = Db::name('video_share')->where('id', $id)->find();
  827. if ($user->money < $money) {
  828. $this->error(__('余额不足'));
  829. }
  830. $before = $user->money;
  831. $after = $user->money - $money;
  832. if ($data['type'] == 1) {
  833. Db::startTrans();
  834. try {
  835. //更新会员信息
  836. $user->save(['money' => $after]);
  837. //写入日志
  838. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  839. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  840. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  841. if (empty($video_user)) { //判断用户是否购买过该视频版权
  842. $up = [
  843. 'vid' => $data['vid'],
  844. 'vname' => $data['vname'],
  845. 'uid' => $data['uid'],
  846. 'share' => $data['num'],
  847. 'contribution' => $data['num'],
  848. 'income' => 0,
  849. 'createtime' => time(),
  850. ];
  851. $id = Db::name('VideoUser')->insertGetId($up);
  852. }
  853. $this->share_record($id);//添加贡献值记录
  854. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  855. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  856. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  857. // 扣除短剧总版权
  858. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  859. $share = $shares - $data['num'];
  860. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  861. Db::commit();
  862. $this->success(__('操作成功'));
  863. } catch (Exception $e) {
  864. Db::rollback();
  865. $this->error($e->getMessage());
  866. }
  867. } else if ($data['type'] > 1) {
  868. $this->error(__('已经支付'));
  869. }
  870. } else {
  871. $this->error(__('金额不对'));
  872. }
  873. }
  874. // u币充值(展示二维码)
  875. public function recharge()
  876. {
  877. $uid = input('uid');
  878. $recharge_type = input('type');
  879. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  880. $user = Db::name('user')->where('id', $uid)->find();
  881. if (empty($user)) {
  882. $this->error(__('用户不存在'));
  883. }
  884. // $data = [
  885. // 'order_id'=> $number,
  886. // 'uid' => $uid,
  887. // 'paid' => 0,
  888. // 'add_time' => time()
  889. // ];
  890. $return = [];
  891. $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
  892. if ($recharge_type == 'trx') {
  893. // $data['recharge_type']='trx';
  894. $return['address'] = $address['trx_address'];
  895. $return['16_address'] = $address['trx_16_address'];
  896. } elseif ($recharge_type == 'bsc') {
  897. // $data['recharge_type']='bsc';
  898. $return['address'] = $address['bsc_address'];
  899. }
  900. // Db::name('user_recharge')->insert($data);
  901. Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
  902. $this->success($return);
  903. }
  904. // 查询u币转账记录
  905. public function search_recharge()
  906. {
  907. $time = strtotime("-1 hours", time());
  908. $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
  909. foreach ($user_address as $k => $v) {
  910. $user_info = Db::name('user')->where('id', $v['uid'])->find();
  911. // trx部分
  912. $service = TronService::instance('usdt');
  913. $time =bcmul($v['last_time'], 1000, 0);
  914. $info = $service->getTransfer($v['trx_address'], $time);
  915. foreach ($info['data'] as $k2 => $v2){
  916. $transaction_id = $v2['transaction_id'];
  917. $value =bcdiv($v2['value'], 1000000, 0);
  918. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  919. if ($be) { //hash重复
  920. continue;
  921. }
  922. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  923. $data = [
  924. 'order_id' => $number,
  925. 'uid' => $v['uid'],
  926. 'paid' => 1,
  927. 'price' => $value,
  928. 'hash' => $transaction_id,
  929. 'recharge_type' => 'trx',
  930. 'pay_time' => time(),
  931. 'add_time' => time()
  932. ];
  933. Db::name('user_recharge')->insert($data);
  934. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  935. $money = bcadd($user_info['money'], $value, 18);
  936. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  937. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  938. }
  939. // bsc部分
  940. $service = Web3Service::instance('bsc', 'usdt', '');
  941. $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
  942. foreach ($info as $k2 => $v2) {
  943. $value = $v2->params['value']->toString(); //金额
  944. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  945. $transactionHash = $v2->transactionHash;
  946. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  947. if ($be) { //hash重复
  948. continue;
  949. }
  950. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  951. $data = [
  952. 'order_id' => $number,
  953. 'uid' => $v['uid'],
  954. 'paid' => 1,
  955. 'price' => $value2,
  956. 'hash' => $transactionHash,
  957. 'recharge_type' => 'bsc',
  958. 'pay_time' => time(),
  959. 'add_time' => time()
  960. ];
  961. Db::name('user_recharge')->insert($data);
  962. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  963. $money = bcadd($user_info['money'], $value2, 18);
  964. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  965. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  966. }
  967. }
  968. $this->success('查询完成');
  969. }
  970. public function withdrawal(){
  971. $uid = input('uid');
  972. $address = input('address');
  973. $type = input('type');
  974. $img = input('img');
  975. $price = input('price');
  976. if (empty($address)&&empty($img)){
  977. $this->error(__('请上传地址或上传收款码'));
  978. }
  979. Db::name('txjl')->insert(['uid' => $uid, 'address' => $address, 'type' => $type, 'img' => $img,'money'=> $price,'iscl'=>1]);
  980. $this->success('提交成功');
  981. }
  982. public function test()
  983. {
  984. $service = Web3Service::instance('bsc', 'usdt', '');
  985. $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
  986. $user_info = Db::name('user')->where('id', 1571)->find();
  987. foreach ($info as $k2 => $v2) {
  988. $value = $v2->params['value']->toString(); //金额
  989. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12);
  990. $transactionHash = $v2->transactionHash;
  991. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  992. if ($be) { //hash重复
  993. continue;
  994. }
  995. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  996. $data = [
  997. 'order_id' => $number,
  998. 'uid' => $user_info['id'],
  999. 'paid' => 1,
  1000. 'price' => $value2,
  1001. 'hash' => $transactionHash,
  1002. 'recharge_type' => 'bsc',
  1003. 'pay_time' => time(),
  1004. 'add_time' => time()
  1005. ];
  1006. Db::name('user_recharge')->insert($data);
  1007. Db::name('user_usdt_address')->where('uid', $user_info['id'])->update(['last_time' => time()]);
  1008. $money = bcadd($user_info['money'], $value2, 18);
  1009. Db::name('user')->where('id', $user_info['id'])->update(['money' => $money]);
  1010. MoneyLog::create(['user_id' => $user_info['id'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  1011. }
  1012. // foreach ($info as $k => $v) {
  1013. // $value = $v->params['value']->toString();
  1014. // var_dump($value);
  1015. // $transactionHash = $v->transactionHash;
  1016. // var_dump($transactionHash);
  1017. // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
  1018. // var_dump($value);
  1019. // }
  1020. var_dump($info);
  1021. die();
  1022. // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
  1023. // $service = TronService::instance('usdt');
  1024. //
  1025. // $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
  1026. // $user_info = Db::name('user')->where('id', 1571)->find();
  1027. // foreach ($info['data'] as $k2 => $v2){
  1028. // $transaction_id = $v2['transaction_id'];
  1029. // $value =bcdiv($v2['value'], 1000000, 0);
  1030. // $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  1031. // if ($be) { //hash重复
  1032. // continue;
  1033. // }
  1034. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1035. // $data = [
  1036. // 'order_id' => $number,
  1037. // 'uid' => 1571,
  1038. // 'paid' => 1,
  1039. // 'price' => $value,
  1040. // 'hash' => $transaction_id,
  1041. // 'type' => 'trx',
  1042. // 'pay_time' => time(),
  1043. // 'add_time' => time()
  1044. // ];
  1045. // Db::name('user_recharge')->insert($data);
  1046. // Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
  1047. // $money = bcadd($user_info['money'], $value, 18);
  1048. // Db::name('user')->where('id', 1571)->update(['money' => $money]);
  1049. // MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  1050. //
  1051. // }
  1052. // var_dump($info);die();
  1053. }
  1054. }