PaymentOrder.php 14 KB

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