PaymentOrder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. public function audit()
  90. {
  91. DB::beginTransaction();
  92. try {
  93. $validate = [
  94. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  95. 'ids.*' => ['required', 'integer', 'min:1'],
  96. 'status' => ['required', 'integer', 'in:1,3'],
  97. ];
  98. $status = request()->input('status', null);
  99. if ($status != 1) {
  100. $validate['remark'] = ['required', 'string', 'min:1', 'max:120'];
  101. }
  102. $params = request()->validate($validate);
  103. $remark = request()->input('remark', '');
  104. $count = 0;
  105. foreach ($params['ids'] as $id) {
  106. if ($params['status'] == 1) {
  107. $ret = PaymentOrderService::createPayout($id);
  108. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  109. } else {
  110. $ret = PaymentOrderService::withdrawalFailed($id, $remark);
  111. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  112. }
  113. $count++;
  114. }
  115. if ($count < 1) throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  116. DB::commit();
  117. } catch (ValidationException $e) {
  118. DB::rollBack();
  119. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  120. } catch (Exception $e) {
  121. DB::rollBack();
  122. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  123. }
  124. return $this->success();
  125. }
  126. /**
  127. * @apiParam {int} [page]
  128. * @apiParam {int} [limit]
  129. * @apiParam {int=0,1,2} type 类型 0全部 1=代收,2=代付
  130. * @apiParam {String} order_no 订单号
  131. * @apiPaaram {String} member_id 会员ID
  132. * @apiParam {int=0,1,2,3} [status] 状态:0待处理 1处理中 2成功 3失败
  133. *
  134. */
  135. public function index()
  136. {
  137. try {
  138. $params = request()->validate([
  139. 'page' => ['nullable', 'integer', 'min:1'],
  140. 'limit' => ['nullable', 'integer', 'min:1'],
  141. 'order_no' => ['nullable', 'string'],
  142. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  143. 'member_id' => ['nullable', 'integer'],
  144. 'first_name' => ['nullable'],
  145. 'payment_type' => ['nullable', 'integer'],
  146. 'channel' => ['nullable', 'string'],
  147. ]);
  148. $params['type'] = 1;
  149. $result = PaymentOrderService::paginate($params);
  150. } catch (ValidationException $e) {
  151. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  152. } catch (Exception $e) {
  153. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  154. }
  155. return $this->success($result);
  156. }
  157. /**
  158. * @description: 查询订单的支付情况
  159. * @return {*}
  160. */
  161. public function check()
  162. {
  163. $id = request()->input('id');
  164. if (empty($id)) {
  165. return $this->error(HttpStatus::CUSTOM_ERROR, '参数错误');
  166. }
  167. try {
  168. $result = PaymentOrderService::singlePayOrder($id);
  169. $this->success([], $result['msg'] ?? '操作成功');
  170. } catch (Exception $e) {
  171. return $this->error(intval($e->getCode()));
  172. }
  173. return $this->success([], $result['msg'] ?? '操作成功');
  174. }
  175. }