| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Http\Controllers\admin;
- use App\Constants\HttpStatus;
- use App\Http\Controllers\Controller;
- use App\Services\Payment\PaymentProviderCatalog;
- use App\Services\Payment\PaymentProviderService;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\ValidationException;
- class PaymentCompany extends Controller
- {
- public function list(PaymentProviderService $service)
- {
- return $this->run(function () use ($service) {
- $params = request()->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'payment_company' => ['nullable', Rule::in(PaymentProviderCatalog::codes())],
- 'status' => ['nullable', 'integer', 'in:0,1'], 'data_type' => ['nullable', 'integer', 'in:1,2'],
- ]);
- return $service->listing($params);
- });
- }
- public function status(PaymentProviderService $service)
- {
- return $this->run(function () use ($service) {
- $params = request()->validate([
- 'payment_company' => ['required', Rule::in(PaymentProviderCatalog::codes())],
- 'status' => ['required', 'integer', 'in:0,1'],
- ]);
- $service->setStatus($params['payment_company'], (int)$params['status']);
- return [];
- });
- }
- private function run(callable $callback)
- {
- try {
- return $this->success($callback());
- } catch (ValidationException $e) {
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
- } catch (ModelNotFoundException $e) {
- return $this->error(HttpStatus::CUSTOM_ERROR, '支付公司不存在');
- } catch (\PDOException $e) {
- report($e);
- return $this->error(HttpStatus::CUSTOM_ERROR, '支付公司操作失败,请稍后重试');
- } catch (\InvalidArgumentException | \RuntimeException $e) {
- return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
- }
- }
- }
|