Pay.php 7.7 KB

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