Recharge.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $isWechat = strpos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  15. if($isWechat)
  16. {
  17. return $this->view->fetch('tips');
  18. }
  19. $conf_list = Db::name('paygold_webcon')->order('id asc')->select();
  20. $this->assign('conf_list',$conf_list);
  21. return $this->view->fetch();
  22. }
  23. public function recharge_success(){
  24. $this->success('支付成功','/index/recharge/index');
  25. }
  26. //创建订单
  27. public function recharge_pc(){
  28. //微信不能打开
  29. $isWechat = strpos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  30. if($isWechat)
  31. {$this->error('不能在微信浏览器打开');}
  32. //
  33. $rc_id = input('rc_id',0);
  34. $pay_type = input('pay_type','alipay');
  35. $mobile = input('mobile',0);
  36. $inputmoney = floatval(input('money',0));
  37. if(!$rc_id && !$inputmoney){
  38. $this->error('请选择或手动填写充值金额');
  39. }
  40. if(!$mobile){
  41. $this->error('请输入用户手机号');
  42. }
  43. //查找用户
  44. $user_info = Db::name('user')->field('id')->where('mobile',$mobile)->find();
  45. if (empty($user_info)) {
  46. $this->error('用户信息不存在');
  47. }
  48. //赋值money
  49. if($rc_id){
  50. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  51. if(!$recharge_config){
  52. $this->error('请选择充值金额');
  53. }
  54. $money = $recharge_config['money'];
  55. $gold = $recharge_config['gold'];
  56. }else{
  57. $money = $inputmoney;
  58. if($money < 1)
  59. {
  60. $this->error('输入金额只能最低为1的正整数');
  61. }
  62. if($money > 1000){
  63. $this->error('输入金额最高1000');
  64. }
  65. $money_to_gold = config('site.money_to_gold');
  66. $gold = bcmul($money,$money_to_gold,0);
  67. }
  68. //method
  69. $payment_method = $pay_type == 'alipay' ? 'web' : 'scan';
  70. if($this->request->isMobile()){
  71. $payment_method = 'wap';
  72. }
  73. //创建订单
  74. $data = [];
  75. $data['status'] = 0;
  76. $pay_no = createUniqueNo('P',$user_info['id']);
  77. $data['pay_no'] = $pay_no;
  78. $data['money'] = $money;
  79. $data['payment_class'] = $pay_type;
  80. $data['user_id'] = $user_info['id'];
  81. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  82. $data['memo'] = '充值金币支付';
  83. $data['createtime'] = time();
  84. $data['payment'] = $payment_method;
  85. $orderid = Db::name('pay_order')->insertGetId($data);
  86. //创建回调
  87. $even_data = [];
  88. $even_data['event'] = 'success';
  89. $even_data['class'] = 'app\common\model\Recharge';
  90. $even_data['method'] = 'rechargepaysucc';
  91. $even_data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  92. $even_data['pay_no'] = $pay_no;
  93. Db::name('pay_event')->insertGetId($even_data);
  94. //下单
  95. $params = [
  96. 'type' => $pay_type,
  97. 'orderid' => $pay_no,
  98. 'title' => $data['memo'],
  99. 'amount' => $data['money'],
  100. // 'amount' => 0.01,
  101. 'method' => $payment_method,
  102. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  103. 'returnurl' => $this->request->root(true) . '/index/recharge/recharge_success',
  104. ];
  105. $res = Service::submitOrder($params);
  106. if($pay_type == 'wechat' && $payment_method == 'scan'){
  107. $code_url = $res['code_url'];
  108. $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>$code_url]);
  109. $response = Response::create()->header("Content-Type", 'image/png');
  110. // 直接显示二维码
  111. header('Content-Type: ' . $qrCode->getContentType());
  112. $response->content($qrCode->writeString());
  113. return $response;
  114. }else{
  115. return $res;
  116. }
  117. }
  118. }