Pay.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. use think\Exception;
  8. use app\utils\CurlUtil;
  9. /**
  10. * 充值配置与充值订单
  11. */
  12. class Pay extends Api
  13. {
  14. protected $noNeedLogin = ['testpay'];
  15. protected $noNeedRight = ['*'];
  16. //vip用的
  17. public function vip_config(){
  18. $list = Db::name('payvip_config')->where('is_show',1)->order('weight asc,id asc')->select();
  19. foreach ($list as &$v) {
  20. $diff_price = '';
  21. if($v['info'] > $v['money']){
  22. $diff_price = '立减'.bcsub($v['info'],$v['money'],0).'元';
  23. }
  24. $v['diff_price'] = $diff_price;
  25. }
  26. $data['vipconfig'] = $list;
  27. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  28. $this->success('success',$data);
  29. }
  30. //vip用的,创建订单
  31. /*public function vip_recharge(){
  32. $rc_id = input('rc_id',0);
  33. $pay_type = input('pay_type','wechat');
  34. $uid = $this->auth->id;
  35. if(!$rc_id){
  36. $this->error('请选择会员套餐');
  37. }
  38. //赋值money
  39. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  40. $money = $recharge_config['money'];
  41. if($money<=0)
  42. {
  43. $this->error('支付金额必须大于0');
  44. }
  45. if($money > 10000){
  46. $this->error('支付金额太大');
  47. }
  48. //会员等级冲突
  49. //当前是会员,但是却要向下级续费,直接提示报错
  50. //修改等级,向上立刻改,向下不允许
  51. $wallet_info = model('wallet')->getWallet($this->auth->id);
  52. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  53. $this->error('当前会员没有过期,不能续费');
  54. }
  55. //创建订单
  56. $data = [];
  57. $data['status'] = 0;
  58. $pay_no = createUniqueNo('V',$uid);
  59. $data['pay_no'] = $pay_no;
  60. $data['money'] = $money;
  61. $data['payment_class'] = $pay_type;
  62. $data['user_id'] = $uid;
  63. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  64. $data['memo'] = '充值会员支付';
  65. $data['createtime'] = time();
  66. //$data['payment'] = 'miniapp';
  67. $data['payment'] = 'app';
  68. $orderid = Db::name('pay_order')->insertGetId($data);
  69. //创建回调
  70. $even_data = [];
  71. $even_data['event'] = 'success';
  72. $even_data['class'] = 'app\common\model\Recharge';
  73. $even_data['method'] = 'vippaysucc';
  74. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  75. $even_data['pay_no'] = $pay_no;
  76. Db::name('pay_event')->insertGetId($even_data);
  77. //下单
  78. $params = [
  79. 'type' => $pay_type,
  80. 'orderid' => $pay_no,
  81. 'title' => $data['memo'],
  82. 'amount' => $data['money'],
  83. //'amount' => 0.01,
  84. //'method' => 'miniapp',
  85. 'method' => 'app',
  86. //'openid' => $openid,
  87. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  88. 'returnurl' => '',
  89. ];
  90. $res = Service::submitOrder($params);
  91. if($pay_type == 'wechat'){
  92. $this->success('success',json_decode($res,true));
  93. }else{
  94. $this->success('success',$res);
  95. }
  96. }*/
  97. public function vip_recharge()
  98. {
  99. exit;//功能重复,采用精秀,屏蔽
  100. Db::startTrans();
  101. try {
  102. $rc_id = input('rc_id',0);
  103. $pay_type = input('pay_type','wechat');
  104. $uid = $this->auth->id;
  105. if(!$rc_id){
  106. throw new Exception('请选择会员套餐');
  107. }
  108. //赋值money
  109. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  110. $money = $recharge_config['money'];
  111. if($money <= 0) {
  112. throw new Exception('支付金额必须大于0');
  113. }
  114. if($money > 10000){
  115. throw new Exception('支付金额太大');
  116. }
  117. //会员等级冲突
  118. //当前是会员,但是却要向下级续费,直接提示报错
  119. //修改等级,向上立刻改,向下不允许
  120. $wallet_info = model('wallet')->getWallet($this->auth->id);
  121. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  122. throw new Exception('当前会员没有过期,不能续费');
  123. }
  124. //创建订单
  125. $data = [];
  126. $data['status'] = 0;
  127. $pay_no = createUniqueNo('V',$uid);
  128. $data['pay_no'] = $pay_no;
  129. $data['money'] = $money;
  130. $data['payment_class'] = $pay_type;
  131. $data['user_id'] = $uid;
  132. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  133. $data['memo'] = '充值会员支付';
  134. $data['createtime'] = time();
  135. //$data['payment'] = 'miniapp';
  136. $data['payment'] = 'app';
  137. $orderid = Db::name('pay_order')->insertGetId($data);
  138. //创建回调
  139. $even_data = [];
  140. $even_data['event'] = 'success';
  141. $even_data['class'] = 'app\common\model\Recharge';
  142. $even_data['method'] = 'vippaysucc';
  143. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  144. $even_data['pay_no'] = $pay_no;
  145. Db::name('pay_event')->insertGetId($even_data);
  146. if ($pay_type == 'wechat') {
  147. //$money = 0.01;
  148. $sandpay = new \app\common\library\Sandpay();
  149. $sandpayParams = [
  150. 'order_no' => $pay_no,
  151. 'goods_name' => '充值vip支付',
  152. 'money' => $money,
  153. 'type' => 'vip',
  154. ];
  155. $sandRes = $sandpay->wechath5($sandpayParams);
  156. if ($sandRes['status'] != 1) {
  157. throw new Exception('失败');
  158. }
  159. $res = $sandRes['data'];
  160. } else {
  161. //下单
  162. $params = [
  163. 'type' => $pay_type,
  164. 'orderid' => $pay_no,
  165. 'title' => $data['memo'],
  166. 'amount' => $data['money'],
  167. //'amount' => 0.01,
  168. 'method' => 'app',
  169. 'notifyurl' => CurlUtil::getHttp('/notify.php/paytype/'.$pay_type),
  170. 'returnurl' => '',
  171. ];
  172. $res = Service::submitOrder($params);
  173. }
  174. Db::commit();
  175. $this->success('success',$res);
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. }
  181. //金币充值
  182. public function gold_config(){
  183. $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
  184. $data['goldconfig'] = $list;
  185. $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
  186. $data['money_to_gold'] = config('site.money_to_gold');
  187. $data['is_first'] = Db::name('user_paygold_log')->where(['uid' => $this->auth->id])->count('id');
  188. if ($list) {
  189. $arr = [
  190. 'id' => -1,
  191. 'money' => 0,
  192. 'gold' => 0,
  193. 'title' => '自定义',
  194. 'is_show' => 1,
  195. 'weight' => 1,
  196. 'first_gold' => 0,
  197. 'first_vipdays' => 0
  198. ];
  199. array_push($data['goldconfig'], $arr);
  200. foreach ($data['goldconfig'] as &$v) {
  201. $v['is_first'] = $data['is_first'];
  202. }
  203. }
  204. $this->success('success',$data);
  205. }
  206. //充值金币 创建订单
  207. public function gold_recharge()
  208. {
  209. exit;//功能重复,采用精秀,屏蔽
  210. Db::startTrans();
  211. try {
  212. $rc_id = input_post('rc_id',0);
  213. $pay_type = input_post('pay_type','wechat');
  214. $freemoney = input_post('freemoney', 0, 'intval');
  215. $uid = $this->auth->id;
  216. if(!$rc_id && !$freemoney){
  217. throw new Exception('请选择或填写充值金额');
  218. }
  219. if (!in_array($pay_type,['wechat','alipay'])) {
  220. throw new Exception('错误的支付类型');
  221. }
  222. //赋值money
  223. if($rc_id){
  224. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  225. $money = $recharge_config ? $recharge_config['money']: 0;
  226. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  227. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  228. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  229. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  230. }
  231. //自由输入覆盖
  232. if(!empty($freemoney)){
  233. $rc_id = 0;
  234. $money = floatval($freemoney);
  235. $bili = config('site.money_to_gold') ?: 10;
  236. $gold = bcmul($money,$bili,0);
  237. $first_gold = 0;
  238. $first_vipdays = 0;
  239. $vip_gold = 0;
  240. }
  241. if($money <= 0) {
  242. throw new Exception('支付金额必须大于0');
  243. }
  244. if($money > 10000){
  245. throw new Exception('支付金额太大');
  246. }
  247. //查询是不是会员,若不是则不赠送金币
  248. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  249. if ($vip_endtime < time()) {
  250. $vip_gold = 0;
  251. }
  252. //创建订单
  253. $data = [];
  254. $data['status'] = 0;
  255. $pay_no = createUniqueNo('P',$uid);
  256. $data['pay_no'] = $pay_no;
  257. $data['money'] = $money;
  258. $data['payment_class'] = $pay_type;
  259. $data['user_id'] = $uid;
  260. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  261. $data['memo'] = '充值金币支付';
  262. $data['createtime'] = time();
  263. $data['payment'] = 'app';
  264. $orderid = Db::name('pay_order')->insertGetId($data);
  265. //创建回调
  266. $even_data = [];
  267. $even_data['event'] = 'success';
  268. $even_data['class'] = 'app\common\model\Recharge';
  269. $even_data['method'] = 'goldpaysucc';
  270. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  271. $even_data['pay_no'] = $pay_no;
  272. Db::name('pay_event')->insertGetId($even_data);
  273. if ($pay_type == 'wechat') {
  274. //$money = 0.01;
  275. $sandpay = new \app\common\library\Sandpay();
  276. $sandpayParams = [
  277. 'order_no' => $pay_no,
  278. 'goods_name' => '充值金币支付',
  279. 'money' => $money,
  280. 'type' => 'gold',
  281. ];
  282. $sandRes = $sandpay->wechath5($sandpayParams);
  283. if ($sandRes['status'] != 1) {
  284. throw new Exception('失败');
  285. }
  286. $res = $sandRes['data'];
  287. } else {
  288. //下单
  289. $params = [
  290. 'type' => $pay_type,
  291. 'orderid' => $pay_no,
  292. 'title' => $data['memo'],
  293. 'amount' => $data['money'],
  294. //'amount' => 0.01,
  295. 'method' => 'app',
  296. 'notifyurl' => CurlUtil::getHttp('/notify.php/paytype/'.$pay_type),
  297. 'returnurl' => '',
  298. ];
  299. $res = Service::submitOrder($params);
  300. }
  301. Db::commit();
  302. $this->success('成功', $res);
  303. } catch (Exception $e) {
  304. Db::rollback();
  305. $this->error($e->getMessage());
  306. }
  307. }
  308. }