Pay.php 7.2 KB

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