Recharge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Db;
  5. use addons\epay\library\Service;
  6. /*
  7. * 公众号搜索用户进行充值
  8. * */
  9. class Recharge extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. //充值配置列表
  18. public function config(){
  19. $rs = Db::name('paygold_webcon')->field('id,money,gold')->order('id asc')->select();
  20. $this->success('success',$rs);
  21. }
  22. //根据uid检索人
  23. public function check_user()
  24. {
  25. $mobile = input('mobile');
  26. if(!$mobile){
  27. $this->error('请输入用户手机号');
  28. }
  29. //填充0
  30. $map = [
  31. 'mobile'=>$mobile,
  32. ];
  33. $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
  34. if (empty($user_info)) {
  35. $this->error('用户信息不存在');
  36. }
  37. $this->success('success',$user_info);
  38. }
  39. //创建订单,公众号
  40. public function recharge(){
  41. $rc_id = input('rc_id',0);
  42. $pay_type = input('pay_type','wechat');
  43. $username = input('username',0);
  44. if(!$rc_id){
  45. $this->error('请选择充值金额');
  46. }
  47. if(!$username){
  48. $this->error('请输入用户手机号并选择');
  49. }
  50. //查找用户
  51. $user_info = Db::name('user')->field('id')->where('username',$username)->find();
  52. if (empty($user_info)) {
  53. $this->error('用户信息不存在');
  54. }
  55. //赋值money
  56. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  57. $money = $recharge_config['money'];
  58. $gold = $recharge_config['gold'];
  59. //创建订单
  60. $data = [];
  61. $data['status'] = 0;
  62. $pay_no = createUniqueNo('P',$user_info['id']);
  63. $data['pay_no'] = $pay_no;
  64. $data['money'] = $money;
  65. $data['payment_class'] = $pay_type;
  66. $data['user_id'] = $user_info['id'];
  67. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  68. $data['memo'] = '充值金币支付';
  69. $data['createtime'] = time();
  70. $data['payment'] = 'mp';
  71. $orderid = Db::name('pay_order')->insertGetId($data);
  72. //创建回调
  73. $data = [];
  74. $data['event'] = 'success';
  75. $data['class'] = 'app\common\model\Recharge';
  76. $data['method'] = 'rechargepaysucc';
  77. $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  78. $data['pay_no'] = $pay_no;
  79. Db::name('pay_event')->insertGetId($data);
  80. $return = [
  81. 'pay_no'=>$pay_no,
  82. 'title' => '充值金币支付',
  83. ];
  84. $this->success('success',$return);
  85. }
  86. //公众号
  87. public function topay(){
  88. $openid = input('openid');
  89. $pay_no = input('pay_no');
  90. $pay_type = input_post('pay_type','wechat');
  91. $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
  92. //下单
  93. $params = [
  94. 'type' => $pay_type,
  95. 'orderid' => $pay_no,
  96. 'title' => $orderInfo['memo'],
  97. 'amount' => $orderInfo['money'],
  98. // 'amount' => 0.01,
  99. 'method' => 'mp',
  100. 'openid' => $openid,
  101. 'notifyurl' => $this->request->root(true) . '/notify.php',
  102. //'returnurl' => $this->request->root(true) . '/location.php',
  103. ];
  104. $res = Service::submitOrder($params);
  105. $this->success('请求成功',json_decode($res,true));
  106. }
  107. }