Pay.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. }else if ($data['paytype'] == '汇聚') {
  197. $paid_price = 0;
  198. // if ($recharId) {
  199. // $data = SystemGroupData::getDateValue($recharId);
  200. // if ($data === false) {
  201. // return app('json')->fail('您选择的充值方式已下架!');
  202. // } else {
  203. // $paid_price = $data['give_money'] ?? 0;
  204. // }
  205. // }
  206. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  207. $recharge = Huifu::wxJsPay(session('openid'),$number,$price,$paid_price,'video_pay');
  208. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  209. }
  210. }
  211. public function paywxh5($oid, $number, $amount, $buytype)
  212. {
  213. $site = config('site');
  214. $user = $this->auth->getUser();
  215. if (!$user) {
  216. $this->error(__('user不对'));
  217. }
  218. if ($amount <= 0) {
  219. $this->error(__('金额不对'));
  220. }
  221. $up = [
  222. 'type' => 1,
  223. 'uid' => $user['id'],
  224. 'tdnum' => input('tdnum'),//支付通道
  225. 'amount' => $amount,
  226. 'out_trade_no' => $number,
  227. 'createtime' => time(),
  228. 'buytype' => $buytype,
  229. 'oid' => $oid,
  230. ];
  231. $id = Db::name('paylog')->insertGetId($up);
  232. $urls = $site['imgurl'] . '/index/paydsf/wxh5?out_trade_no=' . $number;
  233. if ($buytype == '微信公众号') {
  234. $urls = $site['imgurl'] . '/index/index/pay?id=' . $id;
  235. }
  236. $this->success('订单提交成功 正在跳转支付', $urls);
  237. }
  238. public function paywxxcx($oid, $number, $amount, $openid)
  239. {
  240. $site = config('site');
  241. $user = $this->auth->getUser();
  242. if (!$user) {
  243. $this->error(__('user不对'));
  244. }
  245. if ($amount <= 0) {
  246. $this->error(__('金额不对'));
  247. }
  248. $up = [
  249. 'type' => 1,
  250. 'uid' => $user['id'],
  251. 'amount' => $amount,
  252. 'out_trade_no' => $number,
  253. 'createtime' => time(),
  254. 'buytype' => '微信小程序',
  255. 'oid' => $oid,
  256. ];
  257. $id = Db::name('paylog')->insertGetId($up);
  258. $modelxcx = new \app\api\controller\Paywx;
  259. $trs = $modelxcx->wxxcx($number, $amount, $openid);
  260. $this->success('提交成功', $trs);
  261. }
  262. public function videoyuepay($money, $user_id, $id)//余额支付
  263. {
  264. $user = $this->Usermodel::get($user_id);
  265. if ($user && $money != 0) {
  266. $data = Db::name('video_order')->where('id', $id)->find();
  267. if ($user->money < $money) {
  268. $this->error(__('余额不足'));
  269. }
  270. $before = $user->money;
  271. $after = $user->money - $money;
  272. if ($data['type'] == 1) {
  273. Db::startTrans();
  274. try {
  275. //更新会员信息
  276. $user->save(['money' => $after]);
  277. //写入日志
  278. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  279. Db::name('video_order')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'ftime' => time()]);
  280. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  281. $this->dailiyongjin($id, $data['sid'], $data['smid'], $user_id, $money, '佣金结算');//后台代理佣金结算
  282. Db::commit();
  283. $this->success(__('操作成功'));
  284. } catch (Exception $e) {
  285. Db::rollback();
  286. $this->error($e->getMessage());
  287. }
  288. } else if ($data['type'] > 1) {
  289. $this->error(__('已经支付'));
  290. }
  291. } else {
  292. $this->error(__('金额不对'));
  293. }
  294. }
  295. public function yuepay($money, $user_id, $id)//余额支付
  296. {
  297. $user = $this->Usermodel::get($user_id);
  298. if ($user && $money != 0) {
  299. $data = Db::name('task')->where('id', $id)->find();
  300. if ($user->money < $money) {
  301. $this->error(__('余额不足'));
  302. }
  303. $before = $user->money;
  304. $after = $user->money - $money;
  305. //更新会员信息
  306. $user->save(['money' => $after]);
  307. //写入日志
  308. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  309. if ($data['paytype'] == 1) {
  310. $up = [
  311. 'paytype' => 2,
  312. 'buytype' => '余额',
  313. 'ftime' => time()
  314. ];
  315. if (Db::name('task')->where('id', $id)->update($up)) {
  316. $this->success(__('操作成功'));
  317. }
  318. } else if ($data['paytype'] > 1) {
  319. $this->error(__('已经支付'));
  320. }
  321. } else {
  322. $this->error(__('金额不对'));
  323. }
  324. }
  325. public function czpay()
  326. {
  327. $site = config('site');
  328. $user = $this->auth->getUser();
  329. if (!$user) {
  330. $this->error(__('user不对'));
  331. }
  332. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  333. $amount = input('total', 0);
  334. $notify_url = $site['zfb']['notifyurlcz'];
  335. // 订单标题
  336. $subject = 'wool充值';
  337. // 订单详情
  338. $body = 'wool致力于打造最好的移动服务平台';
  339. if ($amount <= 0) {
  340. $this->error(__('金额不对'));
  341. }
  342. $up = [
  343. 'type' => 1,
  344. 'uid' => $user['id'],
  345. 'amount' => input('total'),
  346. 'out_trade_no' => $number,
  347. 'createtime' => time(),
  348. ];
  349. $id = Db::name('paylog')->insertGetId($up);
  350. if (input('type', 'allpay') == 'iap') {
  351. $this->success('请求成功', $number);
  352. } else {
  353. $this->allpay($amount, $subject, $body, $number, $notify_url);
  354. }
  355. }
  356. public function dsfczpay()
  357. {
  358. $site = config('site');
  359. $user = $this->auth->getUser();
  360. if (!$user) {
  361. $this->error(__('user不对'));
  362. }
  363. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  364. $amount = input('total', 0);
  365. if ($amount <= 0) {
  366. $this->error(__('金额不对'));
  367. }
  368. $up = [
  369. 'type' => 1,
  370. 'uid' => $user['id'],
  371. 'tdnum' => input('tdnum'),
  372. 'amount' => $amount,
  373. 'out_trade_no' => $number,
  374. 'createtime' => time(),
  375. 'buytype' => isset($site['dsfzf']['zfname']) ? $site['dsfzf']['zfname'] : '',
  376. ];
  377. $id = Db::name('paylog')->insertGetId($up);
  378. $urls = $site['imgurl'] . '/index/paydsf?out_trade_no=' . $number;
  379. $this->success('订单提交成功 正在跳转支付', $urls);
  380. }
  381. public function czpayiaptz()
  382. {
  383. $out_trade_no = input('out_trade_no');
  384. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  385. //$this->dingding_log($order);
  386. if ($order['type'] == 1) {
  387. $up = [
  388. 'type' => 2,
  389. 'payment' => null,
  390. 'trade_no' => null,
  391. 'buyer' => null,
  392. 'buytype' => '苹果',
  393. 'ftime' => time()
  394. ];
  395. Db::startTrans();
  396. try {
  397. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  398. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  399. Db::commit();
  400. $this->success('支付成功');
  401. } catch (Exception $e) {
  402. Db::rollback();
  403. $this->error($e->getMessage());
  404. }
  405. } else {
  406. $this->success('已经支付');
  407. }
  408. }
  409. public function allpay($amount, $subject, $body, $out_trade_no, $notify_url)//支付宝支付
  410. {
  411. $total = floatval($amount);
  412. if (!$total) {
  413. $this->error(__('金额不对'));
  414. }
  415. //require_once('/vendor/alipayrsa2/AopSdk.php');
  416. //Vendor('AopSdk.alipayrsa2');
  417. // require_once './vendor/alipayrsa2/aop/AopClient.php';
  418. // require_once './vendor/alipayrsa2/aop/request/AlipayTradeAppPayRequest.php';
  419. vendor('alipayrsa2.AopSdk');
  420. $site = config('site');
  421. $aop = new \AopClient;
  422. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  423. $aop->appId = $site['zfb']['appId'];
  424. //开发者私钥去头去尾去回车,一行字符串
  425. $aop->rsaPrivateKey = $site['zfb']['rsaPrivateKey'];
  426. $aop->format = "json";
  427. $aop->charset = "UTF-8";
  428. $aop->signType = "RSA2";
  429. //请填写支付宝公钥,一行字符串
  430. $aop->alipayrsaPublicKey = $site['zfb']['alipayrsaPublicKey'];
  431. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  432. $request = new \AlipayTradeAppPayRequest();
  433. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  434. $bizcontent = "{\"body\":\"" . $body . "\","
  435. . "\"subject\": \"" . $subject . "\","
  436. . "\"out_trade_no\": \"" . $out_trade_no . "\","
  437. . "\"timeout_express\": \"30m\","
  438. . "\"total_amount\": \"" . $total . "\","
  439. . "\"product_code\":\"QUICK_MSECURITY_PAY\""
  440. . "}";
  441. $request->setNotifyUrl($notify_url);
  442. $request->setBizContent($bizcontent);
  443. //这里和普通的接口调用不同,使用的是sdkExecute
  444. $response = $aop->sdkExecute($request);
  445. // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
  446. //$this->success(__('获取成功'),$response);
  447. echo $response;
  448. }
  449. //支付宝回调
  450. public function notify()
  451. {
  452. if ($_POST) {
  453. //Db::transaction(function () {
  454. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  455. $out_trade_no = $_POST['out_trade_no'];//交易号
  456. $trade_no = $_POST['trade_no'];//订单支付时间
  457. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  458. $order = Db::name('task')->where('out_trade_no', $out_trade_no)->find();
  459. if ($order['paytype'] == 1) {
  460. $up = [
  461. 'paytype' => 2,
  462. 'payment' => $gmt_payment,
  463. 'trade_no' => $trade_no,
  464. 'buyer' => $_POST['buyer_logon_id'],
  465. 'buytype' => '支付宝',
  466. 'ftime' => time()
  467. ];
  468. if (Db::name('task')->where('out_trade_no', $out_trade_no)->update($up)) {
  469. echo 'success';
  470. }
  471. } else {
  472. echo 'success';
  473. }
  474. }
  475. //});
  476. }
  477. }
  478. public function notifycz()
  479. {
  480. if ($_POST) {
  481. //$this->dingding_log($_POST);
  482. //Db::transaction(function () {
  483. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  484. $out_trade_no = $_POST['out_trade_no'];//交易号
  485. $trade_no = $_POST['trade_no'];//订单支付时间
  486. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  487. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  488. //$this->dingding_log($order);
  489. if ($order['type'] == 1) {
  490. $up = [
  491. 'type' => 2,
  492. 'payment' => $gmt_payment,
  493. 'trade_no' => $trade_no,
  494. 'buyer' => $_POST['buyer_logon_id'],
  495. 'buytype' => '支付宝',
  496. 'ftime' => time()
  497. ];
  498. Db::startTrans();
  499. try {
  500. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  501. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  502. Db::commit();
  503. echo 'success';
  504. } catch (Exception $e) {
  505. Db::rollback();
  506. $this->error($e->getMessage());
  507. }
  508. } else {
  509. echo 'success';
  510. }
  511. }
  512. //});
  513. }
  514. }
  515. public function notifydsf()//第三方充值回调
  516. {
  517. $this->dingding_log($_REQUEST);
  518. //Db::transaction(function () {
  519. $returncode = isset($_REQUEST["returncode"]) ? $_REQUEST["returncode"] : 1;
  520. if ($returncode == "00") {//处理交易完成或者支付成功的通知 //获取订单号
  521. $out_trade_no = isset($_REQUEST["orderid"]) ? $_REQUEST["orderid"] : '';
  522. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  523. $this->dingding_log($order);
  524. if ($order['type'] == 1) {
  525. $up = [
  526. 'type' => 2,
  527. 'ftime' => time()
  528. ];
  529. Db::startTrans();
  530. try {
  531. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  532. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  533. Db::commit();
  534. echo 'OK';
  535. } catch (Exception $e) {
  536. Db::rollback();
  537. $this->error($e->getMessage());
  538. }
  539. } else {
  540. echo 'OK';
  541. }
  542. }
  543. //});
  544. }
  545. public function money($money, $user_id, $id, $sxf = 0)
  546. {
  547. $user = $this->Usermodel::get($user_id);
  548. if ($user && $money != 0) {
  549. $before = $user->money;
  550. $after = $user->money + $money;
  551. //更新会员信息
  552. $user->save(['money' => $after]);
  553. //写入日志
  554. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => $id, 'sxf' => $sxf, 'memo' => '用户充值']);
  555. } else {
  556. $this->error(__('金额不对'));
  557. }
  558. }
  559. public function dingding_log($content)
  560. {
  561. $r = $_SERVER['DOCUMENT_ROOT'] . '/api_log/' . date('Y-m-d_H-i-s', time()) . '.txt';
  562. $fp = fopen($r, 'w+');
  563. fwrite($fp, "执行日期:" . date('Y-m-d H:i:s', time()) . ' ' . var_export($content, true));
  564. fclose($fp);
  565. }
  566. public function FromXml($xml)
  567. {
  568. //将XML转为array
  569. //禁止引用外部xml实体
  570. libxml_disable_entity_loader(true);
  571. $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  572. return $values;
  573. }
  574. public function sharepay()
  575. {
  576. // $datavideo_order=Db::name('video_share')->where(['vid'=>input('id'),'uid'=>input('uid')])->find();
  577. // $datavideo_order1=Db::name('video_share')->where(['sid'=>input('id'),'uid'=>input('uid'),'lx'=>1])->find();
  578. $sad = 1;
  579. // $lx=input('lx');
  580. $num = input('num'); //购买的版权数量
  581. $uid = input('uid');
  582. $vid = input('vid');
  583. // 验证数量
  584. $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
  585. if ($user_num) {
  586. $cs_num = $user_num['share'] + $num;
  587. if ($cs_num > 10 || $num > 10) {
  588. $this->error(__('单剧集购买版权不能超过10份'));
  589. }
  590. }
  591. $user_share = Db::name('videolist')->where('id', $vid)->value('share');
  592. $user_shares = $user_share - $num;
  593. if ($user_shares < 0) {
  594. $this->error(__('该局总版权只剩下' .$user_share. '份'));
  595. }
  596. // if($datavideo_order){
  597. // if($datavideo_order['type']==2){
  598. // $this->error(__('该订单已支付'));
  599. // }
  600. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  601. // $price=input('priceq');
  602. // $pricebuy=input('pricebuy');
  603. // if(input('lx')==2){
  604. // $price=input('priced');
  605. // $pricebuy=input('pricebuy');
  606. // }
  607. // $up=[
  608. // 'type'=>1,
  609. // 'lx'=>$lx,//1 全章购买 2单集购买
  610. // 'uid'=>input('uid'),
  611. // 'name'=>input('namet'),
  612. // 'img'=>input('img'),
  613. // 'price'=>$price,
  614. // 'sum'=>input('sum'),
  615. // 'sid'=>input('pid'),
  616. // 'smid'=>input('smid'),
  617. // 'paytype'=>input('buytype'),
  618. // 'zprice'=>$pricebuy*input('sum'),
  619. // 'out_trade_no'=>$number,
  620. // ];
  621. // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
  622. // $id=$datavideo_order['id'];
  623. // $data=$up;
  624. // }else{
  625. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  626. // if (!input('pricebuy')) {
  627. // $this->error(__('金额不对'));
  628. // }
  629. $price = input('price');
  630. // $pricebuy = input('pricebuy');
  631. // if(input('lx')==2){
  632. // $price=input('priced');
  633. // $pricebuy=input('pricebuy');
  634. // }
  635. // 获取当前时间的时间戳
  636. $now = time();
  637. // 计算三年后的时间戳
  638. $threeYearsLater = strtotime('+3 years', $now);
  639. $vname = Db::name('videolist')->where('id', input('vid'))->value('name');
  640. $total_price = $price * $num;
  641. $up = [
  642. 'type' => 1,
  643. 'uid' => input('uid'),
  644. 'vid' => input('vid'),
  645. 'vname' => $vname,
  646. 'num' => input('num'),
  647. 'price' => $price,
  648. 'total_price' => $total_price,
  649. 'paytype' => input('buytype'),
  650. 'out_trade_no' => $number,
  651. 'createtime' => time(),
  652. 'endtime' => $threeYearsLater,
  653. 'updatetime' => time(),
  654. ];
  655. //$this->error($up);
  656. $id = Db::name('video_share')->insertGetId($up);
  657. $data = $up;
  658. // }
  659. $site = config('site');
  660. $amount = $price;
  661. // $amount = $data['zprice'];
  662. // if (!$amount) {
  663. // $this->error(__('金额不对'));
  664. // }
  665. // 异步通知地址
  666. $notify_url = $site['zfb']['notifyurl'];
  667. // 订单标题
  668. $subject = 'wool订单';
  669. // 订单详情
  670. $body = 'wool致力于打造最好的移动服务平台';
  671. // 订单号,示例代码使用时间值作为唯一的订单ID号
  672. //var_dump($data['paytype']);
  673. if ($data['paytype'] == '支付宝') {
  674. $this->allpay($amount, $subject, $body, $number, $notify_url);
  675. } else if ($data['paytype'] == '余额') {
  676. $this->shareyuepay($amount, input('uid'), $id);
  677. } else if ($data['paytype'] == '微信') {
  678. $wxlx = input('wxlx');
  679. if ($wxlx == 'wxxcx') {
  680. $openid = input('openid');
  681. if (!$openid) {
  682. $this->error(__('openid不对'));
  683. }
  684. $this->paywxxcx($id, $number, $amount, $openid);
  685. } else if ($wxlx == 'wxh5') {
  686. $buytype = '微信h5';
  687. $this->paywxh5($id, $number, $amount, $buytype);
  688. } else if ($wxlx == 'wxgzh') {
  689. $buytype = '微信公众号';
  690. $this->paywxh5($id, $number, $amount, $buytype);
  691. }else if ($data['paytype'] == '汇聚') {
  692. $paid_price = 0;
  693. // if ($recharId) {
  694. // $data = SystemGroupData::getDateValue($recharId);
  695. // if ($data === false) {
  696. // return app('json')->fail('您选择的充值方式已下架!');
  697. // } else {
  698. // $paid_price = $data['give_money'] ?? 0;
  699. // }
  700. // }
  701. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  702. $recharge = Huifu::wxJsPay(session('openid'),$number,$price,$paid_price,'share_pay');
  703. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  704. }
  705. }
  706. }
  707. // 判断订单的用户是否有上级以及判断推荐等级
  708. public function grade_sell($out_trade_no)
  709. {
  710. $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
  711. if ($order) {
  712. $user = Db::name('user')->where('id', $order['uid'])->find();
  713. while ($user['pid'] != 1) { //默认账号pid为1
  714. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  715. $grade = $user['grade'];
  716. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  717. if (empty($video_user)){ //如果上级没有购买过该视频版权
  718. $up = [
  719. 'vid' => $order['vid'],
  720. 'vname' => $order['vname'],
  721. 'uid' => $user['id'],
  722. 'share' => 0,
  723. 'contribution' => 0,
  724. 'income' => 0,
  725. 'createtime' => time(),
  726. ];
  727. $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
  728. }
  729. $contribution = 0;
  730. $check = 0;
  731. // $sell = $video_user['sell'] + $order['num'];
  732. $sell = $user['sell'] + $order['num'];
  733. // switch ($video_user['grade']) {
  734. switch ($user['grade']) {
  735. case 0:
  736. $contribution = 0.5 * $order['num'];
  737. if ($sell>=30){
  738. $check = 1;
  739. $sell = $sell - 30;
  740. }
  741. break;
  742. case 1:
  743. $contribution = 0.2 * $order['num'];
  744. if ($sell>=300){
  745. $check = 1;
  746. $sell = $sell - 300;
  747. }
  748. break;
  749. case 2:
  750. $contribution = 0.1 * $order['num'];
  751. if ($sell>=3000){
  752. $check = 1;
  753. $sell = $sell - 3000;
  754. }
  755. break;
  756. case 3:
  757. $contribution = 0.05 * $order['num'];
  758. default:
  759. }
  760. if ($check == 1){
  761. // $grade = $video_user['grade'] + 1;
  762. $grade = $user['grade'] + 1;
  763. }
  764. $after = $video_user['contribution'] + $contribution;
  765. // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  766. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
  767. $res2 = Db::name('user')->where('id', $user['id'])->update(['grade'=>$grade,'sell' => $sell]);
  768. $up = [
  769. 'vid' => $order['vid'],
  770. 'vname' => $order['vname'],
  771. 'uid' => $user['id'],
  772. 'link_id' => $order['id'],
  773. 'num' => $contribution,
  774. 'after' => $contribution,
  775. 'content' => '下级用户购买版权,获得贡献值',
  776. 'createtime' => time(),
  777. 'pm' => 0,
  778. ];
  779. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  780. }
  781. }
  782. }
  783. // 添加贡献值记录 订单id
  784. public function share_record($id){
  785. $order = Db::name('video_share')->where('id', $id)->find();
  786. if ($order){
  787. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  788. $contribution = $order['num'] +$user['contribution'];
  789. $up = [
  790. 'vid' => $order['vid'],
  791. 'vname' => $order['vname'],
  792. 'uid' => $user['id'],
  793. 'link_id' => $order['id'],
  794. 'num' => $order['num'],
  795. 'after' => $contribution,
  796. 'content' => '用户购买版权,获得贡献值',
  797. 'createtime' => time(),
  798. 'pm' => 0,
  799. ];
  800. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  801. $share = $user['share'] + $order['num'];
  802. // 添加版权和贡献值
  803. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution,'share'=>$share]);
  804. }
  805. }
  806. public function shareyuepay($money, $user_id, $id)//版权余额支付
  807. {
  808. $user = $this->Usermodel::get($user_id);
  809. if ($user && $money != 0) {
  810. $data = Db::name('video_share')->where('id', $id)->find();
  811. if ($user->money < $money) {
  812. $this->error(__('余额不足'));
  813. }
  814. $before = $user->money;
  815. $after = $user->money - $money;
  816. if ($data['type'] == 1) {
  817. Db::startTrans();
  818. try {
  819. //更新会员信息
  820. $user->save(['money' => $after]);
  821. //写入日志
  822. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  823. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  824. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  825. if (empty($video_user)){ //判断用户是否购买过该视频版权
  826. $up = [
  827. 'vid' => $data['vid'],
  828. 'vname' => $data['vname'],
  829. 'uid' => $data['uid'],
  830. 'share' => $data['num'],
  831. 'contribution' => $data['num'],
  832. 'income' => 0,
  833. 'createtime' => time(),
  834. ];
  835. $id = Db::name('VideoUser')->insertGetId($up);
  836. }
  837. $this->share_record($id);//添加贡献值记录
  838. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  839. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  840. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  841. // 扣除短剧总版权
  842. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  843. $share = $shares - $data['num'];
  844. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  845. Db::commit();
  846. $this->success(__('操作成功'));
  847. } catch (Exception $e) {
  848. Db::rollback();
  849. $this->error($e->getMessage());
  850. }
  851. } else if ($data['type'] > 1) {
  852. $this->error(__('已经支付'));
  853. }
  854. } else {
  855. $this->error(__('金额不对'));
  856. }
  857. }
  858. }