PaymentChannelLinkService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Services;
  3. use App\Models\RechargeChannel;
  4. use App\Models\RechargeChannelGroup;
  5. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Collection;
  8. use RuntimeException;
  9. use App\Services\Payment\PaymentProviderCatalog;
  10. class PaymentChannelLinkService
  11. {
  12. public function validate(int $channelId, array $groupIds, int $expectedDataType, bool $lockChannel = false): array
  13. {
  14. $query = RechargeChannel::query();
  15. if ($lockChannel) $query->lockForUpdate();
  16. $channel = $query->find($channelId);
  17. if (!$channel) throw new RuntimeException('充值通道不存在');
  18. if ((int)$channel->data_type !== $expectedDataType) {
  19. throw new RuntimeException($expectedDataType === 2 ? '请选择提现通道' : '请选择充值通道');
  20. }
  21. $groupIds = array_values(array_unique(array_filter(array_map('intval', $groupIds))));
  22. if (!$groupIds) throw new RuntimeException('至少选择一个通道组合');
  23. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get();
  24. if ($groups->count() !== count($groupIds)) throw new RuntimeException('部分通道组合不存在');
  25. $field = $this->groupField($expectedDataType);
  26. foreach ($groups as $group) {
  27. if (!in_array((string)$channel->type, (array)$group->{$field}, true)) {
  28. throw new RuntimeException("通道组合“{$group->name}”没有包含通道“{$channel->name}”");
  29. }
  30. }
  31. return [$channel, $groupIds];
  32. }
  33. public function formatMany($models): Collection
  34. {
  35. $models = $models instanceof Collection ? $models : collect($models);
  36. if ($models instanceof EloquentCollection) $models->loadMissing('rechargeChannel');
  37. $groupIds = $models->flatMap(fn ($model) => (array)($model->recharge_channel_group_ids ?? []))
  38. ->map(fn ($id) => (int)$id)->filter()->unique()->values();
  39. $groups = RechargeChannelGroup::query()->whereIn('id', $groupIds)->get()->keyBy('id');
  40. return $models->map(fn (Model $model) => $this->formatOne($model, $groups));
  41. }
  42. public function formatOne(Model $model, ?Collection $groups = null): array
  43. {
  44. $model->loadMissing('rechargeChannel');
  45. $groups ??= RechargeChannelGroup::query()
  46. ->whereIn('id', (array)($model->recharge_channel_group_ids ?? []))->get()->keyBy('id');
  47. $channel = $model->rechargeChannel;
  48. $assignedIds = array_values(array_unique(array_filter(array_map(
  49. 'intval',
  50. (array)($model->recharge_channel_group_ids ?? [])
  51. ))));
  52. $effectiveIds = [];
  53. $unavailableIds = [];
  54. $groupRows = [];
  55. foreach ($assignedIds as $groupId) {
  56. $group = $groups->get($groupId);
  57. $effective = false;
  58. if ($group && $channel) {
  59. $effective = in_array(
  60. (string)$channel->type,
  61. (array)$group->{$this->groupField((int)$channel->data_type)},
  62. true
  63. );
  64. }
  65. if ($effective) {
  66. $effectiveIds[] = $groupId;
  67. } else {
  68. $unavailableIds[] = $groupId;
  69. }
  70. $groupRows[] = [
  71. 'id' => $groupId,
  72. 'name' => (string)($group->name ?? ''),
  73. 'effective' => $effective ? 1 : 0,
  74. ];
  75. }
  76. $data = $model->toArray();
  77. $identity = $channel ? PaymentProviderCatalog::channelIdentity($channel->toArray()) : null;
  78. if (array_key_exists('payment_company', $data)) {
  79. $data['payment_company_label'] = PaymentProviderCatalog::label((string)$data['payment_company']);
  80. $data['payment_method_label'] = $identity && $identity['method_code'] === $data['payment_method']
  81. ? $identity['method_label'] : (string)$data['payment_method'];
  82. }
  83. if (array_key_exists('provider_name', $data)) {
  84. $data['provider_label'] = PaymentProviderCatalog::label((string)$data['provider_name']);
  85. $data['collection_method_label'] = $identity && $identity['method_code'] === $data['collection_method']
  86. ? $identity['method_label'] : (string)$data['collection_method'];
  87. }
  88. if (array_key_exists('payment_company', $data) || array_key_exists('provider_name', $data)) {
  89. $data['credential_source'] = 'environment';
  90. $data['merchant_config_applied'] = false;
  91. }
  92. $data['recharge_channel_group_ids'] = $assignedIds;
  93. $data['effective_group_ids'] = $effectiveIds;
  94. $data['unavailable_group_ids'] = $unavailableIds;
  95. $data['groups'] = $groupRows;
  96. $data['recharge_channel'] = $channel ? [
  97. 'id' => (int)$channel->id,
  98. 'data_type' => (int)$channel->data_type,
  99. 'name' => (string)$channel->name,
  100. 'key' => (string)$channel->key,
  101. 'type' => (string)$channel->type,
  102. 'rate' => number_format((float)$channel->rate, 4, '.', ''),
  103. 'fee_rate' => bcmul((string)$channel->rate, '100', 4),
  104. 'min_amount' => $channel->min === null ? null : number_format((float)$channel->min, 2, '.', ''),
  105. 'max_amount' => $channel->max === null ? null : number_format((float)$channel->max, 2, '.', ''),
  106. 'fixed_amounts' => $channel->fixed ?: [],
  107. 'sort' => (int)$channel->sort,
  108. 'status' => (int)$channel->status,
  109. ] : null;
  110. $data['fee_rate'] = $channel ? bcmul((string)$channel->rate, '100', 4) : null;
  111. $data['min_amount'] = $channel?->min;
  112. $data['max_amount'] = $channel?->max;
  113. $data['sort'] = $channel ? (int)$channel->sort : null;
  114. $data['config_status'] = (int)$model->status;
  115. $data['channel_status'] = $channel ? (int)$channel->status : 0;
  116. $data['runtime_channel_available'] = $channel
  117. && (int)$channel->status === 1 && count($effectiveIds) > 0 ? 1 : 0;
  118. $data['effective_status'] = (int)$model->status === 1
  119. && $data['runtime_channel_available'] === 1 ? 1 : 0;
  120. if (array_key_exists('amount', $data)) {
  121. $data['actual_amount'] = $channel
  122. ? bcsub((string)$model->amount, bcmul((string)$model->amount, (string)$channel->rate, 6), 2)
  123. : null;
  124. }
  125. $data['channel_fields_readonly'] = [
  126. 'recharge_channel.name', 'recharge_channel.key', 'recharge_channel.type',
  127. 'fee_rate', 'min_amount', 'max_amount', 'recharge_channel.fixed_amounts',
  128. 'sort', 'channel_status',
  129. ];
  130. unset($data['level_scope']);
  131. return $data;
  132. }
  133. private function groupField(int $dataType): string
  134. {
  135. return match ($dataType) {
  136. 2 => 'withdraw_type',
  137. 3 => 'activity_type',
  138. default => 'recharge_type',
  139. };
  140. }
  141. }