Recharge.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $inputmoney = floatval(input('money',0));
  24. if(!$rc_id && !$inputmoney){
  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. $money = $inputmoney;
  45. if($money < 1)
  46. {
  47. $this->error('输入金额只能最低为1的正整数');
  48. }
  49. if($money > 1000){
  50. $this->error('输入金额最高1000');
  51. }
  52. $money_to_gold = config('site.money_to_gold');
  53. $gold = bcmul($money,$money_to_gold,0);
  54. }
  55. //创建订单
  56. $data = [];
  57. $data['status'] = 0;
  58. $pay_no = createUniqueNo('P',$user_info['id']);
  59. $data['pay_no'] = $pay_no;
  60. $data['money'] = $money;
  61. $data['payment_class'] = $pay_type;
  62. $data['user_id'] = $user_info['id'];
  63. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  64. $data['memo'] = '充值金币支付';
  65. $data['createtime'] = time();
  66. $data['payment'] = $pay_type == 'alipay' ? 'web' : 'scan';
  67. $orderid = Db::name('pay_order')->insertGetId($data);
  68. //创建回调
  69. $even_data = [];
  70. $even_data['event'] = 'success';
  71. $even_data['class'] = 'app\common\model\Recharge';
  72. $even_data['method'] = 'rechargepaysucc';
  73. $even_data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  74. $even_data['pay_no'] = $pay_no;
  75. Db::name('pay_event')->insertGetId($even_data);
  76. //下单
  77. $params = [
  78. 'type' => $pay_type,
  79. 'orderid' => $pay_no,
  80. 'title' => $data['memo'],
  81. 'amount' => $data['money'],
  82. // 'amount' => 0.01,
  83. 'method' => $pay_type == 'alipay' ? 'web' : 'scan',
  84. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  85. 'returnurl' => $this->request->root(true) . '/index/recharge',
  86. ];
  87. $res = Service::submitOrder($params);
  88. if($pay_type == 'wechat'){
  89. $code_url = $res['code_url'];
  90. $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>$code_url]);
  91. $response = Response::create()->header("Content-Type", 'image/png');
  92. // 直接显示二维码
  93. header('Content-Type: ' . $qrCode->getContentType());
  94. $response->content($qrCode->writeString());
  95. return $response;
  96. }else{
  97. return $res;
  98. }
  99. }
  100. }