PaymentOrder.php 15 KB

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