PaymentOrder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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' => 1]);
  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 != 4) 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->is_send = 0;//标记发送通知
  69. $info->save();
  70. $memberId = $info->member_id;
  71. $amount = $info->amount;
  72. $changeType = '人工充值';
  73. $wallet = Wallet::where('member_id', $memberId)->first();
  74. if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
  75. $availableBalance = bcadd($wallet->available_balance, $amount, 10);
  76. BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, $changeType, null, $remark);
  77. $wallet->available_balance = $availableBalance;
  78. $wallet->save();
  79. }
  80. DB::commit();
  81. } catch (ValidationException $e) {
  82. DB::rollBack();
  83. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  84. } catch (Exception $e) {
  85. DB::rollBack();
  86. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  87. }
  88. return $this->success();
  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. ];
  99. $status = request()->input('status', null);
  100. if ($status != 1) {
  101. $validate['remark'] = ['required', 'string', 'min:1', 'max:120'];
  102. }
  103. $params = request()->validate($validate);
  104. $remark = request()->input('remark', '');
  105. $count = 0;
  106. foreach ($params['ids'] as $id) {
  107. if ($params['status'] == 1) {
  108. $ret = PaymentOrderService::createPayout($id);
  109. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  110. } else {
  111. $ret = PaymentOrderService::withdrawalFailed($id, $remark);
  112. if ($ret['code'] !== 0) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  113. }
  114. $count++;
  115. }
  116. if ($count < 1) throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  117. DB::commit();
  118. } catch (ValidationException $e) {
  119. DB::rollBack();
  120. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  121. } catch (Exception $e) {
  122. DB::rollBack();
  123. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  124. }
  125. return $this->success();
  126. }
  127. /**
  128. * @apiParam {int} [page]
  129. * @apiParam {int} [limit]
  130. * @apiParam {int=0,1,2} type 类型 0全部 1=代收,2=代付
  131. * @apiParam {String} order_no 订单号
  132. * @apiPaaram {String} member_id 会员ID
  133. * @apiParam {int=0,1,2,3} [status] 状态:0待处理 1处理中 2成功 3失败
  134. *
  135. */
  136. public function index()
  137. {
  138. try {
  139. $params = request()->validate([
  140. 'page' => ['nullable', 'integer', 'min:1'],
  141. 'limit' => ['nullable', 'integer', 'min:1'],
  142. 'order_no' => ['nullable', 'string'],
  143. 'status' => ['nullable', 'integer', 'in:0,1,2,3'],
  144. 'member_id' => ['nullable', 'integer'],
  145. 'first_name' => ['nullable'],
  146. 'payment_type' => ['nullable', 'integer'],
  147. 'channel' => ['nullable', 'string'],
  148. ]);
  149. $params['type'] = 1;
  150. $result = PaymentOrderService::paginate($params);
  151. } catch (ValidationException $e) {
  152. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  153. } catch (Exception $e) {
  154. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  155. }
  156. return $this->success($result);
  157. }
  158. /**
  159. * @description: 查询订单的支付情况
  160. * @return {*}
  161. */
  162. public function check()
  163. {
  164. $id = request()->input('id');
  165. if (empty($id)) {
  166. return $this->error(HttpStatus::CUSTOM_ERROR, '参数错误');
  167. }
  168. try {
  169. $result = PaymentOrderService::singlePayOrder($id);
  170. $this->success([], $result['msg'] ?? '操作成功');
  171. } catch (Exception $e) {
  172. return $this->error(intval($e->getCode()));
  173. }
  174. return $this->success([], $result['msg'] ?? '操作成功');
  175. }
  176. }