Pay.php 44 KB

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