Pay.php 7.4 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 desc,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. $cityname = input('cityname','','trim');
  28. $uid = $this->auth->id;
  29. if(!$rc_id && !$freemoney){
  30. $this->error('请选择或填写充值金额');
  31. }
  32. if($this->auth->is_auth != 2){
  33. $this->error('请先完成实名认证');
  34. }
  35. //赋值money
  36. if($rc_id){
  37. $recharge_config = Db::name('payjewel_config')->where('id',$rc_id)->find();
  38. $money = $recharge_config['money'] ?: 0;
  39. $jewel = $recharge_config['jewel'] ?: 0;
  40. }
  41. //自由输入覆盖
  42. if(!empty($freemoney)){
  43. $rc_id = 0;
  44. $money = floatval($freemoney);
  45. $bili = config('rmb_to_jewel') ?: 10000;
  46. $jewel = bcmul($money,$bili,0);
  47. }
  48. //
  49. if($money<=0)
  50. {
  51. $this->error('支付金额必须大于0');
  52. }
  53. if($money > 10000){
  54. $this->error('支付金额太大');
  55. }
  56. //创建订单
  57. $data = [];
  58. $data['user_id'] = $uid;
  59. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  60. $data['order_amount'] = $money;
  61. $data['createtime'] = time();
  62. $data['pay_type'] = $pay_type;
  63. $data['platform'] = $platform;
  64. $data['order_status'] = 0;
  65. $data['table_name'] = 'jewel_recharge';
  66. $data['table_id'] = 0;
  67. $data['args'] = json_encode(['jewel'=>$jewel,'cityname'=>$cityname]);
  68. $orderid = Db::name('pay_order')->insertGetId($data);
  69. // $openid = $this->auth->mini_openid;
  70. //下单
  71. $params = [
  72. 'type' => $pay_type,
  73. 'orderid' => $data['out_trade_no'],
  74. 'title' => '支付订单',
  75. 'amount' => $data['order_amount'],
  76. 'method' => $platform,
  77. // 'openid' => $openid,
  78. 'notifyurl' => config('pay_notify_url').'/api/notifynew/recharge_notify_base/paytype/'.$pay_type,
  79. 'returnurl' => '',
  80. ];
  81. $res = Service::submitOrder($params);
  82. if($pay_type == 'wechat'){
  83. $this->success('success',json_decode($res,true));
  84. }else{
  85. $this->success('success',$res);
  86. }
  87. }
  88. //海钻兑换金币 配置
  89. public function moneytojewel_config(){
  90. $list = Db::name('moneytojewel_config')->order('weigh asc,id asc')->select();
  91. $data['jewelconfig'] = $list;
  92. $wallet = model('wallet')->getWallet($this->auth->id);
  93. $data['wallet'] = $wallet;
  94. $data['money_to_jewel'] = config('money_ex_jewel');
  95. $data['remark'] = config('site.money_to_jewel_rule');
  96. $this->success('success',$data);
  97. }
  98. //海钻兑换金币 兑换
  99. public function moneytojewel_exchange(){
  100. $rc_id = input('rc_id',0);
  101. $freemoney = input('freemoney',0);
  102. if(!$rc_id && !$freemoney){
  103. $this->error('请选择或填写兑换金额');
  104. }
  105. $uid = $this->auth->id;
  106. //赋值money
  107. if($rc_id){
  108. $recharge_config = Db::name('moneytojewel_config')->where('id',$rc_id)->find();
  109. $money = $recharge_config['money'] ?: 0;
  110. $jewel = $recharge_config['jewel'] ?: 0;
  111. }
  112. //自由输入覆盖
  113. if(!empty($freemoney)){
  114. $rc_id = 0;
  115. $money = floatval($freemoney);
  116. $bili = config('money_ex_jewel');
  117. $jewel = bcmul($money,$bili,0);
  118. }
  119. //
  120. if($money<=0)
  121. {
  122. $this->error('兑换金额必须大于0');
  123. }
  124. //扣除money
  125. $rs_wallet = model('Wallet')->lockChangeAccountRemain($uid,$money,'-',0,'海钻兑换金币',114,'money');
  126. if($rs_wallet['status']===false)
  127. {
  128. Db::rollback();
  129. $this->error($rs_wallet['msg']);
  130. }
  131. //增加jewel
  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->auth->is_auth != 2){
  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. }