PaymentOrder.php 11 KB

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