Recharge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $pay_type = 'alipay';
  23. $mobile = input('mobile',0);
  24. if(!$rc_id){
  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. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  37. if(!$recharge_config){
  38. $this->error('请选择充值金额');
  39. }
  40. $money = $recharge_config['money'];
  41. $gold = $recharge_config['gold'];
  42. //创建订单
  43. $data = [];
  44. $data['status'] = 0;
  45. $pay_no = createUniqueNo('P',$user_info['id']);
  46. $data['pay_no'] = $pay_no;
  47. $data['money'] = $money;
  48. $data['payment_class'] = $pay_type;
  49. $data['user_id'] = $user_info['id'];
  50. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  51. $data['memo'] = '充值金币支付';
  52. $data['createtime'] = time();
  53. $data['payment'] = $pay_type == 'alipay' ? 'web' : 'scan';
  54. $orderid = Db::name('pay_order')->insertGetId($data);
  55. //创建回调
  56. $even_data = [];
  57. $even_data['event'] = 'success';
  58. $even_data['class'] = 'app\common\model\Recharge';
  59. $even_data['method'] = 'rechargepaysucc';
  60. $even_data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  61. $even_data['pay_no'] = $pay_no;
  62. Db::name('pay_event')->insertGetId($even_data);
  63. //下单
  64. $params = [
  65. 'type' => $pay_type,
  66. 'orderid' => $pay_no,
  67. 'title' => $data['memo'],
  68. 'amount' => $data['money'],
  69. // 'amount' => 0.01,
  70. 'method' => $pay_type == 'alipay' ? 'web' : 'scan',
  71. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  72. 'returnurl' => $this->request->root(true) . '/index/recharge',
  73. ];
  74. $res = Service::submitOrder($params);
  75. if($pay_type == 'wechat'){
  76. $code_url = $res['code_url'];
  77. $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>$code_url]);
  78. $response = Response::create()->header("Content-Type", 'image/png');
  79. // 直接显示二维码
  80. header('Content-Type: ' . $qrCode->getContentType());
  81. $response->content($qrCode->writeString());
  82. return $response;
  83. }else{
  84. return $res;
  85. }
  86. }
  87. }