PaymentCompany.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\Payment\PaymentProviderCatalog;
  6. use App\Services\Payment\PaymentProviderService;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. use Illuminate\Validation\Rule;
  9. use Illuminate\Validation\ValidationException;
  10. class PaymentCompany extends Controller
  11. {
  12. public function list(PaymentProviderService $service)
  13. {
  14. return $this->run(function () use ($service) {
  15. $params = request()->validate([
  16. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  17. 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
  18. 'status' => ['nullable', 'integer', 'in:0,1'], 'data_type' => ['nullable', 'integer', 'in:1,2'],
  19. ]);
  20. return $service->listing($params);
  21. });
  22. }
  23. public function status(PaymentProviderService $service)
  24. {
  25. return $this->run(function () use ($service) {
  26. $params = request()->validate([
  27. 'payment_company' => ['required', Rule::in(PaymentProviderCatalog::codes())],
  28. 'status' => ['required', 'integer', 'in:0,1'],
  29. ]);
  30. $service->setStatus($params['payment_company'], (int)$params['status']);
  31. return [];
  32. });
  33. }
  34. private function run(callable $callback)
  35. {
  36. try {
  37. return $this->success($callback());
  38. } catch (ValidationException $e) {
  39. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  40. } catch (ModelNotFoundException $e) {
  41. return $this->error(HttpStatus::CUSTOM_ERROR, '支付公司不存在');
  42. } catch (\PDOException $e) {
  43. report($e);
  44. return $this->error(HttpStatus::CUSTOM_ERROR, '支付公司操作失败,请稍后重试');
  45. } catch (\InvalidArgumentException | \RuntimeException $e) {
  46. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  47. }
  48. }
  49. }