'CStest001', 'openid' => 'or1pO5f5zi-atTmKVuni3KGgvCns', 'check_name' => 'NO_CHECK', 'amount' => 0.01 * 100, 'desc' => '转账' ]; $wechat = \Yii::$app->controller->wechatPay; $res = $wechat->transfer->toBalance($data); var_dump($res);die; } //商城余额信息 public function actionStoreMoney() { $store_id = get_store_id(); $store = \app\models\Store::findOne($store_id); $data = []; //商城总金额 $maxMoney = StoreCash::getMaxCash($store, $data); //已提现 $moneyCash = StoreCash::find()->where(['store_id' => $store->id, 'is_delete' => 0, 'status' => 2, 'cash_type' => StoreCash::CASH_TYPE_NORMAL]) ->andWhere(['>', 'price', 0]) ->sum('price'); $user_cash = Cash::find()->where(['store_id' => $store->id, 'is_delete' => 0, 'status' => 2]) ->andWhere(['>', 'price', 0]) ->sum('price') ?: '0.00'; $yunst_money = 0; $yunst_frozen_money = 0; $yunst_store_money_total = 0; if ($store->cusid){ //查询余额账户信息 $orderYunst = new OrderYunst(); $res = $orderYunst->queryAcctInfo($store->store_number); // $res = $orderYunst->queryAcctInfo('XDQXCJSZPJYB'); if ($res['subCode']=='SUCCESS' ){ $freezenAmount = $res['data']['freezenAmount'];//冻结 $amount = $res['data']['amount'];//可用余额 if (($freezenAmount + $amount) >0){ $yunst_store_money_total = ($freezenAmount + $amount) / 100;//累计 }else{ $yunst_store_money_total = 0;//累计 } $yunst_frozen_money = $freezenAmount?$freezenAmount/100:0;;//冻结 $yunst_money = $amount?$amount/100:0;//账户总金额 可提现的 } } return $this->asJson([ 'code' => 0, 'msg' => 'ok', 'data' => [ 'max_cash' => $maxMoney, 'moneyFrozen' => $data['moneyFrozen'], 'moneyCash' => $moneyCash ?: '0.00', 'moneyTotal' => bcadd($data['storeMoneyTotal'], $yunst_store_money_total, 2), 'user_cash' => $user_cash, 'yunst_money' => $yunst_money ?: '0.00', 'yunst_frozen_money' => $yunst_frozen_money ?: '0.00', 'yunst_store_money_total' => $yunst_store_money_total ] ]); } //saas余额信息 public function actionSaasMoney() { $data = []; $moneyCash = StoreCash::find()->where(['store_id' =>'-1', 'is_delete' => 0, 'status' => 2, 'cash_type' => StoreCash::CASH_TYPE_NORMAL]) ->andWhere(['>', 'price', 0]) ->sum('price'); //查询saas余额账户信息 $orderYunst = new OrderYunst(); $store_id = get_store_id(); $store = \app\models\Store::findOne($store_id); // $res = $orderYunst->queryAcctInfo($store->store_number); // $res = $orderYunst->queryAcctInfo('66065100000219T'); $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value']; if ($oldConf) { $oldConf = json_decode($oldConf, true); if (!empty($oldConf)) { $customer_number = $oldConf['customer_number']; $res = $orderYunst->queryAcctInfo($customer_number); if ($res['subCode']=='SUCCESS' ){ $freezenAmount = $res['data']['freezenAmount'];//冻结 $amount = $res['data']['amount'];//可用余额 if (($freezenAmount + $amount) >0){ $storeMoneyTotal = ($freezenAmount + $amount ) / 100 + $moneyCash; }else{ $storeMoneyTotal = 0; } $data['moneyFrozen'] = $freezenAmount?$freezenAmount/100:0;;//冻结 $data['storeMoneyTotal'] = $storeMoneyTotal;//累计 $maxMoney = $amount?$amount/100:0;//账户总金额 可提现的 } } } return $this->asJson([ 'code' => 0, 'msg' => 'ok', 'data' => [ 'max_cash' => $maxMoney, 'moneyFrozen' => $data['moneyFrozen'], 'moneyCash' => $moneyCash, 'moneyTotal' => $data['storeMoneyTotal'], ] ]); } //商城提现删除 public function actionStoreCashDel() { $id = post_params('id'); if (empty($id) || !is_array($id)) { \Yii::error([__METHOD__, $id]); return $this->asJson([ 'code' => 1, 'msg' => '数据格式错误' ]); } $del = StoreCash::updateAll(['is_delete' => 1], ['id' => $id]); return $this->asJson([ 'code' => $del ? 0 : 1 , 'msg' => '删除数量:' . $del, ]); } //商城提现 public function actionStoreCashCreate() { $price = post_params('cash'); $type = post_params('type'); $name = post_params('name'); $mobile = post_params('mobile'); $bank_name = post_params('bank_name'); if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat','yunst'])) { return $this->asJson([ 'code' => 1, 'msg' => '参数非法', ]); } $store_id = get_store_id(); $store = Store::findOne($store_id); if ($store->cusid){ $orderYunst = new OrderYunst(); $res = $orderYunst->queryAcctInfo($store->store_number); $amount = 0; if ($res['subCode'] == 'SUCCESS'){ $amount = $res['data']['amount'];//真的 // $amount = 10;//写的假的 } if ($price > $amount) { \Yii::error([__METHOD__, $price, $amount]); return $this->asJson([ 'code' => 1, 'msg' => '当前大于可提现金额' ]); } }else{ //商城余额是否充足 $storeMoney = \app\models\StoreCash::getMaxCash($store); if ($price > $storeMoney) { \Yii::error([__METHOD__, $price, $storeMoney]); return $this->asJson([ 'code' => 1, 'msg' => '当前大于可提现金额' ]); } } $exit = StoreCash::find()->andWhere(['=', 'status', 0])->andWhere(['store_id' => $store_id, 'cash_type' => StoreCash::CASH_TYPE_NORMAL])->exists(); if ($exit) { \Yii::error([__METHOD__, $store_id, $exit]); // return $this->asJson([ // 'code' => 1, // 'msg' => '尚有未完成的提现申请' // ]); } $t = \Yii::$app->db->beginTransaction(); $cash = new StoreCash(); $cash->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH); $cash->is_delete = 0; $cash->status = 0; $cash->price = $price; $cash->created_at = time(); $cash->store_id = $store_id; if ($type == 'wechat') { $cash->type = 0; $cash->name = $name; $cash->mobile = $mobile; } if ($type == 'alipay') { $cash->type = 1; $cash->name = $name; $cash->mobile = $mobile; } if ($type == 'bank_card') { $cash->type = 2; $cash->name = $name; $cash->mobile = $mobile; $cash->bank_name = $bank_name; } if ($type == 'yunst') { $cash->type =6;//通联提现 $cash->name = $name; $cash->mobile = $mobile; } $cash->pay_time = 0; $cash->service_charge = 0; $cash->cash_type = 0; if ($cash->save()) { $subMoney = \app\models\Store::subMoney($store, $price, '商城提现'); if (!$subMoney) { $t->rollBack(); \Yii::error([__METHOD__, $subMoney, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '扣款失败', ]); } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '申请成功' ]); } else { \Yii::error([__METHOD__, $cash]); $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $cash->errors ]); } } //平台提现 public function actionSaasCashCreate() { $price = post_params('cash'); $type = post_params('type'); $store_id = get_store_id(); $name = post_params('name'); $mobile = post_params('mobile'); $bank_name = post_params('bank_name'); if (!$price || !in_array($type, ['alipay', 'bank_card', 'wechat','yunst'])) { return $this->asJson([ 'code' => 1, 'msg' => '参数非法', ]); } $orderYunst = new OrderYunst(); $store_id = get_store_id(); $store = \app\models\Store::findOne($store_id); $res = $orderYunst->queryAcctInfo($store->store_number); // $res = $orderYunst->queryAcctInfo('XDQXCJSZPJYB'); debug_log($res); $amount = 0; if ($res['subCode'] == 'SUCCESS'){ $amount = $res['data']['amount'];//真的 // $amount = 10;//写的假的 } if ($price > $amount) { \Yii::error([__METHOD__, $price, $amount]); return $this->asJson([ 'code' => 1, 'msg' => '当前大于可提现金额' ]); } $exit = StoreCash::find()->andWhere(['=', 'status', 0])->andWhere(['store_id' => -1, 'cash_type' => StoreCash::CASH_TYPE_NORMAL])->exists(); if ($exit) { // return $this->asJson([ // 'code' => 1, // 'msg' => '尚有未完成的提现申请' // ]); } $t = \Yii::$app->db->beginTransaction(); $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH); $orderYunst = new OrderYunst(); $data['bizOrderNo'] = $order_no; $store = Store::findOne(['id' => $store_id]); $data['bizUserId'] = $store->store_number; // $data['bizUserId'] = "XDQXCJSZPJYB";//测试服务端客户号 $data['amount'] = $price * 100; $data['notifyUrl'] = Yii::$app->request->hostInfo; $data['remark'] = ""; $data['authWay'] = "3"; $res_cash = $orderYunst->servicerWithdraw($data); debug_log($res_cash); if ($res_cash['subCode'] != 'SUCCESS'){ return $this->asJson([ 'code' => 1, 'msg' =>$res_cash['subMsg'] ]); }else{ $cash = new StoreCash(); $cash->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH); $cash->is_delete = 0; $cash->status = 2; $cash->price = $price; $cash->created_at = time(); $cash->store_id = -1; $cash->type =6;//通联提现 $cash->name = $name; $cash->mobile = $mobile; $cash->pay_time = time(); $cash->service_charge = 0; $cash->cash_type = 0; if ($cash->save()) { $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '提现成功' ]); } else { \Yii::error([__METHOD__, $cash]); $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => $cash->errors ]); } } } /** * 提现列表 * @return \yii\web\Response */ public function actionCashList() { $store_id = get_params('store_id', 0); $export = get_params('export'); $start_time = get_params('start_time'); $end_time = get_params('end_time'); $status = get_params('status'); $type = get_params('type'); $cash_store_id = get_params('cash_store_id'); $audit_start_time = get_params('audit_start_time'); $audit_end_time = get_params('audit_end_time'); $query = StoreCash::find()->alias('c')->where(['c.is_delete' => 0]); // $store_id > 0 && $query->andWhere(['c.store_id' => $store_id]); ($type >= 0 && $type != '') && $query->andWhere(['c.type' => $type]); if ($start_time) { $query->andWhere(['>=', 'c.created_at', strtotime($start_time)]); } if ($end_time) { $query->andWhere(['<=', 'c.created_at', strtotime($end_time)]); } if ($cash_store_id > 0) { $query->andWhere(['c.store_id' => $cash_store_id]); } if ($store_id <= 0) { // $query->andWhere(['c.store_id' => [0, -1]]); } else { $query->andWhere(['c.store_id' => $store_id]); } if ($audit_start_time) { $query->andWhere(['>=', 'c.updated_at', strtotime($audit_start_time)]); } if ($audit_end_time) { $query->andWhere(['<=', 'c.updated_at', strtotime($audit_end_time)]); } if ($status == StoreCash::STATUS_APPLY) { //待审核 $query->andWhere(['c.status' => StoreCash::STATUS_APPLY]); } if ($status == StoreCash::STATUS_CONFIRM) { // 待打款 $query->andWhere(['c.status' => StoreCash::STATUS_CONFIRM]); } if ($status == StoreCash::STATUS_GIVEN) { // 已打款 $query->andWhere(['in', 'c.status', [StoreCash::STATUS_GIVEN, StoreCash::STATUS_RECHARGE]]); } if ($status == StoreCash::STATUS_REFUSE) { // 已拒绝 $query->andWhere(['c.status' => StoreCash::STATUS_REFUSE]); } $query->orderBy('c.id DESC'); $pagination = pagination_make($query); $list = $pagination['list']; foreach($list as &$value){ $value['service_money'] = $value['service_charge'] * $value['price'] / 100; $value['money'] = StoreCash::getServiceMoney($value); if (!$cash_store_id) { $store = Store::findOne($value['store_id']); $value['store_name'] = $store->name; $value['store_logo'] = $store->logo; $value['store_cat_name'] = '-'; if ($store['category_id'] > 0) { $cat = SaasCategory::findOne($store['category_id']); if ($cat) { $value['store_cat_name'] = $cat->name; } } } } if($export){ return $this->export($list); } $store_id = get_store_id(); $store = Store::findOne($store_id); //商城余额是否充足 $storeMoney = \app\models\StoreCash::getMaxCash($store); return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], 'store_cash_money' => $storeMoney ?: '0.00' ], ]); } private function export($list) { $rows = [[ 'ID', '提现金额(元)', '提现方式', '提现账号', '提现姓名', '提现开户行', '时间', '说明', '状态', ]]; foreach($list as $item){ $r = [ $item['id'], $item['price'], StoreCash::getTypeName($item), $item['mobile'], $item['name'], $item['bank_name'], date('Y-m-d H:i:s', $item['created_at']), $item['reject_reason'], StoreCash::getStatusName($item), ]; $rows[] = $r; } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } /** * 提现申请审核 * @return \yii\web\Response */ public function actionCashApply() { $id = post_params('id'); $status = post_params('status'); $reject_reason = input_params('reject_reason', ''); if (empty($id) || !is_array($id)) { \Yii::error([__METHOD__, $id]); return $this->asJson([ 'code' => 1, 'msg' => '数据格式错误' ]); } $t = \Yii::$app->db->beginTransaction(); foreach ($id as $value) { $cash = StoreCash::findOne(['id' => $value, 'is_delete' => StoreCash::IS_DELETE_NO]); if (!$cash) { $t->rollBack(); \Yii::error([__METHOD__, $value, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '提现记录不存在,请刷新重试' ]); } if (!$cash->order_no) { $order_no = null; while (true) { $order_no = date('YmdHis') . mt_rand(100000, 999999); $exist_order_no = StoreCash::find()->where(['order_no' => $order_no])->exists(); if (!$exist_order_no) { break; } } $cash->order_no = $order_no; $cash->save(); } if (!in_array($status, [1, 3])) { $t->rollBack(); \Yii::error([__METHOD__, $status, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '提现记录ID: ' . $value . '已审核,请刷新重试' ]); } $store = Store::findOne($cash->store_id); $cash->status = $status; $cash->reject_reason = $reject_reason; if ($status == StoreCash::STATUS_REFUSE && $cash->cash_type == StoreCash::CASH_TYPE_NORMAL) { $addMoney = \app\models\Store::addMoney($store, $cash->price, '商城提现-驳回'); if (!$addMoney) { \Yii::error([__METHOD__, $addMoney, $cash]); $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => '网络异常,addMoney失败' ]); } } if ($status == StoreCash::STATUS_REFUSE && $cash->cash_type == StoreCash::CASH_TYPE_ALLIANCE_COUPON) { $store = Store::findOne($cash->store_id); $before = $store->league_price; $store->updateCounters(['league_price' => floatval($cash->price)]); \app\models\SaaSLeaguePriceLog::setLeaguePriceLog( $store->id, $cash->saas_id, $cash->price, $before, \app\models\SaaSLeaguePriceLog::TYPE_WITHDRAW_REJECT, \app\models\SaaSLeaguePriceLog::SEND_TYPE, \app\models\SaaSLeaguePriceLog::ROLE_STORE, $cash->id ); } if (!$cash->save()) { \Yii::error([__METHOD__, $cash]); $t->rollBack(); return $this->asJson([ 'code' => 1, 'msg' => '网络异常,请刷新重试' ]); } } $t->commit(); return $this->asJson([ 'code' => 0, 'msg' => '审核成功' ]); } /** * 打款 * @return \yii\web\Response */ public function actionCashConfirm() { $id = post_params('id'); $cash = StoreCash::findOne(['id' => $id, 'is_delete' => StoreCash::IS_DELETE_NO]); if (!$cash) { \Yii::error([__METHOD__, $id, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '提现记录不存在,请刷新重试' ]); } if (!$cash->order_no) { $order_no = null; while (true) { $order_no = date('YmdHis') . mt_rand(100000, 999999); $exist_order_no = StoreCash::find()->where(['order_no' => $order_no])->exists(); if (!$exist_order_no) { break; } } $cash->order_no = $order_no; $cash->save(); } if ($cash->status != 1) { \Yii::error([__METHOD__, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '操作错误,请刷新重试' ]); } // 微信自动打款 if ($cash->type == 4) { try { $storeAdmin = Admin::findOne([ 'type' => 'store', 'type_id' => $cash->store_id, 'is_delete' => 0, ]); if (!$storeAdmin) { return $this->asJson([ 'code' => 1, 'msg' => '商城绑定的管理员不存在,操作失败' ]); } $saasUser = SaasUser::findOne($storeAdmin->saas_user_id); if (!$saasUser) { return $this->asJson([ 'code' => 1, 'msg' => '商城绑定的管理员不存在,操作失败' ]); } if (empty($saasUser->platform_open_id_merchant)) { return $this->asJson([ 'code' => 1, 'msg' => '商城绑定的管理员open_id不存在,操作失败' ]); } $data = [ 'partner_trade_no' => $cash->order_no, 'openid' => $saasUser->platform_open_id_merchant, 'check_name' => 'NO_CHECK', 'amount' => $cash->price * 100, 'user_name' => $cash->name, 'desc' => '商城提现' ]; $wechat = getServiceClientWxConfig(); $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(0, $data); if ($res['code'] == 1) { return $this->asJson([ 'code' => 1, 'msg' => $res['msg'], ]); } $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW; $cash->wx_cash_state = $res['data']['state']; $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE); } catch (\Exception $e) { return $this->asJson([ 'code' => 1, 'msg' => '操作失败,保存失败' . $e->getMessage() ]); } } // 通联提现打款 if ($cash->type == 6) { $store = Store::findOne(['id' => $cash->store_id]); if ($store->cusid){ $orderYunst = new OrderYunst(); $data['bizOrderNo'] = $cash->order_no; $data['bizUserId'] = $store->store_number; // $data['bizUserId'] = "XDQXCJSZPJYB"; $data['amount'] = ceil($cash->price * 100); $data['notifyUrl'] = Yii::$app->request->hostInfo; $data['remark'] = ""; $data['authWay'] = "3"; debug_log('平台审核提现通联'); debug_log($data); $res = $orderYunst->servicerWithdraw($data); debug_log($res); if ($res['subCode'] != 'SUCCESS'){ $order_no = date('YmdHis') . mt_rand(100000, 999999); $cash->order_no = $order_no; $cash->save(); return $this->asJson([ 'code' => 1, 'msg' =>$res['subMsg'] ]); } } } //手动打款 $cash->status = StoreCash::STATUS_GIVEN; $cash->pay_time = time(); $cash->pay_type = StoreCash::PAY_TYPE_HAND; // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price)); $save = $cash->save(); if ($save) { return $this->asJson([ 'code' => 0, 'msg' => '成功' ]); } else { \Yii::error([__METHOD__, $cash]); return $this->asJson([ 'code' => 1, 'msg' => '操作失败,保存失败' ]); } } /** * 商城账户明细列表 * @return \yii\web\Response */ public function actionStoreAccountLogList() { $store_id = get_params('store_id'); $opt = get_params('opt'); $export = get_params('export'); $start_time = get_params('start_time'); $end_time = get_params('end_time'); $cash_store_id = get_params('cash_store_id'); $type = get_params('type'); $query = StoreAccountLog::find()->alias('c'); $store_id > 0 && $query->andWhere(['c.store_id' => $store_id]); if ($start_time) { $query->andWhere(['>=', 'c.time', strtotime($start_time)]); } if ($end_time) { $query->andWhere(['<=', 'c.time', strtotime($end_time)]); } if ($cash_store_id) { $query->andWhere(['c.store_id' => $cash_store_id]); } if ($type > 0) { $query->andWhere(['c.type' => $type]); } if($opt == 1){ $query->andWhere(['like', 'c.desc', '商城提现']); } $query->orderBy('c.id DESC'); $pagination = pagination_make($query); $list = $pagination['list']; foreach($list as &$value){ if (!$cash_store_id) { $store = Store::findOne($value['store_id']); $value['store_name'] = $store->name; $value['store_logo'] = $store->logo; $value['store_cat_name'] = '-'; if ($store['category_id'] > 0) { $cat = SaasCategory::findOne($store['category_id']); if ($cat) { $value['store_cat_name'] = $cat->name; } } } } if($export){ return $this->exportStoreAccountLog($list); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'] ], ]); } private function exportStoreAccountLog($list) { $rows = [[ 'ID', '余额类型', '变动金额(元)', '变动前', '变动后', '时间', '说明', ]]; foreach($list as $item){ $r = [ $item['id'], $item['type'] == 1 ? '收入' : '支出', $item['price'], $item['before'], $item['after'], date('Y-m-d H:i:s', $item['time']), $item['desc'], ]; $rows[] = $r; } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } }