PaymentOrder.php 14 KB

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