PaymentOrder.php 11 KB

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