PaymentOrder.php 11 KB

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