Pay.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 充值配置与充值订单
  8. */
  9. class Pay extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //vip用的
  14. public function vip_config(){
  15. $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
  16. $data['vipconfig'] = $list;
  17. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  18. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  19. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  20. $this->success('success',$data);
  21. }
  22. //vip用的,创建订单
  23. public function vip_recharge(){
  24. $rc_id = input('rc_id',0);
  25. $pay_type = input('pay_type','wechat');
  26. $platform = 'app';
  27. $uid = $this->auth->id;
  28. if(!$rc_id){
  29. $this->error('请选择会员套餐');
  30. }
  31. if(!$this->user_auth_limit()){
  32. $this->error('请先完成实名认证');
  33. }
  34. //赋值money
  35. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  36. $money = $recharge_config['money'];
  37. if($money<=0)
  38. {
  39. $this->error('支付金额必须大于0');
  40. }
  41. if($money > 10000){
  42. $this->error('支付金额太大');
  43. }
  44. //创建订单
  45. $data = [];
  46. $data['user_id'] = $uid;
  47. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  48. $data['order_amount'] = $money;
  49. $data['createtime'] = time();
  50. $data['pay_type'] = $pay_type;
  51. $data['platform'] = $platform;
  52. $data['order_status'] = 0;
  53. $data['table_name'] = 'vip_recharge';
  54. $data['table_id'] = 0;
  55. $data['args'] = json_encode(['days'=>$recharge_config['days']]);
  56. $orderid = Db::name('pay_order')->insertGetId($data);
  57. $openid = $this->auth->wechat_openid;
  58. //下单
  59. $params = [
  60. 'type' => $pay_type,
  61. 'orderid' => $data['out_trade_no'],
  62. 'title' => '支付订单',
  63. 'amount' => $data['order_amount'],
  64. 'method' => $platform,
  65. 'openid' => $openid,
  66. 'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
  67. 'returnurl' => '',
  68. ];
  69. $res = Service::submitOrder($params);
  70. if($pay_type == 'wechat'){
  71. $this->success('success',json_decode($res,true));
  72. }else{
  73. $this->success('success',$res);
  74. }
  75. }
  76. //金币充值
  77. public function gold_config(){
  78. $list = Db::name('paygold_webcon')->order('weigh asc,id asc')->select();
  79. $data['goldconfig'] = $list;
  80. $wallet = model('wallet')->getWallet($this->auth->id);
  81. $data['wallet'] = $wallet;
  82. $this->success('success',$data);
  83. }
  84. //充值金币 创建订单
  85. public function gold_recharge(){
  86. $rc_id = input_post('rc_id',0);
  87. $pay_type = input_post('pay_type','wechat');
  88. $platform = 'app';
  89. // $freemoney = input_post('freemoney',0);
  90. $freemoney = 0;
  91. $uid = $this->auth->id;
  92. if(!$rc_id && !$freemoney){
  93. $this->error('请选择或填写充值金额');
  94. }
  95. if(!$this->user_auth_limit()){
  96. $this->error('请先完成实名认证');
  97. }
  98. //赋值money
  99. if($rc_id){
  100. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  101. $money = $recharge_config['money'] ?: 0;
  102. $gold = $recharge_config['gold'] ?: 0;
  103. }
  104. //自由输入覆盖
  105. if(!empty($freemoney)){
  106. $rc_id = 0;
  107. $money = floatval($freemoney);
  108. $bili = config('rmb_to_gold') ?: 10;
  109. $gold = bcmul($money,$bili,0);
  110. }
  111. //
  112. if($money<=0)
  113. {
  114. $this->error('支付金额必须大于0');
  115. }
  116. if($money > 10000){
  117. $this->error('支付金额太大');
  118. }
  119. //充值上限
  120. $limit_rs = $this->limit_gift($money);
  121. if($limit_rs !== true){
  122. $this->error($limit_rs);
  123. }
  124. //创建订单
  125. $data = [];
  126. $data['user_id'] = $uid;
  127. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  128. $data['order_amount'] = $money;
  129. $data['createtime'] = time();
  130. $data['pay_type'] = $pay_type;
  131. $data['platform'] = $platform;
  132. $data['order_status'] = 0;
  133. $data['table_name'] = 'gold_recharge';
  134. $data['table_id'] = 0;
  135. $data['args'] = json_encode(['gold'=>$gold]);
  136. $orderid = Db::name('pay_order')->insertGetId($data);
  137. $openid = $this->auth->wechat_openid;
  138. //下单
  139. $params = [
  140. 'type' => $pay_type,
  141. 'orderid' => $data['out_trade_no'],
  142. 'title' => '支付订单',
  143. 'amount' => $data['order_amount'],
  144. 'method' => $platform,
  145. 'openid' => $openid,
  146. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  147. 'returnurl' => '',
  148. ];
  149. $res = Service::submitOrder($params);
  150. if($pay_type == 'wechat'){
  151. $this->success('success',json_decode($res,true));
  152. }else{
  153. $this->success('success',$res);
  154. }
  155. }
  156. //充值上限
  157. public function limit_gift($money){
  158. $limit_info = Db::name('gift_limit')->where('level',$this->auth->wealth_level)->find();
  159. if(empty($limit_info)){
  160. return true;
  161. }
  162. //今天的
  163. $today = date('Y-m-d');
  164. $starttime = strtotime($today);
  165. $endtime = $starttime + 86399;
  166. $today_sum = Db::name('pay_order')
  167. ->where('user_id',$this->auth->id)
  168. ->where('createtime','BETWEEN',[$starttime,$endtime])
  169. ->where('table_name','gold_recharge')
  170. ->where('order_status',1)
  171. ->sum('order_amount');
  172. if($today_sum + $money >= $limit_info['recharge_day']){
  173. return '您今天已经达到充值上限';
  174. }
  175. //总共的
  176. $total_sum = Db::name('pay_order')
  177. ->where('user_id',$this->auth->id)
  178. ->where('table_name','gold_recharge')
  179. ->where('order_status',1)
  180. ->sum('order_amount');
  181. if($total_sum + $money >= $limit_info['recharge_total']){
  182. return '您今天已经达到充值上限';
  183. }
  184. return true;
  185. }
  186. }