Pay.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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->user_auth_limit()){
  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,'gold',$jewel,22, '收益兑换金币',$rs_wallet['log_table'],$rs_wallet['log_id']);
  132. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,$jewel,'+',0,'收益兑换金币',14,'jewel');
  133. if($rs_wallet['status']===false)
  134. {
  135. Db::rollback();
  136. $this->error($rs_wallet['msg']);
  137. }
  138. Db::commit();
  139. $this->success();
  140. }
  141. ///////////没用到/////////
  142. //vip用的
  143. public function vip_config(){
  144. $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
  145. $data['vipconfig'] = $list;
  146. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  147. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  148. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  149. $this->success('success',$data);
  150. }
  151. //vip用的,创建订单
  152. public function vip_recharge(){
  153. $rc_id = input('rc_id',0);
  154. $pay_type = input('pay_type','wechat');
  155. $platform = 'app';
  156. $uid = $this->auth->id;
  157. if(!$rc_id){
  158. $this->error('请选择会员套餐');
  159. }
  160. if(!$this->user_auth_limit()){
  161. $this->error('请先完成实名认证');
  162. }
  163. //赋值money
  164. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  165. $money = $recharge_config['money'];
  166. if($money<=0)
  167. {
  168. $this->error('支付金额必须大于0');
  169. }
  170. if($money > 10000){
  171. $this->error('支付金额太大');
  172. }
  173. //创建订单
  174. $data = [];
  175. $data['user_id'] = $uid;
  176. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  177. $data['order_amount'] = $money;
  178. $data['createtime'] = time();
  179. $data['pay_type'] = $pay_type;
  180. $data['platform'] = $platform;
  181. $data['order_status'] = 0;
  182. $data['table_name'] = 'vip_recharge';
  183. $data['table_id'] = 0;
  184. $data['args'] = json_encode(['days'=>$recharge_config['days']]);
  185. $orderid = Db::name('pay_order')->insertGetId($data);
  186. // $openid = $this->auth->mini_openid;
  187. //下单
  188. $params = [
  189. 'type' => $pay_type,
  190. 'orderid' => $data['out_trade_no'],
  191. 'title' => '支付订单',
  192. 'amount' => $data['order_amount'],
  193. 'method' => $platform,
  194. // 'openid' => $openid,
  195. 'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
  196. 'returnurl' => '',
  197. ];
  198. $res = Service::submitOrder($params);
  199. if($pay_type == 'wechat'){
  200. $this->success('success',json_decode($res,true));
  201. }else{
  202. $this->success('success',$res);
  203. }
  204. }
  205. }