Pay.php 7.1 KB

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