Pay.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\model\Wallet;
  7. /**
  8. * 充值配置与充值订单
  9. */
  10. class Pay extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = ['*'];
  14. //vip用的
  15. public function vip_config(){
  16. $list = Db::name('payvip_config')->where('is_show',1)->order('weight asc,id asc')->select();
  17. $data['vipconfig'] = $list;
  18. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  19. $this->success('success',$data);
  20. }
  21. //vip用的,创建订单
  22. public function vip_recharge(){
  23. $rc_id = input('rc_id',0);
  24. $pay_type = input('pay_type','wechat');
  25. $uid = $this->auth->id;
  26. if(!$rc_id){
  27. $this->error('请选择会员套餐');
  28. }
  29. //赋值money
  30. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  31. $money = $recharge_config['money'];
  32. if($money<=0)
  33. {
  34. $this->error('支付金额必须大于0');
  35. }
  36. if($money > 10000){
  37. $this->error('支付金额太大');
  38. }
  39. //创建订单
  40. $data = [];
  41. $data['status'] = 0;
  42. $pay_no = createUniqueNo('V',$uid);
  43. $data['pay_no'] = $pay_no;
  44. $data['money'] = $money;
  45. $data['payment_class'] = $pay_type;
  46. $data['user_id'] = $uid;
  47. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  48. $data['memo'] = '充值会员支付';
  49. $data['createtime'] = time();
  50. //$data['payment'] = 'miniapp';
  51. $data['payment'] = 'app';
  52. $orderid = Db::name('pay_order')->insertGetId($data);
  53. //创建回调
  54. $even_data = [];
  55. $even_data['event'] = 'success';
  56. $even_data['class'] = 'app\common\model\Recharge';
  57. $even_data['method'] = 'vippaysucc';
  58. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days']]);
  59. $even_data['pay_no'] = $pay_no;
  60. Db::name('pay_event')->insertGetId($even_data);
  61. $return = [
  62. 'pay_no'=>$pay_no,
  63. 'title' => '充值vip支付',
  64. ];
  65. /* $this->success('success',$return);
  66. }
  67. public function topay(){*/
  68. //$openid = $this->auth->openid;
  69. /*$pay_no = input('pay_no');
  70. $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
  71. //下单
  72. $params = [
  73. 'type' => $pay_type,
  74. 'orderid' => $pay_no,
  75. 'title' => $data['memo'],
  76. 'amount' => $data['money'],
  77. 'amount' => 0.01,
  78. //'method' => 'miniapp',
  79. 'method' => 'app',
  80. //'openid' => $openid,
  81. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  82. 'returnurl' => '',
  83. ];
  84. $res = Service::submitOrder($params);
  85. if($pay_type == 'wechat'){
  86. $this->success('success',json_decode($res,true));
  87. }else{
  88. $this->success('success',$res);
  89. }
  90. }
  91. //金币充值
  92. public function gold_config(){
  93. $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
  94. $data['goldconfig'] = $list;
  95. $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
  96. $this->success('success',$data);
  97. }
  98. //充值金币 创建订单
  99. public function gold_recharge(){
  100. $rc_id = input_post('rc_id',0);
  101. $pay_type = input_post('pay_type','wechat');
  102. $freemoney = input_post('freemoney',0);
  103. $uid = $this->auth->id;
  104. if(!$rc_id && !$freemoney){
  105. $this->error('请选择或填写充值金额');
  106. }
  107. //赋值money
  108. if($rc_id){
  109. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  110. $money = $recharge_config['money'] ?: 0;
  111. $gold = $recharge_config['gold'] ?: 0;
  112. $first_gold = $recharge_config['first_gold'] ?: 0;
  113. $first_vipdays = $recharge_config['first_vipdays'] ?: 0;
  114. }
  115. //自由输入覆盖
  116. if(!empty($freemoney)){
  117. $rc_id = 0;
  118. $money = floatval($freemoney);
  119. $bili = config('site.money_to_gold') ?: 10;
  120. $gold = bcmul($money,$bili,0);
  121. $first_gold = 0;
  122. $first_vipdays = 0;
  123. }
  124. //
  125. if($money<=0)
  126. {
  127. $this->error('支付金额必须大于0');
  128. }
  129. if($money > 10000){
  130. $this->error('支付金额太大');
  131. }
  132. //创建订单
  133. $data = [];
  134. $data['status'] = 0;
  135. $pay_no = createUniqueNo('P',$uid);
  136. $data['pay_no'] = $pay_no;
  137. $data['money'] = $money;
  138. $data['payment_class'] = $pay_type;
  139. $data['user_id'] = $uid;
  140. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  141. $data['memo'] = '充值金币支付';
  142. $data['createtime'] = time();
  143. $data['payment'] = 'app';
  144. $orderid = Db::name('pay_order')->insertGetId($data);
  145. //创建回调
  146. $even_data = [];
  147. $even_data['event'] = 'success';
  148. $even_data['class'] = 'app\common\model\Recharge';
  149. $even_data['method'] = 'goldpaysucc';
  150. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays]);
  151. $even_data['pay_no'] = $pay_no;
  152. Db::name('pay_event')->insertGetId($even_data);
  153. $return = [
  154. 'pay_no'=>$pay_no,
  155. 'title' => '充值金币支付',
  156. ];
  157. /* $this->success('success',$return);
  158. }
  159. public function topay(){*/
  160. //$openid = $this->auth->openid;
  161. /*$pay_no = input('pay_no');
  162. $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();*/
  163. //下单
  164. $params = [
  165. 'type' => $pay_type,
  166. 'orderid' => $pay_no,
  167. 'title' => $data['memo'],
  168. 'amount' => $data['money'],
  169. 'amount' => 0.01,
  170. 'method' => 'app',
  171. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  172. 'returnurl' => '',
  173. ];
  174. $res = Service::submitOrder($params);
  175. if($pay_type == 'wechat'){
  176. $this->success('success',json_decode($res,true));
  177. }else{
  178. $this->success('success',$res);
  179. }
  180. }
  181. }