PaymentOrder.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Services\BalanceLogService;
  12. class PaymentOrder extends Controller
  13. {
  14. //人工充值,配置充值信息
  15. public function setPayData()
  16. {
  17. DB::beginTransaction();
  18. try {
  19. $params = request()->validate([
  20. 'ids' => ['required', 'array', 'min:1'],
  21. 'ids.*' => ['required', 'integer', 'min:1'],
  22. 'payment_type' => ['required', 'integer'],
  23. 'pay_data' => ['required', 'string'],
  24. ]);
  25. // $count = PaymentOrderModel::whereIn('id', $params['ids'])->where('payment_type', $params['payment_type'])->where('status', 0)->count();
  26. // if ($count != count($params['ids'])) throw new Exception('数据匹配异常,请刷新页面重试!', HttpStatus::CUSTOM_ERROR);
  27. PaymentOrderModel::whereIn('id', $params['ids'])
  28. ->where('payment_type', $params['payment_type'])
  29. ->where('status', 0)
  30. ->update(['pay_data' => $params['pay_data'], 'status' => 4, 'is_send' => 0]);
  31. DB::commit();
  32. } catch (ValidationException $e) {
  33. DB::rollBack();
  34. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. DB::rollBack();
  37. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  38. }
  39. return $this->success();
  40. }
  41. //人工充值审核
  42. public function manual‌Audit()
  43. {
  44. DB::beginTransaction();
  45. try {
  46. $params = request()->validate([
  47. 'id' => ['required', 'integer'],
  48. 'status' => ['required', 'integer', 'in:2,3'],
  49. 'remark' => ['nullable', 'string'],
  50. ]);
  51. $remark = $params['remark'] ?? '';
  52. $info = PaymentOrderModel::find($params['id']);
  53. if (!$info) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  54. if ($info->status != 5) throw new Exception('用户未提交充值凭证', HttpStatus::CUSTOM_ERROR);
  55. if ($info->payment_type == 0) throw new Exception('当前订单非人工充值,无法操作', HttpStatus::CUSTOM_ERROR);
  56. if ($params['status'] == 3) {
  57. if (empty($remark)) {
  58. throw new Exception('请填写原因', HttpStatus::CUSTOM_ERROR);
  59. }
  60. $info->status = $params['status'];
  61. $info->remark = $remark;
  62. $info->save();
  63. } else {
  64. //充值成功
  65. $info->status = $params['status'];
  66. $info->remark = $remark;
  67. $info->state = 1;
  68. $info->save();
  69. $memberId = $info->member_id;
  70. $amount = $info->amount;
  71. $changeType = '人工充值';
  72. $wallet = Wallet::where('member_id', $memberId)->first();
  73. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  74. $availableBalance = bcadd($wallet->available_balance, $amount, 10);
  75. BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, $changeType, null, $remark);
  76. $wallet->available_balance = $availableBalance;
  77. $wallet->save();
  78. }
  79. DB::commit();
  80. } catch (ValidationException $e) {
  81. DB::rollBack();
  82. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  83. } catch (Exception $e) {
  84. DB::rollBack();
  85. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  86. }
  87. return $this->success();
  88. }
  89. //提现审核
  90. public function audit()
  91. {
  92. DB::beginTransaction();
  93. try {
  94. $validate = [
  95. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  96. 'ids.*' => ['required', 'integer', 'min:1'],
  97. 'status' => ['required', 'integer', 'in:1,3'],
  98. 'is_rgtx' => ['nullable', 'integer', 'in:0,1'],
  99. ];
  100. $status = request()->input('status', null);
  101. if ($status != 1) {
  102. $validate['remark'] = ['required', 'string', 'min:1', 'max:120'];
  103. }
  104. $params = request()->validate($validate);
  105. $remark = request()->input('remark', '');
  106. $count = 0;
  107. foreach ($params['ids'] as $id) {
  108. if ($params['status'] == 1) {
  109. if (!empty($params['is_rgtx'])) {
  110. $order = PaymentOrderModel::where('id', $id)
  111. ->where('type', 4)
  112. ->where('status', PaymentOrderService::STATUS_STAY)
  113. ->first();
  114. if (!$order) throw new Exception("订单不存在_{$id}", HttpStatus::CUSTOM_ERROR);
  115. $order->status = PaymentOrderService::STATUS_SUCCESS;
  116. $order->save();
  117. } else {
  118. $ret = PaymentOrderService::createPayout($id);
  119. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  120. }
  121. } else {
  122. $ret = PaymentOrderService::withdrawalFailed($id, $remark);
  123. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  124. }
  125. $count++;
  126. }
  127. if ($count < 1) throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  128. DB::commit();
  129. } catch (ValidationException $e) {
  130. DB::rollBack();
  131. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  132. } catch (Exception $e) {
  133. DB::rollBack();
  134. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  135. }
  136. return $this->success();
  137. }
  138. /**
  139. * @apiParam {int} [page]
  140. * @apiParam {int} [limit]
  141. * @apiParam {int=0,1,2} type 类型 0全部 1=代收,2=代付
  142. * @apiParam {String} order_no 订单号
  143. * @apiPaaram {String} member_id 会员ID
  144. * @apiParam {int=0,1,2,3} [status] 状态:0待处理 1处理中 2成功 3失败
  145. *
  146. */
  147. public function index()
  148. {
  149. try {
  150. $params = request()->validate([
  151. 'page' => ['nullable', 'integer', 'min:1'],
  152. 'limit' => ['nullable', 'integer', 'min:1'],
  153. 'order_no' => ['nullable', 'string'],
  154. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  155. 'member_id' => ['nullable', 'integer'],
  156. 'first_name' => ['nullable'],
  157. 'payment_type' => ['nullable', 'integer'],
  158. 'channel' => ['nullable', 'string'],
  159. ]);
  160. $params['type'] = 1;
  161. $result = PaymentOrderService::paginate($params);
  162. } catch (ValidationException $e) {
  163. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  164. } catch (Exception $e) {
  165. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  166. }
  167. return $this->success($result);
  168. }
  169. /**
  170. * @description: 查询订单的支付情况
  171. * @return {*}
  172. */
  173. public function check()
  174. {
  175. $id = request()->input('id');
  176. if (empty($id)) {
  177. return $this->error(HttpStatus::CUSTOM_ERROR, '参数错误');
  178. }
  179. try {
  180. $result = PaymentOrderService::singlePayOrder($id);
  181. $this->success([], $result['msg'] ?? '操作成功');
  182. } catch (Exception $e) {
  183. return $this->error(intval($e->getCode()));
  184. }
  185. return $this->success([], $result['msg'] ?? '操作成功');
  186. }
  187. }