Pay.php 33 KB

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