Pay.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 money_recharge(){
  15. $pay_type = input('pay_type','wechat');
  16. $platform = 'app';
  17. $freemoney = input('freemoney',0);
  18. $uid = $this->auth->id;
  19. if(!$freemoney){
  20. $this->error('填写充值金额');
  21. }
  22. if(!$this->user_auth_limit()){
  23. $this->error('请先完成实名认证');
  24. }
  25. //赋值money
  26. /*if($rc_id){
  27. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  28. $money = $recharge_config['money'] ?: 0;
  29. $gold = $recharge_config['gold'] ?: 0;
  30. }*/
  31. //自由输入覆盖
  32. if(!empty($freemoney)){
  33. $rc_id = 0;
  34. $money = floatval($freemoney);
  35. }
  36. //
  37. if($money<=0)
  38. {
  39. $this->error('支付金额必须大于0');
  40. }
  41. if($money > 10000){
  42. $this->error('支付金额太大');
  43. }
  44. //创建订单
  45. $data = [];
  46. $data['user_id'] = $uid;
  47. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  48. $data['order_amount'] = $money;
  49. $data['createtime'] = time();
  50. $data['pay_type'] = $pay_type;
  51. $data['platform'] = $platform;
  52. $data['order_status'] = 0;
  53. $data['table_name'] = 'money_recharge';
  54. $data['table_id'] = 0;
  55. $data['args'] = '';
  56. $orderid = Db::name('pay_order')->insertGetId($data);
  57. $openid = $this->auth->wechat_openid;
  58. //下单
  59. $params = [
  60. 'type' => $pay_type,
  61. 'orderid' => $data['out_trade_no'],
  62. 'title' => '支付订单',
  63. 'amount' => $data['order_amount'],
  64. 'method' => $platform,
  65. 'openid' => $openid,
  66. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  67. 'returnurl' => '',
  68. ];
  69. $res = Service::submitOrder($params);
  70. if($pay_type == 'wechat'){
  71. $this->success('success',json_decode($res,true));
  72. }else{
  73. $this->success('success',$res);
  74. }
  75. }
  76. ///////////////////////////////////////////////////////////////
  77. //金币充值
  78. public function gold_config(){
  79. $list = Db::name('paygold_webcon')->order('weigh asc,id asc')->select();
  80. $data['goldconfig'] = $list;
  81. $wallet = model('wallet')->getWallet($this->auth->id);
  82. $data['wallet'] = $wallet;
  83. $this->success('success',$data);
  84. }
  85. //充值金币 创建订单
  86. public function gold_recharge(){
  87. $rc_id = input('rc_id',0);
  88. $pay_type = input('pay_type','wechat');
  89. $platform = 'app';
  90. // $freemoney = input('freemoney',0);
  91. $freemoney = 0;
  92. $uid = $this->auth->id;
  93. if(!$rc_id && !$freemoney){
  94. $this->error('请选择或填写充值金额');
  95. }
  96. if(!$this->user_auth_limit()){
  97. $this->error('请先完成实名认证');
  98. }
  99. //赋值money
  100. if($rc_id){
  101. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  102. $money = $recharge_config['money'] ?: 0;
  103. $gold = $recharge_config['gold'] ?: 0;
  104. }
  105. //自由输入覆盖
  106. /*if(!empty($freemoney)){
  107. $rc_id = 0;
  108. $money = floatval($freemoney);
  109. $bili = config('rmb_to_gold') ?: 10;
  110. $gold = bcmul($money,$bili,0);
  111. }*/
  112. //
  113. if($money<=0)
  114. {
  115. $this->error('支付金额必须大于0');
  116. }
  117. if($money > 10000){
  118. $this->error('支付金额太大');
  119. }
  120. //充值上限
  121. $limit_rs = $this->limit_gift($money);
  122. if($limit_rs !== true){
  123. $this->error($limit_rs);
  124. }
  125. //创建订单
  126. $data = [];
  127. $data['user_id'] = $uid;
  128. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  129. $data['order_amount'] = $money;
  130. $data['createtime'] = time();
  131. $data['pay_type'] = $pay_type;
  132. $data['platform'] = $platform;
  133. $data['order_status'] = 0;
  134. $data['table_name'] = 'gold_recharge';
  135. $data['table_id'] = 0;
  136. $data['args'] = json_encode(['gold'=>$gold]);
  137. $orderid = Db::name('pay_order')->insertGetId($data);
  138. $openid = $this->auth->wechat_openid;
  139. //下单
  140. $params = [
  141. 'type' => $pay_type,
  142. 'orderid' => $data['out_trade_no'],
  143. 'title' => '支付订单',
  144. 'amount' => $data['order_amount'],
  145. 'method' => $platform,
  146. 'openid' => $openid,
  147. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  148. 'returnurl' => '',
  149. ];
  150. $res = Service::submitOrder($params);
  151. if($pay_type == 'wechat'){
  152. $this->success('success',json_decode($res,true));
  153. }else{
  154. $this->success('success',$res);
  155. }
  156. }
  157. //vip用的
  158. public function vip_config(){
  159. $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
  160. $data['vipconfig'] = $list;
  161. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  162. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  163. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  164. $this->success('success',$data);
  165. }
  166. //vip用的,创建订单
  167. public function vip_recharge(){
  168. $rc_id = input('rc_id',0);
  169. $pay_type = input('pay_type','wechat');
  170. $platform = 'app';
  171. $uid = $this->auth->id;
  172. if(!$rc_id){
  173. $this->error('请选择会员套餐');
  174. }
  175. if(!$this->user_auth_limit()){
  176. $this->error('请先完成实名认证');
  177. }
  178. //赋值money
  179. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  180. $money = $recharge_config['money'];
  181. if($money<=0)
  182. {
  183. $this->error('支付金额必须大于0');
  184. }
  185. if($money > 10000){
  186. $this->error('支付金额太大');
  187. }
  188. //创建订单
  189. $data = [];
  190. $data['user_id'] = $uid;
  191. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  192. $data['order_amount'] = $money;
  193. $data['createtime'] = time();
  194. $data['pay_type'] = $pay_type;
  195. $data['platform'] = $platform;
  196. $data['order_status'] = 0;
  197. $data['table_name'] = 'vip_recharge';
  198. $data['table_id'] = 0;
  199. $data['args'] = json_encode(['days'=>$recharge_config['days']]);
  200. $orderid = Db::name('pay_order')->insertGetId($data);
  201. $openid = $this->auth->wechat_openid;
  202. //下单
  203. $params = [
  204. 'type' => $pay_type,
  205. 'orderid' => $data['out_trade_no'],
  206. 'title' => '支付订单',
  207. 'amount' => $data['order_amount'],
  208. 'method' => $platform,
  209. 'openid' => $openid,
  210. 'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
  211. 'returnurl' => '',
  212. ];
  213. $res = Service::submitOrder($params);
  214. if($pay_type == 'wechat'){
  215. $this->success('success',json_decode($res,true));
  216. }else{
  217. $this->success('success',$res);
  218. }
  219. }
  220. }