Pay.php 38 KB

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