Pay.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. // $lx=input('lx');
  582. $num = input('num'); //购买的版权数量
  583. $uid = input('uid');
  584. $vid = input('vid');
  585. $user = Db::name('user')->where('id', $uid)->find();
  586. if (empty($user)){
  587. $this->error(__('用户不存在'));
  588. }
  589. if (empty($num)){
  590. $this->error(__('请输入数量'));
  591. }
  592. // 验证数量
  593. $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
  594. if ($user_num) {
  595. $cs_num = $user_num['share'] + $num;
  596. if ($cs_num > 10 || $num > 10) {
  597. $this->error(__('单剧集购买版权不能超过10份'));
  598. }
  599. }
  600. $user_share = Db::name('videolist')->where('id', $vid)->value('share');
  601. if (empty($user_share)){
  602. $this->error(__('短剧不存在'));
  603. }
  604. $user_shares = bcsub($user_share , $num, 0);
  605. if ($user_shares < 0) {
  606. $this->error(__('该局总版权只剩下' . $user_share . '份'));
  607. }
  608. // if($datavideo_order){
  609. // if($datavideo_order['type']==2){
  610. // $this->error(__('该订单已支付'));
  611. // }
  612. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  613. // $price=input('priceq');
  614. // $pricebuy=input('pricebuy');
  615. // if(input('lx')==2){
  616. // $price=input('priced');
  617. // $pricebuy=input('pricebuy');
  618. // }
  619. // $up=[
  620. // 'type'=>1,
  621. // 'lx'=>$lx,//1 全章购买 2单集购买
  622. // 'uid'=>input('uid'),
  623. // 'name'=>input('namet'),
  624. // 'img'=>input('img'),
  625. // 'price'=>$price,
  626. // 'sum'=>input('sum'),
  627. // 'sid'=>input('pid'),
  628. // 'smid'=>input('smid'),
  629. // 'paytype'=>input('buytype'),
  630. // 'zprice'=>$pricebuy*input('sum'),
  631. // 'out_trade_no'=>$number,
  632. // ];
  633. // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
  634. // $id=$datavideo_order['id'];
  635. // $data=$up;
  636. // }else{
  637. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  638. // if (!input('pricebuy')) {
  639. // $this->error(__('金额不对'));
  640. // }
  641. // $price = input('price');
  642. $price = Db::name('videolist')->where('id', input('vid'))->value('sharesprice');;
  643. // $pricebuy = input('pricebuy');
  644. // if(input('lx')==2){
  645. // $price=input('priced');
  646. // $pricebuy=input('pricebuy');
  647. // }
  648. // 获取当前时间的时间戳
  649. $now = time();
  650. // 计算三年后的时间戳
  651. $threeYearsLater = strtotime('+3 years', $now);
  652. $vname = Db::name('videolist')->where('id', input('vid'))->value('name');
  653. $total_price = $price * $num;
  654. $up = [
  655. 'type' => 1,
  656. 'uid' => input('uid'),
  657. 'vid' => input('vid'),
  658. 'vname' => $vname,
  659. 'num' => input('num'),
  660. 'price' => $price,
  661. 'total_price' => $total_price,
  662. 'paytype' => input('buytype'),
  663. 'out_trade_no' => $number,
  664. 'createtime' => time(),
  665. 'endtime' => $threeYearsLater,
  666. 'updatetime' => time(),
  667. ];
  668. //$this->error($up);
  669. $id = Db::name('video_share')->insertGetId($up);
  670. $data = $up;
  671. // }
  672. $site = config('site');
  673. $amount = $price;
  674. // $amount = $data['zprice'];
  675. // if (!$amount) {
  676. // $this->error(__('金额不对'));
  677. // }
  678. // 异步通知地址
  679. $notify_url = $site['zfb']['notifyurl'];
  680. // 订单标题
  681. $subject = 'wool订单';
  682. // 订单详情
  683. $body = 'wool致力于打造最好的移动服务平台';
  684. // 订单号,示例代码使用时间值作为唯一的订单ID号
  685. //var_dump($data['paytype']);
  686. if ($data['paytype'] == '支付宝') {
  687. $this->allpay($amount, $subject, $body, $number, $notify_url);
  688. } else if ($data['paytype'] == '余额') {
  689. $this->shareyuepay($amount, input('uid'), $id);
  690. } else if ($data['paytype'] == '微信') {
  691. $wxlx = input('wxlx');
  692. if ($wxlx == 'wxxcx') {
  693. $openid = input('openid');
  694. if (!$openid) {
  695. $this->error(__('openid不对'));
  696. }
  697. $this->paywxxcx($id, $number, $amount, $openid);
  698. } else if ($wxlx == 'wxh5') {
  699. $buytype = '微信h5';
  700. $this->paywxh5($id, $number, $amount, $buytype);
  701. } else if ($wxlx == 'wxgzh') {
  702. $buytype = '微信公众号';
  703. $this->paywxh5($id, $number, $amount, $buytype);
  704. } else if ($data['paytype'] == '汇聚') {
  705. $paid_price = 0;
  706. // if ($recharId) {
  707. // $data = SystemGroupData::getDateValue($recharId);
  708. // if ($data === false) {
  709. // return app('json')->fail('您选择的充值方式已下架!');
  710. // } else {
  711. // $paid_price = $data['give_money'] ?? 0;
  712. // }
  713. // }
  714. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  715. $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'share_pay');
  716. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  717. }
  718. }
  719. }
  720. // 判断订单的用户是否有上级以及判断推荐等级
  721. public function grade_sell($out_trade_no)
  722. {
  723. $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
  724. if ($order) {
  725. $user = Db::name('user')->where('id', $order['uid'])->find();
  726. while ($user['pid'] != 1) { //默认账号pid为1
  727. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  728. $grade = $user['grade'];
  729. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  730. if (empty($video_user)) { //如果上级没有购买过该视频版权
  731. $up = [
  732. 'vid' => $order['vid'],
  733. 'vname' => $order['vname'],
  734. 'uid' => $user['id'],
  735. 'share' => 0,
  736. 'contribution' => 0,
  737. 'income' => 0,
  738. 'createtime' => time(),
  739. ];
  740. $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
  741. }
  742. $contribution = 0;
  743. $check = 0;
  744. // $sell = $video_user['sell'] + $order['num'];
  745. $sell = $user['sell'] + $order['num'];
  746. // switch ($video_user['grade']) {
  747. switch ($user['grade']) {
  748. case 0:
  749. $contribution = 0.5 * $order['num'];
  750. if ($sell >= 30) {
  751. $check = 1;
  752. $sell = $sell - 30;
  753. }
  754. break;
  755. case 1:
  756. $contribution = 0.2 * $order['num'];
  757. if ($sell >= 300) {
  758. $check = 1;
  759. $sell = $sell - 300;
  760. }
  761. break;
  762. case 2:
  763. $contribution = 0.1 * $order['num'];
  764. if ($sell >= 3000) {
  765. $check = 1;
  766. $sell = $sell - 3000;
  767. }
  768. break;
  769. case 3:
  770. $contribution = 0.05 * $order['num'];
  771. default:
  772. }
  773. if ($check == 1) {
  774. // $grade = $video_user['grade'] + 1;
  775. $grade = $user['grade'] + 1;
  776. }
  777. $after = $video_user['contribution'] + $contribution;
  778. // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  779. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
  780. $res2 = Db::name('user')->where('id', $user['id'])->update(['grade' => $grade, 'sell' => $sell]);
  781. $up = [
  782. 'vid' => $order['vid'],
  783. 'vname' => $order['vname'],
  784. 'uid' => $user['id'],
  785. 'link_id' => $order['id'],
  786. 'num' => $contribution,
  787. 'after' => $contribution,
  788. 'content' => '下级用户购买版权,获得贡献值',
  789. 'createtime' => time(),
  790. 'pm' => 0,
  791. ];
  792. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  793. }
  794. }
  795. }
  796. // 添加贡献值记录 订单id
  797. public function share_record($id)
  798. {
  799. $order = Db::name('video_share')->where('id', $id)->find();
  800. if ($order) {
  801. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  802. $contribution = $order['num'] + $user['contribution'];
  803. $up = [
  804. 'vid' => $order['vid'],
  805. 'vname' => $order['vname'],
  806. 'uid' => $user['id'],
  807. 'link_id' => $order['id'],
  808. 'num' => $order['num'],
  809. 'after' => $contribution,
  810. 'content' => '用户购买版权,获得贡献值',
  811. 'createtime' => time(),
  812. 'pm' => 0,
  813. ];
  814. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  815. $share = $user['share'] + $order['num'];
  816. // 添加版权和贡献值
  817. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
  818. }
  819. }
  820. public function shareyuepay($money, $user_id, $id)//版权余额支付
  821. {
  822. $user = $this->Usermodel::get($user_id);
  823. if ($user) {
  824. $data = Db::name('video_share')->where('id', $id)->find();
  825. if ($user->money < $money) {
  826. $this->error(__('余额不足'));
  827. }
  828. $before = $user->money;
  829. $after = $user->money - $money;
  830. if ($data['type'] == 1) {
  831. Db::startTrans();
  832. try {
  833. //更新会员信息
  834. $user->save(['money' => $after]);
  835. //写入日志
  836. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  837. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  838. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  839. if (empty($video_user)) { //判断用户是否购买过该视频版权
  840. $up = [
  841. 'vid' => $data['vid'],
  842. 'vname' => $data['vname'],
  843. 'uid' => $data['uid'],
  844. 'share' => $data['num'],
  845. 'contribution' => $data['num'],
  846. 'income' => 0,
  847. 'createtime' => time(),
  848. ];
  849. $id = Db::name('VideoUser')->insertGetId($up);
  850. }
  851. $this->share_record($id);//添加贡献值记录
  852. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  853. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  854. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  855. // 扣除短剧总版权
  856. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  857. $share = $shares - $data['num'];
  858. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  859. Db::commit();
  860. $this->success(__('操作成功'));
  861. } catch (Exception $e) {
  862. Db::rollback();
  863. $this->error($e->getMessage());
  864. }
  865. } else if ($data['type'] > 1) {
  866. $this->error(__('已经支付'));
  867. }
  868. } else {
  869. $this->error(__('用户不存在'));
  870. }
  871. }
  872. // u币充值(展示二维码)
  873. public function recharge()
  874. {
  875. $uid = input('uid');
  876. $recharge_type = input('type');
  877. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  878. $user = Db::name('user')->where('id', $uid)->find();
  879. if (empty($user)) {
  880. $this->error(__('用户不存在'));
  881. }
  882. // $data = [
  883. // 'order_id'=> $number,
  884. // 'uid' => $uid,
  885. // 'paid' => 0,
  886. // 'add_time' => time()
  887. // ];
  888. $return = [];
  889. $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
  890. if ($recharge_type == 'trx') {
  891. // $data['recharge_type']='trx';
  892. $return['address'] = $address['trx_address'];
  893. $return['16_address'] = $address['trx_16_address'];
  894. } elseif ($recharge_type == 'bsc') {
  895. // $data['recharge_type']='bsc';
  896. $return['address'] = $address['bsc_address'];
  897. }
  898. // Db::name('user_recharge')->insert($data);
  899. Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
  900. $this->success($return);
  901. }
  902. // 查询u币转账记录
  903. public function search_recharge()
  904. {
  905. $time = strtotime("-1 hours", time());
  906. $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
  907. foreach ($user_address as $k => $v) {
  908. $user_info = Db::name('user')->where('id', $v['uid'])->find();
  909. // trx部分
  910. $service = TronService::instance('usdt');
  911. $time =bcmul($v['last_time'], 1000, 0);
  912. $info = $service->getTransfer($v['trx_address'], $time);
  913. foreach ($info['data'] as $k2 => $v2){
  914. $transaction_id = $v2['transaction_id'];
  915. $value =bcdiv($v2['value'], 1000000, 0);
  916. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  917. if ($be) { //hash重复
  918. continue;
  919. }
  920. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  921. $data = [
  922. 'order_id' => $number,
  923. 'uid' => $v['uid'],
  924. 'paid' => 1,
  925. 'price' => $value,
  926. 'hash' => $transaction_id,
  927. 'recharge_type' => 'trx',
  928. 'pay_time' => time(),
  929. 'add_time' => time()
  930. ];
  931. Db::name('user_recharge')->insert($data);
  932. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  933. $money = bcadd($user_info['money'], $value, 18);
  934. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  935. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  936. }
  937. // bsc部分
  938. $service = Web3Service::instance('bsc', 'usdt', '');
  939. $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
  940. foreach ($info as $k2 => $v2) {
  941. $value = $v2->params['value']->toString(); //金额
  942. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  943. $transactionHash = $v2->transactionHash;
  944. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  945. if ($be) { //hash重复
  946. continue;
  947. }
  948. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  949. $data = [
  950. 'order_id' => $number,
  951. 'uid' => $v['uid'],
  952. 'paid' => 1,
  953. 'price' => $value2,
  954. 'hash' => $transactionHash,
  955. 'recharge_type' => 'bsc',
  956. 'pay_time' => time(),
  957. 'add_time' => time()
  958. ];
  959. Db::name('user_recharge')->insert($data);
  960. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  961. $money = bcadd($user_info['money'], $value2, 18);
  962. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  963. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  964. }
  965. }
  966. $this->success('查询完成');
  967. }
  968. public function withdrawal(){
  969. $uid = input('uid');
  970. $address = input('address');
  971. $type = input('type');
  972. $img = input('img');
  973. $price = input('price');
  974. if (empty($address)&&empty($img)){
  975. $this->error(__('请上传地址或上传收款码'));
  976. }
  977. Db::name('txjl')->insert(['uid' => $uid, 'address' => $address, 'type' => $type, 'img' => $img,'money'=> $price,'iscl'=>1]);
  978. $this->success('提交成功');
  979. }
  980. public function test()
  981. {
  982. $site = config('site');
  983. var_dump($site['gxz']['普通会员']);
  984. var_dump($site['gxz']['初级合伙人']);
  985. var_dump($site['gxz']['高级合伙人']);
  986. var_dump($site['gxz']['董事']);
  987. die();
  988. $service = Web3Service::instance('bsc', 'usdt', '');
  989. $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
  990. $user_info = Db::name('user')->where('id', 1571)->find();
  991. foreach ($info as $k2 => $v2) {
  992. $value = $v2->params['value']->toString(); //金额
  993. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12);
  994. $transactionHash = $v2->transactionHash;
  995. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  996. if ($be) { //hash重复
  997. continue;
  998. }
  999. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1000. $data = [
  1001. 'order_id' => $number,
  1002. 'uid' => $user_info['id'],
  1003. 'paid' => 1,
  1004. 'price' => $value2,
  1005. 'hash' => $transactionHash,
  1006. 'recharge_type' => 'bsc',
  1007. 'pay_time' => time(),
  1008. 'add_time' => time()
  1009. ];
  1010. Db::name('user_recharge')->insert($data);
  1011. Db::name('user_usdt_address')->where('uid', $user_info['id'])->update(['last_time' => time()]);
  1012. $money = bcadd($user_info['money'], $value2, 18);
  1013. Db::name('user')->where('id', $user_info['id'])->update(['money' => $money]);
  1014. MoneyLog::create(['user_id' => $user_info['id'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  1015. }
  1016. // foreach ($info as $k => $v) {
  1017. // $value = $v->params['value']->toString();
  1018. // var_dump($value);
  1019. // $transactionHash = $v->transactionHash;
  1020. // var_dump($transactionHash);
  1021. // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
  1022. // var_dump($value);
  1023. // }
  1024. var_dump($info);
  1025. die();
  1026. // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
  1027. // $service = TronService::instance('usdt');
  1028. //
  1029. // $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
  1030. // $user_info = Db::name('user')->where('id', 1571)->find();
  1031. // foreach ($info['data'] as $k2 => $v2){
  1032. // $transaction_id = $v2['transaction_id'];
  1033. // $value =bcdiv($v2['value'], 1000000, 0);
  1034. // $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  1035. // if ($be) { //hash重复
  1036. // continue;
  1037. // }
  1038. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1039. // $data = [
  1040. // 'order_id' => $number,
  1041. // 'uid' => 1571,
  1042. // 'paid' => 1,
  1043. // 'price' => $value,
  1044. // 'hash' => $transaction_id,
  1045. // 'type' => 'trx',
  1046. // 'pay_time' => time(),
  1047. // 'add_time' => time()
  1048. // ];
  1049. // Db::name('user_recharge')->insert($data);
  1050. // Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
  1051. // $money = bcadd($user_info['money'], $value, 18);
  1052. // Db::name('user')->where('id', 1571)->update(['money' => $money]);
  1053. // MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  1054. //
  1055. // }
  1056. // var_dump($info);die();
  1057. }
  1058. }