Recharge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use think\Response;
  7. class Recharge extends Frontend
  8. {
  9. protected $noNeedLogin = '*';
  10. protected $noNeedRight = '*';
  11. protected $layout = '';
  12. public function index()
  13. {
  14. $conf_list = Db::name('paygold_webcon')->order('id asc')->select();
  15. $this->assign('conf_list',$conf_list);
  16. return $this->view->fetch();
  17. }
  18. //创建订单
  19. public function recharge_pc(){
  20. $rc_id = input('rc_id',0);
  21. $pay_type = input('pay_type','alipay');
  22. $mobile = input('mobile',0);
  23. $money = floatval(input('money',0));
  24. if(!$rc_id && !$money){
  25. $this->error('请选择或手动填写充值金额');
  26. }
  27. if(!$mobile){
  28. $this->error('请输入用户手机号');
  29. }
  30. //查找用户
  31. $user_info = Db::name('user')->field('id')->where('mobile',$mobile)->find();
  32. if (empty($user_info)) {
  33. $this->error('用户信息不存在');
  34. }
  35. //赋值money
  36. if($rc_id){
  37. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  38. if(!$recharge_config){
  39. $this->error('请选择充值金额');
  40. }
  41. $money = $recharge_config['money'];
  42. $gold = $recharge_config['gold'];
  43. }else{
  44. if($money<=0)
  45. {
  46. $this->error('支付金额必须大于0');
  47. }
  48. if($money > 1000){
  49. $this->error('输入金额最高1000');
  50. }
  51. $money_to_gold = config('site.money_to_gold');
  52. $gold = bcmul($money,$money_to_gold,0);
  53. }
  54. //创建订单
  55. $data = [];
  56. $data['status'] = 0;
  57. $pay_no = createUniqueNo('P',$user_info['id']);
  58. $data['pay_no'] = $pay_no;
  59. $data['money'] = $money;
  60. $data['payment_class'] = $pay_type;
  61. $data['user_id'] = $user_info['id'];
  62. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  63. $data['memo'] = '充值金币支付';
  64. $data['createtime'] = time();
  65. $data['payment'] = $pay_type == 'alipay' ? 'web' : 'scan';
  66. $orderid = Db::name('pay_order')->insertGetId($data);
  67. //创建回调
  68. $even_data = [];
  69. $even_data['event'] = 'success';
  70. $even_data['class'] = 'app\common\model\Recharge';
  71. $even_data['method'] = 'rechargepaysucc';
  72. $even_data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  73. $even_data['pay_no'] = $pay_no;
  74. Db::name('pay_event')->insertGetId($even_data);
  75. //下单
  76. $params = [
  77. 'type' => $pay_type,
  78. 'orderid' => $pay_no,
  79. 'title' => $data['memo'],
  80. 'amount' => $data['money'],
  81. // 'amount' => 0.01,
  82. 'method' => $pay_type == 'alipay' ? 'web' : 'scan',
  83. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  84. 'returnurl' => $this->request->root(true) . '/index/recharge',
  85. ];
  86. $res = Service::submitOrder($params);
  87. if($pay_type == 'wechat'){
  88. $code_url = $res['code_url'];
  89. $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>$code_url]);
  90. $response = Response::create()->header("Content-Type", 'image/png');
  91. // 直接显示二维码
  92. header('Content-Type: ' . $qrCode->getContentType());
  93. $response->content($qrCode->writeString());
  94. return $response;
  95. }else{
  96. return $res;
  97. }
  98. }
  99. }