Recharge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if ($rs) {
  21. foreach ($rs as &$v) {
  22. $v['money_to_gold'] = config('site.money_to_gold');
  23. }
  24. $arr = [
  25. 'id' => -1,
  26. 'money' => 0,
  27. 'gold' => 0,
  28. 'money_to_gold' => config('site.money_to_gold')
  29. ];
  30. array_push($rs, $arr);
  31. }
  32. $this->success('success',$rs);
  33. }
  34. //根据uid检索人
  35. public function check_user()
  36. {
  37. $mobile = input('mobile');
  38. if(!$mobile){
  39. $this->error('请输入用户手机号');
  40. }
  41. //填充0
  42. $map = [
  43. 'mobile'=>$mobile,
  44. ];
  45. $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
  46. if (empty($user_info)) {
  47. $this->error('用户信息不存在');
  48. }
  49. $this->success('success',$user_info);
  50. }
  51. //创建订单,公众号
  52. public function recharge(){
  53. $rc_id = input('rc_id',0);
  54. $pay_type = input('pay_type','wechat');
  55. $username = input('username',0);
  56. if(!$rc_id){
  57. $this->error('请选择充值金额');
  58. }
  59. if(!$username){
  60. $this->error('请输入用户手机号并选择');
  61. }
  62. //查找用户
  63. $user_info = Db::name('user')->field('id')->where('username',$username)->find();
  64. if (empty($user_info)) {
  65. $this->error('用户信息不存在');
  66. }
  67. //赋值money
  68. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  69. $money = $recharge_config['money'];
  70. $gold = $recharge_config['gold'];
  71. //创建订单
  72. $data = [];
  73. $data['status'] = 0;
  74. $pay_no = createUniqueNo('P',$user_info['id']);
  75. $data['pay_no'] = $pay_no;
  76. $data['money'] = $money;
  77. $data['payment_class'] = $pay_type;
  78. $data['user_id'] = $user_info['id'];
  79. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  80. $data['memo'] = '充值金币支付';
  81. $data['createtime'] = time();
  82. $data['payment'] = 'mp';
  83. $orderid = Db::name('pay_order')->insertGetId($data);
  84. //创建回调
  85. $data = [];
  86. $data['event'] = 'success';
  87. $data['class'] = 'app\common\model\Recharge';
  88. $data['method'] = 'rechargepaysucc';
  89. $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  90. $data['pay_no'] = $pay_no;
  91. Db::name('pay_event')->insertGetId($data);
  92. $return = [
  93. 'pay_no'=>$pay_no,
  94. 'title' => '充值金币支付',
  95. ];
  96. $this->success('success',$return);
  97. }
  98. //公众号
  99. public function topay(){
  100. $openid = input('openid');
  101. $pay_no = input('pay_no');
  102. $pay_type = input_post('pay_type','wechat');
  103. $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
  104. //下单
  105. $params = [
  106. 'type' => $pay_type,
  107. 'orderid' => $pay_no,
  108. 'title' => $orderInfo['memo'],
  109. 'amount' => $orderInfo['money'],
  110. // 'amount' => 0.01,
  111. 'method' => 'mp',
  112. 'openid' => $openid,
  113. 'notifyurl' => $this->request->root(true) . '/notify.php',
  114. //'returnurl' => $this->request->root(true) . '/location.php',
  115. ];
  116. $res = Service::submitOrder($params);
  117. $this->success('请求成功',json_decode($res,true));
  118. }
  119. //兑换金币
  120. public function exchangegold() {
  121. $id = input('id', 0, 'intval');
  122. $freemoney = input_post('freemoney', 0, 'intval'); //自定义
  123. $uid = $this->auth->id;
  124. if(!$id && !$freemoney){
  125. $this->error('请选择或填写兑换金额');
  126. }
  127. if ($id) {
  128. //赋值money
  129. $paygold_webcon = Db::name('paygold_webcon')->where('id', $id)->find();
  130. $money = $paygold_webcon ? $paygold_webcon['money'] : 0;
  131. $gold = $paygold_webcon ? $paygold_webcon['gold'] : 0;
  132. }
  133. if ($freemoney) {
  134. $money = $freemoney;
  135. $bili = config('site.money_to_gold') ?: 10;
  136. $gold = bcmul($money,$bili,0);
  137. }
  138. if($money<=0) {
  139. $this->error('支付金额必须大于0');
  140. }
  141. if($money > 10000){
  142. $this->error('支付金额太大');
  143. }
  144. //验证余额
  145. $user_info = model('wallet')->getWallet($this->auth->id);
  146. if ($user_info['money'] < $money) {
  147. $this->success('您的余额不足', ['code' => 2]);
  148. }
  149. //开启事务
  150. Db::startTrans();
  151. //扣费
  152. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,18,'兑换金币');
  153. if($wallet_rs['status'] === false){
  154. Db::rollback();
  155. $this->error($wallet_rs['msg']);
  156. }
  157. //赠送金币
  158. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$gold,19,'获得兑换金币');
  159. if($wallet_rs['status'] === false){
  160. Db::rollback();
  161. $this->error($wallet_rs['msg']);
  162. }
  163. //tag任务赠送金币
  164. //开通VIP 50金币
  165. // $task_rs = \app\common\model\TaskLog::tofinish($uid,9);
  166. // if($task_rs === false){
  167. // Db::rollback();
  168. // $this->error('完成任务赠送奖励失败');
  169. // }
  170. Db::commit();
  171. $this->success('兑换成功');
  172. }
  173. }