| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Services\Payment;
- use App\Models\PaymentProvider;
- use App\Models\RechargeChannel;
- use App\Services\OperationAuditService;
- use Illuminate\Support\Facades\DB;
- use InvalidArgumentException;
- use RuntimeException;
- class PaymentProviderService
- {
- public static function statuses(): array
- {
- // No process-wide cache: long-running workers must see the latest switch.
- return PaymentProvider::query()->pluck('status', 'code')->map(fn ($status) => (int)$status)->all();
- }
- public static function assertEnabled(string $code): void
- {
- if (!in_array($code, PaymentProviderCatalog::codes(), true)) throw new InvalidArgumentException('支付公司不存在');
- if ((self::statuses()[$code] ?? 0) !== 1) throw new RuntimeException(PaymentProviderCatalog::label($code) . '已禁用');
- }
- public static function requestEnabled(string $channel, int $direction): bool
- {
- // Mirror actual dispatch, including historical aliases accepted by provider services.
- if (JdPayService::isChannel($channel)) $code = 'jd';
- elseif ($direction === 1 ? NoPayService::isRechargeChannel($channel) : NoPayService::isWithdrawChannel($channel)) $code = 'no';
- elseif ($direction === 1 ? ZimuPayService::isRechargeChannel($channel) : ZimuPayService::isPayoutChannel($channel)) $code = 'zimu';
- else $code = $direction === 1 ? 'sanjin' : 'qianbao';
- return (self::statuses()[$code] ?? 0) === 1;
- }
- public function listing(array $params): array
- {
- $channels = RechargeChannel::query()->get(['from', 'data_type', 'type', 'status'])->toArray();
- $definitions = PaymentProviderCatalog::definitions();
- $rows = PaymentProvider::query()->whereIn('code', array_keys($definitions))->orderBy('id')->get()
- ->map(function ($provider) use ($definitions, $channels) {
- $code = $provider->code;
- $definition = $definitions[$code];
- $matching = array_filter($channels, fn ($channel) => (PaymentProviderCatalog::channelIdentity($channel)['provider_code'] ?? null) === $code);
- return [
- 'id' => (int)$provider->id, 'payment_company' => $code, 'payment_company_label' => $definition['label'],
- 'data_types' => $definition['directions'], 'signature_algorithm' => $definition['signature'],
- 'status' => (int)$provider->status, 'status_text' => $provider->status === 1 ? '启用' : '禁用',
- 'channel_count' => count($matching),
- 'enabled_channel_count' => count(array_filter($matching, fn ($channel) => (int)$channel['status'] === 1)),
- 'created_at' => $provider->created_at, 'updated_at' => $provider->updated_at,
- ];
- })->filter(function ($row) use ($params) {
- if (!empty($params['payment_company']) && $row['payment_company'] !== $params['payment_company']) return false;
- if (isset($params['status']) && $row['status'] !== (int)$params['status']) return false;
- return empty($params['data_type']) || in_array((int)$params['data_type'], $row['data_types'], true);
- })->values();
- $page = (int)($params['page'] ?? 1);
- $limit = (int)($params['limit'] ?? 20);
- return ['total' => $rows->count(), 'data' => $rows->forPage($page, $limit)->values()->all()];
- }
- public function setStatus(string $code, int $status): void
- {
- if (!in_array($code, PaymentProviderCatalog::codes(), true) || !in_array($status, [0, 1], true)) {
- throw new InvalidArgumentException('支付公司或状态错误');
- }
- DB::transaction(function () use ($code, $status) {
- $provider = PaymentProvider::query()->where('code', $code)->lockForUpdate()->firstOrFail();
- if ($provider->status === $status) return;
- $before = $provider->toArray();
- $provider->status = $status;
- $provider->save();
- OperationAuditService::record('payment_company', (int)$provider->id, 'update', $before, $provider);
- }, 3);
- }
- }
|