PrepaymentWithdrawController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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\exceptions\AppException;
  10. use app\common\helpers\Url;
  11. use app\common\models\Withdraw;
  12. use app\backend\modules\finance\services\WithdrawService;
  13. use app\common\components\BaseController;
  14. use app\common\services\finance\BalanceNoticeService;
  15. use Illuminate\Support\Facades\Log;
  16. use app\backend\modules\withdraw\controllers\AuditRejectedController;
  17. class PrepaymentWithdrawController extends BaseController
  18. {
  19. public $withdrawModel;
  20. public function detail()
  21. {
  22. $this->withdrawModel = $this->attachedMode();
  23. return view('finance.balance.withdraw', ['item' => $this->withdrawModel->toArray(),])->render();
  24. }
  25. /**
  26. * @return mixed
  27. * @throws AppException
  28. */
  29. public function examine()
  30. {
  31. $requestData = \YunShop::request();
  32. $this->withdrawModel = $this->attachedMode();
  33. if (isset($requestData['submit_check'])) {
  34. //审核
  35. return $this->submitCheck();
  36. } elseif (isset($requestData['submit_cancel'])) {
  37. //重新审核
  38. return $this->submitCancel();
  39. } elseif (isset($requestData['submit_pay'])) {
  40. //打款
  41. return $this->submitPayNew();
  42. }elseif (isset($requestData['again_pay'])) {
  43. //重新打款
  44. $this->withdrawModel->status = 1;
  45. return $this->submitPayNew();
  46. } elseif (isset($requestData['confirm_pay'])) {
  47. //线下打款
  48. $this->withdrawModel->pay_at = time();
  49. $this->withdrawModel->status = 2;
  50. $this->withdrawModel->pay_way = 'manual';
  51. $this->withdrawUpdate();
  52. // BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
  53. return $this->message('打款成功', yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]));
  54. }
  55. return $this->message('提交数据有误,请刷新重试', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]), 'error');
  56. }
  57. /**
  58. * 把打款方法剔出,临时使用
  59. */
  60. private function submitPayNew()
  61. {
  62. $result = $this->submitPay();
  63. if (!empty($result) && 0 == $result['errno']) {
  64. //todo 临时增加手动打款成功通知,重构时候注意优化
  65. if ($this->withdrawModel->pay_way == 'manual') {
  66. // BalanceNoticeService::withdrawSuccessNotice($this->withdrawModel);
  67. }
  68. return $this->message('提现申请成功', yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]));
  69. }
  70. // BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);//提现失败通知
  71. return $this->message('提现申请失败', yzWebUrl('finance.balance-withdraw.detail', ['id' => \YunShop::request()->id]), 'error');
  72. }
  73. /**
  74. * 提现审核
  75. * @return mixed
  76. */
  77. private function submitCheck()
  78. {
  79. $this->withdrawModel->status = $this->getPostStatus();
  80. $this->withdrawModel->audit_at = time();
  81. if ($this->getPostStatus() == -1) {
  82. // BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
  83. }
  84. if ($this->getPostStatus() == 3) {
  85. return (new AuditRejectedController())->index();
  86. // BalanceNoticeService::withdrawRejectNotice($this->withdrawModel);
  87. }
  88. $this->withdrawUpdate();
  89. return $this->message('提交审核成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
  90. }
  91. /**
  92. * 提现重新审核
  93. * @return mixed
  94. */
  95. private function submitCancel()
  96. {
  97. return $this->submitCheck();
  98. }
  99. /**
  100. * 提现打款
  101. * @return mixed
  102. * @throws AppException
  103. */
  104. public function submitPay()
  105. {
  106. if ($this->withdrawModel->status !== 1) {
  107. throw new AppException('打款失败,数据不存在或不符合打款规则!');
  108. }
  109. $result = $this->payment();
  110. if (!empty($result) && 0 == $result['errno']) {
  111. $this->withdrawModel->pay_at = time();
  112. $this->withdrawModel->status = 2;
  113. $this->withdrawUpdate();
  114. } elseif ($this->withdrawModel->pay_way == 'alipay' || $this->withdrawModel->pay_way == 'yop_pay') {
  115. $this->withdrawModel->pay_at = time();
  116. $this->withdrawModel->status = 4;
  117. $this->withdrawUpdate();
  118. }
  119. if ($this->withdrawModel->pay_way == 'converge_pay') {
  120. $this->withdrawModel->pay_at = time();
  121. $this->withdrawModel->status = 4;
  122. $this->withdrawUpdate();
  123. }
  124. return $result;
  125. }
  126. /**
  127. * 提现 model 数据保存
  128. * @return bool
  129. * @throws AppException
  130. */
  131. private function withdrawUpdate()
  132. {
  133. if (!$this->withdrawModel->save()) {
  134. throw new AppException('数据修改失败,请刷新重试');
  135. }
  136. return true;
  137. }
  138. /**
  139. * 提现打款,区分打款方式
  140. * @return mixed
  141. * @throws AppException
  142. */
  143. private function payment()
  144. {
  145. switch ($this->withdrawModel->pay_way) {
  146. case 'alipay':
  147. return $this->alipayPayment($this->paymentRemark());
  148. break;
  149. case 'wechat':
  150. return $this->wechatPayment();
  151. break;
  152. case 'manual':
  153. return $this->manualPayment();
  154. break;
  155. case 'eup_pay':
  156. return $this->eupPayment();
  157. break;
  158. case 'huanxun':
  159. return $this->huanxunPayment();
  160. break;
  161. case 'yop_pay': //易宝余额提现
  162. return $this->yopPayment();
  163. break;
  164. case 'converge_pay': //汇聚余额提现
  165. return $this->convergePayment($this->paymentRemark());
  166. break;
  167. case 'balance': //余额提现
  168. return $this->balancePayment($this->paymentRemark());
  169. break;
  170. default:
  171. throw new AppException('未知打款方式!!!');
  172. }
  173. }
  174. /**
  175. * 打款日志(备注)
  176. * @return string
  177. */
  178. private function paymentRemark()
  179. {
  180. return $remark = '提现打款-' . $this->withdrawModel->type_name . '-金额:' . $this->withdrawModel->actual_amounts . '元,' . '手续费:' . $this->withdrawModel->actual_poundage;
  181. }
  182. /**
  183. * 余额打款
  184. * @param $remark
  185. */
  186. private function balancePayment($remark)
  187. {
  188. $result = WithdrawService::balanceWithdrawPay($this->withdrawModel, $remark);
  189. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "余额打款中!");
  190. if (!empty($result) && 1 == $result['errno']) {
  191. return $this->paymentError($result['message']);
  192. }
  193. return $result;
  194. }
  195. /**
  196. * 支付宝打款
  197. * @param $remark
  198. * @param null $withdraw
  199. */
  200. private function alipayPayment($remark, $withdraw = null)
  201. {
  202. $result = [];
  203. if (!is_null($withdraw)) {
  204. $result = WithdrawService::alipayWithdrawPay($withdraw, $remark);
  205. } else {
  206. $result = WithdrawService::alipayWithdrawPay($this->withdrawModel, $remark);
  207. }
  208. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $remark . "支付宝打款中!");
  209. if (!empty($result) && 1 == $result['errno']) {
  210. return $this->paymentError($result['message']);
  211. }
  212. return $result;
  213. }
  214. /**
  215. * 微信打款
  216. * @return mixed
  217. */
  218. private function wechatPayment()
  219. {
  220. $result = WithdrawService::wechatWithdrawPay($this->withdrawModel, $this->paymentRemark());
  221. //file_put_contents(storage_path('logs/withdraw1.log'),print_r($resultPay,true));
  222. Log::info('MemberId:' . $this->withdrawModel->member_id . ', ' . $this->paymentRemark() . "微信打款中!");
  223. if (!empty($result) && 1 == $result['errno']) {
  224. return $this->paymentError($result['message']);
  225. }
  226. return $result;
  227. //return $this->paymentSuccess();
  228. }
  229. /**
  230. * @return array|mixed|void
  231. * @author blank
  232. * EUP打款
  233. */
  234. private function eupPayment()
  235. {
  236. $result = WithdrawService::eupWithdrawPay($this->withdrawModel);
  237. if (!empty($result) && $result['errno'] == 1) {
  238. return $this->paymentError($result['message']);
  239. }
  240. return $result;
  241. }
  242. /**
  243. * @return array|mixed|void
  244. * @author blank
  245. * 易宝余额提现
  246. */
  247. private function yopPayment()
  248. {
  249. $result = WithdrawService::yopWithdrawPay($this->withdrawModel);
  250. if (!empty($result) && $result['errno'] == 1) {
  251. return $this->paymentError($result['message']);
  252. }
  253. return $result;
  254. }
  255. private function huanxunPayment()
  256. {
  257. $result = WithdrawService::huanxunPayment($this->withdrawModel);
  258. if ($result['result'] == 10 || $result['result'] == 8) {
  259. return ['errno' => 0, 'message' => '打款成功'];
  260. }
  261. $result['errno'] = 1;
  262. return $result;
  263. }
  264. /**
  265. * 汇聚余额提现
  266. *
  267. * @param $remark
  268. * @return array|mixed
  269. * @throws AppException
  270. */
  271. private function convergePayment($remark)
  272. {
  273. $result = WithdrawService::convergePayMent($this->withdrawModel, $remark);
  274. if ($result['verify']) {
  275. return $result;
  276. }
  277. $msg = "收入提现ID:{$this->withdrawModel->id},汇聚提现失败:{$result['msg']}";
  278. return $this->paymentError($msg);
  279. }
  280. /**
  281. * 手动打款
  282. * @return mixed
  283. */
  284. private function manualPayment()
  285. {
  286. return ['errno' => 0, 'message' => '手动打款成功'];
  287. //return $this->paymentSuccess();
  288. }
  289. /**
  290. * 打款成功
  291. * @return mixed
  292. */
  293. private function paymentSuccess()
  294. {
  295. return $this->message('打款成功', yzWebUrl("finance.balance-withdraw.detail", ['id' => $this->getPostId()]));
  296. }
  297. /**
  298. * 打款失败
  299. * @param string $message
  300. * @throws AppException
  301. */
  302. private function paymentError($message = '')
  303. {
  304. $this->withdrawModel->status = 1;
  305. $this->withdrawUpdate();
  306. //发送打款失败通知
  307. // BalanceNoticeService::withdrawFailureNotice($this->withdrawModel);
  308. throw new AppException($message ?: '打款失败,请重试');
  309. }
  310. /**
  311. * 附值打款 model
  312. * @return mixed
  313. * @throws AppException
  314. */
  315. private function attachedMode()
  316. {
  317. $result = Withdraw::getBalanceWithdrawById($this->getPostId());
  318. if (!$result) {
  319. throw new AppException('数据错误,请刷新重试');
  320. }
  321. return $result;
  322. }
  323. /**
  324. * @return string
  325. */
  326. private function getPostId()
  327. {
  328. return trim(\YunShop::request()->id);
  329. }
  330. /**
  331. * @return string
  332. */
  333. private function getPostStatus()
  334. {
  335. return trim(\YunShop::request()->status);
  336. }
  337. /**
  338. * 丁冉增加批量打款
  339. * @return mixed
  340. */
  341. public function batchAlipay()
  342. {
  343. $ids = \YunShop::request()->ids;
  344. $withdrawId = explode(',', $ids);
  345. $withdraw = [];
  346. if (!empty($withdrawId)) {
  347. foreach ($withdrawId as $id) {
  348. $withdraw_modle = Withdraw::getBalanceWithdrawById($id);
  349. if (!is_null($withdraw_modle)) {
  350. if ($withdraw_modle->status != '1') {
  351. // BalanceNoticeService::withdrawFailureNotice($withdraw_modle);
  352. return $this->message('打款失败,数据不存在或不符合打款规则!', yzWebUrl("finance.balance-withdraw.detail", ['id' => $id]), 'error');
  353. }
  354. $withdraw[] = $withdraw_modle;
  355. $remark[] = '提现打款-' . $withdraw_modle->type_name . '-金额:' . $withdraw_modle->actual_amounts . '元,' .
  356. '手续费:' . $withdraw_modle->actual_poundage;
  357. }
  358. }
  359. $remark = json_encode($remark);
  360. $this->alipayPayment($remark, $withdraw);
  361. }
  362. }
  363. }