Pay.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. public function notify()
  437. {
  438. if ($_POST) {
  439. //Db::transaction(function () {
  440. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  441. $out_trade_no = $_POST['out_trade_no'];//交易号
  442. $trade_no = $_POST['trade_no'];//订单支付时间
  443. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  444. $order = Db::name('task')->where('out_trade_no', $out_trade_no)->find();
  445. if ($order['paytype'] == 1) {
  446. $up = [
  447. 'paytype' => 2,
  448. 'payment' => $gmt_payment,
  449. 'trade_no' => $trade_no,
  450. 'buyer' => $_POST['buyer_logon_id'],
  451. 'buytype' => '支付宝',
  452. 'ftime' => time()
  453. ];
  454. if (Db::name('task')->where('out_trade_no', $out_trade_no)->update($up)) {
  455. echo 'success';
  456. }
  457. } else {
  458. echo 'success';
  459. }
  460. }
  461. //});
  462. }
  463. }
  464. public function notifycz()
  465. {
  466. if ($_POST) {
  467. //$this->dingding_log($_POST);
  468. //Db::transaction(function () {
  469. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  470. $out_trade_no = $_POST['out_trade_no'];//交易号
  471. $trade_no = $_POST['trade_no'];//订单支付时间
  472. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  473. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  474. //$this->dingding_log($order);
  475. if ($order['type'] == 1) {
  476. $up = [
  477. 'type' => 2,
  478. 'payment' => $gmt_payment,
  479. 'trade_no' => $trade_no,
  480. 'buyer' => $_POST['buyer_logon_id'],
  481. 'buytype' => '支付宝',
  482. 'ftime' => time()
  483. ];
  484. Db::startTrans();
  485. try {
  486. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  487. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  488. Db::commit();
  489. echo 'success';
  490. } catch (Exception $e) {
  491. Db::rollback();
  492. $this->error($e->getMessage());
  493. }
  494. } else {
  495. echo 'success';
  496. }
  497. }
  498. //});
  499. }
  500. }
  501. public function notifydsf()//第三方充值回调
  502. {
  503. $this->dingding_log($_REQUEST);
  504. //Db::transaction(function () {
  505. $returncode = isset($_REQUEST["returncode"]) ? $_REQUEST["returncode"] : 1;
  506. if ($returncode == "00") {//处理交易完成或者支付成功的通知 //获取订单号
  507. $out_trade_no = isset($_REQUEST["orderid"]) ? $_REQUEST["orderid"] : '';
  508. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  509. $this->dingding_log($order);
  510. if ($order['type'] == 1) {
  511. $up = [
  512. 'type' => 2,
  513. 'ftime' => time()
  514. ];
  515. Db::startTrans();
  516. try {
  517. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  518. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  519. Db::commit();
  520. echo 'OK';
  521. } catch (Exception $e) {
  522. Db::rollback();
  523. $this->error($e->getMessage());
  524. }
  525. } else {
  526. echo 'OK';
  527. }
  528. }
  529. //});
  530. }
  531. public function money($money, $user_id, $id, $sxf = 0)
  532. {
  533. $user = $this->Usermodel::get($user_id);
  534. if ($user && $money != 0) {
  535. $before = $user->money;
  536. $after = $user->money + $money;
  537. //更新会员信息
  538. $user->save(['money' => $after]);
  539. //写入日志
  540. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => $id, 'sxf' => $sxf, 'memo' => '用户充值']);
  541. } else {
  542. $this->error(__('金额不对'));
  543. }
  544. }
  545. public function dingding_log($content)
  546. {
  547. $r = $_SERVER['DOCUMENT_ROOT'] . '/api_log/' . date('Y-m-d_H-i-s', time()) . '.txt';
  548. $fp = fopen($r, 'w+');
  549. fwrite($fp, "执行日期:" . date('Y-m-d H:i:s', time()) . ' ' . var_export($content, true));
  550. fclose($fp);
  551. }
  552. public function FromXml($xml)
  553. {
  554. //将XML转为array
  555. //禁止引用外部xml实体
  556. libxml_disable_entity_loader(true);
  557. $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  558. return $values;
  559. }
  560. public function sharepay()
  561. {
  562. // $datavideo_order=Db::name('video_share')->where(['vid'=>input('id'),'uid'=>input('uid')])->find();
  563. // $datavideo_order1=Db::name('video_share')->where(['sid'=>input('id'),'uid'=>input('uid'),'lx'=>1])->find();
  564. $sad = 1;
  565. // $lx=input('lx');
  566. $num = input('num'); //购买的版权数量
  567. $uid = input('uid');
  568. $vid = input('vid');
  569. // 验证数量
  570. $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
  571. if ($user_num) {
  572. $cs_num = $user_num['share'] + $num;
  573. if ($cs_num > 10 || $num > 10) {
  574. $this->error(__('单剧集购买版权不能超过10份'));
  575. }
  576. }
  577. // if($datavideo_order){
  578. // if($datavideo_order['type']==2){
  579. // $this->error(__('该订单已支付'));
  580. // }
  581. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  582. // $price=input('priceq');
  583. // $pricebuy=input('pricebuy');
  584. // if(input('lx')==2){
  585. // $price=input('priced');
  586. // $pricebuy=input('pricebuy');
  587. // }
  588. // $up=[
  589. // 'type'=>1,
  590. // 'lx'=>$lx,//1 全章购买 2单集购买
  591. // 'uid'=>input('uid'),
  592. // 'name'=>input('namet'),
  593. // 'img'=>input('img'),
  594. // 'price'=>$price,
  595. // 'sum'=>input('sum'),
  596. // 'sid'=>input('pid'),
  597. // 'smid'=>input('smid'),
  598. // 'paytype'=>input('buytype'),
  599. // 'zprice'=>$pricebuy*input('sum'),
  600. // 'out_trade_no'=>$number,
  601. // ];
  602. // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
  603. // $id=$datavideo_order['id'];
  604. // $data=$up;
  605. // }else{
  606. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  607. // if (!input('pricebuy')) {
  608. // $this->error(__('金额不对'));
  609. // }
  610. $price = input('price');
  611. // $pricebuy = input('pricebuy');
  612. // if(input('lx')==2){
  613. // $price=input('priced');
  614. // $pricebuy=input('pricebuy');
  615. // }
  616. // 获取当前时间的时间戳
  617. $now = time();
  618. // 计算三年后的时间戳
  619. $threeYearsLater = strtotime('+3 years', $now);
  620. $total_price = $price * $num;
  621. $up = [
  622. 'type' => 1,
  623. 'uid' => input('uid'),
  624. 'vid' => input('vid'),
  625. 'num' => input('num'),
  626. 'price' => $price,
  627. 'total_price' => $total_price,
  628. 'paytype' => input('buytype'),
  629. 'out_trade_no' => $number,
  630. 'createtime' => time(),
  631. 'endtime' => $threeYearsLater,
  632. 'updatetime' => time(),
  633. ];
  634. //$this->error($up);
  635. $id = Db::name('video_share')->insertGetId($up);
  636. $data = $up;
  637. // }
  638. $site = config('site');
  639. $amount = $price;
  640. // $amount = $data['zprice'];
  641. // if (!$amount) {
  642. // $this->error(__('金额不对'));
  643. // }
  644. // 异步通知地址
  645. $notify_url = $site['zfb']['notifyurl'];
  646. // 订单标题
  647. $subject = 'wool订单';
  648. // 订单详情
  649. $body = 'wool致力于打造最好的移动服务平台';
  650. // 订单号,示例代码使用时间值作为唯一的订单ID号
  651. //var_dump($data['paytype']);
  652. if ($data['paytype'] == '支付宝') {
  653. $this->allpay($amount, $subject, $body, $number, $notify_url);
  654. } else if ($data['paytype'] == '余额') {
  655. $this->shareyuepay($amount, input('uid'), $id);
  656. } else if ($data['paytype'] == '微信') {
  657. $wxlx = input('wxlx');
  658. if ($wxlx == 'wxxcx') {
  659. $openid = input('openid');
  660. if (!$openid) {
  661. $this->error(__('openid不对'));
  662. }
  663. $this->paywxxcx($id, $number, $amount, $openid);
  664. } else if ($wxlx == 'wxh5') {
  665. $buytype = '微信h5';
  666. $this->paywxh5($id, $number, $amount, $buytype);
  667. } else if ($wxlx == 'wxgzh') {
  668. $buytype = '微信公众号';
  669. $this->paywxh5($id, $number, $amount, $buytype);
  670. }
  671. }
  672. }
  673. // 判断订单的用户是否有上级以及判断推荐等级
  674. public function grade_sell($out_trade_no)
  675. {
  676. $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
  677. if ($order) {
  678. $user = Db::name('user')->where('id', $order['uid'])->find();
  679. while ($user['pid'] != 1) { //默认账号pid为1
  680. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  681. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  682. $contribution = 0;
  683. $check = 0;
  684. $sell = $video_user['sell'] + $order['num'];
  685. switch ($video_user['grade']) {
  686. case 0:
  687. $contribution = 0.5 * $order['num'];
  688. if ($sell>=30){
  689. $check = 1;
  690. $sell = $sell - 30;
  691. }
  692. break;
  693. case 1:
  694. $contribution = 0.2 * $order['num'];
  695. if ($sell>=300){
  696. $check = 1;
  697. $sell = $sell - 300;
  698. }
  699. break;
  700. case 2:
  701. $contribution = 0.1 * $order['num'];
  702. if ($sell>=3000){
  703. $check = 1;
  704. $sell = $sell - 3000;
  705. }
  706. break;
  707. case 3:
  708. $contribution = 0.05 * $order['num'];
  709. default:
  710. }
  711. if ($check == 1){
  712. $grade = $video_user['grade'] + 1;
  713. }
  714. $after = $video_user['contribution'] + $contribution;
  715. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  716. $up = [
  717. 'vid' => $order['vid'],
  718. 'uid' => $user['id'],
  719. 'link_id' => $order['id'],
  720. 'num' => $contribution,
  721. 'after' => $contribution,
  722. 'content' => '下级用户购买版权,获得贡献值',
  723. 'createtime' => time(),
  724. 'pm' => 0,
  725. ];
  726. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  727. }
  728. }
  729. }
  730. // 添加贡献值记录 订单id
  731. public function share_record($id){
  732. $order = Db::name('video_share')->where('id', $id)->find();
  733. if ($order){
  734. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  735. $contribution = $order['num'] +$user['contribution'];
  736. $up = [
  737. 'vid' => $order['vid'],
  738. 'uid' => $user['id'],
  739. 'link_id' => $order['id'],
  740. 'num' => $order['num'],
  741. 'after' => $contribution,
  742. 'content' => '用户购买版权,获得贡献值',
  743. 'createtime' => time(),
  744. 'pm' => 0,
  745. ];
  746. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  747. $share = $user['share'] + $order['num'];
  748. // 添加版权和贡献值
  749. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $contribution,'share'=>$share]);
  750. }
  751. }
  752. public function shareyuepay($money, $user_id, $id)//版权余额支付
  753. {
  754. $user = $this->Usermodel::get($user_id);
  755. if ($user && $money != 0) {
  756. $data = Db::name('video_share')->where('id', $id)->find();
  757. if ($user->money < $money) {
  758. $this->error(__('余额不足'));
  759. }
  760. $before = $user->money;
  761. $after = $user->money - $money;
  762. if ($data['type'] == 1) {
  763. Db::startTrans();
  764. try {
  765. //更新会员信息
  766. $user->save(['money' => $after]);
  767. //写入日志
  768. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  769. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  770. $this->share_record($id);//添加贡献值记录
  771. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  772. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  773. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  774. Db::commit();
  775. $this->success(__('操作成功'));
  776. } catch (Exception $e) {
  777. Db::rollback();
  778. $this->error($e->getMessage());
  779. }
  780. } else if ($data['type'] > 1) {
  781. $this->error(__('已经支付'));
  782. }
  783. } else {
  784. $this->error(__('金额不对'));
  785. }
  786. }
  787. }