Pay.php 7.7 KB

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