Pay.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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'];
  35. $giftmoney = $recharge_config['giftmoney'];
  36. }
  37. //自由输入覆盖
  38. if(!empty($freemoney)){
  39. $rc_id = 0;
  40. $money = floatval($freemoney);
  41. $giftmoney = 0;
  42. }
  43. //
  44. if($money<=0)
  45. {
  46. $this->error('支付金额必须大于0');
  47. }
  48. if($money > 10000){
  49. $this->error('支付金额太大');
  50. }
  51. //创建订单
  52. $data = [];
  53. $data['user_id'] = $uid;
  54. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  55. $data['order_amount'] = $money;
  56. $data['createtime'] = time();
  57. $data['pay_type'] = $pay_type;
  58. $data['platform'] = $platform;
  59. $data['order_status'] = 0;
  60. $data['table_name'] = 'money_recharge';
  61. $data['table_id'] = 0;
  62. $data['args'] = json_encode(['giftmoney' => $giftmoney]);
  63. $orderid = Db::name('pay_order')->insertGetId($data);
  64. $openid = $this->auth->wxmini_openid;
  65. //下单
  66. $params = [
  67. 'type' => $pay_type,
  68. 'orderid' => $data['out_trade_no'],
  69. 'title' => '充值余额',
  70. 'amount' => $data['order_amount'],
  71. 'method' => $platform,
  72. 'openid' => $openid,
  73. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  74. 'returnurl' => '',
  75. ];
  76. $res = Service::submitOrder($params);
  77. if($pay_type == 'wechat'){
  78. $this->success('success',json_decode($res,true));
  79. }else{
  80. $this->success('success',$res);
  81. }
  82. }
  83. }