Recharge.php 4.3 KB

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