PaymentOrder.php 11 KB

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