Pay.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. $level = input('level',0,'intval');
  16. $where = [];
  17. if($level){
  18. $where['vip_level'] = $level;
  19. }
  20. $list = Db::name('payvip_config')->where('is_show',1)->where($where)->order('weight asc,id asc')->select();
  21. foreach ($list as &$v) {
  22. $diff_price = '';
  23. if($v['info'] > $v['money']){
  24. $diff_price = '立减'.bcsub($v['info'],$v['money'],0).'元';
  25. }
  26. $v['diff_price'] = $diff_price;
  27. }
  28. $data['vipconfig'] = $list;
  29. $user_wallet = model('wallet')->getWallet($this->auth->id);
  30. $data['vip_endtime'] = $user_wallet['vip_endtime'];
  31. $data['vip_level'] = $user_wallet['vip_level'];
  32. $data['is_vip'] = $this->is_vip($user_wallet['vip_endtime'],$user_wallet['vip_level']);
  33. $this->success('success',$data);
  34. }
  35. //vip用的,创建订单
  36. public function vip_recharge()
  37. {
  38. $rc_id = input('rc_id',0);
  39. $pay_type = input('pay_type','wechat');
  40. $uid = $this->auth->id;
  41. if (!in_array($pay_type, ['wechat', 'alipay'])) {
  42. $this->error('错误的支付类型');
  43. }
  44. if(!$rc_id){
  45. $this->error('请选择会员套餐');
  46. }
  47. //赋值money
  48. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  49. if(empty($recharge_config)){
  50. $this->error('不存在的充值金额');
  51. }
  52. $money = $recharge_config['money'];
  53. if($money <= 0) {
  54. $this->error('支付金额必须大于0');
  55. }
  56. if($money > 10000){
  57. $this->error('支付金额太大');
  58. }
  59. //会员等级冲突
  60. //当前是会员,但是却要向下级续费,直接提示报错
  61. //修改等级,向上立刻改,向下不允许
  62. $wallet_info = model('wallet')->getWallet($this->auth->id);
  63. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  64. $this->error('当前会员没有过期,不能降级续费');
  65. }
  66. //创建订单
  67. $data = [];
  68. $data['status'] = 0;
  69. $pay_no = createUniqueNo('V',$uid);
  70. $data['pay_no'] = $pay_no;
  71. $data['money'] = $money;
  72. $data['payment_class'] = $pay_type;
  73. $data['type'] = 'recharge_vip';
  74. $data['pay_type'] = $pay_type;
  75. $data['user_id'] = $uid;
  76. $data['memo'] = '充值会员支付';
  77. $data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  78. $data['createtime'] = time();
  79. $data['payment'] = 'app';
  80. $pay_order = Db::name('pay_order')->insertGetId($data);
  81. if (!$pay_order) {
  82. $this->error('下单失败');
  83. }
  84. //下单
  85. $params = [
  86. 'type' => $pay_type,
  87. 'orderid' => $pay_no,
  88. 'title' => $data['memo'],
  89. 'amount' => $data['money'],
  90. //'amount' => 0.01,
  91. 'method' => 'app',
  92. 'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
  93. 'returnurl' => '',
  94. ];
  95. $res = Service::submitOrder($params);
  96. if($pay_type == 'wechat'){
  97. $this->success('success',json_decode($res,true));
  98. }else{
  99. $this->success('success',$res);
  100. }
  101. }
  102. //金币充值
  103. public function gold_config(){
  104. $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
  105. $data['rmb_to_gold'] = config('site.rmb_to_gold');
  106. $data['is_first'] = Db::name('user_paygold_log')->where(['uid' => $this->auth->id])->count('id');
  107. $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
  108. if ($list) {
  109. $arr = [
  110. 'id' => -1,
  111. 'money' => 0,
  112. 'gold' => 0,
  113. 'title' => '自定义',
  114. 'is_show' => 1,
  115. 'weight' => 1,
  116. 'first_gold' => 0,
  117. 'first_vipdays' => 0,
  118. 'vip_gold' => 0,
  119. 'is_default' => 0,
  120. ];
  121. array_push($list, $arr);
  122. foreach ($list as &$v) {
  123. $v['is_first'] = $data['is_first'];
  124. }
  125. }
  126. $data['goldconfig'] = $list;
  127. $this->success('success',$data);
  128. }
  129. //充值金币 创建订单
  130. public function gold_recharge()
  131. {
  132. $rc_id = input_post('rc_id',0);
  133. $pay_type = input_post('pay_type','wechat');
  134. $freemoney = input_post('freemoney', 0, 'intval');
  135. $uid = $this->auth->id;
  136. if(!$rc_id && !$freemoney){
  137. $this->error('请选择或填写充值金额');
  138. }
  139. if (!in_array($pay_type,['wechat','alipay'])) {
  140. $this->error('错误的支付类型');
  141. }
  142. //赋值money
  143. if($rc_id){
  144. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  145. if(empty($recharge_config)){
  146. $this->error('不存在的充值金额');
  147. }
  148. $money = $recharge_config['money'];
  149. $gold = $recharge_config['gold'];
  150. $first_gold = $recharge_config['first_gold'];
  151. $first_vipdays = $recharge_config['first_vipdays'];
  152. $vip_gold = $recharge_config['vip_gold'];
  153. }
  154. //自由输入覆盖
  155. if(!empty($freemoney)){
  156. $rc_id = 0;
  157. $money = floatval($freemoney);
  158. $bili = config('site.rmb_to_gold') ?: 10;
  159. $gold = bcmul($money,$bili,0);
  160. $first_gold = 0;
  161. $first_vipdays = 0;
  162. $vip_gold = 0;
  163. }
  164. if($money <= 0) {
  165. $this->error('支付金额必须大于0');
  166. }
  167. if($money > 10000){
  168. $this->error('支付金额太大');
  169. }
  170. //查询是不是会员,若不是则不赠送金币
  171. $user_wallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  172. $is_vip = $this->is_vip($user_wallet['vip_endtime'],$user_wallet['vip_level']);
  173. if ($is_vip == 0) {
  174. $vip_gold = 0;
  175. }
  176. //创建订单
  177. $data = [];
  178. $data['status'] = 0;
  179. $pay_no = createUniqueNo('P',$uid);
  180. $data['pay_no'] = $pay_no;
  181. $data['money'] = $money;
  182. $data['payment_class'] = $pay_type;
  183. $data['type'] = 'recharge_gold';
  184. $data['pay_type'] = $pay_type;
  185. $data['user_id'] = $uid;
  186. $data['memo'] = '充值金币支付';
  187. $data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  188. $data['createtime'] = time();
  189. $data['payment'] = 'app';
  190. $pay_order = Db::name('pay_order')->insertGetId($data);
  191. if (!$pay_order) {
  192. $this->error('下单失败');
  193. }
  194. //下单
  195. $params = [
  196. 'type' => $pay_type,
  197. 'orderid' => $pay_no,
  198. 'title' => $data['memo'],
  199. 'amount' => $data['money'],
  200. //'amount' => 0.01,
  201. 'method' => 'app',
  202. 'notifyurl' => config('pay_notify_url').'/api/notify/gold_notify_base/paytype/'.$pay_type,
  203. 'returnurl' => '',
  204. ];
  205. $res = Service::submitOrder($params);
  206. if($pay_type == 'wechat'){
  207. $this->success('success',json_decode($res,true));
  208. }else{
  209. $this->success('success',$res);
  210. }
  211. }
  212. }