Pay.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. //金币充值
  14. public function jewel_config(){
  15. $list = Db::name('payjewel_config')->order('weigh asc,id asc')->select();
  16. $data['jewelconfig'] = $list;
  17. $wallet = model('wallet')->getWallet($this->auth->id);
  18. $data['wallet'] = $wallet;
  19. $this->success('success',$data);
  20. }
  21. //充值金币 创建订单
  22. public function jewel_recharge(){
  23. $rc_id = input('rc_id',0);
  24. $pay_type = input('pay_type','wechat');
  25. $platform = 'app';
  26. $freemoney = input('freemoney',0);
  27. $uid = $this->auth->id;
  28. if(!$rc_id && !$freemoney){
  29. $this->error('请选择或填写充值金额');
  30. }
  31. if($this->auth->is_auth != 2){
  32. $this->error('请先完成实名认证');
  33. }
  34. //赋值money
  35. if($rc_id){
  36. $recharge_config = Db::name('payjewel_config')->where('id',$rc_id)->find();
  37. $money = $recharge_config['money'] ?: 0;
  38. $jewel = $recharge_config['jewel'] ?: 0;
  39. }
  40. //自由输入覆盖
  41. if(!empty($freemoney)){
  42. $rc_id = 0;
  43. $money = floatval($freemoney);
  44. $bili = config('site.money_to_jewel') ?: 10;
  45. $jewel = bcmul($money,$bili,0);
  46. }
  47. //
  48. if($money<=0)
  49. {
  50. $this->error('支付金额必须大于0');
  51. }
  52. if($money > 10000){
  53. $this->error('支付金额太大');
  54. }
  55. //创建订单
  56. $data = [];
  57. $data['user_id'] = $uid;
  58. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  59. $data['order_amount'] = $money;
  60. $data['createtime'] = time();
  61. $data['pay_type'] = $pay_type;
  62. $data['platform'] = $platform;
  63. $data['order_status'] = 0;
  64. $data['table_name'] = 'jewel_recharge';
  65. $data['table_id'] = 0;
  66. $data['args'] = json_encode(['jewel'=>$jewel]);
  67. $orderid = Db::name('pay_order')->insertGetId($data);
  68. // $openid = $this->auth->mini_openid;
  69. //下单
  70. $params = [
  71. 'type' => $pay_type,
  72. 'orderid' => $data['out_trade_no'],
  73. 'title' => '支付订单',
  74. 'amount' => $data['order_amount'],
  75. 'method' => $platform,
  76. // 'openid' => $openid,
  77. 'notifyurl' => config('pay_notify_url').'/api/notifynew/recharge_notify_base/paytype/'.$pay_type,
  78. 'returnurl' => '',
  79. ];
  80. $res = Service::submitOrder($params);
  81. if($pay_type == 'wechat'){
  82. $this->success('success',json_decode($res,true));
  83. }else{
  84. $this->success('success',$res);
  85. }
  86. }
  87. //收益兑换金币 配置
  88. public function moneytojewel_config(){
  89. $list = Db::name('moneytojewel_config')->order('weigh asc,id asc')->select();
  90. $data['jewelconfig'] = $list;
  91. $wallet = model('wallet')->getWallet($this->auth->id);
  92. $data['wallet'] = $wallet;
  93. $data['money_to_jewel'] = config('site.money_to_jewel');
  94. $data['remark'] = config('site.money_to_jewel_rule');
  95. $this->success('success',$data);
  96. }
  97. //收益兑换金币 兑换
  98. public function moneytojewel_exchange(){
  99. $rc_id = input('rc_id',0);
  100. $freemoney = input('freemoney',0);
  101. if(!$rc_id && !$freemoney){
  102. $this->error('请选择或填写兑换金额');
  103. }
  104. $uid = $this->auth->id;
  105. //赋值money
  106. if($rc_id){
  107. $recharge_config = Db::name('moneytojewel_config')->where('id',$rc_id)->find();
  108. $money = $recharge_config['money'] ?: 0;
  109. $jewel = $recharge_config['jewel'] ?: 0;
  110. }
  111. //自由输入覆盖
  112. if(!empty($freemoney)){
  113. $rc_id = 0;
  114. $money = floatval($freemoney);
  115. $bili = config('site.money_to_jewel') ?: 10;
  116. $jewel = bcmul($money,$bili,0);
  117. }
  118. //
  119. if($money<=0)
  120. {
  121. $this->error('兑换金额必须大于0');
  122. }
  123. //扣除money
  124. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,$money,'-',0,'收益兑换金币',114,'money');
  125. if($rs_wallet['status']===false)
  126. {
  127. Db::rollback();
  128. $this->error($rs_wallet['msg']);
  129. }
  130. //增加jewel
  131. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,$jewel,'+',0,'收益兑换金币',14,'jewel');
  132. if($rs_wallet['status']===false)
  133. {
  134. Db::rollback();
  135. $this->error($rs_wallet['msg']);
  136. }
  137. Db::commit();
  138. $this->success();
  139. }
  140. ///////////没用到/////////
  141. //vip用的
  142. public function vip_config(){
  143. $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
  144. $data['vipconfig'] = $list;
  145. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  146. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  147. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  148. $this->success('success',$data);
  149. }
  150. //vip用的,创建订单
  151. public function vip_recharge(){
  152. $rc_id = input('rc_id',0);
  153. $pay_type = input('pay_type','wechat');
  154. $platform = 'app';
  155. $uid = $this->auth->id;
  156. if(!$rc_id){
  157. $this->error('请选择会员套餐');
  158. }
  159. if($this->auth->is_auth != 2){
  160. $this->error('请先完成实名认证');
  161. }
  162. //赋值money
  163. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  164. $money = $recharge_config['money'];
  165. if($money<=0)
  166. {
  167. $this->error('支付金额必须大于0');
  168. }
  169. if($money > 10000){
  170. $this->error('支付金额太大');
  171. }
  172. //创建订单
  173. $data = [];
  174. $data['user_id'] = $uid;
  175. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  176. $data['order_amount'] = $money;
  177. $data['createtime'] = time();
  178. $data['pay_type'] = $pay_type;
  179. $data['platform'] = $platform;
  180. $data['order_status'] = 0;
  181. $data['table_name'] = 'vip_recharge';
  182. $data['table_id'] = 0;
  183. $data['args'] = json_encode(['days'=>$recharge_config['days']]);
  184. $orderid = Db::name('pay_order')->insertGetId($data);
  185. // $openid = $this->auth->mini_openid;
  186. //下单
  187. $params = [
  188. 'type' => $pay_type,
  189. 'orderid' => $data['out_trade_no'],
  190. 'title' => '支付订单',
  191. 'amount' => $data['order_amount'],
  192. 'method' => $platform,
  193. // 'openid' => $openid,
  194. 'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
  195. 'returnurl' => '',
  196. ];
  197. $res = Service::submitOrder($params);
  198. if($pay_type == 'wechat'){
  199. $this->success('success',json_decode($res,true));
  200. }else{
  201. $this->success('success',$res);
  202. }
  203. }
  204. }