| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- <?php
- namespace addons\HiGo\common\models;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\logic\HiBeiLogic;
- use addons\HiGo\common\logic\WalletLogic;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\PayTypeEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\globals\GlobalInvoke;
- use common\helpers\LockHelper;
- use common\models\common\Download;
- use common\models\common\PaidSinglePayLog;
- use common\models\common\PaymentTransfer;
- use common\enums\AssetsType;
- use common\models\base\BaseActiveRecord;
- use common\models\user\User;
- use common\models\user\UserInfo;
- use services\common\PayService;
- use services\common\UserWalletService;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%higo_withdraw_log}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property string $order_no 提现订单号
- * @property float $num 提现金额
- * @property float $actual_num 实际到账金额
- * @property float $poundage_num 手续费扣除金额
- * @property float $poundage_ratio 手续费比例
- * @property int $from 提现来源1用户;2商户
- * @property string $type 提现方式 auto--自动打款 wechat--微信打款 alipay--支付宝打款 bank--银行转账 balance--打款到余额
- * @property string $extra 额外信息 例如微信账号、支付宝账号等
- * @property string $content 用户额外输入信息
- * @property int $status 提现状态 0--申请 1--同意 2--已打款 3--驳回 4--失败
- * @property string|null $transfer 支付宝打款信息
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class HigoWithdrawLog extends BaseActiveRecord
- {
- const FROM_BALANCE = 2;
- const FROM_INCOME = 1;
- const STATUS_APPLY = 0;
- const STATUS_AGREE = 1;
- const STATUS_ALREADY_TRANS = 2;
- const STATUS_ALREADY_REJECT = 3;
- const STATUS_FAIL = 4;
- const STATUS_WAIT = 5;
- //提现状态
- public static $status_msg = [
- self::STATUS_APPLY => '申请',
- self::STATUS_AGREE => '同意',
- self::STATUS_ALREADY_TRANS => '已打款',
- self::STATUS_ALREADY_REJECT => '驳回',
- self::STATUS_FAIL => '失败',
- self::STATUS_WAIT => '打款中'
- ];
- //打款类型
- public static $type_msg = [
- 'auto' => '自动打款',
- 'wechat' => '微信',
- 'alipay' => '支付宝',
- 'bank' => '银行转账',
- 'balance' => '打款到余额'
- ];
- /**
- * @var float|int|mixed|null
- */
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_higo_withdraw_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'num', 'actual_num', 'extra', 'content', 'from', 'type'], 'required'],
- [['id', 'mall_id', 'user_id', 'from', 'status', 'created_at', 'updated_at'], 'integer'],
- [['num', 'actual_num', 'poundage_num', 'poundage_ratio',], 'number'],
- [['extra', 'content', 'transfer'], 'string'],
- [['order_no'], 'string', 'max' => 255],
- [['type'], 'string', 'max' => 45],
- [['from'], 'in', 'range' => [1, 2]],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'order_no' => 'Order No',
- 'num' => 'Num',
- 'actual_num' => 'Actual Num',
- 'poundage_num' => 'Poundage Num',
- 'poundage_ratio' => 'Poundage Ratio',
- 'from' => 'From',
- 'type' => 'Type',
- 'extra' => 'Extra',
- 'content' => 'Content',
- 'status' => 'Status',
- 'transfer' => 'Transfer',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function fieldsList($field = null)
- {
- $fields = [
- 'user_id' => '会员ID',
- // 'user_info' => '基本信息',
- 'username' => '会员编号',
- 'nickname' => '会员昵称',
- 'user_mobile' => '手机号码',
- 'from' => '提现类型',
- // 'type' => '账户信息',
- 'alipay_name' => '支付宝姓名',
- 'mobile' => '支付宝账号',
- 'bank_account_type' => '账户类型',
- 'name' => '持卡/开户人',
- 'bank_name' => '开户行',
- 'bank_account' => '银行卡号',
- 'num' => '提现金额',
- 'actual_num' => '实际到账金额',
- 'poundage_num' => '手续费',
- 'status' => '状态',
- 'updated_at' => '时间',
- 'remark' => '备注',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- public function saveRecord($params)
- {
- if ($params['price'] <= 0) {
- return [
- 'state' => false,
- 'msg' => '提现金额必须大于0!'
- ];
- }
- $exists = self::find()->where([
- 'mall_id' => $params['mall_id'], 'status' => 0, 'user_id' => $params['user_id'], 'from' => $params['from']
- ])->asArray()->one();
- if ($exists) {
- return [
- 'state' => false,
- 'msg' => '尚有未审核的提现申请!'
- ];
- }
- $higo_wallet = HigoWallet::getOne([['user_id' => $params['user_id']], ['status' => StatusEnum::ENABLED], ['mall_id' => $params['mall_id']]], true, [], false, 'hi_bei');
- $hi_bei = $higo_wallet['hi_bei'] ?? 0;
- if ($hi_bei < $params['price']) {
- return [
- 'state' => false,
- 'msg' => '提现金额超出可提现金额!'
- ];
- }
- $withdraw = Yii::$app->services->setting->addons->get($this->mall_id, HiGoEnum::ADDONS_NAME, 'withdraw');
- $cash_status = $withdraw['cash_status'] ? json_decode($withdraw['cash_status'], true) : 0;
- $cash_status = is_array($cash_status) ? $cash_status['value'] : $cash_status;
- if (!$cash_status) {
- return [
- 'state' => false,
- 'msg' => '暂未开启提现功能!'
- ];
- }
- $cash_service_fee = $withdraw['cash_service_fee'] ? json_decode($withdraw['cash_service_fee'], true) : 0;
- $poundage_ratio = is_array($cash_service_fee) ? $cash_service_fee['value'] : $cash_service_fee;
- $wallet_type = HiGoEnum::WALLET_TYPE_HI_BEI;
- $balance_config = \Yii::$app->services->setting->mall->get($params['mall_id'], 'pay.balance');
- if ($balance_config) {
- $balance_config = Json::decode((string)$balance_config);
- if ($balance_config['pay_password_status'] == 1) {
- $transactionPassword = $params['transaction_password'];
- $password = User::getOne([['id' => $params['user_id']]], true, '', '', 'transaction_password');
- if (!$params['transaction_password']) {
- return [
- 'state' => false,
- 'msg' => '请输入支付密码!'
- ];
- }
- if (empty($password['transaction_password'])) {
- return [
- 'state' => false,
- 'msg' => '您未设置支付密码!'
- ];
- }
- if (empty($transactionPassword)) {
- return [
- 'state' => false,
- 'msg' => '请输入交易密码!'
- ];
- }
- if (!\Yii::$app->getSecurity()->validatePassword(trim($transactionPassword), $password['transaction_password'])) {
- return [
- 'state' => false,
- 'msg' => '支付密码错误!'
- ];
- }
- }
- }
- if (!$params['type']) {
- return [
- 'state' => false,
- 'msg' => '请选择提现方式!'
- ];
- }
- if ($params['type'] == 'wechat') {
- if (!isset($params['wechat_qrcode'])) {
- return [
- 'state' => false,
- 'msg' => '请上传微信收款码!'
- ];
- }
- }
- if ($params['type'] == 'alipay') {
- if (!$params['mobile']) {
- return [
- 'state' => false,
- 'msg' => '请填写支付宝账号!'
- ];
- }
- if (!$params['name']) {
- return [
- 'state' => false,
- 'msg' => '请填写支付宝姓名!'
- ];
- }
- }
- if ($params['type'] == 'third_party_address') {
- if (!$params['address']) {
- return [
- 'state' => false,
- 'msg' => '请填写第三地址提现地址'
- ];
- }
- }
- if ($params['type'] == 'paid') { //代付银行卡
- if (!$params['name']) {
- return [
- 'state' => false,
- 'msg' => '请填写持卡人!'
- ];
- }
- if (!$params['bank_account']) {
- return [
- 'state' => false,
- 'msg' => '请填写银行卡号!'
- ];
- }
- }
- if ($params['type'] == 'alipay_tax') {
- if (!$params['mobile']) {
- return [
- 'state' => false,
- 'msg' => '请填写支付宝账号(手机号)'
- ];
- }
- if (!$params['name']) {
- return [
- 'state' => false,
- 'msg' => '请填写支付宝姓名'
- ];
- }
- if (!$params['id_card']) {
- return [
- 'state' => false,
- 'msg' => '请填写身份证信息'
- ];
- }
- $service = new \addons\AvoidTax\common\service\UserService(['mall_id' => $this->mall_id]);
- $res = $service->checkSign($this->user_id);
- if ($res === false) {
- $res = $service->goToSing($this->user_id, $params['mobile'], $params['id_card'], $params['name']);
- if (!$res) {
- return [
- 'state' => false,
- 'msg' => $service->error
- ];
- }
- // 跳转url签约
- return [
- 'state' => true,
- 'data' => $res
- ];
- }
- }
- $hi_bei_now_price = HiBeiLogic::getNowPrice($params['mall_id']);
- $cash = new self();
- $cash->mall_id = $params['mall_id'];
- $cash->user_id = $params['user_id'];
- $cash->num = $params['price'];
- $cash->actual_num = $params['price'];
- $cash->content = Json::encode(['user_content' => $params['content']]);
- $cash->from = $params['from'];
- $cash->type = $params['type'];
- $cash->extra = Json::encode([
- 'name' => $params['name'],
- 'mobile' => $params['mobile'],
- 'bank_name' => $params['bank_name'],
- 'bank_account' => (string)$params['bank_account'],
- 'bank_account_type' => $params['bank_account_type'],
- 'wechat_qrcode' => $params['wechat_qrcode'],
- 'third_party_address' => $params['address'],
- 'now_price' => $hi_bei_now_price
- ], JSON_UNESCAPED_UNICODE);
- if ($poundage_ratio < 0 || $poundage_ratio > 100) {
- return [
- 'state' => false,
- 'msg' => '手续费设置不正确!'
- ];
- }
- //手续费
- $poundage = 0;
- if ($poundage_ratio > 0 && $poundage_ratio < 100) {
- //手续费:取小数点2位,截断
- $poundage = bcmul($poundage_ratio / 100, $params['price'], 4);
- // 四舍五入,保留2位
- $poundage = number_format($poundage, 2, '.', '');
- $cash->actual_num = bcsub($params['price'], $poundage, 2);
- $cash->poundage_num = $poundage;
- $cash->poundage_ratio = $poundage_ratio;
- }
- if (intval(bcmul($cash->actual_num, 100)) < 1) {
- return [
- 'state' => false,
- 'msg' => '提现金额扣除费用后金额不足!'
- ];
- }
- //计算当前价格
- $cash->actual_num = bcmul($cash->actual_num,$hi_bei_now_price,2);
- $t = \Yii::$app->db->beginTransaction();
- $basic = HiGoEnum::getHigoWalletText($params['mall_id']);
- $hi_bei_name = $basic[HiGoEnum::WALLET_TYPE_HI_BEI];
- try {
- $cash->order_no = $this->getCashOrder($params['mall_id']);
- $desc = $hi_bei_name . '提现';
- $desc .= $cash->num;
- if ($poundage > 0) {
- $desc .= ':手续费¥' . $poundage;
- }
- if (!$cash->save()) {
- throw new Exception($cash->getErrorMessage());
- }
- $res_data = [
- 'state' => true,
- 'data' => ['cash_id' => $cash->id],
- 'msg' => ''
- ];
- $t->commit();
- $wallet_log = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'change_num' => bcmul($params['price'], -1, 20),
- 'desc' => $desc,
- 'source_table' => self::tableName(),
- 'source_table_id' => $cash->id,
- 'from_type' => HiGoEnum::FROM_TYPE_WITHDRAW_HI_BEI,
- 'wallet_type' => $wallet_type,
- 'is_send' => false
- ];
- $result = WalletLogic::handleInQueue($wallet_log);
- if (!$result) {
- $cash->status = 4;
- $cash->save();
- return [
- 'state' => false,
- 'msg' => '提现失败,系统繁忙!'
- ];
- }
- } catch (Exception $e) {
- $t->rollBack();
- \Yii::error($e->getMessage());
- return [
- 'state' => false,
- 'msg' => '提现失败!' . $e->getMessage()
- ];
- }
- return $res_data;
- }
- private function getCashOrder($mall_id): string
- {
- $order_no = null;
- while (true) {
- $order_no = 'HG' . date('YmdHis') . rand(10000, 99999);
- $exist = self::find()->where(['mall_id' => $mall_id, 'order_no' => $order_no,])->asArray()->one();
- if (!$exist) {
- break;
- }
- }
- return $order_no;
- }
- public function searchLogList($get): array
- {
- $mall_id = $get['mall_id'];
- $params['alias'] = 'b';
- $params['joinWith'] = ['user'];
- if ($get['status'] && $get['status'] >= 0) {
- $params['where'][] = ['=', 'b.status', $get['status']];
- }
- if ($get['status'] == 0) {
- $params['where'][] = ['=', 'b.status', $get['status']];
- }
- if (!empty($get['keyword'])) {
- $params['where'] = [[
- 'or',
- ['like', 'nickname', $get['keyword']],
- ['like', 'mobile', $get['keyword']]
- ]];
- }
- if (!empty($get['user_id'])) {
- $params['where'][] = ['=', 'user_id', $get['user_id']];
- }
- if (!empty($get['withdraw_type'])) {
- $params['where'][] = ['=', 'b.type', $get['withdraw_type']];
- }
- if (!empty($get['withdraw_from'])) {
- $params['where'][] = ['=', 'b.from', $get['withdraw_from']];
- }
- if (!empty($get['start_date']) && !empty($get['end_date'])) {
- $get['start_date'] = str_replace('+', ' ', $get['start_date']);
- $get['end_date'] = str_replace('+', ' ', $get['end_date']);
- $params['where'][] = ['>=', 'b.created_at', strtotime($get['start_date'])];
- $params['where'][] = ['<=', 'b.created_at', strtotime($get['end_date'])];
- }
- $params['where'][] = ['=', 'b.mall_id', $mall_id];
- $params['order'] = ['status' => SORT_ASC, 'updated_at' => SORT_DESC];
- $params['limit'] = 10;
- $list = self::listPage($params, false, false);
- $newList = [];
- foreach ($list['list'] as $item) {
- $serviceCharge = $item->poundage_num;
- $fact_price = $item->actual_num;
- $extra = $item->extra ? json_decode($item->extra, true) : [];
- if (!empty($extra)) $extra['bank_account'] = (string)$extra['bank_account'];
- if ($item->type == 'paid' || $item->type == 'joinpay') {
- //汇聚代付
- $pay_log = PaidSinglePayLog::getOne([['mall_id' => $item->mall_id, 'order_no' => $item->order_no]]);
- if ($pay_log) {
- if ($pay_log->status == PaidSinglePayLog::STATUS_SUCCESS) {
- $joinpay_msg = '打款成功';
- } else {
- $joinpay_msg = $pay_log->error_msg ?: '打款中';
- }
- }
- }
- $total_num = bcsub($item->num,$serviceCharge,2);
- $now_price = bcdiv($fact_price,$total_num,2);
- $newItem = [
- 'id' => $item->id,
- 'from' => $item->from,
- 'order_no' => $item->order_no,
- 'type' => $item->type,
- 'status' => $item->status,
- 'extra' => $extra,
- 'created_at' => $item->created_at,
- 'updated_at' => $item->updated_at,
- 'content' => $item->content ? Json::decode($item->content) : [],
- 'user' => [
- 'avatar' => isset($item->user->avatar_url) ? $item->user->avatar_url : '',
- 'nickname' => isset($item->user->nickname) ? $item->user->nickname : '',
- 'id' => isset($item->user->id) ? $item->user->id : '',
- 'mobile' => isset($item->user->mobile) ? $item->user->mobile : '',
- ],
- 'cash' => [
- 'price' => round($item->num, 2),
- 'service_fee_rate' => bcadd($serviceCharge,0,2),
- 'fact_price' => bcadd($fact_price,0,2),
- 'now_price' => $now_price
- ],
- 'joinpay_msg' => $joinpay_msg
- ];
- $newList[] = $newItem;
- }
- $export_list = self::fieldsList();
- return [
- 'code' => 0,
- 'data' => [
- 'list' => $newList,
- 'export_list' => $export_list,
- 'pagination' => $list['pagination'],
- ]
- ];
- }
- /**
- * @throws Exception
- */
- public function updateLog(): bool
- {
- $where = [['id' => $this->id]];
- $cash = self::getOne($where);
- if (!$cash) {
- throw new \Exception('提现记录不存在');
- }
- if ($cash->status == 2) {
- throw new \Exception('提现已打款');
- }
- if ($cash->status == 3) {
- throw new \Exception('提现已被驳回');
- }
- if ($this->status <= $cash->status) {
- throw new \Exception('状态错误, 请刷新重试');
- }
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_USER_CASH, $cash->user_id);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
- \Yii::error('cashForm 获取锁(' . $lock_key . ')失败阻塞,提现打款:cash,操作:save,金额:' . $cash->actual_num);
- return false;
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- switch ($this->status) {
- case 1:
- $this->validateCash($cash);
- break;
- case 2:
- $this->remit($cash);
- break;
- case 3:
- $this->reject($cash);
- break;
- default:
- throw new \Exception('错误的提现类型');
- }
- $t->commit();
- LockHelper::uLock($lock_key, $identification);
- } catch (\Exception $exception) {
- $t->rollBack();
- LockHelper::uLock($lock_key, $identification);
- \Yii::error("finance cashForm error=" . $exception->getFile() . ";line:" . $exception->getLine() . ";message:" . $exception->getMessage());
- throw new \Exception($exception->getMessage());
- }
- return true;
- }
- /**
- * 申请
- * @param $cash
- * @return bool
- * @throws Exception
- */
- private function validateCash($cash): bool
- {
- if (!$cash->content) {
- $content = [];
- } else {
- $content = Json::decode($cash->content);
- }
- $cash->status = 1;
- $content['validate_content'] = $this->content ?: '审核通过';
- $cash->content = Json::encode($content);
- if (!$cash->save()) {
- throw new \Exception('失败');
- }
- return true;
- }
- /**
- * @Author: 广东七件事 ganxiaohao
- * @Date: 2020-05-30
- * @Time: 11:22
- * @Note:拒绝
- * @param $cash
- * @return void
- * @throws \Exception
- */
- private function reject($cash): void
- {
- if (!$this->content) {
- throw new \Exception('请填写备注');
- }
- if (!$cash->content) {
- $content = [];
- } else {
- $content = Json::decode($cash->content);
- }
- $cash->status = 3;
- $content['reject_content'] = $this->content;
- $cash->content = Json::encode($content);
- if (!$cash->save()) {
- throw new \Exception('失败');
- }
- $wallet_type = HiGoEnum::WALLET_TYPE_HI_BEI;
- $basic = HiGoEnum::getHigoWalletText($cash->mall_id);
- $hi_bei_name = $basic[HiGoEnum::WALLET_TYPE_HI_BEI];
- $desc = $hi_bei_name."提现申请被驳回,驳回原因:" . $this->content ?? '';
- $wallet_log = [
- 'mall_id' => $cash->mall_id,
- 'user_id' => $cash->user_id,
- 'change_num' => abs($cash->num),
- 'desc' => $desc,
- 'source_table' => self::tableName(),
- 'source_table_id' => $cash->id,
- 'from_type' => HiGoEnum::FROM_TYPE_WITHDRAW_REFUSE_HI_BEI,
- 'wallet_type' => $wallet_type,
- 'is_send' => false
- ];
- $result = WalletLogic::handleInQueue($wallet_log);
- if ($result === false) {
- throw new Exception('系统繁忙,请稍后再试');
- }
- }
- public function getUser(): \yii\db\ActiveQuery
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * @throws Exception
- */
- private function remit($cash): void
- {
- $res = false;
- if ($cash->type === 'auto') {
- //todo 待测试联调
- /**
- * @var UserInfo $userInfo ;
- */
- $userInfo = UserInfo::getOne([["user_id" => $cash->user_id, "platform" => [User::PLATFORM_WECHAT, User::PLATFORM_MP_WX], "status" => StatusEnum::ENABLED]]);
- if (empty($userInfo)) {
- throw new \Exception('该用户不存在微信相关信息!');
- }
- // $wechatPaySetting = OptionLogic::get(Option::NAME_PAYMENT, \Yii::$app->mall->id, Option::GROUP_APP);
- // if (!$wechatPaySetting) {
- // throw new \Exception('系统未开启微信支付!');
- // }
- // if (!$wechatPaySetting['wechat_status']) {
- // throw new \Exception('系统未开启微信支付!');
- // }
- try {
- $payment = new PayService();
- $paymentTransfer = new PaymentTransfer();
- $paymentTransfer->orderNo = PaymentTransfer::generate_order_no("TR");
- $paymentTransfer->user = $userInfo;
- $paymentTransfer->title = "收益提现";
- $paymentTransfer->amount = $cash->actual_num;
- $paymentTransfer->transferType = PayTypeEnum::WECHAT;
- // $paymentTransfer->transferType = PaymentTransfer::TRANSFER_TYPE_WECHAT;
- $paymentTransfer->cashInfo = $cash;
- $res = $payment->transfer($paymentTransfer);
- } catch (Exception $e) {
- throw new \Exception($e->getMessage());
- }
- } else if ($cash->type === 'balance') {
- $userInfo = User::getOne([['id' => $cash->user_id, 'status' => StatusEnum::ENABLED]]);
- if (!$userInfo) {
- throw new \Exception('该用户不存在!');
- }
- $desc = "用户收益提现到余额:" . $cash->actual_num . "元";
- $data = [
- 'message_id' => 0,
- 'mall_id' => $cash->mall_id, 'user_id' => $cash->user_id,
- 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
- 'is_frozen' => false, 'unfrozen' => false, 'money' => abs($cash->actual_num), 'desc' => $desc,
- 'source_table' => AssetsType::CASH_OUT, 'source_table_id' => $cash->id,
- 'from_type' => UserWalletEnum::RECHARGE,
- 'capital_type' => UserWalletEnum::RECHARGE_COMMISSION,
- ];
- $res = UserWalletService::handle(0, $data);
- //免审核,免打款
- $withdraw_config = \Yii::$app->services->setting->mall->get($cash->mall_id, 'withdraw_income');
- if (isset($withdraw_config['is_examine']) && $withdraw_config['is_examine']) {
- $cash->content = Json::encode(['validate_content' => '提现免审核,免打款']);
- }
- } elseif ($cash->type === 'paid' || $cash->type === 'joinpay') { //代付
- $res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_EXTENSION_PAY, $cash->mall_id, ['cash' => $cash]);
- if ($res) {
- // 提交成功
- self::remit_wait($cash, "提交成功");
- return;
- } else {
- // 提交失败
- $msg = '代付打款失败';
- throw new \Exception($msg);
- }
- } else if ($cash->type == 'alipay' || $cash->type == 'wechat' || $cash->type == 'bank' || $cash->type == 'third_party_address') { //线下转账
- $res = true;
- }
- if ($res) {
- self::remit_success($cash);
- } else {
- $msg = '打款失败';
- throw new \Exception($msg);
- }
- }
- /**
- * 明确打款成功
- *
- * @param HigoWithdrawLog $cash
- * @param string $msg
- * @return void
- */
- public static function remit_success(HigoWithdrawLog $cash, $msg = '打款成功')
- {
- // 保存提现信息
- if ($cash->content) {
- $content = Json::decode($cash->content);
- } else {
- $content = [];
- }
- $cash->status = 2;
- $content['remit_at'] = date('Y-m-d H:i:s', time());
- $content['remit_content'] = $msg;
- $cash->content = Json::encode($content);
- if (!$cash->save()) {
- throw new \Exception();
- }
- return true;
- }
- /**
- * 打款失败,变成已同意状态,让客户自己去驳回
- *
- * @param HigoWithdrawLog $cash
- * @param string $msg
- * @return void
- */
- public static function remit_fail(HigoWithdrawLog $cash, $msg)
- {
- $content = empty($cash->content) ? [] : Json::decode($cash->content);
- $content['remit_content'] = $msg;
- $res = HigoWithdrawLog::updateAll([
- 'status' => 1,
- 'content' => Json::encode($content),
- 'updated_at' => time()
- ], ['id' => $cash->id]);
- }
- /**
- * 打款中
- *
- * @param [type] $cash
- * @param [type] $msg
- * @return void
- */
- public static function remit_wait(HigoWithdrawLog $cash, $msg)
- {
- $content = empty($cash['content']) ? [] : Json::decode($cash['content']);
- $content['remit_content'] = $msg;
- $res = self::updateAll(['content' => Json::encode($content), 'status' => 5], ['id' => $cash['id']]);
- }
- public static function exportHandle(array $data)
- {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $user_withdrawlog_model = HigoWithdrawLog::find();
- $user_withdrawlog_model->where(['mall_id' => $params['mall_id']]);
- if (!empty($params['keyword'])) {
- $user = User::find()->where([
- 'OR',
- ['like', 'nickname', $params['keyword'] . '%', false],
- ['like', 'mobile', $params['keyword'] . '%', false],
- ])->all();
- $where[] = ['in', 'user_id', array_column($user, 'id')];
- $user_withdrawlog_model->andWhere(['in', 'user_id', array_column($user, 'id')]);
- }
- if (!empty($params['user_id'])) $user_withdrawlog_model->andWhere(['=', 'user_id', $params['user_id']]);
- if (!empty($params['type'])) $user_withdrawlog_model->andWhere(['=', 'type', $params['withdraw_type']]);
- if (!empty($params['withdraw_from'])) $user_withdrawlog_model->andWhere(['=', 'from', $params['withdraw_from']]);
- if (!empty($params['start_date'])) $user_withdrawlog_model->andWhere(['>=', 'created_at', strtotime($params['start_date'])]);
- if (!empty($params['end_date'])) $user_withdrawlog_model->andWhere(['<=', 'created_at', strtotime($params['end_date'])]);
- if (isset($params['status'])) {
- $user_withdrawlog_model->andWhere(['=', 'status', $params['status']]);
- } else {
- $user_withdrawlog_model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
- }
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- $have_select_field_arr[] = $fields_arr[$value];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $user_withdrawlog_model->with(['user' => function ($query) {
- $query->select('id,nickname,username,avatar_url,mobile');
- }]);
- $user_withdrawlog_model->orderBy('status asc,created_at desc');
- foreach ($user_withdrawlog_model->batch() as $withdrawlog) {
- $csv->data = array_map(function ($withdrawlog) use ($fields) {
- $row = [];
- $extra = json_decode($withdrawlog['extra'], true) ?: [];
- foreach ($fields as $field) {
- switch (true) {
- case (in_array($field, ['updated_at'])):
- $row[] = !empty($withdrawlog[$field]) ? date('Y-m-d H:i:s', $withdrawlog[$field]) : '';
- break;
- case ($field == 'username'):
- $row[] = $withdrawlog['user']['username'] ?: '';
- break;
- case ($field == 'nickname'):
- $row[] = $withdrawlog['user']['nickname'] ?: '';
- break;
- case ($field == 'user_mobile'):
- $row[] = $withdrawlog['user']['mobile'] ?: '';
- break;
- case ($field == 'from'):
- $row[] = $withdrawlog[$field] == 1 ? '佣金收益提现' : '余额提现';
- break;
- case ($field == 'alipay_name'):
- $row[] = $extra['name'] ?: '';
- break;
- case ($field == 'mobile'):
- $row[] = empty($extra['mobile']) ? '' : $extra['mobile'] . "\t";
- break;
- case ($field == 'bank_account_type'):
- $row[] = $extra['bank_account_type'] == 1 ? '对私账户' : ($extra['bank_account_type'] == 2 ? '对公账号' : '');
- break;
- case ($field == 'name'):
- $row[] = $extra['name'] ?: '';
- break;
- case ($field == 'bank_name'):
- $row[] = $extra['bank_name'] ?: '';
- break;
- case ($field == 'bank_account'):
- $row[] = empty($extra['bank_account']) ? '' : $extra['bank_account'] . "\t";
- break;
- case ($field == 'wechat_qrcode'):
- $row[] = $extra['wechat_qrcode'] ?: '';
- break;
- case ($field == 'third_party_address'):
- $row[] = $extra['third_party_address'] ?: '';
- break;
- case ($field == 'status'):
- $str = '';
- switch ($withdrawlog[$field]) {
- case 0:
- $str = '待审核';
- break;
- case 1:
- $str = '待打款';
- break;
- case 2:
- $str = '已打款';
- break;
- case 5:
- $str = '打款中';
- break;
- case 3:
- $str = '已驳回';
- break;
- }
- $row[] = $str;
- break;
- case ($field == 'remark'):
- $withdrawlog['content'] = json_decode($withdrawlog['content'], true);
- $str = '';
- if (!empty($withdrawlog['content']['reject_content'])) $str .= '驳回备注:' . $withdrawlog['content']['reject_content'];
- if (!empty($withdrawlog['content']['validate_content']) && in_array($withdrawlog['status'], [1, 2])) $str .= ';审核备注:' . $withdrawlog['content']['validate_content'];
- if (!empty($withdrawlog['content']['remit_content']) && in_array($withdrawlog['status'], [1, 2])) $str .= ';打款备注:' . $withdrawlog['content']['remit_content'];
- if (!empty($withdrawlog['content']['joinpay_msg'])) $str .= ';汇聚备注:' . $withdrawlog['content']['joinpay_msg'];
- $row[] = $str;
- break;
- default:
- $row[] = !empty($withdrawlog[$field]) ? $withdrawlog[$field] : '';
- }
- }
- return $row;
- }, $withdrawlog);
- $csv->export();
- }
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- }
|