Pay.php 45 KB

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