Pay.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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->mini_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. $data['money_to_gold'] = config('site.money_to_gold');
  83. $this->success('success',$data);
  84. }
  85. //充值金币 创建订单
  86. public function gold_recharge(){
  87. $rc_id = input_post('rc_id',0);
  88. $pay_type = input_post('pay_type','wechat');
  89. $platform = 'app';
  90. $freemoney = input_post('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('site.money_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. $data = [];
  121. $data['user_id'] = $uid;
  122. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  123. $data['order_amount'] = $money;
  124. $data['createtime'] = time();
  125. $data['pay_type'] = $pay_type;
  126. $data['platform'] = $platform;
  127. $data['order_status'] = 0;
  128. $data['table_name'] = 'gold_recharge';
  129. $data['table_id'] = 0;
  130. $data['args'] = json_encode(['gold'=>$gold]);
  131. $orderid = Db::name('pay_order')->insertGetId($data);
  132. // $openid = $this->auth->mini_openid;
  133. //下单
  134. $params = [
  135. 'type' => $pay_type,
  136. 'orderid' => $data['out_trade_no'],
  137. 'title' => '支付订单',
  138. 'amount' => $data['order_amount'],
  139. 'method' => $platform,
  140. // 'openid' => $openid,
  141. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  142. 'returnurl' => '',
  143. ];
  144. $res = Service::submitOrder($params);
  145. if($pay_type == 'wechat'){
  146. $this->success('success',json_decode($res,true));
  147. }else{
  148. $this->success('success',$res);
  149. }
  150. }
  151. //收益兑换金币 配置
  152. public function moneytogold_config(){
  153. $list = Db::name('moneytogold_config')->order('weigh asc,id asc')->select();
  154. $data['goldconfig'] = $list;
  155. $wallet = model('wallet')->getWallet($this->auth->id);
  156. $data['wallet'] = $wallet;
  157. $data['money_to_gold'] = config('site.money_to_gold');
  158. $data['remark'] = config('site.money_to_gold_rule');
  159. $this->success('success',$data);
  160. }
  161. //收益兑换金币 兑换
  162. public function moneytogold_exchange(){
  163. $rc_id = input_post('rc_id',0);
  164. $freemoney = input_post('freemoney',0);
  165. if(!$rc_id && !$freemoney){
  166. $this->error('请选择或填写兑换金额');
  167. }
  168. $uid = $this->auth->id;
  169. //赋值money
  170. if($rc_id){
  171. $recharge_config = Db::name('moneytogold_config')->where('id',$rc_id)->find();
  172. $money = $recharge_config['money'] ?: 0;
  173. $gold = $recharge_config['gold'] ?: 0;
  174. }
  175. //自由输入覆盖
  176. if(!empty($freemoney)){
  177. $rc_id = 0;
  178. $money = floatval($freemoney);
  179. $bili = config('site.money_to_gold') ?: 10;
  180. $gold = bcmul($money,$bili,0);
  181. }
  182. //
  183. if($money<=0)
  184. {
  185. $this->error('兑换金额必须大于0');
  186. }
  187. //扣除money
  188. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,'money',-$money,21, '收益兑换金币');
  189. if($rs_wallet['status']===false)
  190. {
  191. Db::rollback();
  192. $this->error($rs_wallet['msg']);
  193. }
  194. //增加gold
  195. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,'gold',$gold,22, '收益兑换金币',$rs_wallet['log_table'],$rs_wallet['log_id']);
  196. if($rs_wallet['status']===false)
  197. {
  198. Db::rollback();
  199. $this->error($rs_wallet['msg']);
  200. }
  201. Db::commit();
  202. $this->success();
  203. }
  204. }