Example #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;
     }
 }
Example #2
0
 /**
  * Auther:langxi
  * $member_id:用户id,$product_id:项目id,$money:用户投资钱数
  * 用户投资
  */
 public static function invest($member_id, $product_id, $money)
 {
     ini_set('max_execution_time', 60);
     $member_id = (int) $member_id;
     $product_id = (int) $product_id;
     $money = (double) $money;
     //判断用户是否可进行提现操作
     $is_go = Info::find()->select(['status'])->where(['member_id' => $member_id])->asArray()->one();
     if ($is_go['status'] > 0) {
         throw new ErrorException('处理中,请稍后再试');
     }
     Info::updateAll(['status' => Info::GO_TWO], ['member_id' => $member_id]);
     //进行操作,状态变为投资处理中
     //检测用户投资资金,次数
     $check_invest = self::check_invest($member_id, $product_id, $money);
     if ($check_invest) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException($check_invest);
     }
     //检测用户账户余额是否满足投资金额
     $card = Info::find()->where(['member_id' => $member_id])->asArray()->one();
     if ($card['balance'] < $money) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException('账户金额不足,请进行充值', 6001);
     }
     //检测投资金额是否满足项目的每次投资最大最小额度限制
     $checkMoney = self::checkMoney($product_id, $money);
     if ($checkMoney) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException($checkMoney);
     }
     //检测用户状态
     $checkMember = self::checkMember($member_id);
     if ($checkMember) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException($checkMember);
     }
     //检测项目状态
     $checkProduct = self::checkProduct($product_id, $money);
     if ($checkProduct) {
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException($checkProduct);
     }
     $asset = Info::find()->where(['member_id' => $member_id])->asArray()->one();
     $bank_card = $asset['bank_card'];
     //用户银行卡
     $product = Product::find()->where(['id' => $product_id])->asArray()->one();
     $end_at = $product['end_at'];
     //项目投资终止时间
     //事物回滚
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         //将账户中的钱取出,放入冻结金额中
         $asset = Info::findOne($member_id);
         $asset->balance = $asset['balance'] - $money;
         $asset->freeze = $asset['freeze'] + $money;
         $asset = $asset->save();
         if (!$asset) {
             throw new ErrorException('投资失败', 6002);
         }
         if ($product['type'] == Product::TYPE_THIRD) {
             //进行投资将钱放入order中  债权转让项目订单(一对多)
             $order = new Order();
             $order->member_id = $member_id;
             //用户id
             $order->product_id = $product_id;
             //投资项目id
             $order->type = Product::TYPE_THIRD;
             //投资项目类型
             $order->money = $money;
             //剩余投资金额
             $order->start_money = $money;
             //最初投资金额
             $order->status = Order::STATUS_ACTIVE;
             //订单状态
             $order->start_at = time();
             //订单生效时间
             $order->end_at = $end_at;
             $order = $order->save();
             if (!$order) {
                 throw new ErrorException('投资失败', 6002);
             }
         } else {
             //进行投资将钱放入order中  type=0债权项目(一对一)
             $order = new Order();
             $order->member_id = $member_id;
             //用户id
             $order->product_id = $product_id;
             //投资项目id
             $order->type = Product::TYPE_PRO;
             //投资项目类型
             $order->money = $money;
             //剩余投资金额
             $order->start_money = $money;
             //最初投资金额
             $order->status = Order::STATUS_ACTIVE;
             //订单状态
             $order->start_at = time();
             //订单生效时间
             $order->end_at = $end_at;
             $order = $order->save();
             if (!$order) {
                 throw new ErrorException('投资失败', 6002);
             }
         }
         //检测用户是否投资过该项目,进行项目投资人数加一处理,项目已投金额增长
         $check_people = self::check_people($member_id, $product_id);
         if ($check_people) {
             $product = Product::find()->where(['id' => $product_id])->asArray()->one();
             $product_money = $product['invest_sum'] + $money;
             $total_money = $product['amount'];
             if ($product_money > $total_money) {
                 throw new ErrorException('您投资的金额大于该项目剩余的额度', 6001);
             }
             $product = Product::findOne($product_id);
             $product->invest_sum = $product['invest_sum'] + $money;
             if ($product['invest_sum'] == $product['amount']) {
                 $product->status = Product::STATUS_OUT;
             }
             $product->invest_people = $product['invest_people'] + 1;
             $res = $product->save();
             if (!$res) {
                 throw new ErrorException('项目已投金额、投资人数增加失败', 6002);
             }
         } else {
             $product = Product::find()->where(['id' => $product_id])->asArray()->one();
             $product_money = $product['invest_sum'] + $money;
             $total_money = $product['amount'];
             if ($product_money > $total_money) {
                 throw new ErrorException('您投资的金额大于该项目剩余的额度', 6001);
             }
             $product = Product::findOne($product_id);
             $product->invest_sum = $product['invest_sum'] + $money;
             if ($product['invest_sum'] == $product['amount']) {
                 $product->status = Product::STATUS_OUT;
             }
             $res = $product->save();
             if (!$res) {
                 throw new ErrorException('项目已投金额增加失败', 6002);
             }
         }
         //type=Product::TYPE_THIRD为一对多项目债权转让
         //获取用户此次投资此项目的债权字典
         $thirdArr = self::creditor_dic($product_id, $money);
         //按照生成的债权字典,将钱分配给债权表
         $setthird = self::set_Third($thirdArr, $member_id);
         if (!$setthird) {
             throw new ErrorException('资金分配到债权失败', 6002);
         }
         //删减用户账户冻结金额
         $asset = Info::find()->where(['member_id' => $member_id])->asArray()->one();
         $freeze = $asset['freeze'];
         //获取用户账户新的冻结金额
         $asset = Info::findOne($member_id);
         $asset->freeze = $freeze - $money;
         $asset->invest = $asset['invest'] + $money;
         $asset->total_invest = $asset['total_invest'] + $money;
         //网站投资流程完成,增加用户累计投资额
         $asset = $asset->save();
         if (!$asset) {
             throw new ErrorException('删减冻结金额失败', 6002);
         }
         //投资成功进行投资记录
         $assetlog = new Log();
         $assetlog->member_id = $member_id;
         $assetlog->product_id = $product_id;
         $assetlog->step = $money;
         $assetlog->action = 'Invest/invest';
         $assetlog->status = self::INVESTSUCCEED;
         //2标识投资成功
         $assetlog->bankcard = $bank_card;
         $assetlog->remark = '网站投资成功';
         $assetlog->save();
         //处理债权字典,生成用于第三方代收的数据字典,键值为用户id,值为钱数 , $sina_pay为第三方代收函数需要的参数
         $sina_dic = array();
         foreach ($thirdArr as $v) {
             $sina_dic['m' . $v['maxcreditor']][] = array($v['mcmoney'], $v['maxcreditor']);
             $sina_dic['c' . $v['creditor']][] = array($v['ocmoney'], $v['creditor']);
         }
         $sina_pay = array();
         foreach ($sina_dic as $key => $val) {
             foreach ($val as $k => $v) {
                 if (!isset($sina_pay[$v['1']])) {
                     $sina_pay[$v['1']] = $v['0'];
                 } else {
                     $sina_pay[$v['1']] += $v['0'];
                 }
             }
         }
         //过滤掉金额为空的数组,得到第三方代收函数需要的参数,$sina_pay
         foreach ($sina_pay as $key => $value) {
             if (empty($value)) {
                 unset($sina_pay[$key]);
             }
         }
         //调用封装好的第三方接口
         $sina_invest = sinapay::invest((string) $member_id, (string) $product_id, (string) $money);
         //托管代收接口
         if ($sina_invest['errorNum']) {
             throw new ErrorException($sina_invest['errorMsg'], 7001);
         }
         //更新网站交易记录
         Log::updateAll(['trade_no' => $sina_invest['data']['out_trade_no'], 'remark' => '用户投资,新浪代收成功'], ['id' => $assetlog['id']]);
         $sina_peyee = sinapay::batchPay($sina_pay, $sina_invest['data']['out_trade_no']);
         //托管批量代付接口
         if ($sina_peyee['errorNum']) {
             $sina_refund = sinapay::hostingRefund($sina_invest['data']['identity_id'], $sina_invest['data']['out_trade_no'], $sina_invest['data']['money']);
             if ($sina_refund['errorNum']) {
                 throw new ErrorException($sina_refund['errorMsg'], 7001);
             }
             throw new ErrorException($sina_peyee['errorMsg'], 7001);
         }
         //更新网站交易记录
         Log::updateAll(['remark' => '用户投资成功'], ['id' => $assetlog['id']]);
         //总投资记录
         self::total_log($member_id, $money);
         $transaction->commit();
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         //对投资失败信息进行记录
         $remark = $e->getMessage();
         $assetlog = new Log();
         $assetlog->member_id = $member_id;
         $assetlog->product_id = $product_id;
         $assetlog->step = $money;
         $assetlog->action = 'Invest/Invest';
         $assetlog->status = self::INVESTERROR;
         //-2标识投资失败
         $assetlog->bankcard = $bank_card;
         $assetlog->remark = $remark;
         $assetlog->save();
         Info::updateAll(['status' => '0'], ['member_id' => $member_id]);
         throw new ErrorException($remark, 6002);
     }
 }