WithdrawController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/31
  6. * Time: 上午11:28
  7. */
  8. namespace app\backend\modules\finance\controllers;
  9. use app\backend\modules\finance\models\Withdraw;
  10. use app\backend\modules\finance\services\WithdrawService;
  11. use app\backend\modules\withdraw\controllers\AgainPayController;
  12. use app\backend\modules\withdraw\controllers\AuditController;
  13. use app\backend\modules\withdraw\controllers\AuditedRebutController;
  14. use app\backend\modules\withdraw\controllers\ConfirmPayController;
  15. use app\backend\modules\withdraw\controllers\PayController;
  16. use app\common\components\BaseController;
  17. use app\common\events\withdraw\WithdrawAuditedEvent;
  18. use app\common\events\withdraw\WithdrawPayedEvent;
  19. use app\common\exceptions\AppException;
  20. use app\common\facades\Setting;
  21. use app\common\models\Income;
  22. use app\common\services\Session;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Log;
  25. class WithdrawController extends BaseController
  26. {
  27. private $withdrawModel;
  28. public function dealt()
  29. {
  30. $resultData = \YunShop::request();
  31. \Log::debug('开始执行提现程序+++++++++++++++++', $resultData['submit_check']);
  32. if (isset($resultData['submit_check'])) {
  33. //审核
  34. return (new AuditController())->index();
  35. } elseif (isset($resultData['submit_pay'])) {
  36. //打款
  37. return (new PayController())->index();
  38. } elseif (isset($resultData['submit_cancel'])) {
  39. //重新审核
  40. return (new AuditController())->index();
  41. } elseif (isset($resultData['confirm_pay'])) {
  42. //确认打款
  43. $check = $this->checkVerify();//打款验证
  44. if (!$check) {
  45. $this->message('提现验证失败或验证已过期', yzWebUrl("withdraw.records", ['id' => $resultData['id']]), 'error');
  46. }
  47. return (new ConfirmPayController())->index();
  48. } elseif (isset($resultData['again_pay'])) {
  49. //重新打款
  50. $check = $this->checkVerify();//打款验证
  51. if (!$check) {
  52. $this->message('提现验证失败或验证已过期', yzWebUrl("withdraw.records", ['id' => $resultData['id']]), 'error');
  53. }
  54. return (new AgainPayController())->index();
  55. } elseif (isset($resultData['audited_rebut'])) {
  56. //审核后驳回
  57. return (new AuditedRebutController())->index();
  58. }
  59. return $this->message('提交数据有误,请刷新重试', yzWebUrl("withdraw.records", ['id' => $resultData['id']]));
  60. }
  61. /**
  62. * 打款验证
  63. * @return bool
  64. */
  65. private function checkVerify()
  66. {
  67. $set = Setting::getByGroup('pay_password')['withdraw_verify'] ?: [];
  68. if (empty($set) || empty($set['is_phone_verify'])) {
  69. return true;
  70. }
  71. $verify = Session::get('withdraw_verify'); //没获取到
  72. if ($verify && $verify >= time()) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * 发送验证码
  79. * @return \Illuminate\Http\JsonResponse
  80. */
  81. public function sendCode()
  82. {
  83. $set = Setting::getByGroup('pay_password')['withdraw_verify'] ?: [];
  84. $phone = $set['phone'];
  85. if (!$phone) {
  86. return $this->errorJson('无需发送验证码');
  87. }
  88. $sms = app('sms')->sendWithdrawSet($phone,'86','_withdrawVerify');
  89. if ($sms['status'] == 0) {
  90. return $this->errorJson($sms['json']);
  91. }
  92. return $this->successJson();
  93. }
  94. /**
  95. * 校验提现打款验证码
  96. * @return \Illuminate\Http\JsonResponse
  97. */
  98. public function checkVerifyCode()
  99. {
  100. $code = request()->code;
  101. if (empty($code)) {
  102. return $this->errorJson('请填写验证码');
  103. }
  104. $set = Setting::getByGroup('pay_password')['withdraw_verify'] ?: [];
  105. if (empty($set)) {
  106. return $this->successJson('无需验证');
  107. }
  108. $check = app('sms')->checkCode($set['phone'],$code,'_withdrawVerify');
  109. if ($check['status'] == 0) {
  110. return $this->errorJson($check['json']);
  111. }
  112. $expire = ($set['verify_expire']&&intval($set['verify_expire'])?intval($set['verify_expire']):10) * 60;
  113. Session::set('withdraw_verify',($expire + time()),$expire);
  114. return $this->successJson('校验成功',['expire'=>($expire + time())]);
  115. }
  116. public function submitCheck($withdrawId, $incomeData)
  117. {
  118. \Log::debug('审核检测接口+++++++++++++++++');
  119. $this->withdrawModel = $this->getWithdrawModel($withdrawId);
  120. if ($this->withdrawModel->status != Withdraw::STATUS_INITIAL) {
  121. return ['msg' => '审核失败,数据不符合提现规则!'];
  122. }
  123. return $this->examine();
  124. /*$withdraw = Withdraw::getWithdrawById($withdrawId)->first();
  125. if ($withdraw->status != '0') {
  126. return json_encode(['msg' => '审核失败,数据不符合提现规则!']);
  127. }
  128. $withdrawStatus = "-1";
  129. $actual_amounts = 0;
  130. // 修改 yz_member_income 表
  131. foreach ($incomeData as $key => $income) {
  132. if ($income == 1) {
  133. $withdrawStatus = "1";
  134. $actual_amounts += Income::getIncomeById($key)->get()->sum('amount');
  135. Income::updatedIncomePayStatus($key, ['pay_status' => '1']);
  136. } elseif ($income == -1) {
  137. $withdrawStatus = "1";
  138. Income::updatedIncomePayStatus($key, ['pay_status' => '3','status'=> '0']);
  139. } else {
  140. Income::updatedIncomePayStatus($key, ['pay_status' => '-1']);
  141. }
  142. }
  143. $actual_poundage = sprintf("%.2f", $actual_amounts / 100 * $withdraw['poundage_rate']);
  144. $actual_servicetax = sprintf("%.2f", ($actual_amounts - $actual_poundage) / 100 * $withdraw['servicetax_rate']);
  145. $updatedData = [
  146. 'status' => $withdrawStatus,
  147. 'actual_amounts' => $actual_amounts - $actual_poundage - $actual_servicetax,
  148. 'actual_poundage' => $actual_poundage,
  149. 'actual_servicetax' => $actual_servicetax,
  150. 'audit_at' => time(),
  151. ];
  152. $result = Withdraw::updatedWithdrawStatus($withdrawId, $updatedData);
  153. if ($result) {
  154. $noticeData = $withdraw;
  155. $noticeData->status = $withdrawStatus;
  156. $noticeData->actual_amounts = $updatedData['actual_amounts'];
  157. $noticeData->actual_poundage = $updatedData['actual_poundage'];
  158. $noticeData->audit_at = $updatedData['audit_at'];
  159. //审核通知事件
  160. event(new AfterIncomeWithdrawCheckEvent($noticeData));
  161. return json_encode(['msg' => '审核成功!']);
  162. }
  163. return json_encode(['msg' => '审核失败!'];)*/
  164. }
  165. public function submitCancel($withdrawId, $incomeData)
  166. {
  167. $this->withdrawModel = $this->getWithdrawModel($withdrawId);
  168. if ($this->withdrawModel->status != Withdraw::STATUS_INVALID) {
  169. return ['msg' => '重审核失败,数据不符合提现规则!'];
  170. }
  171. return $this->examine();
  172. /*$withdraw = Withdraw::getWithdrawById($withdrawId)->first();
  173. if ($withdraw->status != '-1') {
  174. return json_encode(['msg' => '审核失败,数据不符合提现规则!']);
  175. }
  176. $withdrawStatus = "-1";
  177. $actual_amounts = 0;
  178. foreach ($incomeData as $key => $income) {
  179. if ($income == 1) {
  180. $actual_amounts += Income::getIncomeById($key)->get()->sum('amount');
  181. $withdrawStatus = "1";
  182. Income::updatedIncomePayStatus($key, ['pay_status' => '1']);
  183. } elseif ($income == -1) {
  184. $withdrawStatus = "1";
  185. Income::updatedIncomePayStatus($key, ['pay_status' => '3','status'=> '0']);
  186. } else {
  187. Income::updatedIncomePayStatus($key, ['pay_status' => '-1']);
  188. }
  189. }
  190. $actual_poundage = sprintf("%.2f", $actual_amounts / 100 * $withdraw['poundage_rate']);
  191. $actual_servicetax = sprintf("%.2f", ($actual_amounts - $actual_poundage) / 100 * $withdraw['servicetax_rate']);
  192. $updatedData = [
  193. 'status' => $withdrawStatus,
  194. 'actual_amounts' => $actual_amounts - $actual_poundage - $actual_servicetax,
  195. 'actual_poundage' => $actual_poundage,
  196. 'actual_servicetax' => $actual_servicetax,
  197. 'audit_at' => time(),
  198. ];
  199. $result = Withdraw::updatedWithdrawStatus($withdrawId, $updatedData);
  200. if ($result) {
  201. $noticeData = $withdraw;
  202. $noticeData->status = $withdrawStatus;
  203. $noticeData->actual_amounts = $updatedData['actual_amounts'];
  204. $noticeData->actual_poundage = $updatedData['actual_poundage'];
  205. $noticeData->audit_at = $updatedData['audit_at'];
  206. //重新审核通知事件
  207. event(new AfterIncomeWithdrawCheckEvent($noticeData));
  208. return json_encode(['msg' => '审核成功!']);
  209. }
  210. return json_encode(['msg' => '审核失败!']);*/
  211. }
  212. public function submitPay($withdrawId, $payWay)
  213. {
  214. if (!is_array($withdrawId)) {
  215. $withdraw = Withdraw::getWithdrawById($withdrawId)->first();
  216. if ($withdraw->status != '1') {
  217. return ['msg' => '打款失败,数据不存在或不符合打款规则!'];
  218. }
  219. $remark = '提现打款-' . $withdraw->type_name . '-金额:' . $withdraw->actual_amounts . '元,' .
  220. '手续费:' . $withdraw->actual_poundage;
  221. } else {
  222. //支付宝批量打款
  223. $withdraw = [];
  224. if ($payWay == '2' && !empty($withdrawId)) {
  225. foreach ($withdrawId as $id) {
  226. $withdraw_modle = Withdraw::getWithdrawById($id)->first();
  227. if (!is_null($withdraw_modle)) {
  228. if ($withdraw_modle->status != '1') {
  229. return ['msg' => '打款失败,数据不存在或不符合打款规则!'];
  230. }
  231. $withdraw[] = $withdraw_modle;
  232. $remark[] = '提现打款-' . $withdraw_modle->type_name . '-金额:' . $withdraw_modle->actual_amounts . '元,' .
  233. '手续费:' . $withdraw_modle->actual_poundage;
  234. }
  235. }
  236. $remark = json_encode($remark);
  237. }
  238. }
  239. if ($payWay == '3') {
  240. //余额打款
  241. $resultPay = WithdrawService::balanceWithdrawPay($withdraw, $remark);
  242. if (is_bool($resultPay)) {
  243. $resultPay = ['errno' => 0, 'message' => '余额打款成功'];
  244. }
  245. Log::info('MemberId:' . $withdraw->member_id . ', ' . $remark . "打款到余额中!");
  246. } elseif ($payWay == '2') {
  247. //支付宝打款
  248. $resultPay = WithdrawService::alipayWithdrawPay($withdraw, $remark);
  249. Log::info('MemberId:' . $withdraw->member_id . ', ' . $remark . "支付宝打款中!");
  250. } elseif ($payWay == '1') {
  251. //微信打款
  252. $resultPay = WithdrawService::wechatWithdrawPay($withdraw, $remark);
  253. Log::info('resultPay:' . $resultPay);
  254. Log::info('MemberId:' . $withdraw->member_id . ', ' . $remark . "微信打款中!");
  255. } elseif ($payWay == '4') {
  256. //手动打款
  257. $resultPay = ['errno' => 0, 'message' => '手动打款成功'];
  258. Log::info('MemberId:' . $withdraw->member_id . ', ' . $remark . "手动打款!");
  259. }
  260. if (!empty($resultPay) && 0 == $resultPay['errno']) {
  261. $withdraw->pay_status = 1;
  262. //审核通知事件
  263. event(new WithdrawPayedEvent($withdraw));
  264. $updatedData = ['pay_at' => time()];
  265. Withdraw::updatedWithdrawStatus($withdrawId, $updatedData);
  266. $result = WithdrawService::otherWithdrawSuccess($withdrawId);
  267. return ['msg' => '提现打款成功!'];
  268. } elseif ($payWay == '2') {
  269. //修改提现记录状态
  270. $updatedData = [
  271. 'status' => 4,
  272. 'arrival_at' => time(),
  273. ];
  274. \Log::info('修改提现记录状态',print_r($updatedData,true));
  275. return Withdraw::updatedWithdrawStatus($withdrawId, $updatedData);
  276. }
  277. }
  278. public function batchAlipay()
  279. {
  280. $ids = \YunShop::request()->ids;
  281. $ids = explode(',', $ids);
  282. $result = $this->submitPay($ids, 2);
  283. }
  284. public function getAllWithdraw()
  285. {
  286. $type = request('type');
  287. $res = Withdraw::getAllWithdraw($type);
  288. return json_encode($res);
  289. }
  290. public function updateWidthdrawOrderStatus()
  291. {
  292. $ids = \YunShop::request()->ids;
  293. $status = 0;
  294. if (empty($ids)) {
  295. return ['status' => $status];
  296. }
  297. $withdrawId = explode(',', $ids);
  298. if (Withdraw::updateWidthdrawOrderStatus($withdrawId)) {
  299. $status = 1;
  300. }
  301. return ['status' => $status];
  302. }
  303. private function examine()
  304. {
  305. $audit_data = \YunShop::request()->audit;
  306. $audit_count = count($audit_data);
  307. $actual_amounts = 0;
  308. $adopt_count = 0;
  309. $invalid_count = 0;
  310. $reject_count = 0;
  311. DB::beginTransaction();
  312. foreach ($audit_data as $income_id => $status) {
  313. //通过
  314. if ($status == Withdraw::STATUS_AUDIT) {
  315. $adopt_count += 1;
  316. $actual_amounts += Income::uniacid()->where('id', $income_id)->sum('amount');
  317. Income::where('id',$income_id)->update(['pay_status' => Income::PAY_STATUS_WAIT]);
  318. }
  319. //无效
  320. if ($status == Withdraw::STATUS_INVALID) {
  321. $invalid_count += 1;
  322. Income::where('id',$income_id)->update(['pay_status' => Income::PAY_STATUS_INVALID]);
  323. }
  324. //驳回
  325. if ($status == Withdraw::STATUS_REBUT) {
  326. $reject_count += 1;
  327. Income::where('id',$income_id)->update(['status' => Income::STATUS_INITIAL, 'pay_status' => Income::PAY_STATUS_REJECT]);
  328. }
  329. }
  330. $this->withdrawModel->status = Withdraw::STATUS_AUDIT;
  331. //如果全无效
  332. if ($invalid_count > 0 && $invalid_count == $audit_count) {
  333. $this->withdrawModel->status = Withdraw::STATUS_INVALID;
  334. }
  335. //如果全驳回
  336. if ($reject_count > 0 && $reject_count == $audit_count) {
  337. $this->withdrawModel->status = Withdraw::STATUS_PAY;
  338. $this->withdrawModel->pay_at = $this->withdrawModel->arrival_at = time();
  339. }
  340. //如果是无效 + 驳回 [同全驳回,直接完成]
  341. if ($invalid_count > 0 && $reject_count > 0 && ($invalid_count + $reject_count) == $audit_count) {
  342. $this->withdrawModel->status = Withdraw::STATUS_PAY;
  343. $this->withdrawModel->pay_at = $this->withdrawModel->arrival_at = time();
  344. }
  345. $this->withdrawModel->audit_at = time();
  346. $this->withdrawModel->actual_poundage = $this->getActualPoundage($actual_amounts);
  347. $this->withdrawModel->actual_servicetax = $this->getActualServiceTax($actual_amounts);
  348. $this->withdrawModel->actual_amounts = $actual_amounts - $this->getActualPoundage($actual_amounts) - $this->getActualServiceTax($actual_amounts);
  349. $result = $this->withdrawModel->save();
  350. if ($result !== true) {
  351. DB::rollBack();
  352. return ['msg' => '审核失败:记录修改失败!'];
  353. }
  354. event(new WithdrawAuditedEvent($this->withdrawModel));
  355. DB::commit();
  356. return ['msg' => '审核成功!'];
  357. }
  358. /**
  359. * 手续费
  360. * @return string
  361. */
  362. private function getActualPoundage($amount)
  363. {
  364. return bcdiv(bcmul($amount,$this->withdrawModel->poundage_rate,4),100,2);
  365. }
  366. /**
  367. * 劳务税
  368. * @return string
  369. */
  370. private function getActualServiceTax($amount)
  371. {
  372. $amount =$amount - $this->getActualPoundage($amount);
  373. return bcdiv(bcmul($amount,$this->withdrawModel->servicetax_rate,4),100,2);
  374. }
  375. private function getWithdrawModel($withdraw_id)
  376. {
  377. $_model = Withdraw::find($withdraw_id);
  378. if (!$_model) {
  379. throw new AppException('数据不存在或已被删除!');
  380. }
  381. return $_model;
  382. }
  383. }