PaymentOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\PaymentOrderService;
  6. use Exception;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. use App\Models\PaymentOrder as PaymentOrderModel;
  10. use App\Models\Wallet;
  11. use App\Models\Recharge;
  12. use App\Models\Withdraw;
  13. use App\Services\BalanceLogService;
  14. use App\Services\AdminRealtime\AdminRealtime;
  15. class PaymentOrder extends Controller
  16. {
  17. //统计后台待处理订单及领取审核数(在线平台人数见 GET /admin/online/platformCount)
  18. public function unProcessed()
  19. {
  20. //USDT充值订单
  21. $data['usdtRecharge'] = (int)Recharge::where('status', 0)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  22. //人工充值订单
  23. $data['rgRecharge'] = (int)PaymentOrderModel::whereIn('status', [0,1,5])->where('type', 3)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  24. //人民币充值订单
  25. $data['rmbRecharge'] = (int)PaymentOrderModel::whereIn('status', [0,1])->where('type', 1)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  26. //USDT提现订单
  27. $data['usdtWithdraw'] = (int)Withdraw::where('status', 0)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  28. //人工提现订单
  29. $data['rgWithdraw'] = (int)PaymentOrderModel::where('status', 0)->where('type', 4)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  30. //人民币提现订单
  31. $data['rmbWithdraw'] = (int)PaymentOrderModel::where('status', 0)->where('type', 2)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-1 day')))->count();
  32. // 代理充值待处理:人工充值待发送收款信息/待审核 + USDT 凭证待审核(与会员订单分开统计)。
  33. // 迁移未执行前(先发代码后迁移)表不存在,捕获后跳过,正常情况下不额外查表结构。
  34. try {
  35. $data['agentRecharge'] = (int)\App\Models\Agent\AgentPaymentOrder::where('type', 3)->whereIn('status', [0, 5])->count()
  36. + (int)\App\Models\Agent\AgentRecharge::where('type', 2)->where('status', 0)->count();
  37. } catch (\Illuminate\Database\QueryException $e) {
  38. }
  39. // 代理申请(会员前台提交)与合营代理分成方案申请:所有待审核持续提示,不限制申请日期。
  40. try {
  41. $data['agentApplication'] = (int)DB::table('agent_applications')->where('status', 'pending')->count();
  42. $data['agentPlanApplication'] = (int)DB::table('agent_plan_applications')->where('status', 'pending')->count();
  43. } catch (\Illuminate\Database\QueryException $e) {
  44. }
  45. // 分享赚钱领取审核:所有未审核申请持续提示,不限制申请日期。
  46. $data['shareClaim'] = (int)DB::table('share_claims')->where('status', 'pending')->count();
  47. return $this->success($data);
  48. }
  49. //人工充值,配置充值信息
  50. public function setPayData()
  51. {
  52. DB::beginTransaction();
  53. try {
  54. $params = request()->validate([
  55. 'ids' => ['required', 'array', 'min:1'],
  56. 'ids.*' => ['required', 'integer', 'min:1'],
  57. 'payment_type' => ['required', 'integer'],
  58. 'pay_data' => ['required', 'string'],
  59. ]);
  60. // $count = PaymentOrderModel::whereIn('id', $params['ids'])->where('payment_type', $params['payment_type'])->where('status', 0)->count();
  61. // if ($count != count($params['ids'])) throw new Exception('数据匹配异常,请刷新页面重试!', HttpStatus::CUSTOM_ERROR);
  62. $pending = PaymentOrderModel::whereIn('id', $params['ids'])
  63. ->where('payment_type', $params['payment_type'])
  64. ->where('status', 0);
  65. $ids = (clone $pending)->pluck('id')->all();
  66. $pending->update(['pay_data' => $params['pay_data'], 'status' => 4, 'is_send' => 0]);
  67. AdminRealtime::record('payment_orders', $ids);
  68. DB::commit();
  69. } catch (ValidationException $e) {
  70. DB::rollBack();
  71. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  72. } catch (Exception $e) {
  73. DB::rollBack();
  74. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  75. }
  76. return $this->success();
  77. }
  78. //人工充值审核
  79. public function manualAudit()
  80. {
  81. DB::beginTransaction();
  82. try {
  83. $params = request()->validate([
  84. 'id' => ['required', 'integer', 'min:1'],
  85. 'status' => ['required', 'integer', 'in:2,3'],
  86. 'remark' => ['nullable', 'string'],
  87. ]);
  88. $remark = $params['remark'] ?? '';
  89. $info = PaymentOrderModel::where('id', $params['id'])
  90. ->where('type', PaymentOrderService::TYPE_SELF_PAY)
  91. ->lockForUpdate()->first();
  92. if (!$info) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  93. if ($params['status'] == 2 && $info->status != 5) throw new Exception('用户未提交充值凭证', HttpStatus::CUSTOM_ERROR);
  94. if ($params['status'] == 3 && !in_array($info->status, [0,1,4,5])) throw new Exception('订单已完成,不可驳回', HttpStatus::CUSTOM_ERROR);
  95. if ($params['status'] == 3) {
  96. //驳回
  97. if (empty($remark)) {
  98. throw new Exception('请填写原因', HttpStatus::CUSTOM_ERROR);
  99. }
  100. $info->status = $params['status'];
  101. $info->remark = $remark;
  102. $info->save();
  103. } else {
  104. //充值成功
  105. $info->status = $params['status'];
  106. $info->remark = $remark;
  107. $info->state = 1;
  108. $info->save();
  109. $memberId = $info->member_id;
  110. $amount = $info->amount;
  111. // 前台提交并审核到账的人工充值属于真实充值;后台直接加款仍使用“人工充值”。
  112. $changeType = '充值';
  113. $wallet = Wallet::where('member_id', $memberId)->lockForUpdate()->first();
  114. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  115. $availableBalance = bcadd($wallet->available_balance, $amount, 10);
  116. $beforeBalance = (string)$wallet->available_balance;
  117. $wallet->available_balance = $availableBalance;
  118. $wallet->save();
  119. $principal = BalanceLogService::addLog($memberId, $amount, $beforeBalance, $availableBalance, $changeType, $info->id, $remark);
  120. \App\Services\RechargeBonusService::grant('payment_order', $info, $principal);
  121. }
  122. DB::commit();
  123. } catch (ValidationException $e) {
  124. DB::rollBack();
  125. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  126. } catch (Exception $e) {
  127. DB::rollBack();
  128. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  129. }
  130. return $this->success();
  131. }
  132. // 在线充值(代收)拒绝:未完成的订单直接置为失败,之后三方回调/查单不会再入账
  133. public function reject()
  134. {
  135. DB::beginTransaction();
  136. try {
  137. $params = request()->validate([
  138. 'id' => ['required', 'integer', 'min:1'],
  139. 'remark' => ['nullable', 'string', 'max:120'],
  140. ]);
  141. $order = PaymentOrderModel::where('id', $params['id'])
  142. ->where('type', PaymentOrderService::TYPE_PAY)
  143. ->lockForUpdate()
  144. ->first();
  145. if (!$order) throw new Exception('订单不存在', HttpStatus::CUSTOM_ERROR);
  146. $rejectable = [PaymentOrderService::STATUS_STAY, PaymentOrderService::STATUS_PROCESS, PaymentOrderService::STATUS_TIMEOUT];
  147. if (!in_array((int)$order->status, $rejectable, true)) {
  148. throw new Exception('订单已结束,不可拒绝', HttpStatus::CUSTOM_ERROR);
  149. }
  150. $order->status = PaymentOrderService::STATUS_FAIL;
  151. $order->remark = trim((string)($params['remark'] ?? '')) ?: '后台拒绝';
  152. $order->save();
  153. AdminRealtime::record('payment_orders', [$order->id]);
  154. DB::commit();
  155. } catch (ValidationException $e) {
  156. DB::rollBack();
  157. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  158. } catch (Exception $e) {
  159. DB::rollBack();
  160. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  161. }
  162. return $this->success();
  163. }
  164. //提现审核
  165. public function audit()
  166. {
  167. DB::beginTransaction();
  168. try {
  169. $validate = [
  170. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  171. 'ids.*' => ['required', 'integer', 'min:1'],
  172. 'status' => ['required', 'integer', 'in:1,3'],
  173. 'is_rgtx' => ['nullable', 'integer', 'in:0,1'],
  174. ];
  175. $status = request()->input('status', null);
  176. if ($status != 1) {
  177. $validate['remark'] = ['required', 'string', 'min:1', 'max:120'];
  178. }
  179. $params = request()->validate($validate);
  180. $remark = request()->input('remark', '');
  181. $count = 0;
  182. foreach ($params['ids'] as $id) {
  183. if ($params['status'] == 1) {
  184. if (!empty($params['is_rgtx'])) {
  185. $order = PaymentOrderModel::where('id', $id)
  186. ->where('type', 4)
  187. ->where('status', PaymentOrderService::STATUS_STAY)
  188. ->lockForUpdate()
  189. ->first();
  190. if (!$order) throw new Exception("订单不存在_{$id}", HttpStatus::CUSTOM_ERROR);
  191. $order->status = PaymentOrderService::STATUS_SUCCESS;
  192. $order->save();
  193. // 人工提现打款完成:扣除冻结余额
  194. \App\Services\WithdrawFundsService::settle(\App\Services\WithdrawFundsService::PAYMENT_ORDERS, $order);
  195. } else {
  196. $ret = PaymentOrderService::createPayout($id);
  197. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  198. }
  199. } else {
  200. $ret = PaymentOrderService::withdrawalFailed($id, $remark);
  201. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  202. }
  203. $count++;
  204. }
  205. if ($count < 1) throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  206. DB::commit();
  207. } catch (ValidationException $e) {
  208. DB::rollBack();
  209. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  210. } catch (Exception $e) {
  211. DB::rollBack();
  212. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  213. }
  214. return $this->success();
  215. }
  216. /**
  217. * @apiParam {int} [page]
  218. * @apiParam {int} [limit]
  219. * @apiParam {int=0,1,2} type 类型 0全部 1=代收,2=代付
  220. * @apiParam {String} order_no 订单号
  221. * @apiPaaram {String} member_id 会员ID
  222. * @apiParam {int=1,2,3,7} [status] 状态:1处理中 2成功 3失败 7超时(处理中超过30分钟由 payment:pay-timeout 标记)
  223. *
  224. */
  225. public function index()
  226. {
  227. try {
  228. $params = request()->validate([
  229. 'page' => ['nullable', 'integer', 'min:1'],
  230. 'limit' => ['nullable', 'integer', 'min:1'],
  231. 'order_no' => ['nullable', 'string'],
  232. 'status' => ['nullable', 'integer'],
  233. 'member_id' => ['nullable', 'integer'],
  234. 'account' => ['nullable', 'string', 'max:64'],
  235. 'first_name' => ['nullable'],
  236. 'payment_type' => ['nullable', 'integer'],
  237. 'channel' => ['nullable', 'string'],
  238. ]);
  239. $params['type'] = 1;
  240. $result = PaymentOrderService::paginate($params);
  241. } catch (ValidationException $e) {
  242. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  243. } catch (Exception $e) {
  244. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  245. }
  246. return $this->success($result);
  247. }
  248. /**
  249. * @description: 查询订单的支付情况
  250. * @return {*}
  251. */
  252. public function check()
  253. {
  254. $id = request()->input('id');
  255. if (empty($id)) {
  256. return $this->error(HttpStatus::CUSTOM_ERROR, '参数错误');
  257. }
  258. try {
  259. $result = PaymentOrderService::singlePayOrder($id);
  260. $this->success([], $result['msg'] ?? '操作成功');
  261. } catch (Exception $e) {
  262. return $this->error(intval($e->getCode()));
  263. }
  264. return $this->success([], $result['msg'] ?? '操作成功');
  265. }
  266. //设置提现订单是否加锁
  267. public function setIsLocked()
  268. {
  269. DB::beginTransaction();
  270. try {
  271. $params = request()->validate([
  272. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  273. 'ids.*' => ['required', 'integer', 'min:1'],
  274. 'is_locked' => ['required', 'integer', 'in:1,0'],
  275. ]);
  276. PaymentOrderModel::whereIn('id',$params['ids'])->whereIn('type',[2,4])->update(['is_locked' => $params['is_locked']]);
  277. AdminRealtime::record('payment_orders', $params['ids']);
  278. DB::commit();
  279. } catch (ValidationException $e) {
  280. DB::rollBack();
  281. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  282. } catch (Exception $e) {
  283. DB::rollBack();
  284. return $this->error($e->getCode(), $e->getMessage());
  285. }
  286. return $this->success();
  287. }
  288. }