BalanceWithdrawController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/14
  6. * Time: 下午5:06
  7. */
  8. namespace app\backend\modules\finance\controllers;
  9. use app\common\events\withdraw\BalanceWithdrawSuccessEvent;
  10. use app\common\exceptions\AppException;
  11. use app\common\facades\Setting;
  12. use app\common\helpers\Url;
  13. use app\common\models\Member;
  14. use app\common\models\Withdraw;
  15. use app\backend\modules\finance\services\WithdrawService;
  16. use app\common\components\BaseController;
  17. use app\common\services\finance\BalanceNoticeService;
  18. use app\common\services\Session;
  19. use Illuminate\Support\Facades\Log;
  20. use app\backend\modules\withdraw\controllers\AuditRejectedController;
  21. class BalanceWithdrawController extends BaseController
  22. {
  23. public $withdrawModel;
  24. public function detail()
  25. {
  26. $this->withdrawModel = $this->attachedMode();
  27. $set = Setting::getByGroup('pay_password') ?: [];
  28. return view('finance.balance.withdraw', [
  29. 'item' => $this->withdrawModel->toArray(),
  30. 'is_verify' => !empty($set['withdraw_verify']['is_phone_verify']) ? true : false,
  31. 'expire_time' => Session::get('withdraw_verify') ?: null,
  32. 'verify_phone' => $set['withdraw_verify']['phone'] ?: "",
  33. 'verify_expire' => $set['withdraw_verify']['verify_expire'] ? intval(
  34. $set['withdraw_verify']['verify_expire']
  35. ) : 10
  36. ])->render();
  37. }
  38. /**
  39. * @return mixed
  40. * @throws AppException
  41. */
  42. public function examine()
  43. {
  44. $requestData = \YunShop::request();
  45. $this->withdrawModel = $this->attachedMode();
  46. if (isset($requestData['submit_check'])) {
  47. //审核
  48. return $this->submitCheck();
  49. } elseif (isset($requestData['audited_rebut'])) {
  50. //审核后驳回
  51. return $this->auditedRebut();
  52. } elseif (isset($requestData['submit_cancel'])) {
  53. //重新审核
  54. return $this->submitCancel();
  55. } elseif (isset($requestData['submit_pay'])) {
  56. //打款
  57. return $this->submitPayNew();
  58. } elseif (isset($requestData['again_pay'])) {
  59. //重新打款
  60. $this->withdrawModel->status = 1;
  61. return $this->submitPayNew();
  62. } elseif (isset($requestData['confirm_pay'])) {
  63. //线下打款
  64. $this->withdrawModel->pay_at = time();
  65. $this->withdrawModel->status = 2;
  66. $this->withdrawModel->pay_way = 'manual';
  67. $this->withdrawUpdate();
  68. event(new BalanceWithdrawSuccessEvent($this->withdrawModel));
  69. BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
  70. return $this->message(
  71. '打款成功',
  72. yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id])
  73. );
  74. }
  75. return $this->message(
  76. '提交数据有误,请刷新重试',
  77. yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]),
  78. 'error'
  79. );
  80. }
  81. /**
  82. * 把打款方法剔出,临时使用
  83. */
  84. private function submitPayNew()
  85. {
  86. $check = $this->checkVerify();//打款验证
  87. if (!$check) {
  88. $this->message(
  89. '提现验证失败或验证已过期',
  90. yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
  91. 'error'
  92. );
  93. }
  94. $result = $this->submitPay();
  95. if (!empty($result) && 0 == $result['errno']) {
  96. //todo 临时增加手动打款成功通知,重构时候注意优化
  97. if ($this->withdrawModel->pay_way == 'manual') {
  98. event(new BalanceWithdrawSuccessEvent($this->withdrawModel));
  99. BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
  100. }
  101. return $this->message(
  102. '提现申请成功',
  103. yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id])
  104. );
  105. }
  106. BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);//提现失败通知
  107. $message = $result['message'] ?: '提现申请失败';
  108. return $this->message(
  109. $message,
  110. yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
  111. 'error'
  112. );
  113. }
  114. /**
  115. * 打款验证
  116. * @return bool
  117. */
  118. private function checkVerify()
  119. {
  120. $set = Setting::getByGroup('pay_password')['withdraw_verify'] ?: [];
  121. if (empty($set) || empty($set['is_phone_verify'])) {
  122. return true;
  123. }
  124. $verify = Session::get('withdraw_verify'); //没获取到
  125. if ($verify && $verify >= time()) {
  126. return true;
  127. }
  128. return false;
  129. }
  130. //提现审核
  131. private function submitCheck()
  132. {
  133. if ($this->withdrawModel->status != 0) {
  134. $this->message(
  135. '提交审核失败,该状态不能审核',
  136. yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]),
  137. 'error'
  138. );
  139. }
  140. $this->withdrawModel->status = $this->getPostStatus();
  141. $this->withdrawModel->reject_reason = $this->withdrawModel->status != 1 ? trim(
  142. request()->reject_reason
  143. ) : '';//驳回理由
  144. $this->withdrawModel->audit_at = time();
  145. if ($this->getPostStatus() == -1) {
  146. BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
  147. }
  148. if ($this->getPostStatus() == 3) {
  149. return (new AuditRejectedController())->index();
  150. //BalanceNoticeService::withdrawRejectNotice($this->withdrawModel);
  151. }
  152. $this->withdrawUpdate();
  153. return $this->message('提交审核成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
  154. }
  155. //审核后驳回
  156. private function auditedRebut()
  157. {
  158. return (new AuditRejectedController())->index();
  159. }
  160. /**
  161. * 提现重新审核
  162. * @return mixed
  163. */
  164. private function submitCancel()
  165. {
  166. return $this->submitCheck();
  167. }
  168. /**
  169. * 提现打款
  170. * @return mixed
  171. * @throws AppException
  172. */
  173. public function submitPay()
  174. {
  175. if ($this->withdrawModel->status !== 1) {
  176. throw new AppException('打款失败,数据不存在或不符合打款规则!');
  177. }
  178. $result = $this->payment();
  179. if (!empty($result) && 0 == $result['errno']) {
  180. $this->withdrawModel->pay_at = time();
  181. $this->withdrawModel->status = 2;
  182. $this->withdrawUpdate();
  183. } elseif (
  184. $this->withdrawModel->pay_way == 'alipay'
  185. || $this->withdrawModel->pay_way == 'yop_pay'
  186. || $this->withdrawModel->pay_way == 'silver_point'
  187. ) {
  188. $this->withdrawModel->pay_at = time();
  189. $this->withdrawModel->status = 4;
  190. $this->withdrawUpdate();
  191. }
  192. if (in_array($this->withdrawModel->pay_way, [
  193. 'high_light_wechat',
  194. 'high_light_alipay',
  195. 'high_light_bank',
  196. 'worker_withdraw_wechat',
  197. 'worker_withdraw_alipay',
  198. 'worker_withdraw_bank',
  199. ])) {
  200. if (!empty($result) && $result['errno'] == 200) {
  201. // $this->withdrawModel->pay_at = time();
  202. $this->withdrawModel->status = 4;
  203. $this->withdrawUpdate();
  204. $result['errno'] = 0;
  205. } else {
  206. \Log::debug($this->withdrawModel->pay_way . '提现错误' . $this->withdrawModel->withdraw_sn, $result);
  207. }
  208. }
  209. if ($this->withdrawModel->pay_way == 'wechat') {
  210. $memberModel = Member::uniacid()->where('uid', $this->withdrawModel->member_id)
  211. ->with(['hasOneFans', 'hasOneMiniApp', 'hasOneWechat'])->first();
  212. $v3_switch = false;
  213. if ($memberModel->hasOneFans->openid) {
  214. $income_set = Setting::get('shop.pay');
  215. $v3_switch = (bool)$income_set['weixin_apiv3'];
  216. } elseif (app('plugins')->isEnabled('min-app') && $memberModel->hasOneMiniApp->openid) {
  217. $appletSet = Setting::get('plugin.min_app');
  218. $v3_switch = (bool)$appletSet['v3_switch'];
  219. } elseif (app('plugins')->isEnabled('app-set') && $memberModel->hasOneWechat->openid) {
  220. $appSet = Setting::get('shop_app.pay');
  221. $v3_switch = (bool)$appSet['weixin_v3'];
  222. }
  223. if ($v3_switch && !empty($result) && 0 == $result['errno']) {
  224. //使用新版V3接口,保持打款中
  225. $this->withdrawModel->pay_at = time();
  226. $this->withdrawModel->status = 4;
  227. $this->withdrawUpdate();
  228. } else {
  229. \Log::debug(
  230. $this->withdrawModel->pay_way . '提现' . $this->withdrawModel->withdraw_sn,
  231. [$v3_switch, $result]
  232. );
  233. }
  234. }
  235. if ($this->withdrawModel->pay_way == 'converge_pay') {
  236. $this->withdrawModel->pay_at = time();
  237. $this->withdrawModel->status = 4;
  238. $this->withdrawUpdate();
  239. }
  240. return $result;
  241. }
  242. /**
  243. * 提现 model 数据保存
  244. * @return bool
  245. * @throws AppException
  246. */
  247. private function withdrawUpdate()
  248. {
  249. if (!$this->withdrawModel->save()) {
  250. throw new AppException('数据修改失败,请刷新重试');
  251. }
  252. return true;
  253. }
  254. /**
  255. * 提现打款,区分打款方式
  256. * @return mixed
  257. * @throws AppException
  258. */
  259. private function payment()
  260. {
  261. switch ($this->withdrawModel->pay_way) {
  262. case 'alipay':
  263. return $this->alipayPayment($this->paymentRemark());
  264. case 'wechat':
  265. return $this->wechatPayment();
  266. case 'manual':
  267. return $this->manualPayment();
  268. case 'eup_pay':
  269. return $this->eupPayment();
  270. case 'huanxun':
  271. return $this->huanxunPayment();
  272. case 'yop_pay': //易宝余额提现
  273. return $this->yopPayment();
  274. case 'converge_pay': //汇聚余额提现
  275. return $this->convergePayment($this->paymentRemark());
  276. case 'high_light_wechat': //高灯微信余额提现
  277. case 'high_light_alipay': //高灯支付宝余额提现
  278. case 'high_light_bank': //高灯银行卡余额提现
  279. return $this->highLightPayment();
  280. case 'worker_withdraw_wechat':
  281. case 'worker_withdraw_alipay':
  282. case 'worker_withdraw_bank':
  283. return $this->workWithdrawPayment();
  284. case 'eplus_withdraw_bank':
  285. return $this->eplusWithdrawPayment();
  286. case 'balance': //余额提现 todo:给保证金提现用
  287. return $this->balancePayment($this->paymentRemark());
  288. case 'silver_point':
  289. return $this->silverPointWithdrawPayment();
  290. default:
  291. throw new AppException('未知打款方式!!!');
  292. }
  293. }
  294. /**
  295. * 打款日志(备注)
  296. * @return string
  297. */
  298. private function paymentRemark()
  299. {
  300. return $remark = '提现打款-' . $this->withdrawModel->type_name . '-金额:' . $this->withdrawModel->actual_amounts . '元,' . '手续费:' . $this->withdrawModel->actual_poundage;
  301. }
  302. /**
  303. * 支付宝打款
  304. * @param $remark
  305. * @param null $withdraw
  306. */
  307. private function alipayPayment($remark, $withdraw = null)
  308. {
  309. $result = [];
  310. if (!is_null($withdraw)) {
  311. $result = WithdrawService::alipayWithdrawPay($withdraw, $remark);
  312. } else {
  313. $result = WithdrawService::alipayWithdrawPay($this->withdrawModel, $remark);
  314. }
  315. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "支付宝打款中!");
  316. if (!empty($result) && 1 == $result['errno']) {
  317. return $this->paymentError($result['message']);
  318. }
  319. return $result;
  320. }
  321. /**
  322. * 微信打款
  323. * @return mixed
  324. */
  325. private function wechatPayment()
  326. {
  327. $result = WithdrawService::wechatWithdrawPay($this->withdrawModel, $this->paymentRemark());
  328. //file_put_contents(storage_path('logs/withdraw1.log'),print_r($resultPay,true));
  329. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $this->paymentRemark() . "微信打款中!");
  330. if (!empty($result) && 1 == $result['errno']) {
  331. return $this->paymentError($result['message']);
  332. }
  333. return $result;
  334. //return $this->paymentSuccess();
  335. }
  336. /**
  337. * 余额打款
  338. * @param $remark
  339. */
  340. private function balancePayment($remark)
  341. {
  342. $result = WithdrawService::balanceWithdrawPay($this->withdrawModel, $remark);
  343. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "余额打款中!");
  344. if (!empty($result) && 1 == $result['errno']) {
  345. return $this->paymentError($result['message']);
  346. }
  347. return $result;
  348. }
  349. /**
  350. * @return array|mixed|void
  351. * @author blank
  352. * EUP打款
  353. */
  354. private function eupPayment()
  355. {
  356. $result = WithdrawService::eupWithdrawPay($this->withdrawModel);
  357. if (!empty($result) && $result['errno'] == 1) {
  358. return $this->paymentError($result['message']);
  359. }
  360. return $result;
  361. }
  362. /**
  363. * @return array|mixed|void
  364. * @author blank
  365. * 易宝余额提现
  366. */
  367. private function yopPayment()
  368. {
  369. $result = WithdrawService::yopWithdrawPay($this->withdrawModel);
  370. if (!empty($result) && $result['errno'] == 1) {
  371. return $this->paymentError($result['message']);
  372. }
  373. return $result;
  374. }
  375. private function huanxunPayment()
  376. {
  377. $result = WithdrawService::huanxunPayment($this->withdrawModel);
  378. if ($result['result'] == 10 || $result['result'] == 8) {
  379. return ['errno' => 0, 'message' => '打款成功'];
  380. }
  381. $result['errno'] = 1;
  382. return $result;
  383. }
  384. /**
  385. * 汇聚余额提现
  386. *
  387. * @param $remark
  388. * @return array|mixed
  389. * @throws AppException
  390. */
  391. private function convergePayment($remark)
  392. {
  393. $result = WithdrawService::convergePayMent($this->withdrawModel, $remark);
  394. if ($result['verify']) {
  395. return $result;
  396. }
  397. $msg = "收入提现ID:{$this->withdrawModel->id},汇聚提现失败:{$result['msg']}";
  398. return $this->paymentError($msg);
  399. }
  400. private function highLightPayment()
  401. {
  402. $result = WithdrawService::highLightWithdrawPay($this->withdrawModel);
  403. // if (!empty($result) && $result['errno'] == 500) {
  404. // return $this->paymentError($result['message']);
  405. // }
  406. return $result;
  407. }
  408. private function eplusWithdrawPayment()
  409. {
  410. return WithdrawService::eplusWithdrawPay($this->withdrawModel);
  411. }
  412. private function silverPointWithdrawPayment()
  413. {
  414. return WithdrawService::silverPointWithdrawPayment($this->withdrawModel);
  415. }
  416. private function workWithdrawPayment()
  417. {
  418. return WithdrawService::workerWithdrawPay($this->withdrawModel);
  419. }
  420. /**
  421. * 手动打款
  422. * @return mixed
  423. */
  424. private function manualPayment()
  425. {
  426. return ['errno' => 0, 'message' => '手动打款成功'];
  427. //return $this->paymentSuccess();
  428. }
  429. /**
  430. * 打款成功
  431. * @return mixed
  432. */
  433. private function paymentSuccess()
  434. {
  435. return $this->message('打款成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
  436. }
  437. /**
  438. * 打款失败
  439. * @param string $message
  440. * @throws AppException
  441. */
  442. private function paymentError($message = '')
  443. {
  444. $this->withdrawModel->status = 1;
  445. $this->withdrawUpdate();
  446. //发送打款失败通知
  447. BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
  448. throw new AppException($message ?: '打款失败,请重试');
  449. }
  450. /**
  451. * 附值打款 model
  452. * @return mixed
  453. * @throws AppException
  454. */
  455. private function attachedMode()
  456. {
  457. $result = Withdraw::getBalanceWithdrawById($this->getPostId());
  458. if (!$result) {
  459. throw new AppException('数据错误,请刷新重试');
  460. }
  461. return $result;
  462. }
  463. /**
  464. * @return string
  465. */
  466. private function getPostId()
  467. {
  468. return trim(\YunShop::request()->id);
  469. }
  470. /**
  471. * @return string
  472. */
  473. private function getPostStatus()
  474. {
  475. return trim(\YunShop::request()->status);
  476. }
  477. /**
  478. * 丁冉增加批量打款
  479. * @return mixed
  480. */
  481. public function batchAlipay()
  482. {
  483. $ids = \YunShop::request()->ids;
  484. $withdrawId = explode(',', $ids);
  485. $withdraw = [];
  486. if (!empty($withdrawId)) {
  487. foreach ($withdrawId as $id) {
  488. $withdraw_modle = Withdraw::getBalanceWithdrawById($id);
  489. if (!is_null($withdraw_modle)) {
  490. if ($withdraw_modle->status != '1') {
  491. BalanceNoticeService::withdrawFailureNotice($withdraw_modle);
  492. return $this->message(
  493. '打款失败,数据不存在或不符合打款规则!',
  494. yzWebUrl("finance.balance-withdraw.detail", ['id' => $id]),
  495. 'error'
  496. );
  497. }
  498. $withdraw[] = $withdraw_modle;
  499. $remark[] = '提现打款-' . $withdraw_modle->type_name . '-金额:' . $withdraw_modle->actual_amounts . '元,' .
  500. '手续费:' . $withdraw_modle->actual_poundage;
  501. }
  502. }
  503. $remark = json_encode($remark);
  504. $this->alipayPayment($remark, $withdraw);
  505. }
  506. }
  507. }