PaymentOrder.php 10 KB

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