Recharge.php 4.3 KB

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