| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- <?php
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/4/14
- * Time: 下午5:06
- */
- namespace app\backend\modules\finance\controllers;
- use app\common\events\withdraw\BalanceWithdrawSuccessEvent;
- use app\common\exceptions\AppException;
- use app\common\facades\Setting;
- use app\common\helpers\Url;
- use app\common\models\Member;
- use app\common\models\Withdraw;
- use app\backend\modules\finance\services\WithdrawService;
- use app\common\components\BaseController;
- use app\common\services\finance\BalanceNoticeService;
- use app\common\services\Session;
- use Illuminate\Support\Facades\Log;
- use app\backend\modules\withdraw\controllers\AuditRejectedController;
- class BalanceWithdrawController extends BaseController
- {
- public $withdrawModel;
- public function detail()
- {
- $this->withdrawModel = $this->attachedMode();
- $set = Setting::getByGroup('pay_password') ?: [];
- return view('finance.balance.withdraw', [
- 'item' => $this->withdrawModel->toArray(),
- 'is_verify' => !empty($set['withdraw_verify']['is_phone_verify']) ? true : false,
- 'expire_time' => Session::get('withdraw_verify') ?: null,
- 'verify_phone' => $set['withdraw_verify']['phone'] ?: "",
- 'verify_expire' => $set['withdraw_verify']['verify_expire'] ? intval(
- $set['withdraw_verify']['verify_expire']
- ) : 10
- ])->render();
- }
- /**
- * @return mixed
- * @throws AppException
- */
- public function examine()
- {
- $requestData = \YunShop::request();
- $this->withdrawModel = $this->attachedMode();
- if (isset($requestData['submit_check'])) {
- //审核
- return $this->submitCheck();
- } elseif (isset($requestData['audited_rebut'])) {
- //审核后驳回
- return $this->auditedRebut();
- } elseif (isset($requestData['submit_cancel'])) {
- //重新审核
- return $this->submitCancel();
- } elseif (isset($requestData['submit_pay'])) {
- //打款
- return $this->submitPayNew();
- } elseif (isset($requestData['again_pay'])) {
- //重新打款
- $this->withdrawModel->status = 1;
- return $this->submitPayNew();
- } elseif (isset($requestData['confirm_pay'])) {
- //线下打款
- $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 2;
- $this->withdrawModel->pay_way = 'manual';
- $this->withdrawUpdate();
- event(new BalanceWithdrawSuccessEvent($this->withdrawModel));
- BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
- return $this->message(
- '打款成功',
- yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id])
- );
- }
- return $this->message(
- '提交数据有误,请刷新重试',
- yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]),
- 'error'
- );
- }
- /**
- * 把打款方法剔出,临时使用
- */
- private function submitPayNew()
- {
- $check = $this->checkVerify();//打款验证
- if (!$check) {
- $this->message(
- '提现验证失败或验证已过期',
- yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
- 'error'
- );
- }
- $result = $this->submitPay();
- if (!empty($result) && 0 == $result['errno']) {
- //todo 临时增加手动打款成功通知,重构时候注意优化
- if ($this->withdrawModel->pay_way == 'manual') {
- event(new BalanceWithdrawSuccessEvent($this->withdrawModel));
- BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
- }
- return $this->message(
- '提现申请成功',
- yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id])
- );
- }
- BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);//提现失败通知
- $message = $result['message'] ?: '提现申请失败';
- return $this->message(
- $message,
- yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
- 'error'
- );
- }
- /**
- * 打款验证
- * @return bool
- */
- private function checkVerify()
- {
- $set = Setting::getByGroup('pay_password')['withdraw_verify'] ?: [];
- if (empty($set) || empty($set['is_phone_verify'])) {
- return true;
- }
- $verify = Session::get('withdraw_verify'); //没获取到
- if ($verify && $verify >= time()) {
- return true;
- }
- return false;
- }
- //提现审核
- private function submitCheck()
- {
- if ($this->withdrawModel->status != 0) {
- $this->message(
- '提交审核失败,该状态不能审核',
- yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
- 'error'
- );
- }
- $this->withdrawModel->status = $this->getPostStatus();
- $this->withdrawModel->reject_reason = $this->withdrawModel->status != 1 ? trim(
- request()->reject_reason
- ) : '';//驳回理由
- $this->withdrawModel->audit_at = time();
- if ($this->getPostStatus() == -1) {
- BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
- }
- if ($this->getPostStatus() == 3) {
- return (new AuditRejectedController())->index();
- //BalanceNoticeService::withdrawRejectNotice($this->withdrawModel);
- }
- $this->withdrawUpdate();
- return $this->message('提交审核成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
- }
- //审核后驳回
- private function auditedRebut()
- {
- return (new AuditRejectedController())->index();
- }
- /**
- * 提现重新审核
- * @return mixed
- */
- private function submitCancel()
- {
- return $this->submitCheck();
- }
- /**
- * 提现打款
- * @return mixed
- * @throws AppException
- */
- public function submitPay()
- {
- if ($this->withdrawModel->status !== 1) {
- throw new AppException('打款失败,数据不存在或不符合打款规则!');
- }
- $result = $this->payment();
- if (!empty($result) && 0 == $result['errno']) {
- $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 2;
- $this->withdrawUpdate();
- } elseif (
- $this->withdrawModel->pay_way == 'alipay'
- || $this->withdrawModel->pay_way == 'yop_pay'
- || $this->withdrawModel->pay_way == 'silver_point'
- ) {
- $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 4;
- $this->withdrawUpdate();
- }
- if (in_array($this->withdrawModel->pay_way, [
- 'high_light_wechat',
- 'high_light_alipay',
- 'high_light_bank',
- 'worker_withdraw_wechat',
- 'worker_withdraw_alipay',
- 'worker_withdraw_bank',
- ])) {
- if (!empty($result) && $result['errno'] == 200) {
- // $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 4;
- $this->withdrawUpdate();
- $result['errno'] = 0;
- } else {
- \Log::debug($this->withdrawModel->pay_way . '提现错误' . $this->withdrawModel->withdraw_sn, $result);
- }
- }
- if ($this->withdrawModel->pay_way == 'wechat') {
- $memberModel = Member::uniacid()->where('uid', $this->withdrawModel->member_id)
- ->with(['hasOneFans', 'hasOneMiniApp', 'hasOneWechat'])->first();
- $v3_switch = false;
- if ($memberModel->hasOneFans->openid) {
- $income_set = Setting::get('shop.pay');
- $v3_switch = (bool)$income_set['weixin_apiv3'];
- } elseif (app('plugins')->isEnabled('min-app') && $memberModel->hasOneMiniApp->openid) {
- $appletSet = Setting::get('plugin.min_app');
- $v3_switch = (bool)$appletSet['v3_switch'];
- } elseif (app('plugins')->isEnabled('app-set') && $memberModel->hasOneWechat->openid) {
- $appSet = Setting::get('shop_app.pay');
- $v3_switch = (bool)$appSet['weixin_v3'];
- }
- if ($v3_switch && !empty($result) && 0 == $result['errno']) {
- //使用新版V3接口,保持打款中
- $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 4;
- $this->withdrawUpdate();
- } else {
- \Log::debug(
- $this->withdrawModel->pay_way . '提现' . $this->withdrawModel->withdraw_sn,
- [$v3_switch, $result]
- );
- }
- }
- if ($this->withdrawModel->pay_way == 'converge_pay') {
- $this->withdrawModel->pay_at = time();
- $this->withdrawModel->status = 4;
- $this->withdrawUpdate();
- }
- return $result;
- }
- /**
- * 提现 model 数据保存
- * @return bool
- * @throws AppException
- */
- private function withdrawUpdate()
- {
- if (!$this->withdrawModel->save()) {
- throw new AppException('数据修改失败,请刷新重试');
- }
- return true;
- }
- /**
- * 提现打款,区分打款方式
- * @return mixed
- * @throws AppException
- */
- private function payment()
- {
- switch ($this->withdrawModel->pay_way) {
- case 'alipay':
- return $this->alipayPayment($this->paymentRemark());
- case 'wechat':
- return $this->wechatPayment();
- case 'manual':
- return $this->manualPayment();
- case 'eup_pay':
- return $this->eupPayment();
- case 'huanxun':
- return $this->huanxunPayment();
- case 'yop_pay': //易宝余额提现
- return $this->yopPayment();
- case 'converge_pay': //汇聚余额提现
- return $this->convergePayment($this->paymentRemark());
- case 'high_light_wechat': //高灯微信余额提现
- case 'high_light_alipay': //高灯支付宝余额提现
- case 'high_light_bank': //高灯银行卡余额提现
- return $this->highLightPayment();
- case 'worker_withdraw_wechat':
- case 'worker_withdraw_alipay':
- case 'worker_withdraw_bank':
- return $this->workWithdrawPayment();
- case 'eplus_withdraw_bank':
- return $this->eplusWithdrawPayment();
- case 'balance': //余额提现 todo:给保证金提现用
- return $this->balancePayment($this->paymentRemark());
- case 'silver_point':
- return $this->silverPointWithdrawPayment();
- default:
- throw new AppException('未知打款方式!!!');
- }
- }
- /**
- * 打款日志(备注)
- * @return string
- */
- private function paymentRemark()
- {
- return $remark = '提现打款-' . $this->withdrawModel->type_name . '-金额:' . $this->withdrawModel->actual_amounts . '元,' . '手续费:' . $this->withdrawModel->actual_poundage;
- }
- /**
- * 支付宝打款
- * @param $remark
- * @param null $withdraw
- */
- private function alipayPayment($remark, $withdraw = null)
- {
- $result = [];
- if (!is_null($withdraw)) {
- $result = WithdrawService::alipayWithdrawPay($withdraw, $remark);
- } else {
- $result = WithdrawService::alipayWithdrawPay($this->withdrawModel, $remark);
- }
- Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "支付宝打款中!");
- if (!empty($result) && 1 == $result['errno']) {
- return $this->paymentError($result['message']);
- }
- return $result;
- }
- /**
- * 微信打款
- * @return mixed
- */
- private function wechatPayment()
- {
- $result = WithdrawService::wechatWithdrawPay($this->withdrawModel, $this->paymentRemark());
- //file_put_contents(storage_path('logs/withdraw1.log'),print_r($resultPay,true));
- Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $this->paymentRemark() . "微信打款中!");
- if (!empty($result) && 1 == $result['errno']) {
- return $this->paymentError($result['message']);
- }
- return $result;
- //return $this->paymentSuccess();
- }
- /**
- * 余额打款
- * @param $remark
- */
- private function balancePayment($remark)
- {
- $result = WithdrawService::balanceWithdrawPay($this->withdrawModel, $remark);
- Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "余额打款中!");
- if (!empty($result) && 1 == $result['errno']) {
- return $this->paymentError($result['message']);
- }
- return $result;
- }
- /**
- * @return array|mixed|void
- * @author blank
- * EUP打款
- */
- private function eupPayment()
- {
- $result = WithdrawService::eupWithdrawPay($this->withdrawModel);
- if (!empty($result) && $result['errno'] == 1) {
- return $this->paymentError($result['message']);
- }
- return $result;
- }
- /**
- * @return array|mixed|void
- * @author blank
- * 易宝余额提现
- */
- private function yopPayment()
- {
- $result = WithdrawService::yopWithdrawPay($this->withdrawModel);
- if (!empty($result) && $result['errno'] == 1) {
- return $this->paymentError($result['message']);
- }
- return $result;
- }
- private function huanxunPayment()
- {
- $result = WithdrawService::huanxunPayment($this->withdrawModel);
- if ($result['result'] == 10 || $result['result'] == 8) {
- return ['errno' => 0, 'message' => '打款成功'];
- }
- $result['errno'] = 1;
- return $result;
- }
- /**
- * 汇聚余额提现
- *
- * @param $remark
- * @return array|mixed
- * @throws AppException
- */
- private function convergePayment($remark)
- {
- $result = WithdrawService::convergePayMent($this->withdrawModel, $remark);
- if ($result['verify']) {
- return $result;
- }
- $msg = "收入提现ID:{$this->withdrawModel->id},汇聚提现失败:{$result['msg']}";
- return $this->paymentError($msg);
- }
- private function highLightPayment()
- {
- $result = WithdrawService::highLightWithdrawPay($this->withdrawModel);
- // if (!empty($result) && $result['errno'] == 500) {
- // return $this->paymentError($result['message']);
- // }
- return $result;
- }
- private function eplusWithdrawPayment()
- {
- return WithdrawService::eplusWithdrawPay($this->withdrawModel);
- }
- private function silverPointWithdrawPayment()
- {
- return WithdrawService::silverPointWithdrawPayment($this->withdrawModel);
- }
- private function workWithdrawPayment()
- {
- return WithdrawService::workerWithdrawPay($this->withdrawModel);
- }
- /**
- * 手动打款
- * @return mixed
- */
- private function manualPayment()
- {
- return ['errno' => 0, 'message' => '手动打款成功'];
- //return $this->paymentSuccess();
- }
- /**
- * 打款成功
- * @return mixed
- */
- private function paymentSuccess()
- {
- return $this->message('打款成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
- }
- /**
- * 打款失败
- * @param string $message
- * @throws AppException
- */
- private function paymentError($message = '')
- {
- $this->withdrawModel->status = 1;
- $this->withdrawUpdate();
- //发送打款失败通知
- BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
- throw new AppException($message ?: '打款失败,请重试');
- }
- /**
- * 附值打款 model
- * @return mixed
- * @throws AppException
- */
- private function attachedMode()
- {
- $result = Withdraw::getBalanceWithdrawById($this->getPostId());
- if (!$result) {
- throw new AppException('数据错误,请刷新重试');
- }
- return $result;
- }
- /**
- * @return string
- */
- private function getPostId()
- {
- return trim(\YunShop::request()->id);
- }
- /**
- * @return string
- */
- private function getPostStatus()
- {
- return trim(\YunShop::request()->status);
- }
- /**
- * 丁冉增加批量打款
- * @return mixed
- */
- public function batchAlipay()
- {
- $ids = \YunShop::request()->ids;
- $withdrawId = explode(',', $ids);
- $withdraw = [];
- if (!empty($withdrawId)) {
- foreach ($withdrawId as $id) {
- $withdraw_modle = Withdraw::getBalanceWithdrawById($id);
- if (!is_null($withdraw_modle)) {
- if ($withdraw_modle->status != '1') {
- BalanceNoticeService::withdrawFailureNotice($withdraw_modle);
- return $this->message(
- '打款失败,数据不存在或不符合打款规则!',
- yzWebUrl("finance.balance-withdraw.detail", ['id' => $id]),
- 'error'
- );
- }
- $withdraw[] = $withdraw_modle;
- $remark[] = '提现打款-' . $withdraw_modle->type_name . '-金额:' . $withdraw_modle->actual_amounts . '元,' .
- '手续费:' . $withdraw_modle->actual_poundage;
- }
- }
- $remark = json_encode($remark);
- $this->alipayPayment($remark, $withdraw);
- }
- }
- }
|