Ejemplo n.º 1
0
 /**
  *Auther:langxi
  *
  * 前台用户提现
  * @param $member_id
  * @param $money
  * @throws ErrorException
  */
 public static function withdraw($member_id, $money)
 {
     ini_set('max_execution_time', 60);
     $member_id = (int) $member_id;
     //判断是否为公司员工,若是取新浪余额,使网站余额与新浪余额保持一致
     $is_company = Catmiddle::find()->where(['uid' => $member_id])->andWhere(['or', 'cid=1', 'cid=2'])->asArray()->one();
     if ($is_company) {
         $sina_withdraw = sinapay::querySinaBalance($member_id);
         //获取用户新浪余额
         if ($sina_withdraw['errorNum']) {
             Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
             $result = array('errorNum' => '1', 'errorMsg' => '新浪查询余额' . $sina_withdraw['errorMsg'], 'data' => null);
             return $result;
         }
         $info = Info::find()->select(['balance'])->where(['member_id' => $member_id])->asArray()->one();
         $balance = $info['balance'];
         if ($balance < $sina_withdraw) {
             $info = Info::findOne($member_id);
             $info->balance = $sina_withdraw['data']['available_balance'];
             $info = $info->save();
             if (!$info) {
                 Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
                 $result = array('errorNum' => '1', 'errorMsg' => '更新公司员工账户余额失败', 'data' => null);
                 return $result;
             }
         }
     }
     //判断用户是否可进行提现操作
     $is_go = Info::find()->select(['status'])->where(['member_id' => $member_id])->asArray()->one();
     if ($is_go['status'] > 0) {
         $result = array('errorNum' => '1', 'errorMsg' => '处理中,请稍后再试', 'data' => null);
         return $result;
     }
     Info::updateAll(['status' => Info::GO_FOUR], ['member_id' => $member_id]);
     //进行操作,状态变为提现处理中
     //检测用户状态
     $checkMember = self::checkMember($member_id);
     if ($checkMember) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         return $checkMember;
     }
     //检测提现次数,提现金额
     $check_balance = self::check_balance($member_id, $money);
     if ($check_balance) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         return $check_balance;
     }
     $blance = self::showBlance($member_id);
     //账户余额
     if ($blance < $money) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         $result = array('errorNum' => '1', 'errorMsg' => '提现金额大于余额,请重新输入', 'data' => null);
         return $result;
     }
     $asset = Info::find()->where(['member_id' => $member_id])->asArray()->one();
     $bank_card = $asset['bank_card'];
     //将信息传输给第三方,新浪用户账户余额查询接口
     $sina_withdraw = sinapay::withdraw($member_id, $money);
     if ($sina_withdraw['errorNum']) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         $result = array('errorNum' => '1', 'errorMsg' => '新浪查询余额' . $sina_withdraw['errorMsg'], 'data' => null);
         return $result;
     }
     //事物回滚
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         //再次检测余额是否满足提现要求
         $info = Info::find()->where(['member_id' => $member_id])->asArray()->one();
         if ($info['balance'] < $money) {
             throw new ErrorException('可提现金额不足');
         }
         $asset = Info::findOne($member_id);
         $asset->balance = $asset['balance'] - $money;
         $asset->freeze = $asset['freeze'] + $money;
         $asset = $asset->save();
         if (!$asset) {
             throw new ErrorException('提现失败', 4003);
         }
         //消除冻结金额
         $asset = Info::findOne($member_id);
         $asset->freeze = $asset['freeze'] - $money;
         $asset->save();
         //提现成功进行提现记录
         $assetlog = new Log();
         $assetlog->member_id = $member_id;
         $assetlog->step = $money;
         $assetlog->action = 'Withdrawals/withdraw';
         $assetlog->status = self::WSUCCEED;
         //4为提现成功
         $assetlog->bankcard = $bank_card;
         $assetlog->remark = '提现成功';
         $assetlog->save();
         //.网站账户提现余额:$sina_withdraw['data']['money_site'];新浪赎回金额:$sina_withdraw['data']['money_sina'],
         //新浪用户标识:$sina_withdraw['data']['identity_id'] 中间用于信息获取的订单号$sina_withdraw['data']['out_trade_no']
         //调用新浪提现接口,进行用户账户余额提现到银行卡
         //            $sina_sinawithdraw = sinapay::sianWithdrawOnly($member_id, $money);
         //            if ($sina_sinawithdraw['errorNum']) {
         //                throw new ErrorException('新浪提现失败:' . $sina_sinawithdraw['errorMsg'], 7003);
         //            }
         //T+0操作
         $immediate_withdraw = sinapay::immediate_withdraw($member_id, $money);
         if ($immediate_withdraw['errorNum']) {
             throw new ErrorException('新浪提现失败:' . $immediate_withdraw['errorMsg'], 7003);
         }
         Log::updateAll(['trade_no' => $immediate_withdraw['data']['trade_no']], ['id' => $assetlog['id']]);
         //T+0测试
         //            $immediate_array = array('44','72','74','73','79','77');
         //            if(in_array($member_id,$immediate_array)){
         //                $immediate_withdraw = sinapay::immediate_withdraw($member_id,$money);
         //                if ($immediate_withdraw['errorNum']) {
         //                    throw new ErrorException('新浪提现失败:' . $immediate_withdraw['errorMsg'], 7003);
         //                }
         //                Log::updateAll(['trade_no' => $immediate_withdraw['data']['trade_no']],['id' => $assetlog['id']]);
         //            }else{
         //                // 不是T+0操作
         //                //调用新浪提现接口,进行用户账户余额提现到银行卡
         //                $sina_sinawithdraw = sinapay::sianWithdrawOnly($member_id,$money);
         //                if ($sina_sinawithdraw['errorNum']) {
         //                    throw new ErrorException('新浪提现失败:' . $sina_sinawithdraw['errorMsg'], 7003);
         //                }
         //                Log::updateAll(['trade_no' => $sina_sinawithdraw['data']['trade_no']],['id' => $assetlog['id']]);
         //            }
         $transaction->commit();
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         $result = array('errorNum' => '0', 'errorMsg' => 'success', 'data' => null);
         return $result;
     } catch (\Exception $e) {
         $transaction->rollBack();
         //对提现失败信息进行记录
         $remark = $e->getMessage();
         $assetlog = new Log();
         $assetlog->member_id = $member_id;
         $assetlog->step = $money;
         $assetlog->action = 'Withdrawals/withdraw';
         $assetlog->status = self::WERROR;
         //-4为提现失败
         $assetlog->bankcard = $bank_card;
         $assetlog->remark = $remark;
         $assetlog = $assetlog->save();
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         $result = array('errorNum' => '1', 'errorMsg' => $remark, 'data' => null);
         return $result;
     }
 }