Money.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use think\Db;
  5. /**
  6. * 钱包接口
  7. */
  8. class Money extends Common
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. /**
  17. * 充值
  18. */
  19. public function recharge() {
  20. $purpose = $this->request->request("purpose");// 充值用途 1=认证,2=有眼缘,3=购买会员, 4=充值钻石
  21. $type = $this->request->request("type","wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  22. $method = "app";
  23. if(!in_array($purpose,[1,2,3,4]) || !in_array($type,['wechat','alipay'])) {
  24. $this->error(__('Invalid parameters'));
  25. }
  26. // if($type == 'wechat') $this->error(__('微信支付暂未开通!请选择支付宝支付!'));
  27. $user_id = $this->auth->id;
  28. $fate_user_id = 0;
  29. $vip_config_id = 0;
  30. switch ($purpose) {
  31. case 1:
  32. $title = "实名认证支付";
  33. $amount = config("site.auth");
  34. break;
  35. case 2:
  36. $fate_user_id = $this->request->request("fate_user_id",0); // 有眼缘用户ID
  37. if(!$fate_user_id) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $title = "有眼缘支付";
  41. $amount = config("site.fate");
  42. break;
  43. case 3:
  44. $vip_config_id = $this->request->request("vip_config_id",0);
  45. if(!$vip_config_id) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. $title = "会员开通支付";
  49. // 获取会员配置信息
  50. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  51. $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
  52. if (!$vipConfigInfo) {
  53. $this->error('网络延迟,请稍后再试');
  54. }
  55. $amount = $vipConfigInfo['real_price'];
  56. break;
  57. case 4:
  58. $vip_config_id = $this->request->request("vip_config_id",0);
  59. if(!$vip_config_id) {
  60. $this->error(__('Invalid parameters'));
  61. }
  62. $title = "钻石充值支付";
  63. // 获取会员配置信息
  64. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  65. $vipConfigInfo = Db::name('diamond')->where(['id'=>$vip_config_id])->find();
  66. if (!$vipConfigInfo) {
  67. $this->error('网络延迟,请稍后再试');
  68. }
  69. $amount = $vipConfigInfo['price'];
  70. break;
  71. }
  72. $out_trade_no = date("YmdHis").rand(100000,999999);
  73. $notifyurl = request()->root(true) . '/api/notify/notify/paytype/' . $type;
  74. $returnurl = request()->root(true) . '/addons/epay/api/returnx/type/' . $type;
  75. if (!$amount || $amount < 0) {
  76. $this->error("支付金额必须大于0");
  77. }
  78. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  79. $this->error("支付类型错误");
  80. }
  81. $time = time();
  82. $money = $type == 'alipay' ? (float)bcadd($amount,0,2) : (float)bcadd($amount,0,2);
  83. // 生成订单
  84. $recharOrderMode = new \app\common\model\RecharOrder();
  85. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `vip_config_id`, `fate_user_id`, `money` ,`purpose`, `pay_type`, `createtime`) VALUES ($user_id , '$out_trade_no' , $vip_config_id, $fate_user_id, $money ,$purpose, '$type', $time)");
  86. if(!$orderid) $this->error("订单创建失败!");
  87. $params = [
  88. 'type' => $type,
  89. 'orderid' => $out_trade_no,
  90. 'title' => $title,
  91. 'amount' => $money,
  92. 'method' => $method,
  93. 'notifyurl' => $notifyurl,
  94. 'returnurl' => $returnurl,
  95. ];
  96. $result = Service::submitOrder($params);
  97. $this->success("参数获取成功!",$result);
  98. }
  99. /**
  100. * 提现
  101. */
  102. public function withdrow() {
  103. $money = $this->request->request("money");// 申请提现的金额
  104. $withdrawMax = config('site.withdrawMax'); // 最高提现金额
  105. $withdrawCount = config('site.withdrawCount'); // 每天提现次数
  106. $withdrawallogModel = new \app\common\model\UserWithdrawLog();
  107. if($money <= 0 || $money > $withdrawMax) {
  108. $this->error(__('提现金额范围:10-'.$withdrawMax));
  109. }
  110. if(intval($money) != $money) {
  111. $this->error(__('提现金额必须整数!'));
  112. }
  113. if($money%10 !== 0) {
  114. $this->error(__('提现金额必须为10的倍数!'));
  115. }
  116. // 获取今天的提现次数
  117. $starttime = strtotime(date("Y-m-d 00:00:00"));
  118. $endtime = strtotime(date("Y-m-d 23:59:59"));
  119. $where = [];
  120. $where["user_id"] = $this->auth->id;
  121. $where["status"] = ['in',[0,1]]; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  122. $where["createtime"] = ["between","$starttime,$endtime"];
  123. $withdrawCountInfo = $withdrawallogModel->where($where)->count('id');
  124. if($withdrawCountInfo >= $withdrawCount) {
  125. $this->error(__('今日提现次数已达上线!'));
  126. }
  127. // 判断当前用户是否实名认证
  128. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  129. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  130. // 查询资金余额
  131. $userModel = new \app\common\model\User();
  132. $userInfo = $userModel->get($this->auth->id);
  133. if($money > $userInfo["money"]) {
  134. $this->error("资金余额不足!");
  135. }
  136. Db::startTrans();
  137. try{
  138. // 减去用户可用资金余额
  139. $res1 = $userModel->where(["id"=>$this->auth->id])->setDec("money",$money);
  140. // 增加用户冻结资金余额
  141. $res2 = $userModel->where(["id"=>$this->auth->id])->setInc("frozen",$money);
  142. // 新增用户提现记录申请
  143. $data = [];
  144. $data["user_id"] = $this->auth->id;
  145. $data["money"] = $money;
  146. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  147. $data["createtime"] = time();
  148. $res3 = $withdrawallogModel->insert($data);
  149. if($res1 && $res2 && $res3) {
  150. Db::commit();
  151. $this->success("提现申请发送成功!");
  152. } else {
  153. $this->error("操作失败!");
  154. }
  155. }catch (ValidateException $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. } catch (PDOException $e) {
  159. Db::rollback();
  160. $this->error($e->getMessage());
  161. } catch (Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. }
  166. /**
  167. * 获取用户提现记录
  168. */
  169. public function withdrowRecord() {
  170. $page = $this->request->request('page',1); // 分页
  171. $pageNum = $this->request->request('pageNum',10); // 分页
  172. // 分页搜索构建
  173. $pageStart = ($page-1)*$pageNum;
  174. $res = \app\common\model\UserWithdrawLog::where(["user_id"=>$this->auth->id])->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  175. if($res) {
  176. $statusTxt = array("-1"=>"审核拒绝","0"=>"待审核","1"=>"审核通过");
  177. foreach($res as $k => &$v) {
  178. $v["status"] = $statusTxt[$v["status"]];
  179. $v["money"] = $v["money"]>0?$v["money"]:0;
  180. $v["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  181. }
  182. }
  183. $this->success("获取成功!",$res);
  184. }
  185. /**
  186. * 获取收入明细
  187. */
  188. public function getRebateList() {
  189. $page = $this->request->request('page',1); // 分页
  190. $pageNum = $this->request->request('pageNum',10); // 分页
  191. // 分页搜索构建
  192. $pageStart = ($page-1)*$pageNum;
  193. $res = [];
  194. $res['money_count']= \app\common\model\User::where(["id"=>$this->auth->id])->value("money");
  195. $where = [];
  196. $where["user_id"] = $this->auth->id;
  197. $res['money_log'] = \app\common\model\UserProfitLog::where($where)->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  198. if($res['money_log']) foreach($res['money_log'] as $k => &$v) {
  199. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  200. }
  201. $this->success("获取成功!",$res);
  202. }
  203. //获取钻石明细
  204. public function getdiamondlog() {
  205. $page = $this->request->request('page', 1, 'intval'); // 分页
  206. $pageNum = $this->request->request('pageNum', 10, 'intval'); // 分页
  207. $list = Db::name('user_diamond_log')->where(['user_id' => $this->auth->id])->order('id', 'desc')->page($page, $pageNum)->select();
  208. if ($list) {
  209. foreach ($list as &$v) {
  210. $v['createtime'] = date("Y-m-d H:i:s", $v["createtime"]);
  211. }
  212. }
  213. $this->success('获取钻石明细', $list);
  214. }
  215. //苹果内购充值钻石
  216. public function iospay() {
  217. $purpose = 4;//$this->request->request("purpose");// 充值用途 1=认证,2=有眼缘,3=购买会员, 4=充值钻石
  218. // $type = $this->request->request("type","wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  219. // $method = "app";
  220. // if(!in_array($purpose,[1,2,3,4]) || !in_array($type,['wechat','alipay'])) {
  221. // $this->error(__('Invalid parameters'));
  222. // }
  223. if(!in_array($purpose,[1,2,3,4])) {
  224. $this->error(__('Invalid parameters'));
  225. }
  226. // if($type == 'wechat') $this->error(__('微信支付暂未开通!请选择支付宝支付!'));
  227. $user_id = $this->auth->id;
  228. $fate_user_id = 0;
  229. $vip_config_id = 0;
  230. switch ($purpose) {
  231. case 1:
  232. $title = "实名认证支付";
  233. $amount = config("site.auth");
  234. break;
  235. case 2:
  236. $fate_user_id = $this->request->request("fate_user_id",0); // 有眼缘用户ID
  237. if(!$fate_user_id) {
  238. $this->error(__('Invalid parameters'));
  239. }
  240. $title = "有眼缘支付";
  241. $amount = config("site.fate");
  242. break;
  243. case 3:
  244. $vip_config_id = $this->request->request("vip_config_id",0);
  245. if(!$vip_config_id) {
  246. $this->error(__('Invalid parameters'));
  247. }
  248. $title = "会员开通支付";
  249. // 获取会员配置信息
  250. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  251. $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
  252. if (!$vipConfigInfo) {
  253. $this->error('网络延迟,请稍后再试');
  254. }
  255. $amount = $vipConfigInfo['real_price'];
  256. break;
  257. case 4:
  258. $vip_config_id = $this->request->request("vip_config_id",0);
  259. if(!$vip_config_id) {
  260. $this->error(__('Invalid parameters'));
  261. }
  262. $title = "钻石充值支付";
  263. // 获取会员配置信息
  264. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  265. $vipConfigInfo = Db::name('diamond_ios')->where(['id'=>$vip_config_id])->find();
  266. if (!$vipConfigInfo) {
  267. $this->error('网络延迟,请稍后再试');
  268. }
  269. $amount = $vipConfigInfo['price'];
  270. break;
  271. }
  272. $out_trade_no = date("YmdHis").rand(100000,999999);
  273. if (!$amount || $amount < 0) {
  274. $this->error("支付金额必须大于0");
  275. }
  276. $time = time();
  277. $money = $amount;
  278. $type = '苹果内购';
  279. // 生成订单
  280. $recharOrderMode = new \app\common\model\RecharOrder();
  281. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `vip_config_id`, `fate_user_id`, `money` ,`purpose`, `pay_type`, `createtime`) VALUES ($user_id , '$out_trade_no' , $vip_config_id, $fate_user_id, $money ,$purpose, '$type', $time)");
  282. if(!$orderid) $this->error("订单创建失败!");
  283. $this->success("参数获取成功!", $out_trade_no);
  284. }
  285. // 内购支付回调
  286. public function iosresult() {
  287. //苹果内购的验证收据
  288. $receipt_data = input('apple_receipt', '', 'trim');
  289. $order_no = input('order_no', '', 'trim');
  290. //error_log(print_r($receipt_data, 1), 3, './receipt_data.txt');
  291. if (!$receipt_data || !$order_no) {
  292. $this->error('缺少参数');
  293. }
  294. // 查找订单
  295. $Order = Db::name('rechar_order');
  296. $order_info = $Order->where(array('order_no' => $order_no, 'user_id' => $this->auth->id))->find();
  297. if (!$order_info) {
  298. //error_log(print_r(11, 1), 3, './11.txt');
  299. $this->error('订单丢失');
  300. }
  301. if ($order_info['status'] == 1) {
  302. $this->success('充值成功');
  303. }
  304. //查询bundle_id
  305. $vipConfigInfo = Db::name('diamond_ios')->where(['id' => $order_info['vip_config_id']])->find();
  306. if (!$vipConfigInfo) {
  307. $this->error('数据丢失');
  308. }
  309. // 验证支付状态
  310. $result = $this->validate_apple_pay($receipt_data);
  311. if (!$result['status']) {
  312. // 验证不通过
  313. //error_log(print_r($result, 1), 3, './result.txt');
  314. $this->error($result['message']);
  315. }
  316. $count = count($result['data']['receipt']['in_app']);
  317. $use_count = $count - 1;
  318. //p($apple_id);p($result['data']['receipt']['in_app'][$use_count]['product_id']);die;
  319. // if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != 'com.lnkj.authentication.3') {
  320. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $vipConfigInfo['product_id']) {
  321. $this->error('非法请求,请立刻停止');
  322. }
  323. $res3 = true;
  324. Db::startTrans();
  325. // 获取钻石配置信息
  326. $userInfo = Db::name('user')->where(['id' => $order_info['user_id']])->find();
  327. // 修改订单状态
  328. $res1 = $Order->where(["order_no"=>$order_no, 'user_id' => $this->auth->id, 'status' => 0])->setField(["status"=>1,"updatetime"=>time()]);
  329. //修改用户钻石余额
  330. $diamond = $userInfo['diamond'] + $vipConfigInfo['number'];
  331. $res2 = Db::name('user')->where(['id' => $userInfo['id'], 'diamond' => $userInfo['diamond']])->setField('diamond', $diamond);
  332. // 添加钻石明细
  333. $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $userInfo['id']])->order('id', 'desc')->find();
  334. if (!$diamond_log && $userInfo['diamond'] > 0) {
  335. $res3 = false;
  336. }
  337. if ($diamond_log && $diamond_log['after'] != $userInfo['diamond']) {
  338. $res3 = false;
  339. }
  340. if ($res3) {
  341. $data = [];
  342. $data['user_id'] = $userInfo['id'];
  343. $data['diamond'] = $vipConfigInfo['number'];
  344. $data['before'] = $userInfo['diamond'];
  345. $data['after'] = $diamond;
  346. $data['memo'] = '充值';
  347. $data['createtime'] = time();
  348. $res3 = Db::name('user_diamond_log')->insertGetId($data);
  349. }
  350. if($res1 && $res2 !== false && $res3) {
  351. Db::commit();
  352. $this->success('充值成功');
  353. } else {
  354. Db::rollback();
  355. $this->error('充值失败');
  356. }
  357. }
  358. /**
  359. * 验证AppStore内付
  360. * @param string $receipt_data 付款后凭证
  361. * @return array 验证是否成功
  362. */
  363. function validate_apple_pay($receipt_data = '') {
  364. // 验证参数
  365. if (strlen($receipt_data) < 20) {
  366. $result = array(
  367. 'status' => false,
  368. 'message' => '非法参数'
  369. );
  370. return $result;
  371. }
  372. // 请求验证
  373. $html = $this->curl($receipt_data);
  374. $data = json_decode($html, true);
  375. // p($data);die;
  376. if ($data['status'] == '21002') {
  377. $this->error('21002');
  378. }
  379. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  380. // if ($data['status'] == '21007') {
  381. // // 请求验证
  382. // $html = $this->curl($receipt_data, 1);
  383. // $data = json_decode($html, true);
  384. // $data['sandbox'] = '1';
  385. // }
  386. if (isset($_GET['debug'])) {
  387. exit(json_encode($data));
  388. }
  389. // if ($data['receipt']['bundle_id'] != 'com.liuniukeji.mayivideo') {
  390. if ($data['receipt']['bundle_id'] != 'com.nuoruiyang.fate') {
  391. $result = array(
  392. 'status' => false,
  393. 'message' => '非法请求',
  394. 'data' => $data
  395. );
  396. return $result;
  397. }
  398. // 判断是否购买成功
  399. if (intval($data['status']) === 0) {
  400. $result = array(
  401. 'status' => true,
  402. 'message' => '购买成功',
  403. 'data' => $data
  404. );
  405. } else {
  406. $result = array(
  407. 'status' => false,
  408. 'message' => '购买失败 status:' . $data['status']
  409. );
  410. }
  411. return $result;
  412. }
  413. /**
  414. * 21000 App Store不能读取你提供的JSON对象25
  415. * 21002 receipt-data域的数据有问题
  416. * 21003 receipt无法通过验证
  417. * 21004 提供的shared secret不匹配你账号中的shared secret
  418. * 21005 receipt服务器当前不可用
  419. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  420. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  421. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  422. */
  423. function curl($receipt_data, $sandbox = 0) {
  424. //小票信息
  425. $POSTFIELDS = array("receipt-data" => $receipt_data);
  426. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  427. // $POSTFIELDS = '{' . '"receipt-data":' . '"' . $receipt_data . '"' .'}';
  428. // p($POSTFIELDS);die;
  429. //正式购买地址 沙盒购买地址
  430. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  431. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  432. // if ($sandbox > 0) {
  433. if (config('site.ios_pay_sandbox') > 0) {
  434. $url = $url_buy;
  435. } else {
  436. $url = $url_sandbox;
  437. }
  438. // 上架通过直接使用正式地址
  439. // $url = $url_sandbox;
  440. $ch = curl_init($url);
  441. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  442. curl_setopt($ch, CURLOPT_POST, true);
  443. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  444. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  445. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  446. $response = curl_exec($ch);
  447. $errno = curl_errno($ch);
  448. curl_close($ch);
  449. if ($errno != 0) {
  450. return $errno;
  451. } else {
  452. return $response;
  453. }
  454. }
  455. }