PaymentProviderService.php 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Services\Payment;
  3. use App\Models\PaymentProvider;
  4. use App\Models\RechargeChannel;
  5. use App\Services\OperationAuditService;
  6. use Illuminate\Support\Facades\DB;
  7. use InvalidArgumentException;
  8. use RuntimeException;
  9. class PaymentProviderService
  10. {
  11. public static function statuses(): array
  12. {
  13. // No process-wide cache: long-running workers must see the latest switch.
  14. return PaymentProvider::query()->pluck('status', 'code')->map(fn ($status) => (int)$status)->all();
  15. }
  16. public static function assertEnabled(string $code): void
  17. {
  18. if (!in_array($code, PaymentProviderCatalog::codes(), true)) throw new InvalidArgumentException('支付公司不存在');
  19. if ((self::statuses()[$code] ?? 0) !== 1) throw new RuntimeException(PaymentProviderCatalog::label($code) . '已禁用');
  20. }
  21. public static function requestEnabled(string $channel, int $direction): bool
  22. {
  23. return (self::statuses()[self::providerForRequest($channel, $direction)] ?? 0) === 1;
  24. }
  25. public static function providerForRequest(string $channel, int $direction): string
  26. {
  27. // Mirror actual dispatch, including historical aliases accepted by provider services.
  28. if (JdPayService::isChannel($channel)) $code = 'jd';
  29. elseif ($direction === 1 ? NoPayService::isRechargeChannel($channel) : NoPayService::isWithdrawChannel($channel)) $code = 'no';
  30. elseif ($direction === 1 ? ZimuPayService::isRechargeChannel($channel) : ZimuPayService::isPayoutChannel($channel)) $code = 'zimu';
  31. else $code = $direction === 1 ? 'sanjin' : 'qianbao';
  32. return $code;
  33. }
  34. public function listing(array $params): array
  35. {
  36. $channels = RechargeChannel::query()->get(['from', 'data_type', 'type', 'status'])->toArray();
  37. $definitions = PaymentProviderCatalog::definitions();
  38. $rows = PaymentProvider::query()->whereIn('code', array_keys($definitions))->orderBy('id')->get()
  39. ->map(function ($provider) use ($definitions, $channels) {
  40. $code = $provider->code;
  41. $definition = $definitions[$code];
  42. $matching = array_filter($channels, fn ($channel) => (PaymentProviderCatalog::channelIdentity($channel)['provider_code'] ?? null) === $code);
  43. return [
  44. 'id' => (int)$provider->id, 'payment_company' => $code, 'payment_company_label' => $definition['label'],
  45. 'data_types' => $definition['directions'], 'signature_algorithm' => $definition['signature'],
  46. 'status' => (int)$provider->status, 'status_text' => $provider->status === 1 ? '启用' : '禁用',
  47. 'channel_count' => count($matching),
  48. 'enabled_channel_count' => count(array_filter($matching, fn ($channel) => (int)$channel['status'] === 1)),
  49. 'created_at' => $provider->created_at, 'updated_at' => $provider->updated_at,
  50. ];
  51. })->filter(function ($row) use ($params) {
  52. if (!empty($params['payment_company']) && $row['payment_company'] !== $params['payment_company']) return false;
  53. if (isset($params['status']) && $row['status'] !== (int)$params['status']) return false;
  54. return empty($params['data_type']) || in_array((int)$params['data_type'], $row['data_types'], true);
  55. })->values();
  56. $page = (int)($params['page'] ?? 1);
  57. $limit = (int)($params['limit'] ?? 20);
  58. return ['total' => $rows->count(), 'data' => $rows->forPage($page, $limit)->values()->all()];
  59. }
  60. public function setStatus(string $code, int $status): void
  61. {
  62. if (!in_array($code, PaymentProviderCatalog::codes(), true) || !in_array($status, [0, 1], true)) {
  63. throw new InvalidArgumentException('支付公司或状态错误');
  64. }
  65. DB::transaction(function () use ($code, $status) {
  66. $provider = PaymentProvider::query()->where('code', $code)->lockForUpdate()->firstOrFail();
  67. if ($provider->status === $status) return;
  68. $before = $provider->toArray();
  69. $provider->status = $status;
  70. $provider->save();
  71. OperationAuditService::record('payment_company', (int)$provider->id, 'update', $before, $provider);
  72. }, 3);
  73. }
  74. }