| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- <?php
- /**
- * 渠道充值接口控制器
- *
- */
- namespace app\guildapi\controller;
- use app\guildapi\controller\Guild;
- use app\common\model\ChannelRecharge as ChannelRechargeModel;
- use app\common\library\WeixinPay;
- use think\Db;
- use think\Config;
- use app\common\model\Setting;
- use think\Exception;
- class ChannelRecharge extends Guild {
-
- protected $nowTime;
- protected $payRequestLimit = 5; //重复下单的限制时间
- protected $redis; //redis的句柄对象
-
- /**
- * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
- */
- public function _initialize()
- {
- parent::_initialize();
-
- $this->nowTime = NOW_TIMESTAMP;
- $this->redis = \think\Cache::store('default')->handler();
- }
-
- /**
- * 我的充值列表
- * @return [type] [description]
- */
- public function index()
- {
- //判断当前账号是否有此功能
- if(!in_array($this->_channelLevel,[1,2])){
- $this->jsonResult('', 0, '您无权限使用该功能');
- }
- $list_rows = $this->input('list_rows',10);
- $page = $this->input('page',1);
- $type = $this->input('type','0','intval'); //1(充值收入),2(充值支出),3(直充),4(结算充值收入),5(结算充值支出)
- $account = $this->input('account','','trim'); //收入账号
- $apply_begin_time = $this->input('apply_begin_time','','trim');
- $apply_end_time = $this->input('apply_end_time','','trim');
- $finish_begin_time = $this->input('finish_begin_time','','trim');
- $finish_end_time = $this->input('finish_end_time','','trim');
- $condition = [];
- if($account){
- $condition['channel_name'] = $account;
- }
- if($type==1){
- $condition['channel_id'] = $this->_channelId;
- $condition['recharge_type'] = 1;
- }
- else if($type==2){
- $condition['send_channel_id'] = $this->_channelId;
- $condition['recharge_type'] = 1;
- }
- else if($type==3){
- $condition['channel_id'] = $this->_channelId;
- $condition['recharge_type'] = 2;
- }
- else if($type==4){
- $condition['channel_id'] = $this->_channelId;
- $condition['recharge_type'] = 3;
- }
- else if($type==5){
- $condition['send_channel_id'] = $this->_channelId;
- $condition['recharge_type'] = 3;
- }
- //申请开始时间和结束时间不为空时
- if ($apply_begin_time != '' && $apply_end_time != '') {
- $condition['create_time'] = [
- ['>=', strtotime($apply_begin_time)],
- ['<=', strtotime($apply_end_time . ' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($apply_begin_time != '') {
- $condition['create_time'] = ['>=', strtotime($apply_begin_time)];
- } //结束时间不为空时
- elseif ($apply_end_time != '') {
- $condition['create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
- }
- //到账开始时间和结束时间不为空时
- if ($finish_begin_time != '' && $finish_end_time != '') {
- $condition['finish_time'] = [
- ['>=', strtotime($finish_begin_time)],
- ['<=', strtotime($finish_end_time . ' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($finish_begin_time != '') {
- $condition['finish_time'] = ['>=', strtotime($finish_begin_time)];
- } //结束时间不为空时
- elseif ($finish_end_time != '') {
- $condition['finish_time'] = ['<=', strtotime($finish_end_time . ' 23:59:59')];
- }
- // var_dump($condition);
- $rechargeList = model('ChannelRecharge')
- ->where($condition)
- ->whereRaw('channel_id='.$this->_channelId.' or send_channel_id='.$this->_channelId)
- ->paginate(['list_rows'=>$list_rows,'page'=>$page])
- ->toArray();
- // echo model('ChannelRecharge')->getLastSql()."-----lastsql------<br>";
- $this->jsonResult($rechargeList, 20000, '获取列表成功');
- }
- /**
- * 充值审核列表
- * @return [type] [description]
- */
- public function checklist()
- {
- //判断当前账号是否有此功能
- if(!in_array($this->_channelLevel,[1])){
- $this->jsonResult('', 0, '您无权限使用该功能');
- }
-
- $list_rows = input('list_rows',10);
- $page = input('page',1);
- $account = $this->input('account','','trim');
- $orderid = $this->input('orderid','','trim');
- $apply_begin_time = $this->input('apply_begin_time','','trim');
- $apply_end_time = $this->input('apply_end_time','','trim');
- $condition = [];
- $condition['send_channel_id'] = $this->_channelId;
- if($orderid){
- $condition['orderid'] = $orderid;
- }
- if($account){
- $condition['channel_name'] = $account;
- }
- //申请开始时间和结束时间不为空时
- if ($apply_begin_time != '' && $apply_end_time != '') {
- $condition['create_time'] = [
- ['>=', strtotime($apply_begin_time)],
- ['<=', strtotime($apply_end_time . ' 23:59:59')],
- ];
- } //开始时间不为空时
- elseif ($apply_begin_time != '') {
- $condition['create_time'] = ['>=', strtotime($apply_begin_time)];
- } //结束时间不为空时
- elseif ($apply_end_time != '') {
- $condition['create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
- }
- $rechargeList = model('ChannelRecharge')
- ->where($condition)
- ->paginate(['list_rows'=>$list_rows,'page'=>$page])
- ->toArray();
- $this->jsonResult($rechargeList, 20000, '获取列表成功');
- }
- /**
- * 充值审核(查看)
- */
- public function audit(){
- if ($this->request->isPost()) {
- $id = intval($this->input('id')); //渠道ID
-
- if(!in_array($this->_channelLevel,[1])){
- $this->jsonResult('', 0, '您不能进行该操作');
- }
- if(!$id){
- $this->jsonResult('', 0, '请选择要审核的记录');
- }
- $rechargeInfo = model('ChannelRecharge')->where(['id'=>$id,'send_channel_id'=>$this->_channelId])->find();
- if(empty($rechargeInfo)){
- $this->jsonResult('', 0, '记录不存在或您无权限操作');
- }
-
- $this->jsonResult($rechargeInfo, 20000, '获取记录成功');
- exit;
- }
- else{
- $this->jsonResult('', 0, '非法请求');
- }
- }
-
- //充值审核(操作)
- function doAudit(){
- if ($this->request->isPost()) {
- $id = intval($this->input('id')); //渠道ID
- $status = intval($this->input('status')); //审核状态:1(审核通过),2(审核不通过)
- $remark = $this->input('remark','','trim'); //审核备注
- if(!in_array($this->_channelLevel,[1])){
- $this->jsonResult('', 0, '您不能进行该操作');
- }
- if(!$id){
- $this->jsonResult('', 0, '请选择要审核的记录');
- }
- if($status<>1 && $status<>2){
- $this->jsonResult('', 0, '非法审核状态');
- }
- $rechargeInfo = model('ChannelRecharge')->where(['id'=>$id,'send_channel_id'=>$this->_channelId])->find();
- if(empty($rechargeInfo)){
- $this->jsonResult('', 0, '记录不存在或您无权限操作');
- }
- else if($rechargeInfo['status']<>0){
- $this->jsonResult('', 0, '该记录已审核过');
- }
- $channelInfo = model("Channel")->where(['id'=>$this->_channelId])->find();
- if($channelInfo){
- if($channelInfo['amount'] < $rechargeInfo['amount']){
- $this->jsonResult('', 0, '您当前余额不足,不能进行审核发币');
- }
- }
- else{
- $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
- }
- $auditData = array();
- $auditData['id'] = $id;
- $auditData['status'] = $status;
- $auditData['finish_time'] = NOW_TIMESTAMP;
- $auditData['check_remark'] = $remark;
- $auditData['check_admin_type'] = 2;
- $auditData['check_admin_name'] = $this->_channelName;
- Db::startTrans();
- try {
- //审核通过
- if($status==1){
- $detData = array();
- $detData['channel_id'] = $this->_channelId;
- $detData['channel_name'] = $this->_channelName;
- $detData['change_amount'] = -$rechargeInfo['amount'];
- $detData['account_type'] = 1; //通用账户
- $detData['type'] = 5; //充值支出
- $detData['out_orderid'] = $rechargeInfo['orderid'];
- $detData['create_time'] = NOW_TIMESTAMP;
- $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
- if ( !$insertDetId) {
- throw new Exception("添加账户变动明细失败");
- }
- $updData = array();
- $updData['amount'] = Db::raw("amount-".$rechargeInfo['amount']);
- $updData['update_time'] = time();
- $updFromResult = model('Channel')->where(['id'=>$this->_channelId,'amount'=>array('egt',$rechargeInfo['amount'])])->update($updData);
- if (!$updFromResult) {
- throw new Exception("账户金额变动失败");
- }
- $detData = array();
- $detData['channel_id'] = $rechargeInfo['channel_id'];
- $detData['channel_name'] = $rechargeInfo['channel_name'];
- $detData['change_amount'] = $rechargeInfo['amount'];
- $detData['account_type'] = 1; //通用账户
- $detData['type'] = 4; //充值收入
- $detData['out_orderid'] = $rechargeInfo['orderid'];
- $detData['create_time'] = NOW_TIMESTAMP;
- $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
- if ( !$insertDetId) {
- throw new Exception("添加账户变动明细失败");
- }
- $updData = array();
- $updData['amount'] = Db::raw("amount+".$rechargeInfo['amount']);
- $updData['update_time'] = time();
- $updToResult = model('Channel')->where(['id'=>$rechargeInfo['channel_id']])->update($updData);
- if (!$updToResult) {
- throw new Exception("账户金额变动失败");
- }
-
- $auditData['finish_status'] = 1;
- $result = model('ChannelRecharge')->where(['id'=>$id,'status'=>array('in',[0])])->update($auditData);
- if (!$result) {
- throw new Exception("充值审核失败");
- }
- }
- else if($status==2){ //审核不通过
- $result = model('ChannelRecharge')->where(['id'=>$id,'status'=>array('in',[0])])->update($auditData);
- if (!$result) {
- throw new Exception("充值审核失败");
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->jsonResult('', 0, '添加失败: ' . $e->getMessage());
- }
- $this->jsonResult('', 20000, '审核成功');
- exit;
- }
- else{
- $this->jsonResult('', 0, '非法请求');
- }
- }
- /**
- * 充值处理
- */
- public function recharge()
- {
- if ($this->request->isPost()) {
- $amount = $this->input('amount'); //充值金额
- $real_amount = $this->input('real_amount'); //支付金额
- $recharge_type = $this->input('recharge_type'); //充值方式:1(申请),2(直充),3(结算币充值)
- $paytype = $this->input('paytype'); //支付类型:zfb(支付宝),wxpay(微信)
- $out_order_no = $this->input('out_order_no'); //交易凭证
- $remark = $this->input('remark'); //备注
-
- $orderid = 'R'.makeOrderid();
-
- $checkResult = $this->validate($this->input,'ChannelRecharge.add');
- if (true !== $checkResult) {
- $this->jsonResult('', 0, $checkResult);
- }
-
- //判断当前账号是否有充值权限
- if(!in_array($this->_channelLevel,[1,2])){
- $this->jsonResult('', 0, '您不能进行充值');
- }
- if(!in_array($recharge_type,[1,2,3])){
- $this->jsonResult('', 0, '非正确的充值方式');
- }
- else{
- if($recharge_type==2 && !in_array($paytype,['zfb','wxpay'])){
- $this->jsonResult('', 0, '非正确的支付类型');
- }
- }
- if(abs(intval($real_amount*100)/100-$real_amount) > 0.01){
- $this->jsonResult('', 0, '请正确填写支付金额!');
- }
- else{
- //判断支付金额
- $condis = array();
- $condis['channel_id'] = $this->_channelId;
- $condis['begin_time'] = array('elt',$this->nowTime);
- $condis['end_time'] = array('egt',$this->nowTime);
- $rechargeRatio = model("ChannelRechargeRatio")->where($condis)->find();
- if(!empty($rechargeRatio) && $rechargeRatio['ratio']){
- $rechargeRatio = $rechargeRatio['ratio'];
- }
- else{
- $rechargeRatio = 100;
- }
- $discountAmt = floatval($amount*$rechargeRatio/100);
- if($discountAmt <> $real_amount){
- $this->jsonResult('', 0, '充值金额异常,请刷新页面后重新充值');
- }
- }
- $channelInfo = model("Channel")->where(['id'=>$this->_channelId])->find();
- if($channelInfo && $channelInfo['parent_id']){
- if($recharge_type==3 && $channelInfo['js_amount']<$amount){
- $this->jsonResult('', 0, '您的结算币余额不足');
- }
- }
- else{
- $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
- }
- //指定时间内,禁止重复下单
- if(!requestDuplicateCheck('recharge_pay_duplicate_'.$this->_channelId,$this->payRequestLimit)){
- $this->jsonResult('', 0, '充值请求过多,请于'.$this->payRequestLimit.'s以后,再次进行充值操作');
- }
- $rechargeData = array();
- $rechargeData['orderid'] = $orderid;
- $rechargeData['channel_id'] = $this->_channelId;
- $rechargeData['channel_name'] = $this->_channelName;
- if($this->_channelLevel==1){
- $rechargeData['send_channel_id'] = '0';
- $rechargeData['send_channel_name'] = '平台';
- }
- else if($this->_channelLevel==2){
- $parentChannelInfo = model("Channel")->where(['id'=>$channelInfo['parent_id']])->find();
- $rechargeData['send_channel_id'] = $parentChannelInfo['id'];
- $rechargeData['send_channel_name'] = $parentChannelInfo['name'];
- }
- $rechargeData['amount'] = $amount;
- $rechargeData['real_amount'] = $real_amount;
- $rechargeData['recharge_type'] = $recharge_type;
- $rechargeData['paytype'] = $paytype;
- $rechargeData['out_order_no'] = $out_order_no;
- $rechargeData['remark'] = $remark;
- $rechargeData['status'] = 0;
- $rechargeData['create_time'] = time();
- // 启动事务
- Db::startTrans();
-
- try{
- $result = ''; //返回值
-
- //结算币充值
- if($recharge_type==3){
- $rechargeData['status'] = 1;
- $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
- if(!$insertRechargeId){
- throw new Exception("创建订单失败! ");
- }
- $detData = array();
- $detData['channel_id'] = $this->_channelId;
- $detData['channel_name'] = $this->_channelName;
- $detData['change_amount'] = -$amount;
- $detData['account_type'] = 2; //结算账户
- $detData['type'] = 7; //结算充值
- $detData['out_orderid'] = $orderid;
- $detData['create_time'] = NOW_TIMESTAMP;
- $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
- if ( !$insertDetId) {
- throw new Exception("添加账户变动明细失败");
- }
- $updData = array();
- $updData['js_amount'] = Db::raw("js_amount-".$amount);
- $updData['update_time'] = time();
- $updResult = model('Channel')->where(['id'=>$this->_channelId,'js_amount'=>array('egt',$amount)])->update($updData);
- if (!$updResult) {
- throw new Exception("账户金额变动失败");
- }
- $detData = array();
- $detData['channel_id'] = $this->_channelId;
- $detData['channel_name'] = $this->_channelName;
- $detData['change_amount'] = $amount;
- $detData['account_type'] = 1; //通用账户
- $detData['type'] = 4; //充值收入
- $detData['out_orderid'] = $orderid;
- $detData['create_time'] = NOW_TIMESTAMP;
- $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
- if ( !$insertDetId) {
- throw new Exception("添加账户变动明细失败");
- }
- $updData = array();
- $updData['amount'] = Db::raw("amount+".$amount);
- $updData['update_time'] = time();
- $updResult = model('Channel')->where(['id'=>$this->_channelId])->update($updData);
- if (!$updResult) {
- throw new Exception("账户金额变动失败");
- }
- }
- else{
- $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
- if(!$insertRechargeId){
- throw new Exception("创建订单失败! ");
- }
- }
- //第三方支付用的商品简单描述 (去除&+等特殊字符)
- $payBody = "用户充值";
-
- //支付方式是支付宝或者支付宝混合支付时
- if($paytype=='zfb'){
- //支付宝支付参数
- $alipayParam = $this->getAlipayAopParam($orderid,$rechargeData['real_amount'],$payBody);
-
- $result = ['alipay_param'=>$alipayParam];
- }
- //支付方式是微信官方h5支付或者微信官方h5混合支付时
- elseif($paytype=='wxpay')
- {
- $wxpayh5_param = $this->getWxpayH5Param($orderid,$rechargeData['real_amount'],$payBody);
-
- $result = ['wxpayh5_param'=>$wxpayh5_param];
- }
-
- $this->redis->del('recharge_pay_duplicate_'.$this->_channelId);
-
- // 提交事务
- Db::commit();
-
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
-
- $this->jsonResult('', 0, '订单生成失败'.$e->getMessage());
- }
-
- $result['orderid'] = $orderid;
-
- $this->jsonResult($result, 20000, '订单生成成功');
- exit;
- }
- else{
- $this->jsonResult('', 0, '非法请求');
- exit;
- }
- }
-
- /**
- * 获取支付宝支付参数
- *
- * @param string $orderid 订单号
- * @param float $real_amount 支付金额
- * @param string $productname 商品名称
- * @param string $productdesc 描述
- *
- * @return string 支付参数字符串
- */
- private function getAlipayAopParam($orderid,$real_amount,$productname,$productdesc='')
- {
- //Alipay Aop Sdk支付发起
- vendor('alipayAop.AopSdk');
- $aop = new \AopClient();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = Config::get('alipayAop')['appId'];
- $aop->rsaPrivateKey = file_get_contents(Config::get('alipayAop')['private_key']);
- $aop->alipayrsaPublicKey = file_get_contents(Config::get('alipayAop')['alipay_public_key']);
- $aop->apiVersion = '1.0';
- $aop->signType = Config::get('alipayAop')['sign_type'];
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $request = new \AlipayTradeAppPayRequest();
- $bizcontent = array();
- $bizcontent['subject'] = preg_replace("/[\&\+]+/", '', $productname); // 主题(过滤特殊字符)
- $bizcontent['body'] = preg_replace("/[\&\+]+/", '', $productdesc); // 商品描述信息(过滤特殊字符)
- $bizcontent['out_trade_no'] = $orderid; // 订单号
- $bizcontent['timeout_express'] = '30m'; // 超时时间
- $bizcontent['total_amount'] = $real_amount; // 支付金额
- $bizcontent['product_code'] = 'QUICK_MSECURITY_PAY'; // 支付方式
- $request->setBizContent(json_encode($bizcontent));
- $request->setNotifyUrl(Config::get('alipayAop')['notify_url']);
- $result = $aop->sdkExecute($request);
- // echo htmlspecialchars($result);
- return $result;
-
- }
- /**
- * 微信官方h5支付
- *
- * @param string $orderid 订单号
- * @param float $real_amount 支付金额
- * @param string $body 商品名称
- *
- * @return array 支付参数数组
- */
- private function getWxpayH5Param($orderid,$real_amount,$body)
- {
- $pay_return = false;
-
- $wxPay = new WeixinPay;
-
- $params['appid'] = Config::get('wxpay-h5')['appid'];
- $params['mch_id'] = Config::get('wxpay-h5')['mch_id'];
- $params['nonce_str'] = random(10);
- $params['body'] = $body;
- $params['out_trade_no'] = $orderid;
- $params['total_fee'] = $real_amount*100; // 支付金额,以分为单位
- $params['spbill_create_ip'] = $this->request->ip();
- $params['notify_url'] = Config::get('wxpay-h5')['notify_url'];
- $params['trade_type'] = 'MWEB';
- $params['scene_info'] = '{"h5_info":{"type":"Wap","wap_url":"http://m.weilongwl.com","wap_name":"麻花网络游戏"}}';
- //获取签名数据
- $params['sign'] = $wxPay->MakeSign($params,Config::get('wxpay-h5')['key']);
-
-
- $xml = dataToXml($params);
- $response = $wxPay->postXmlCurl($xml);
- if (!$response) {
- return false;
- }
-
- $result = $wxPay->xmlToArray($response);
-
- if( array_key_exists("return_code", $result) && $result["return_code"] == "SUCCESS" && array_key_exists("result_code", $result) && $result["result_code"] == "SUCCESS")
- {
- $pay_return['prepay_id'] = $result['prepay_id'];
- $pay_return['trade_type'] = $result['trade_type'];
- $pay_return['mweb_url'] = $result['mweb_url'];
- $pay_return['referer'] = Config::get('wxpay-h5')['referer'];
- }
- else{
- throw new \Exception(' 错误代码:'.$result["err_code"].' 错误代码描述:'.$result["err_code_des"]);
- }
-
- return $pay_return;
- }
-
- }
|