Pay.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_config(){
  15. $list = Db::name('recharge_config')->order('weigh asc,id asc')->select();
  16. $data['config'] = $list;
  17. $wallet = model('wallet')->getWallet($this->auth->id);
  18. $data['wallet'] = $wallet;
  19. $this->success('success',$data);
  20. }
  21. //充值金币 创建订单
  22. public function money_recharge(){
  23. $rc_id = input('rc_id',0);
  24. $pay_type = 'wechat';
  25. $platform = 'mini';
  26. $freemoney = input('freemoney',0);
  27. $uid = $this->auth->id;
  28. if(!$rc_id && !$freemoney){
  29. $this->error('请选择或填写充值金额');
  30. }
  31. //赋值money
  32. if($rc_id){
  33. $recharge_config = Db::name('recharge_config')->where('id',$rc_id)->find();
  34. $money = $recharge_config['money'] ?: 0;
  35. }
  36. //自由输入覆盖
  37. if(!empty($freemoney)){
  38. $rc_id = 0;
  39. $money = floatval($freemoney);
  40. }
  41. //
  42. if($money<=0)
  43. {
  44. $this->error('支付金额必须大于0');
  45. }
  46. if($money > 10000){
  47. $this->error('支付金额太大');
  48. }
  49. //创建订单
  50. $data = [];
  51. $data['user_id'] = $uid;
  52. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  53. $data['order_amount'] = $money;
  54. $data['createtime'] = time();
  55. $data['pay_type'] = $pay_type;
  56. $data['platform'] = $platform;
  57. $data['order_status'] = 0;
  58. $data['table_name'] = 'money_recharge';
  59. $data['table_id'] = 0;
  60. $data['args'] = '';
  61. $orderid = Db::name('pay_order')->insertGetId($data);
  62. $openid = $this->auth->wxmini_openid;
  63. //下单
  64. $params = [
  65. 'type' => $pay_type,
  66. 'orderid' => $data['out_trade_no'],
  67. 'title' => '充值余额',
  68. 'amount' => $data['order_amount'],
  69. 'method' => $platform,
  70. 'openid' => $openid,
  71. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  72. 'returnurl' => '',
  73. ];
  74. $res = Service::submitOrder($params);
  75. if($pay_type == 'wechat'){
  76. $this->success('success',json_decode($res,true));
  77. }else{
  78. $this->success('success',$res);
  79. }
  80. }
  81. }