PaymentOrder.php 9.9 KB

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