PaymentOrder.php 7.4 KB

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